forked from Trevor3000/RemoteControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaveIn.cs
More file actions
502 lines (420 loc) · 16.4 KB
/
WaveIn.cs
File metadata and controls
502 lines (420 loc) · 16.4 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using RemoteControl.Audio.NativeMethods;
namespace RemoteControl.Audio
{
#region Delegates Implementation
/// <summary>
/// Represents the method that will handle the <b>WavRecorder.BufferFull</b> event.
/// </summary>
/// <param name="buffer">Recorded data.</param>
public delegate void BufferFullHandler(byte[] buffer);
#endregion
/// <summary>
/// This class implements streaming microphone wav data receiver.
/// </summary>
public class WaveIn
{
#region class BufferItem
/// <summary>
/// This class holds queued recording buffer.
/// </summary>
private class BufferItem
{
private GCHandle m_HeaderHandle;
private GCHandle m_DataHandle;
private int m_DataSize = 0;
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="headerHandle">Header handle.</param>
/// <param name="header">Wav header.</param>
/// <param name="dataHandle">Wav header data handle.</param>
/// <param name="dataSize">Data size in bytes.</param>
public BufferItem(ref GCHandle headerHandle,ref GCHandle dataHandle,int dataSize)
{
m_HeaderHandle = headerHandle;
m_DataHandle = dataHandle;
m_DataSize = dataSize;
}
#region method Dispose
/// <summary>
/// Cleans up any resources being used.
/// </summary>
public void Dispose()
{
m_HeaderHandle.Free();
m_DataHandle.Free();
}
#endregion
#region Properties Implementation
/// <summary>
/// Gets header handle.
/// </summary>
public GCHandle HeaderHandle
{
get{ return m_HeaderHandle; }
}
/// <summary>
/// Gets header.
/// </summary>
public WAVEHDR Header
{
get{ return (WAVEHDR)m_HeaderHandle.Target; }
}
/// <summary>
/// Gets wav header data pointer handle.
/// </summary>
public GCHandle DataHandle
{
get{ return m_DataHandle; }
}
/// <summary>
/// Gets wav header data.
/// </summary>
public byte[] Data
{
get{ return (byte[])m_DataHandle.Target; }
}
/// <summary>
/// Gets wav header data size in bytes.
/// </summary>
public int DataSize
{
get{ return m_DataSize; }
}
#endregion
}
#endregion
private WavInDevice m_pInDevice = null;
private int m_SamplesPerSec = 8000;
private int m_BitsPerSample = 8;
private int m_Channels = 1;
private int m_BufferSize = 400;
private IntPtr m_pWavDevHandle = IntPtr.Zero;
private int m_BlockSize = 0;
private List<BufferItem> m_pBuffers = null;
private waveInProc m_pWaveInProc = null;
private bool m_IsRecording = false;
private bool m_IsDisposed = false;
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="outputDevice">Input device.</param>
/// <param name="samplesPerSec">Sample rate, in samples per second (hertz). For PCM common values are
/// 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz.</param>
/// <param name="bitsPerSample">Bits per sample. For PCM 8 or 16 are the only valid values.</param>
/// <param name="channels">Number of channels.</param>
/// <param name="bufferSize">Specifies recording buffer size.</param>
/// <exception cref="ArgumentNullException">Is raised when <b>outputDevice</b> is null.</exception>
/// <exception cref="ArgumentException">Is raised when any of the aruments has invalid value.</exception>
public WaveIn(WavInDevice device,int samplesPerSec,int bitsPerSample,int channels,int bufferSize)
{
if(device == null){
throw new ArgumentNullException("device");
}
if(samplesPerSec < 8000){
throw new ArgumentException("Argument 'samplesPerSec' value must be >= 8000.");
}
if(bitsPerSample < 8){
throw new ArgumentException("Argument 'bitsPerSample' value must be >= 8.");
}
if(channels < 1){
throw new ArgumentException("Argument 'channels' value must be >= 1.");
}
m_pInDevice = device;
m_SamplesPerSec = samplesPerSec;
m_BitsPerSample = bitsPerSample;
m_Channels = channels;
m_BufferSize = bufferSize;
m_BlockSize = m_Channels * (m_BitsPerSample / 8);
m_pBuffers = new List<BufferItem>();
// Try to open wav device.
WAVEFORMATEX format = new WAVEFORMATEX();
format.wFormatTag = WavFormat.PCM;
format.nChannels = (ushort)m_Channels;
format.nSamplesPerSec = (uint)samplesPerSec;
format.nAvgBytesPerSec = (uint)(m_SamplesPerSec * m_Channels * (m_BitsPerSample / 8));
format.nBlockAlign = (ushort)m_BlockSize;
format.wBitsPerSample = (ushort)m_BitsPerSample;
format.cbSize = 0;
// We must delegate reference, otherwise GC will collect it.
m_pWaveInProc = new waveInProc(this.OnWaveInProc);
int result = WavMethods.waveInOpen(out m_pWavDevHandle,m_pInDevice.Index,format,m_pWaveInProc,0,WavConstants.CALLBACK_FUNCTION);
if(result != MMSYSERR.NOERROR){
throw new Exception("Failed to open wav device, error: " + result.ToString() + ".");
}
EnsureBuffers();
}
/// <summary>
/// Default destructor.
/// </summary>
~WaveIn()
{
Dispose();
}
#region method Dispose
/// <summary>
/// Cleans up any resources being used.
/// </summary>
public void Dispose()
{
if(m_IsDisposed){
return;
}
m_IsDisposed = true;
// Release events.
this.BufferFull = null;
try{
// If recording, we need to reset wav device first.
WavMethods.waveInReset(m_pWavDevHandle);
// If there are unprepared wav headers, we need to unprepare these.
foreach(BufferItem item in m_pBuffers){
WavMethods.waveInUnprepareHeader(m_pWavDevHandle,item.HeaderHandle.AddrOfPinnedObject(),Marshal.SizeOf(item.Header));
item.Dispose();
}
// Close input device.
WavMethods.waveInClose(m_pWavDevHandle);
m_pInDevice = null;
m_pWavDevHandle = IntPtr.Zero;
}
catch{
}
}
#endregion
#region method Start
/// <summary>
/// Starts recording.
/// </summary>
public void Start()
{
if(m_IsRecording){
return;
}
m_IsRecording = true;
int result = WavMethods.waveInStart(m_pWavDevHandle);
if(result != MMSYSERR.NOERROR){
throw new Exception("Failed to start wav device, error: " + result + ".");
}
}
#endregion
#region method Stop
/// <summary>
/// Stops recording.
/// </summary>
public void Stop()
{
if(!m_IsRecording){
return;
}
m_IsRecording = false;
int result = WavMethods.waveInStop(m_pWavDevHandle);
if(result != MMSYSERR.NOERROR){
throw new Exception("Failed to stop wav device, error: " + result + ".");
}
}
#endregion
#region method OnWaveInProc
/// <summary>
/// This method is called when wav device generates some event.
/// </summary>
/// <param name="hdrvr">Handle to the waveform-audio device associated with the callback.</param>
/// <param name="uMsg">Waveform-audio input message.</param>
/// <param name="dwUser">User-instance data specified with waveOutOpen.</param>
/// <param name="dwParam1">Message parameter.</param>
/// <param name="dwParam2">Message parameter.</param>
private void OnWaveInProc(IntPtr hdrvr,int uMsg,int dwUser,int dwParam1,int dwParam2)
{
// NOTE: MSDN warns, we may not call any wav related methods here.
try{
if(uMsg == WavConstants.MM_WIM_DATA){
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcessFirstBuffer));
}
}
catch{
}
}
#endregion
#region method ProcessFirstBuffer
/// <summary>
/// Processes first first filled buffer in queue and disposes it if done.
/// </summary>
/// <param name="state">User data.</param>
private void ProcessFirstBuffer(object state)
{
try{
lock(m_pBuffers){
BufferItem item = m_pBuffers[0];
// Raise BufferFull event.
OnBufferFull(item.Data);
// Clean up.
WavMethods.waveInUnprepareHeader(m_pWavDevHandle,item.HeaderHandle.AddrOfPinnedObject(),Marshal.SizeOf(item.Header));
m_pBuffers.Remove(item);
item.Dispose();
}
EnsureBuffers();
}
catch{
}
}
#endregion
#region method EnsureBuffers
/// <summary>
/// Fills recording buffers.
/// </summary>
private void EnsureBuffers()
{
// We keep 3 x buffer.
lock(m_pBuffers){
while(m_pBuffers.Count < 3){
byte[] data = new byte[m_BufferSize];
GCHandle dataHandle = GCHandle.Alloc(data,GCHandleType.Pinned);
WAVEHDR wavHeader = new WAVEHDR();
wavHeader.lpData = dataHandle.AddrOfPinnedObject();
wavHeader.dwBufferLength = (uint)data.Length;
wavHeader.dwBytesRecorded = 0;
wavHeader.dwUser = IntPtr.Zero;
wavHeader.dwFlags = 0;
wavHeader.dwLoops = 0;
wavHeader.lpNext = IntPtr.Zero;
wavHeader.reserved = 0;
GCHandle headerHandle = GCHandle.Alloc(wavHeader,GCHandleType.Pinned);
int result = 0;
result = WavMethods.waveInPrepareHeader(m_pWavDevHandle,headerHandle.AddrOfPinnedObject(),Marshal.SizeOf(wavHeader));
if(result == MMSYSERR.NOERROR){
m_pBuffers.Add(new BufferItem(ref headerHandle,ref dataHandle,m_BufferSize));
result = WavMethods.waveInAddBuffer(m_pWavDevHandle,headerHandle.AddrOfPinnedObject(),Marshal.SizeOf(wavHeader));
if(result != MMSYSERR.NOERROR){
throw new Exception("Error adding wave in buffer, error: " + result + ".");
}
}
}
}
}
#endregion
#region Properties Implementation
/// <summary>
/// Gets all available input audio devices.
/// </summary>
public static WavInDevice[] Devices
{
get{
List<WavInDevice> retVal = new List<WavInDevice>();
// Get all available output devices and their info.
int devicesCount = WavMethods.waveInGetNumDevs();
for(int i=0;i<devicesCount;i++){
WAVEOUTCAPS pwoc = new WAVEOUTCAPS();
if(WavMethods.waveInGetDevCaps((uint)i,ref pwoc,Marshal.SizeOf(pwoc)) == MMSYSERR.NOERROR){
retVal.Add(new WavInDevice(i,pwoc.szPname,pwoc.wChannels));
}
}
return retVal.ToArray();
}
}
/// <summary>
/// Gets if this object is disposed.
/// </summary>
public bool IsDisposed
{
get{ return m_IsDisposed; }
}
/// <summary>
/// Gets current input device.
/// </summary>
/// <exception cref="">Is raised when this object is disposed and this property is accessed.</exception>
public WavInDevice InputDevice
{
get{
if(m_IsDisposed){
throw new ObjectDisposedException("WavRecorder");
}
return m_pInDevice;
}
}
/// <summary>
/// Gets number of samples per second.
/// </summary>
/// <exception cref="">Is raised when this object is disposed and this property is accessed.</exception>
public int SamplesPerSec
{
get{
if(m_IsDisposed){
throw new ObjectDisposedException("WavRecorder");
}
return m_SamplesPerSec;
}
}
/// <summary>
/// Gets number of buts per sample.
/// </summary>
/// <exception cref="">Is raised when this object is disposed and this property is accessed.</exception>
public int BitsPerSample
{
get{
if(m_IsDisposed){
throw new ObjectDisposedException("WavRecorder");
}
return m_BitsPerSample;
}
}
/// <summary>
/// Gets number of channels.
/// </summary>
/// <exception cref="">Is raised when this object is disposed and this property is accessed.</exception>
public int Channels
{
get{
if(m_IsDisposed){
throw new ObjectDisposedException("WavRecorder");
}
return m_Channels;
}
}
/// <summary>
/// Gets recording buffer size.
/// </summary>
/// <exception cref="">Is raised when this object is disposed and this property is accessed.</exception>
public int BufferSize
{
get{
if(m_IsDisposed){
throw new ObjectDisposedException("WavRecorder");
}
return m_BufferSize;
}
}
// <summary>
/// Gets one smaple block size in bytes.
/// </summary>
/// <exception cref="">Is raised when this object is disposed and this property is accessed.</exception>
public int BlockSize
{
get{
if(m_IsDisposed){
throw new ObjectDisposedException("WavRecorder");
}
return m_BlockSize;
}
}
#endregion
#region Events Implementation
/// <summary>
/// This event is raised when record buffer is full and application should process it.
/// </summary>
public event BufferFullHandler BufferFull = null;
/// <summary>
/// This method raises event <b>BufferFull</b> event.
/// </summary>
/// <param name="buffer">Receive buffer.</param>
private void OnBufferFull(byte[] buffer)
{
if(this.BufferFull != null){
this.BufferFull(buffer);
}
}
#endregion
}
}