C# 录音和变调

一直想研究下录音 正好有个项目有机会使用一下强大的 NAudio (https://github.com/naudio/NAudio)库

 

 录音

NAudio 录音类库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public class NAudioRecorder
    {
        public WaveIn waveSource = null;
        public WaveFileWriter waveFile = null;
        private string fileName = string.Empty;
 
        /// <summary>
        /// 开始录音
        /// </summary>
        public void StartRec()
        {
            waveSource = new WaveIn();
            waveSource.WaveFormat = new WaveFormat(16000, 16, 1); // 16bit,16KHz,Mono的录音格式
 
            waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
            waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
            //writer = new WaveFileWriter(outputFilePath, capture.WaveFormat);
            var capture = new WasapiLoopbackCapture();
            Directory.CreateDirectory(fileName);
            var outputFilePath = Path.Combine(fileName, "recorded.wav");
            waveFile = new WaveFileWriter(outputFilePath, waveSource.WaveFormat);
 
            waveSource.StartRecording();
        }
 
        /// <summary>
        /// 停止录音
        /// </summary>
        public void StopRec()
        {
            waveSource.StopRecording();
 
            if (waveSource != null)
            {
                waveSource.Dispose();
                waveSource = null;
            }
 
            if (waveFile != null)
            {
                waveFile.Dispose();
                waveFile = null;
            }
        }
 
        /// <summary>
        /// 录音结束后保存的文件路径
        /// </summary>
        /// <param name="fileName">保存wav文件的路径名</param>
        public void SetFileName(string fileName)
        {
            this.fileName = fileName;
        }
 
        /// <summary>
        /// 开始录音回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (waveFile != null)
            {
                waveFile.Write(e.Buffer, 0, e.BytesRecorded);
                waveFile.Flush();
            }
        }
 
        /// <summary>
        /// 录音结束回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
        {
            if (waveSource != null)
            {
                waveSource.Dispose();
                waveSource = null;
            }
 
            if (waveFile != null)
            {
                waveFile.Dispose();
                waveFile = null;
            }
        }
    }

变声

变声用到的是SoundTouch.dll 

