public MemoryReader(string audioFileName) { using (var audioFileReader = new AudioFileReader(audioFileName)) { // TODO: could add resampling in here if required this.waveFormat = audioFileReader.WaveFormat; this.length = audioFileReader.Length; var wholeFile = new List<float>((int)(audioFileReader.Length / 4)); var readBuffer= new float[audioFileReader.WaveFormat.SampleRate * audioFileReader.WaveFormat.Channels]; int samplesRead; while((samplesRead = audioFileReader.Read(readBuffer,0,readBuffer.Length)) > 0) { wholeFile.AddRange(readBuffer.Take(samplesRead)); } AudioData = wholeFile.ToArray(); } memorySampleProvider = new MemoryReaderSampleProvider(this); }
public AutoDisposeFileReader(AudioFileReader reader) { this.reader = reader; this.WaveFormat = reader.WaveFormat; }
protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // Free other state (managed objects). } // Free your own state (unmanaged objects). // Set large fields to null. audioFileReader = null; waveOut = null; foreach (string file in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\wamp")) { if (file.ToLower().EndsWith(".wav")) File.Delete(file); } disposed = true; } }
public void Stop() { if (waveOut != null) { if (audioFileReader != null) { try { audioFileReader.Dispose(); } catch (Exception) { Console.WriteLine("AudioFileReader already disposed."); } audioFileReader = null; } if (memoryFile != null) { memoryFile.Dispose(); memoryFile = null; } CloseWaveOut(); } }
public void Play(string filePath, AudioOutput output) { if (scheduleMemoryPlaySettingsChange) { if (memoryPlay == true) { memoryPlay = false; if (memoryFile != null) { memoryFile.Dispose(); memoryFile = null; } GC.Collect(); } else memoryPlay = true; scheduleMemoryPlaySettingsChange = false; } if (memoryPlay) { if (!memoryFileTooBig) { PlayFileFromMemory(filePath, output); return; } else memoryFileTooBig = false; } // first check to see if we are playing the same file or are paused. if (waveOut != null) { if (waveOut.PlaybackState == PlaybackState.Playing && filePath.Equals(currentFile)) { //isDriverStopped = false; return; } else if (waveOut.PlaybackState == PlaybackState.Paused) { waveOut.Play(); //playbackTimer.Enabled = true; return; } } if (IsFlacFile(filePath)) { string tempWavName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\wamp\\temp.wav"; string aheadFileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\wamp\\" + filePath.Split('\\').Last().ToLower().Replace(".flac", ".wav"); if (File.Exists(aheadFileName)) { if (File.Exists(tempWavName)) { mixingSampleProvider.RemoveAllMixerInputs(); File.Delete(tempWavName); } File.Move(aheadFileName, tempWavName); filePath = tempWavName; } else { FlacDecoder.DecodeFlacToWav(filePath, tempWavName); filePath = tempWavName; } } if (memoryFile != null) { memoryFile.Dispose(); memoryFile = null; } audioFileReader = new AudioFileReader(filePath); try { bitDepth = audioFileReader.WaveFormat.BitsPerSample; sampleRate = audioFileReader.WaveFormat.SampleRate; } catch (Exception createException) { Console.WriteLine(String.Format("{0}", createException.Message), "Error Loading File"); return; } AutoDisposeFileReader autoDisposeFileReader = new AutoDisposeFileReader(audioFileReader); autoDisposeFileReader.FileReaderFinishedEvent += HandleFileReaderFinishedEvent; if (isVolumeEnabled) { volumeSampleProvider = new VolumeSampleProvider(autoDisposeFileReader); volumeSampleProvider.Volume = currentVolume; } // if we already have an existing output of the same bit depth and sample rate, use it if (waveOut != null && bitDepth == previousBitDepth && sampleRate == previousSampleRate) { if (isVolumeEnabled) mixingSampleProvider.AddMixerInput(volumeSampleProvider); else mixingSampleProvider.AddMixerInput(autoDisposeFileReader); } else //create a new output { try { CreateWaveOut(output); } catch (InvalidOutputDeviceException e) { audioFileReader.Dispose(); audioFileReader = null; throw e; } mixingSampleProvider = null; mixingSampleProvider = new MixingSampleProvider(audioFileReader.WaveFormat); mixingSampleProvider.ReadFully = true; if (isVolumeEnabled) mixingSampleProvider.AddMixerInput(volumeSampleProvider); else mixingSampleProvider.AddMixerInput(autoDisposeFileReader); try { waveOut.Init(mixingSampleProvider); } catch (Exception initException) { Console.WriteLine(String.Format("{0}", initException.Message), "Error Initializing Output"); return; } waveOut.Play(); } currentFile = filePath; previousBitDepth = bitDepth; previousSampleRate = sampleRate; //playbackTimer.Enabled = true; }