在Windows 7中录制时获取麦克风声级

时间:2013-02-11 08:58:15

标签: c# windows-7 winmm mcisendstring

我正在实现一个简单的记录,播放可以播放和录制WAV文件的应用程序 要求如下:

1)Windows 7,Visual Studio 2010(或2012),C#
2)记录,播放带有规格信息的Wav文件:samplerate 22050,mono,bitspersample 16
3)录制时可以显示微声级

使用1)和2),我可以使用lib(winmm.dll)和mciSendString(),如下所示

[DllImport("winmm.dll")]  
private static extern long mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

// Open file to play
public void open(string file)
{
    string command = "open \"" + file + "\" type waveaudio  alias MyWav";
    mciSendString(command, null, 0, 0);
}

// Start playing
public void play()
{
    string command = "play MyWav";
    mciSendString(command, null, 0, 0);
}

// Stop playing
public void stop()
{
    string command = "stop MyWav";
    mciSendString(command, null, 0, 0);

    command = "close MyWav";
    mciSendString(command, null, 0, 0);
}

// Start recording
public void record()
{
   string command = "open new Type waveaudio Alias MyRec";
    mciSendString(command, "", 0, 0);

    command = "set capture time format ms bitspersample 16 channels 1 samplespersec 22050 alignment 4";
    mciSendString(command, "", 0, 0);

    command = "record MyRec";
    mciSendString(command, "", 0, 0);
}

// Stop recording
public void stoprec()
{
    string fileName = @"""D:\Rec1.wav""";

    string command = "stop MyRec";
    mciSendString(command, "", 0, 0);

    command = @"save MyRec " + fileName;

    Debug.WriteLine(command);
    mciSendString(command, "", 0, 0);

    command = "close MyRec";
    mciSendString(command, "", 0, 0);
}

使用3),我也想使用mciSendString。我找到了以下命令(状态):

string command = "status MyRec level ";
long mci_ret = mciSendString(command, "", 0, 0);

但是我不知道从上面的命令得到音量,有人可以告诉我的方式吗?

参考:来自mciSendString()的状态命令可在Microsoft doc中找到: http://msdn.microsoft.com/en-us/library/windows/desktop/dd798683%28v=vs.85%29.aspx
它说: level:返回当前PCM音频样本值。

1 个答案:

答案 0 :(得分:0)

我通过打开两个设备来监控关卡,一个用于录制/播放(track1),另一个用于获取关卡(waveLevel)。以下是foxpro。

* OPEN WAVE DEVICE FOR LEVEL READINGS 
= mciSendString("open new type waveaudio alias waveLevel", '0&', 0, 0) 

* START RECORDING
= mciSendString("open new type waveaudio alias track1", '0&', 0, 0)

然后获得track1的级别我使用waveLevel设备

= mciSendString("status waveLevel level", @cBuff, LEN(cBuff) ,0)