/// <summary> /// Initializes a new instance of the <see cref="SoundInSource"/> class. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <param name="bufferSize">Size of the internal buffer in bytes.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public SoundInSource(ISoundIn soundIn, int bufferSize) { if (soundIn == null) throw new ArgumentNullException("soundIn"); ThrowIfSoundInNotInitialized(soundIn); _buffer = new WriteableBufferingSource(soundIn.WaveFormat, bufferSize) {FillWithZeros = false}; _soundIn = soundIn; _soundIn.DataAvailable += OnDataAvailable; }
/// <summary> /// Creates an new instance of SoundInSource with a default bufferSize of 5 seconds. /// </summary> /// <param name="soundIn">The soundIn which provides recorded data.</param> /// <param name="bufferSize">Size of the buffer in bytes.</param> /// <remarks> /// Note that soundIn has to be already initialized. /// Note that old data ("old" gets specified by the bufferSize) gets overridden. /// For example, if the bufferSize is about 5 seconds big, data which got recorded 6 seconds ago, won't be available anymore. /// </remarks> public SoundInSource(ISoundIn soundIn, int bufferSize) { if (soundIn == null) throw new ArgumentNullException("soundIn"); //bufferSize gets validated by WriteableBufferingSource _buffer = new WriteableBufferingSource(soundIn.WaveFormat, bufferSize); _buffer.FillWithZeros = false; _soundIn = soundIn; _soundIn.DataAvailable += OnDataAvailable; }
protected virtual void Dispose(bool disposing) { if (!_disposed) { if (SoundIn != null) { SoundIn.DataAvailable -= OnDataAvailable; _soundIn = null; } if (_buffer != null) { _buffer.Dispose(); _buffer = null; } } _disposed = true; }
private void _session_MusicDeliver(object sender, MusicDeliveryEventArgs e) { int consumed = 0; lock (_lockObject) { if (_provider == null) { _provider = new WriteableBufferingSource(new WaveFormat(e.Rate, 16, e.Channels, AudioEncoding.Pcm), (e.Rate * 2)) { FillWithZeros = false }; _waveOut.Stop(); _waveOut.Initialize(_provider); _waveOut.Volume = _volume; } List<bool> results = MusicPlayer.Current.Plugins.Select(plugin => plugin.MusicDeliver(new PluginMusicDeliveryArgs(e, _provider))).ToList(); _continuePlay = results.Count == 0 || results.All(result => !result); if (!_continuePlay) { _waveOut?.Stop(); } if (_continuePlay) { _waveOut.Play(); } if ((_provider.MaxBufferSize - _provider.Length) > e.Samples.Length) { _provider.Write(e.Samples, 0, e.Samples.Length); consumed = e.Frames; } } e.ConsumedFrames = consumed; }
public void PlaySong(ITrack song) { _waveOut?.Stop(); Session.PlayerPause(); Session.PlayerUnload(); lock (_lockObject) { _provider = null; } var errorCode = Session.PlayerLoad(song); if (!song.WaitUntilLoaded(500) || errorCode != Error.OK) { MessageBox.Show("Error: " + errorCode); return; } Session.PlayerPlay(); }