4内部调用函数代码: #region 对内操作函数 /// /// 初始化录音设备,此处使用主录音设备. /// /// 调用成功返回true,否则返回false private bool InitCaptureDevice() { CaptureDevicesCollection devices = new CaptureDevicesCollection(); Guid deviceGuid = Guid.Empty; if (devices.Count>0) deviceGuid = devices[0].DriverGuid; else { MessageBox.Show('没有音频捕捉设备'); return false; } try { mCapDev = new Capture(deviceGuid); } catch (DirectXException e) { MessageBox.Show(e.ToString()); return false; } return true; } private void CreateCaptureBuffer() { CaptureBufferDescription bufferdescription = new CaptureBufferDescription(); if (null != mNotify) { mNotify.Dispose(); mNotify = null; } if (null != mRecBuffer) { mRecBuffer.Dispose(); mRecBuffer = null; } mNotifySize = (1024 > mWavFormat.AverageBytesPerSecond/8) ? 1024 : (mWavFormat.AverageBytesPerSecond / 8); mNotifySize -= mNotifySize % mWavFormat.BlockAlign; mBufferSize = mNotifySize * cNotifyNum; bufferdescription.BufferBytes = mBufferSize; bufferdescription.Format = mWavFormat; mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev); mNextCaptureOffset = 0; } private bool InitNotifications() { if (null == mRecBuffer) { MessageBox.Show('没有缓冲区'); return 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.SafeWaitHandle.DangerousGetHandle(); } mNotify = new Notify(mRecBuffer); mNotify.SetNotificationPositions(PositionNotify, cNotifyNum); return true; } private void WaitThread() { while (true) { mNotificationEvent.WaitOne(Timeout.Infinite, true); // 录制数据 RecordCapturedData(); } } /// /// 将录制的数据写入wav文件 /// private void RecordCapturedData() { byte[] CaptureData = null; int ReadPos=0, CapturePos=0, LockSize=0; 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); mWriter.Write(CaptureData, 0, CaptureData.Length); mSampleCount += CaptureData.Length; mNextCaptureOffset += CaptureData.Length; mNextCaptureOffset %= mBufferSize; // Circular buffer } private void CreateSoundFile() { // Open up the wave file for writing. mWaveFile = new FileStream(mFileName, FileMode.Create); mWriter = new BinaryWriter(mWaveFile); char[] ChunkRiff = {'R', 'I','F','F'}; char[] ChunkType = {'W','A','V','E'}; char[] ChunkFmt = {'f','m','t',' '}; char[] ChunkData = {'d','a','t','a'}; short shPad = 1; int nFormatChunkLength = 0x10; int nLength = 0; short shBytesPerSample = 0; if (8 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels) shBytesPerSample = 1; else if ((8 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels) || (16 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels)) shBytesPerSample = 2; else if (16 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels) shBytesPerSample = 4; // RIFF 块 mWriter.Write(ChunkRiff); mWriter.Write(nLength); mWriter.Write(ChunkType); // WAVE块 mWriter.Write(ChunkFmt); mWriter.Write(nFormatChunkLength); mWriter.Write(shPad); mWriter.Write(mWavFormat.Channels); mWriter.Write(mWavFormat.SamplesPerSecond); mWriter.Write(mWavFormat.AverageBytesPerSecond); mWriter.Write(shBytesPerSample); mWriter.Write(mWavFormat.BitsPerSample); // 数据块 mWriter.Write(ChunkData); mWriter.Write((int)0); // The sample length will be written in later. } #end