C#中使用DirectSound来获取麦克音量
大家好,我最近在做一个程序需要使用DirectSound, 当用户说话时我要开始录音,当用户说完了自动停止录音,然后这样循环下去.
现在的问题是如何知道用户是否在说话.请下看下以下代码:
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 }