C#如何通过编程调整WAV 音频文件的音量
1、C#中使用DirectSound录音
声卡录音的基本原理
为了实现一个录音的基本过程,至少需要以下对象的支持:
1. 录音设备,对我们的PC设备就是声卡。这个录音设备可以进行的操作应该有开始和关闭。
2. 缓冲区,也就是录制的声音放在哪里的问题。

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

3、对外操作的函数

4、内部调用函数代码
:
#region 对内操作函数
/// <summary>
/// 初始化录音设备,此处使用主录音设备.
/// </summary>
/// <returns>调用成功返回true,否则返回false</returns>
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();
}
}
/// <summary>
/// 将录制的数据写入wav文件
/// </summary>
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
5、需要添加的外部引用文件
在系统的System32目录下添加以下两个引用文件,如果没有,在DirectX的开发包内可以找到。
Microsoft.DirectX.dll
Microsoft.DirectX.DirectSound.dll