public IStreamAudioSource GetStreamAudioSource(AudioDevice device) { var naudioDevice = NAudioUtilities.GetDevice(device); if (naudioDevice == null) { //no device found return(null); } //try WASAPI first var wasapiSource = ServiceProvider.GetService(typeof(NAudioWasapiStreamAudioSource)) as NAudioWasapiStreamAudioSource; if (wasapiSource != null) { //WASAPI is available try { wasapiSource.SetAudioDevice(device); return(wasapiSource); } catch (Exception e) { //not supported? Logger.LogError(e, "Could not initialize NAudio WASAPI source"); wasapiSource.Dispose(); } } return(null); }
/// <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 NAudioStreamAudioSourceToWaveProviderAdapterSimple(IStreamAudioSource source) { this.WaveFormat = NAudioUtilities.ToNAudioWaveFormat(source.Format); }
public NAudioWaveStreamToStreamAudioSourceAdapter(WaveStream stream) { this.SourceStream = stream; this.Format = NAudioUtilities.FromNAudioWaveFormat(stream.WaveFormat); }
/// <summary> /// Initializes this resampler with the given input audio source and output format. /// Attaches to the given source's event to start resampling as soon as <see cref="IStreamAudioSource.DataAvailable"/> is raised. /// </summary> /// <param name="audioSource"></param> /// <param name="outputFormat"></param> public void Initialize(IStreamAudioSource audioSource, WaveStreamAudioFormat outputFormat) { this.WaveProviderAdapter = new NAudioStreamAudioSourceToWaveProviderAdapterSimple(audioSource); this.Resampler = new MediaFoundationResampler(this.WaveProviderAdapter, NAudioUtilities.ToNAudioWaveFormat(outputFormat)); //set this *after* we initialize the resampler. if it throws, we won't dispose the input audio source by accident this.WrappedAudioSource = audioSource; this.Format = outputFormat; //handle events from the wrapped source audioSource.DataAvailable += (s, e) => { //feed into our adapter WaveProviderAdapter.Write(e); //read from resampler and trigger our own output event int read; while ((read = Resampler.Read(Buffer, 0, Buffer.Length)) > 0) { DataAvailable?.Invoke(this, new StreamAudioSourceDataEvent() { Buffer = new ArraySegment <byte>(Buffer, 0, read), Format = Format }); } }; }