在C#中设置MCI音频参数的正确方法
Correct way to set MCI audio parameters in C#
我正在尝试录制 wav 文件。我的应用程序需要允许用户在不同的采样率和位深度之间进行选择。不管我尝试什么语法,我总是得到具有相同比特率的文件。有人可以告诉我我做错了什么吗?
[DllImport("winmm.dll")]
private static extern long mciSendString(string command, StringBuilder retstring, int returnLength, IntPtr callback);
...
private void btnRecord_Click(object sender, EventArgs e)
{
mciSendString("Open new Type waveaudio alias recSound", null, 0, IntPtr.Zero);
mciSendString("setaudio recSound algorithm pcm", null, 0, IntPtr.Zero);
mciSendString("setaudio recSound samplespersec to 44100", null, 0, IntPtr.Zero);
DisableRecordingButtons();
mciSendString("Record recSound", null, 0, IntPtr.Zero);
}
setaudio 命令用于视频而非音频。显然你需要用set命令来设置音频参数,并且需要按照正确的顺序设置参数。设置命令应如下所示...
string setCommand = "set recSound alignment 4 bitspersample " + BitDepth.ToString() + " samplespersec " + SampleRate.ToString() + " channels 1 bytespersec " +
(BitDepth * SampleRate * 1 / 8).ToString() + " time format milliseconds format tag pcm";
mciSendString(setCommand, null, 0, IntPtr.Zero);
对齐参数始终为 4,bytespersec 始终为 (bitspersample * samplespersec * channels / 8)。
我正在尝试录制 wav 文件。我的应用程序需要允许用户在不同的采样率和位深度之间进行选择。不管我尝试什么语法,我总是得到具有相同比特率的文件。有人可以告诉我我做错了什么吗?
[DllImport("winmm.dll")]
private static extern long mciSendString(string command, StringBuilder retstring, int returnLength, IntPtr callback);
...
private void btnRecord_Click(object sender, EventArgs e)
{
mciSendString("Open new Type waveaudio alias recSound", null, 0, IntPtr.Zero);
mciSendString("setaudio recSound algorithm pcm", null, 0, IntPtr.Zero);
mciSendString("setaudio recSound samplespersec to 44100", null, 0, IntPtr.Zero);
DisableRecordingButtons();
mciSendString("Record recSound", null, 0, IntPtr.Zero);
}
setaudio 命令用于视频而非音频。显然你需要用set命令来设置音频参数,并且需要按照正确的顺序设置参数。设置命令应如下所示...
string setCommand = "set recSound alignment 4 bitspersample " + BitDepth.ToString() + " samplespersec " + SampleRate.ToString() + " channels 1 bytespersec " +
(BitDepth * SampleRate * 1 / 8).ToString() + " time format milliseconds format tag pcm";
mciSendString(setCommand, null, 0, IntPtr.Zero);
对齐参数始终为 4,bytespersec 始终为 (bitspersample * samplespersec * channels / 8)。