111,076
社区成员




private void RecordCapturedData()
{
byte[] CaptureData = null;
int ReadPos;
int CapturePos;
int LockSize;
mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos);
LockSize = ReadPos - mNextCaptureOffset;
if (LockSize < 0)
LockSize += mBufferSize;
LockSize -= (LockSize % mNotifySize);
if (0 == LockSize)
return;
CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);
mNextCaptureOffset += CaptureData.Length;
mNextCaptureOffset %= mBufferSize;
//check state
if (state == State.Recording || state == State.PreRecording)
{
secondBuffer.Add(CaptureData);
}
}
public class Recorder
{
//由于长度限制,变量略
public void Initialize()
{
// Create capture buffer.
CreateCaptureBuffer();
// Create notification system.
InitNotifications();
mRecBuffer.Start(true);
}
public void Dispose()
{
// Close notification
if (null != mNotificationEvent)
mNotificationEvent.Set();
// Stop recording
mRecBuffer.Stop();
}
#endregion
#region Initialize Recorder
public Recorder()
{
InitCaptureDevice();
mWavFormat = CreateWaveFormat();
buffDes = new BufferDescription();
buffDes.GlobalFocus = true;
buffDes.ControlVolume = true;
buffDes.ControlPan = true;
secDev = new Device();
bufferStates = new List<int>();
}
private void RecordCapturedData()
{
byte[] CaptureData = null;
int ReadPos;
int CapturePos;
int LockSize;
mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos);
LockSize = ReadPos - mNextCaptureOffset;
if (LockSize < 0)
LockSize += mBufferSize;
LockSize -= (LockSize % mNotifySize);
if (0 == LockSize)
return;
CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);
mNextCaptureOffset += CaptureData.Length;
mNextCaptureOffset %= mBufferSize;
//check state
if (state == State.Recording || state == State.PreRecording)
{
secondBuffer.Add(CaptureData);
}
}
private void CreateCaptureBuffer()
{
CaptureBufferDescription bufferdescription = new CaptureBufferDescription();
if (null != mNotify)
{
mNotify.Dispose();
mNotify = null;
}
if (null != mRecBuffer)
{
mRecBuffer.Dispose();
mRecBuffer = null;
}
// set notification's size: 1 second
mNotifySize = (1024 > mWavFormat.AverageBytesPerSecond / 8) ? 1024 : (mWavFormat.AverageBytesPerSecond / 8);
mNotifySize -= mNotifySize % mWavFormat.BlockAlign;
// set size of buffer
mBufferSize = mNotifySize * cNotifyNum;
bufferdescription.BufferBytes = mBufferSize;
bufferdescription.Format = mWavFormat;
mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev);
mNextCaptureOffset = 0;
}
private bool InitNotifications()
{
if (null == mRecBuffer)
{
MessageBox.Show("Record buffer is not found!");
return false;
}
mNotificationEvent = new AutoResetEvent(false);
if (null == mNotifyThread)
{
mNotifyThread = new Thread(new ThreadStart(WaitThread));
mNotifyThread.Start();
}
BufferPositionNotify[] PositionNotify = new BufferPositionNotify[cNotifyNum + 1];
for (int i = 0; i < cNotifyNum; i++)
{
PositionNotify[i].Offset = (mNotifySize * i) + mNotifySize - 1;
PositionNotify[i].EventNotifyHandle = mNotificationEvent.Handle;
}
mNotify = new Notify(mRecBuffer);
mNotify.SetNotificationPositions(PositionNotify, cNotifyNum);
return true;
}
private bool InitCaptureDevice()
{
CaptureDevicesCollection devices = new CaptureDevicesCollection();
Guid deviceGuid = Guid.Empty;
if (devices.Count > 0)
deviceGuid = devices[0].DriverGuid;
else
{
MessageBox.Show("No device to capture wave!");
return false;
}
try
{
mCapDev = new Capture(deviceGuid);
}
catch (DirectXException e)
{
MessageBox.Show(e.ToString());
return false;
}
return true;
}
private WaveFormat CreateWaveFormat()
{
WaveFormat format = new WaveFormat();
format.FormatTag = WaveFormatTag.Pcm; // PCM
format.SamplesPerSecond = 16000; // 16KHz
format.BitsPerSample = 16; // 16Bit
format.Channels = 1; // Mono
format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));
format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
return format;
}
private void WaitThread()
{
while (true)
{
// waiting for notification
mNotificationEvent.WaitOne(Timeout.Infinite, true);
// process data captured
RecordCapturedData();
}
}
#endregion
}
private void RecordCapturedData()
{
byte[] CaptureData = null;
int ReadPos;
int CapturePos;
int LockSize;
mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos);
LockSize = ReadPos - mNextCaptureOffset;
if (LockSize < 0)
LockSize += mBufferSize;
LockSize -= (LockSize % mNotifySize);
if (0 == LockSize)
return;
CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);
mNextCaptureOffset += CaptureData.Length;
mNextCaptureOffset %= mBufferSize;
_waveLeft = new double[CaptureData.Length / 4];
_waveRight = new double[CaptureData.Length / 4];
// Split out channels from sample
int h = 0;
for (int i = 0; i < CaptureData.Length; i += 4)
{
_waveLeft[h] = (double)BitConverter.ToInt16(CaptureData, i);
_waveRight[h] = (double)BitConverter.ToInt16(CaptureData, i + 2);
h++;
}
double sum = 0;
foreach (double item in _waveLeft)
{
if (item > 0)
sum += item;
else
sum += -item;
}
int result = (int)(((sum / _waveLeft.Length)) / 32768 * 100);
if (state == State.Starting||state == State.Started)
{
secondBuffer.Add(CaptureData);
}
OnVolumeChanged(new OnVolumeChangedEventArgs(result));
if (state == State.Started)
{
if (result < accuracy)
{
silence++;
if (silence > 10)
{
silence = 0;
SwitchState(State.Paused);
}
}
}
else if (state == State.Paused)
{
if (result > accuracy)
{
silence = 0;
secondBuffer = new List<byte[]>();
int n = _thirdBuffer.Count;
for (int i = 0; i < n; i++)
{
secondBuffer.Add(_thirdBuffer.Dequeue());
}
secondBuffer.Add(CaptureData);
SwitchState(State.Started);
}
}
if (_thirdBuffer.Count <= _THIRD_BUFFER_SIZE)
{
_thirdBuffer.Enqueue(CaptureData);
}
else
{
_thirdBuffer.Enqueue(CaptureData);
_thirdBuffer.Dequeue();
}
}