Is it possible to record sound played on the sound card?

我们两清 提交于 2020-01-19 04:05:29

问题


Is it even remotely possible to record sound that is being played on the sound card? Supposedly, I am playing an audio file, what I need is to redirect the output to the disk. DirectShow or might be feasible.

Any help is greatly appreciated, thanks.


回答1:


You need to enable audio loopback device, and you will be able to record from in a stadnard way with all the well-known APIs (including DirectShow).

  • Loopback Recording
  • Enabling "Stereo Mix" in Vista
  • Capturing Window's audio in C#

Once enabled, you will see the device on DirectShow apps:




回答2:


Check out NAudio and this thread for a WasapiLoopbackCapture class that takes the output from your soundcard and turns it into a wavein device you can record or whatever...

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




回答3:


My Solution C# NAUDIO Library 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

Events

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 -> Optional

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

        }
    }
}


来源:https://stackoverflow.com/questions/8241352/is-it-possible-to-record-sound-played-on-the-sound-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!