public void CanDownsampleAnMp3File() { string testFile = @"D:\Audio\Music\Coldplay\Mylo Xyloto\03 - Paradise.mp3"; if (!File.Exists(testFile)) Assert.Ignore(testFile); string outFile = @"d:\test22.wav"; using (var reader = new AudioFileReader(testFile)) { // downsample to 22kHz var resampler = new WdlResamplingSampleProvider(reader, 22050); var wp = new SampleToWaveProvider(resampler); using (var writer = new WaveFileWriter(outFile, wp.WaveFormat)) { byte[] b = new byte[wp.WaveFormat.AverageBytesPerSecond]; while (true) { int read = wp.Read(b, 0, b.Length); if (read > 0) writer.Write(b, 0, read); else break; } } //WaveFileWriter.CreateWaveFile(outFile, ); } }
private void Play() { if (waveOut != null) { Stop(); } waveOut = new WaveOut(); this.patternSequencer = new DrumPatternSampleProvider(pattern); this.patternSequencer.Tempo = tempo; IWaveProvider wp = new SampleToWaveProvider(patternSequencer); waveOut.Init(wp); waveOut.Play(); }
public AudioDevice(string fileName) : this() { ISampleProvider sampleProvider = new AudioFileReader(fileName); this.fileWaveStream = (WaveStream) sampleProvider; // create sample channel SampleToWaveProvider waveProvider = new SampleToWaveProvider(sampleProvider); this.sampleChannel = new SampleChannel(waveProvider, true); this.sampleChannel.PreVolumeMeter += OnPreVolumeMeter; // play //IWavePlayer waveOut = new WaveOut(); //waveOut.Init(waveProvider); //waveOut.Play(); }
public void OpenFile(string path) { if (System.IO.File.Exists(path)) { try { StopAndCloseStream(); // Dispose Init(); // and reinitialize the waveoutdevice //WaveStream waveStream = (WaveStream) new AudioFileReader(path); //WaveChannel32 waveChannel32 = new WaveChannel32(waveStream); //waveOutDevice.Init(waveChannel32); ISampleProvider sampleProvider = (ISampleProvider) new AudioFileReader(path); WaveProvider = new SampleToWaveProvider(sampleProvider); waveOutDevice.Init(waveProvider); CanPlay = true; } catch { waveProvider = null; CanPlay = false; } } }
public void OpenSampleProvider(ISampleProvider sampleProvider) { try { StopAndCloseStream(); // Dispose Init(); // and reinitialize the waveoutdevice WaveProvider = new SampleToWaveProvider(sampleProvider); waveOutDevice.Init(waveProvider); CanPlay = true; } catch { waveProvider = null; CanPlay = false; } }
private async void PlayThread() { await Activate(); var playbackProvider = Init(); bool isClientRunning = false; try { if (this.resamplerNeeded) { var resampler = new WdlResamplingSampleProvider(playbackProvider.ToSampleProvider(), outputFormat.SampleRate); playbackProvider = new SampleToWaveProvider(resampler); } // fill a whole buffer bufferFrameCount = audioClient.BufferSize; bytesPerFrame = outputFormat.Channels*outputFormat.BitsPerSample/8; readBuffer = new byte[bufferFrameCount*bytesPerFrame]; FillBuffer(playbackProvider, bufferFrameCount); int timeout = 3 * latencyMilliseconds; while (playbackState != WasapiOutState.Disposed) { if (playbackState != WasapiOutState.Playing) { playThreadEvent.WaitOne(500); } // If still playing and notification is ok if (playbackState == WasapiOutState.Playing) { if (!isClientRunning) { audioClient.Start(); isClientRunning = true; } // If using Event Sync, Wait for notification from AudioClient or Sleep half latency var r = NativeMethods.WaitForSingleObjectEx(frameEventWaitHandle, timeout, true); if (r != 0) throw new InvalidOperationException("Timed out waiting for event"); // See how much buffer space is available. int numFramesPadding = 0; // In exclusive mode, always ask the max = bufferFrameCount = audioClient.BufferSize numFramesPadding = (shareMode == AudioClientShareMode.Shared) ? audioClient.CurrentPadding : 0; int numFramesAvailable = bufferFrameCount - numFramesPadding; if (numFramesAvailable > 0) { FillBuffer(playbackProvider, numFramesAvailable); } } if (playbackState == WasapiOutState.Stopping) { // play the buffer out while (audioClient.CurrentPadding > 0) { await Task.Delay(latencyMilliseconds / 2); } audioClient.Stop(); isClientRunning = false; audioClient.Reset(); playbackState = WasapiOutState.Stopped; RaisePlaybackStopped(null); } if (playbackState == WasapiOutState.Disposing) { audioClient.Stop(); isClientRunning = false; audioClient.Reset(); playbackState = WasapiOutState.Disposed; var disposablePlaybackProvider = playbackProvider as IDisposable; if (disposablePlaybackProvider!=null) disposablePlaybackProvider.Dispose(); // do everything on this thread, even dispose in case it is Media Foundation RaisePlaybackStopped(null); } } } catch (Exception e) { RaisePlaybackStopped(e); } finally { audioClient.Dispose(); audioClient = null; renderClient = null; NativeMethods.CloseHandle(frameEventWaitHandle); } }
private void RestartAudio() { if(FWaveOut != null) { Dispose(); } if(FInput[0] != null) { FWaveOut = new WasapiOut(AudioClientShareMode.Shared, 4); FWaveProvider = new SampleToWaveProvider(FInput[0]); FWaveOut.Init(FWaveProvider); FWaveOut.Play(); } }
/// <summary> /// Begins playing a song with the specified file name. Only .mp3, .wma, and .m4a are supported. /// </summary> /// <param name="fileName">File name of the song in the "Content" directory</param> public static void playSong(string fileName) { if (fileName.StartsWith(Directories.MUSIC)) fileName = fileName.Substring(Directories.MUSIC.Length); System.Diagnostics.Debug.WriteLine(fileName); if (fileName.Equals(currentSongName)) { if (fadingOut) { fadeIn(); System.Diagnostics.Debug.WriteLine("FADE in"); } return; } if (playing) stopSong(); //cut off current song if (!tryAllFileTypes(fileName)) { throw new System.IO.FileNotFoundException("Could not find the music file \"" + fileName + "\" in the \"Music\" directory" + " in Content.\nMake sure the file is .mp3, .m4a, or .wma, and \"Copy to output directory\" settings" + " are set to \"Copy if newer\"."); } if (wavePlayer == null) wavePlayer = new WaveOutEvent(); //initialize wavePlayer fadeInOut = new FadeInOutSampleProviderAdapted(file); sampleToWave = new SampleToWaveProvider(fadeInOut); wavePlayer.Init(sampleToWave); if (!paused) { wavePlayer.Play(); playing = true; } currentSongName = fileName; }
public void Play() { if (waveOut != null) { Stop(); } waveOut = new WaveOut(); this.patternSequencer = new DrumPatternSampleProvider(pattern); //this.patternSequencer.Tempo = tempo; this.tempoController.setPatternSequencer(ref this.patternSequencer); this.tempoController.updateTempo(); this.pitchController.setPatternSequencer(this.patternSequencer); this.pitchController.reloadState(); IWaveProvider wp = new SampleToWaveProvider(patternSequencer); waveOut.Init(wp); waveOut.Play(); }