是否可以录制声卡上播放的声音?

时间:2011-11-23 11:24:56

标签: windows audio recording

是否可以远程录制正在声卡上播放的声音? 据说,我正在播放音频文件,我需要的是将输出重定向到磁盘。 DirectShow或可能是可行的。

非常感谢任何帮助,谢谢。

3 个答案:

答案 0 :(得分:3)

您需要启用音频环回设备,并且您将能够以标准方式使用所有众所周知的API(包括DirectShow)进行录制。

启用后,您将在DirectShow应用上看到该设备:

enter image description here

答案 1 :(得分:1)

查看NAudio和这个线程的WasapiLoopbackCapture类,该类从你的声卡中获取输出并将其变成你可以录制的wavein设备或者其他...

https://naudio.codeplex.com/discussions/203605/

答案 2 :(得分:0)

我的解决方案C#NAUDIO库v1.9.0

waveInStream = new WasapiLoopbackCapture(); //record sound card.
waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(this.OnDataAvailable); // sound data event
waveInStream.RecordingStopped += new EventHandler<StoppedEventArgs>(this.OnDataStopped); // record stopped event
MessageBox.Show(waveInStream.WaveFormat.Encoding + " - " + waveInStream.WaveFormat.SampleRate +" - " + waveInStream.WaveFormat.BitsPerSample + " - " + waveInStream.WaveFormat.Channels);
//IEEEFLOAT - 48000 - 32 - 2
//Explanation: IEEEFLOAT = Encoding | 48000 Hz | 32 bit | 2 = STEREO and 1 = mono
writer = new WaveFileWriter("out.wav", new WaveFormat(48000, 24, 2));
waveInStream.StartRecording(); // record start

事件

WaveFormat myOutFormat = new WaveFormat(48000, 24, 2); // --> Encoding PCM standard.
private void OnDataAvailable(object sender, WaveInEventArgs e)
{
    //standard e.Buffer encoding = IEEEFLOAT
    //MessageBox.Show(e.Buffer + " - " + e.BytesRecorded);
    //if you needed change for encoding. FOLLOW WaveFormatConvert ->
    byte[] output = WaveFormatConvert(e.Buffer, e.BytesRecorded, waveInStream.WaveFormat, myOutFormat);            

    writer.Write(output, 0, output.Length);

}

private void OnDataStopped(object sender, StoppedEventArgs e)
{
    if (writer != null)
    {
        writer.Close();
    }

    if (waveInStream != null)
    {
        waveInStream.Dispose();                
    }            

}

WaveFormatConvert->可选

public byte[] WaveFormatConvert(byte[] input, int length, WaveFormat inFormat, WaveFormat outFormat)
{
    if (length == 0)
        return new byte[0];
    using (var memStream = new MemoryStream(input, 0, length))
    {
        using (var inputStream = new RawSourceWaveStream(memStream, inFormat))
        {                    
            using (var resampler = new MediaFoundationResampler(inputStream, outFormat)) {
                resampler.ResamplerQuality = 60; // 1 low - 60 max high                        
                //CONVERTED READ STREAM
                byte[] buffer = new byte[length];
                using (var stream = new MemoryStream())
                {
                    int read;
                    while ((read = resampler.Read(buffer, 0, length)) > 0)
                    {
                        stream.Write(buffer, 0, read);
                    }
                    return stream.ToArray();
                }
            }

        }
    }
}