手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

C#如何通过编程调整WAV 音频文件的音量

时间:2024-10-12 01:05:22

1、C#中使用DirectSound录音声卡袄嬖艽蛞录音的基本原理为了实现一个录音的基本过程,至少需要以下对象的支持:1.录音设备,对我们的PC设备就是驾搭鼯携声卡。这个录音设备可以进行的操作应该有开始和关闭。2.缓冲区,也就是录制的声音放在哪里的问题。

C#如何通过编程调整WAV 音频文件的音量

2、代码解析(SoundRecord类)需要引用的程序集:

C#如何通过编程调整WAV 音频文件的音量

3、对外操作的函数

C#如何通过编程调整WAV 音频文件的音量

4、内部调用函数代码:#region对内操作函数///<summary>///初始化录音设备,此处使用主录音设备.///</summary>///<returns>调用成功返回true,否则返回false</returns>privateboolInitCaptureDevice(){ CaptureDevicesCollectiondevices=newCaptureDevicesCollection(); GuiddeviceGuid=Guid.Empty;if(devices.Count>0)deviceGuid=devices[0].DriverGuid;else{MessageBox.Show("没有音频捕捉设备");returnfalse;} try{mCapDev=newCapture(deviceGuid);}catch(DirectXExceptione){MessageBox.Show(e.ToString());returnfalse;}returntrue;} privatevoidCreateCaptureBuffer(){ CaptureBufferDescriptionbufferdescription=newCaptureBufferDescription();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=newCaptureBuffer(bufferdescription,mCapDev);mNextCaptureOffset=0;}privateboolInitNotifications(){if(null==mRecBuffer){MessageBox.Show("没有缓冲区");returnfalse;} if(null==mNotifyThread){mNotifyThread=newThread(newThreadStart(WaitThread));mNotifyThread.Start();} BufferPositionNotify[]PositionNotify=newBufferPositionNotify[cNotifyNum+1];for(inti=0;i<cNotifyNum;i++){PositionNotify[i].Offset=(mNotifySize*i)+mNotifySize-1;PositionNotify[i].EventNotifyHandle=mNotificationEvent.SafeWaitHandle.DangerousGetHandle();}mNotify=newNotify(mRecBuffer);mNotify.SetNotificationPositions(PositionNotify,cNotifyNum);returntrue;}privatevoidWaitThread(){while(true){ mNotificationEvent.WaitOne(Timeout.Infinite,true);//录制数据RecordCapturedData();}}///<summary>///将录制的数据写入wav文件///</summary>privatevoidRecordCapturedData(){byte[]CaptureData=null;intReadPos=0,CapturePos=0,LockSize=0;mRecBuffer.GetCurrentPosition(outCapturePos,outReadPos);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;//Circularbuffer}privatevoidCreateSoundFile(){//Openupthewavefileforwriting.mWaveFile=newFileStream(mFileName,FileMode.Create);mWriter=newBinaryWriter(mWaveFile); char[]ChunkRiff={'R','I','F','F'};char[]ChunkType={'W','A','V','E'};char[]ChunkFmt={'f','m','t',''};char[]ChunkData={'d','a','t','a'};shortshPad=1; intnFormatChunkLength=0x10;intnLength=0; shortshBytesPerSample=0; if(8==mWavFormat.BitsPerSample&&1==mWavFormat.Channels)shBytesPerSample=1;elseif((8==mWavFormat.BitsPerSample&&2==mWavFormat.Channels)||(16==mWavFormat.BitsPerSample&&1==mWavFormat.Channels))shBytesPerSample=2;elseif(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);//Thesamplelengthwillbewritteninlater.}#end

5、需要添加的外部引用文件在系统的System32目录下添加以下两个引用文件,如果没有,在DirectX的开发包内可以找到。Microsoft.DirectX.dllMicrosoft.DirectX.DirectSound.dll

© 手抄报圈