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

问题描述 投票:3回答:3

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

非常感谢您的帮助,谢谢。

windows audio recording
3个回答
3
投票

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

一旦启用,您将在DirectShow应用程序中看到该设备:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9LeWs3ZS5wbmcifQ==” alt =“在此处输入图像描述”>


1
投票

检查NAudio并在此线程中找到WasapiLoopbackCapture类,该类将从声卡中获取输出,并将其转换为可录制的wavein设备或其他内容...

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


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();
                }
            }

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.