/// <summary> /// Initializes this audio source with the given audio device. <see cref="Start"/> may be called afterwards. /// </summary> /// <param name="device"></param> public void SetAudioDevice(AudioDevice device) { this.Device = device; var naudioDevice = NAudioUtilities.GetDevice(device); if (naudioDevice.DataFlow == DataFlow.Capture) { this.Capture = new WasapiCapture(naudioDevice); } else { this.Capture = new WasapiLoopbackCapture(naudioDevice); } this.Format = NAudioUtilities.FromNAudioWaveFormat(this.Capture.WaveFormat); //set up event listeners this.Capture.DataAvailable += (s, e) => { if (e.BytesRecorded == 0) { return; } this.DataAvailable?.Invoke(this, new StreamAudioSourceDataEvent() { Buffer = new ArraySegment <byte>(e.Buffer, 0, e.BytesRecorded), Format = Format }); }; this.Capture.RecordingStopped += (s, e) => { var cause = StreamAudioSourceStoppedCause.Unknown; if (StopRequested) { cause = StreamAudioSourceStoppedCause.Stopped; } else if (e.Exception != null) { cause = StreamAudioSourceStoppedCause.Exception; } this.Stopped?.Invoke(this, new StreamAudioSourceStoppedEvent() { Cause = cause, Exception = e.Exception }); }; }
public NAudioWaveStreamToStreamAudioSourceAdapter(WaveStream stream) { this.SourceStream = stream; this.Format = NAudioUtilities.FromNAudioWaveFormat(stream.WaveFormat); }