ArchAngel
ArchAngel

Reputation: 644

How to record and playback audio from default audio device

I can't seem to record the audio from default audio device, and play it on another audio device.. I don't want to record the microphone, but the audio device..

When I play a movie, I can hear sound, through my headphones, I want to copy that sound to any audio device I choose..

If you have any suggestions, it doesn't have to be with NAudio..

As far as I can tell, NAudio can't do this..

This is the code I use for the task at this time, but it only takes input from my Microphone: Code snippet with NAudio..

void playSoundCopy(int dv0)
{
    disposeWave0();// stop previous sounds before starting
    var waveOut0 = new WaveOut();
    waveOut0.DeviceNumber = dv0;
    wave0 = waveOut0;

    Defaultwave0 = new WaveIn();
    Defaultwave0.DeviceNumber = (int)GetDefaultDevice(Defaultdevice.FriendlyName);
    var waveinReader0 = new WaveInProvider(Defaultwave0);
    wave0.Init(waveinReader0);

    play0 = false;
    Thread.Sleep(1000);

    play0 = true;
    t0 = new Thread(() => { timeline0(); });
    t0.IsBackground = true;
    t0.Start();

    Defaultwave0.StartRecording();
    wave0.Play();
}

The real problem is actually that I can't record from a WaveOut device, only WaveIn..

Working Result:

void playSoundCopy(int dv0)
{
    disposeWave0();// stop previous sounds before starting
    var waveOut0 = new WaveOut();
    waveOut0.DeviceNumber = dv0;
    wave0 = waveOut0;

    var format0 = Defaultdevice.AudioClient.MixFormat;
    buffer0 = new BufferedWaveProvider(format0);
    wave0.Init(buffer0);

    capture = new WasapiLoopbackCapture(Defaultdevice);
    capture.ShareMode = AudioClientShareMode.Shared;
    capture.DataAvailable += CaptureOnDataAvailable;

    play0 = false;
    Thread.Sleep(1000);

    play0 = true;
    t0 = new Thread(() => { timeline0(); });
    t0.IsBackground = true;
    t0.Start();

    capture.StartRecording();
    wave0.Play();
}
void CaptureOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
     try
     {
         var itm = new byte[waveInEventArgs.BytesRecorded];
         Array.Copy(waveInEventArgs.Buffer, itm, waveInEventArgs.BytesRecorded);
         buffer0.AddSamples(itm, 0, itm.Length);
     }
     catch { }
}

Upvotes: 0

Views: 1364

Answers (1)

Mark Heath
Mark Heath

Reputation: 49522

You can capture audio being sent to a device using WasapiLoopbackCapture. Then you could pipe that into a BufferedWaveProvider and use that to feed another output device. There would be some latency introduced though, so don't expect the two devices to be in sync.

Upvotes: 1

Related Questions