SoundTouch wrapper 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
public class SoundTouch
    {
        private IntPtr handle;
 
        public SoundTouch()
        {
            handle = soundtouch_createInstance();
        }
 
 
        ~SoundTouch()
        {
            soundtouch_destroyInstance(handle);
        }
 
        /// <summary>
        /// Get SoundTouch version string
        /// </summary>
        public static String GetVersionString()
        {
            // convert "char *" data to c# string
            return Marshal.PtrToStringAnsi(soundtouch_getVersionString());
        }
 
 
        /// <summary>
        /// Returns number of processed samples currently available in SoundTouch for immediate output.
        /// </summary>
        public uint NumSamples()
        {
            return soundtouch_numSamples(handle);
        }
 
        /// <summary>
        /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
        /// the input of the object. Notice that sample rate _has_to_ be set before
        /// calling this function, otherwise throws a runtime_error exception.
        /// </summary>
        /// <param name="samples">Sample buffer to input</param>
        /// <param name="numSamples">Number of sample frames in buffer. Notice
        /// that in case of multi-channel sound a single sample frame contains
        /// data for all channels</param>
        public void PutSamples(float[] samples, uint numSamples)
        {
            soundtouch_putSamples(handle, samples, numSamples);
        }
 
 
        /// <summary>
        /// Sets the number of channels
        /// </summary>
        /// <param name="numChannels">1 = mono, 2 = stereo, n = multichannel</param>
        public void SetChannels(uint numChannels)
        {
            soundtouch_setChannels(handle, numChannels);
        }
 
 
        /// <summary>
        /// Sets sample rate.
        /// </summary>
        /// <param name="srate">Samplerate, e.g. 44100</param>
        public void SetSampleRate(uint srate)
        {
            soundtouch_setSampleRate(handle, srate);
        }
 
 
        /// <summary>
        /// Receive processed samples from the processor.
        /// </summary>
        /// <param name="outBuffer">Buffer where to copy output samples</param>
        /// <param name="maxSamples">Max number of sample frames to receive</param>
        /// <returns></returns>
        public uint ReceiveSamples(float[] outBuffer, uint maxSamples)
        {
            return soundtouch_receiveSamples(handle, outBuffer, maxSamples);
        }
 
        /// <summary>
        /// Flushes the last samples from the processing pipeline to the output.
        /// Clears also the internal processing buffers.
        //
        /// Note: This function is meant for extracting the last samples of a sound
        /// stream. This function may introduce additional blank samples in the end
        /// of the sound stream, and thus it's not recommended to call this function
        /// in the middle of a sound stream.
        /// </summary>
        public void Flush()
        {
            soundtouch_flush(handle);
        }
 
        /// <summary>
        /// Clears all the samples in the object's output and internal processing
        /// buffers.
        /// </summary>
        public void Clear()
        {
            soundtouch_clear(handle);
        }
 
        /// <summary>
        /// Sets new tempo control value.
        /// </summary>
        /// <param name="newTempo">Tempo setting. Normal tempo = 1.0, smaller values
        /// represent slower tempo, larger faster tempo.</param>
        public void SetTempo(float newTempo)
        {
            soundtouch_setTempo(handle, newTempo);
        }
 
        /// <summary>
        /// Sets new tempo control value as a difference in percents compared
        /// to the original tempo (-50 .. +100 %);
        /// </summary>
        /// <param name="newTempo">Tempo setting in %</param>
        public void SetTempoChange(float newTempo)
        {
            soundtouch_setTempoChange(handle, newTempo);
        }
 
        /// <summary>
        /// Sets new rate control value.
        /// </summary>
        /// <param name="newRate">Rate setting. Normal rate = 1.0, smaller values
        /// represent slower rate, larger faster rate.</param>
        public void SetRate(float newRate)
        {
            soundtouch_setTempo(handle, newRate);
        }
 
        /// <summary>
        /// Sets new rate control value as a difference in percents compared
        /// to the original rate (-50 .. +100 %);
        /// </summary>
        /// <param name="newRate">Rate setting in %</param>
        public void SetRateChange(float newRate)
        {
            soundtouch_setRateChange(handle, newRate);
        }
 
        /// <summary>
        /// Sets new pitch control value.
        /// </summary>
        /// <param name="newPitch">Pitch setting. Original pitch = 1.0, smaller values
        /// represent lower pitches, larger values higher pitch.</param>
        public void SetPitch(float newPitch)
        {
            soundtouch_setPitch(handle, newPitch);
        }
 
        /// <summary>
        /// Sets pitch change in octaves compared to the original pitch 
        /// (-1.00 .. +1.00 for +- one octave);
        /// </summary>
        /// <param name="newPitch">Pitch setting in octaves</param>
        public void SetPitchOctaves(float newPitch)
        {
            soundtouch_setPitchOctaves(handle, newPitch);
        }
 
        /// <summary>
        /// Sets pitch change in semi-tones compared to the original pitch
        /// (-12 .. +12 for +- one octave);
        /// </summary>
        /// <param name="newPitch">Pitch setting in semitones</param>
        public void SetPitchSemiTones(float newPitch)
        {
            soundtouch_setPitchSemiTones(handle, newPitch);
        }
 
        /// <summary>
        /// int16 version of soundtouch_putSamples(): This accept int16 (short) sample data
        /// and internally converts it to float format before processing
        /// </summary>
        /// <param name="samples">Sample input buffer.</param>
        /// <param name="numSamples">Number of sample frames in buffer. Notice
        /// that in case of multi-channel sound a single
        /// sample frame contains data for all channels.</param>
        public void PutSamples_i16(short[] samples, uint numSamples)
        {
            soundtouch_putSamples_i16(handle, samples, numSamples);
        }
 
        /// <summary>
        /// Changes a setting controlling the processing system behaviour. See the
        /// 'SETTING_...' defines for available setting ID's.
        /// </summary>
        /// <param name="settingId">Setting ID number. see SETTING_... defines.</param>
        /// <param name="value"New setting value></param>
        /// <returns>nonzero if successful, otherwise zero</returns>
        public int SetSetting(int settingId, int value)
        {
            return soundtouch_setSetting(handle, settingId, value);
        }
 
        /// <summary>
        /// Reads a setting controlling the processing system behaviour. See the
        /// 'SETTING_...' defines for available setting ID's.
        /// </summary>
        /// <param name="settingId">Setting ID number</param>
        /// <returns>The setting value</returns>
        public int soundtouch_getSetting(int settingId)
        {
            return soundtouch_getSetting(handle, settingId);
        }
 
        /// <summary>
        /// Returns number of samples currently unprocessed in SoundTouch internal buffer
        /// </summary>
        /// <returns>Number of sample frames</returns>
        public uint NumUnprocessedSamples()
        {
            return soundtouch_numUnprocessedSamples(handle);
        }
 
        /// <summary>
        /// int16 version of soundtouch_receiveSamples(): This converts internal float samples
        /// into int16 (short) return data type
        /// </summary>
        /// <param name="outBuffer">Buffer where to copy output samples.</param>
        /// <param name="maxSamples">How many samples to receive at max.</param>
        /// <returns>Number of received sample frames</returns>
        public uint soundtouch_receiveSamples_i16(short[] outBuffer, uint maxSamples)
        {
            return soundtouch_receiveSamples_i16(handle, outBuffer, maxSamples);
        }
 
        /// <summary>
        /// Check if there aren't any samples available for outputting.
        /// </summary>
        /// <returns>nonzero if there aren't any samples available for outputting</returns>
        public int IsEmpty()
        {
            return soundtouch_isEmpty(handle);
        }
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "soundtouch_getVersionId")]
        /// <summary>
        /// Get SoundTouch library version Id
        /// </summary>
        public static extern int GetVersionId();
 
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr soundtouch_createInstance();
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_destroyInstance(IntPtr h);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr soundtouch_getVersionString();
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setRate(IntPtr h, float newRate);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setTempo(IntPtr h, float newTempo);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setRateChange(IntPtr h, float newRate);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setTempoChange(IntPtr h, float newTempo);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setPitch(IntPtr h, float newPitch);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setPitchOctaves(IntPtr h, float newPitch);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setPitchSemiTones(IntPtr h, float newPitch);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setChannels(IntPtr h, uint numChannels);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setSampleRate(IntPtr h, uint srate);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_flush(IntPtr h);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_putSamples(IntPtr h, float[] samples, uint numSamples);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_putSamples_i16(IntPtr h, short[] samples, uint numSamples);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_clear(IntPtr h);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int soundtouch_setSetting(IntPtr h, int settingId, int value);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int soundtouch_getSetting(IntPtr h, int settingId);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint soundtouch_numUnprocessedSamples(IntPtr h);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint soundtouch_receiveSamples(IntPtr h, float[] outBuffer, uint maxSamples);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint soundtouch_receiveSamples_i16(IntPtr h, short[] outBuffer, uint maxSamples);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint soundtouch_numSamples(IntPtr h);
 
        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int soundtouch_isEmpty(IntPtr h);
    }

  源码下载(https://files.cnblogs.com/files/leoxjy/demo.zip)

posted @   摇光Summer  阅读(3664)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示