3

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.

  • You will probably need to be much more specific in order for someone to help you, or even to figure out if this is appropriate for Stackoverflow rather than one of the other sites. e.g. "I am playing an audio file" could mean anything from "I clicked an MP3 file" to "I am using ASIO to play back samples recorded to file". The sentence "DirectShow or might be feasible" looks like you probably meant to say more, because as written it's gibberish. – tialaramex Nov 23 '11 at 11:29

3 Answers3

3

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).

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

enter image description here

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Hi, would it be possible to manually feed audios into my microphone? – BAKE ZQ May 16 '20 at 05:46
  • @BAKEZQ: Not directly. You can use some of virtual audio driver software to achieve this. – Roman R. May 16 '20 at 07:30
  • If it's possible to write my own virtual MIC driver with a socket endpont exposed or other communication endpoint? Thus all program can control the input of my MIC. I found a softwar named WO MIC that contains both a driver and a client, and the client can communicate(Send audio as input) with this VMIC, but I don't know how it works. – BAKE ZQ May 16 '20 at 09:47
  • @BAKEZQ Writing a driver is a challenging task, most likely you don't want to do it at all because the complexity is underestimated. You should probably look into software like Virtual Audio Cable that already implements a necessary driver. You should be able to create a pair of devices: you play audio to one (virtual speakers) and the software loops signal back to the other virtual mic which applications recognize as a true mic device. – Roman R. May 16 '20 at 10:18
  • Thanks, Can what I said be realized? – BAKE ZQ May 16 '20 at 12:01
  • @BAKEZQ Yes exactly as I mentioned above: you would play audio into one end of the pipe, including like with a regular player, and the other end - virtual microphone - would expose this signal as if it is captured live. Applications set up to use this device as a mic will treat this as a real mic. – Roman R. May 16 '20 at 12:13
  • What about the exposed transmission endpoint? If it's possible? – BAKE ZQ May 16 '20 at 12:16
1

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/

DanW
  • 1,976
  • 18
  • 14
0

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

        }
    }
}
Murat Çakmak
  • 151
  • 2
  • 7