public static void CreateWave(Stream oggStream, Stream wavStream) { VorbisWaveReader vorbisReader = new VorbisWaveReader(oggStream); SampleToWaveProvider16 converter = new SampleToWaveProvider16(vorbisReader); using (var writer = new WaveFileWriter(wavStream, converter.WaveFormat)) { int loopCount = 0; var buffer = new byte[converter.WaveFormat.AverageBytesPerSecond * 4]; while (true) { loopCount++; Debug.WriteLine("looped: " + loopCount.ToString()); int bytesRead = converter.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { // end of source provider break; } // Write will throw exception if WAV file becomes too large writer.Write(buffer, 0, bytesRead); } } vorbisReader.Dispose(); }
public void PlaySound(string id) { NAudio.Vorbis.VorbisWaveReader data = _soundMap[id]; if (data != null) { soundPlayer.Init(data); soundPlayer.Play(); } }
public void PlayMusic(string id) { NAudio.Vorbis.VorbisWaveReader data = _musicMap[id]; if (data != null) { musicPlayer.Init(data); musicPlayer.Play(); } }
public void StopSound(string id) { NAudio.Vorbis.VorbisWaveReader data = _soundMap[id]; if (data != null) { soundPlayer.Stop(); data.Position = 0; data.Dispose(); _soundMap.Remove(id); } }
public void StopMusic(string id) { NAudio.Vorbis.VorbisWaveReader data = _musicMap[id]; if (data != null) { musicPlayer.Stop(); data.Position = 0; data.Dispose(); _musicMap.Remove(id); } }
public WaveStream[] Read() { SectionHeader sectionHeader = _input.ReadContent<SectionHeader>(); SscfHeader sscfHeader = _input.ReadContent<SscfHeader>(); BinaryReader br = new BinaryReader(_input); _input.SetPosition(sscfHeader.WavesOffset); int[] offsets = new int[sscfHeader.NumWaves]; for (int i = 0; i < offsets.Length; i++) offsets[i] = br.ReadInt32(); WaveStream[] result = new WaveStream[sscfHeader.NumWaves]; for (int i = 0; i < offsets.Length; i++) { _input.SetPosition(offsets[i]); SscfWaveHeader waveHeader = _input.ReadContent<SscfWaveHeader>(); if (waveHeader.Format == SscfWaveFormat.Vorbis) { _input.SetPosition(waveHeader.DataOffset); byte[] vorbisData = _input.EnsureRead(waveHeader.DataLength); MemoryStream vorbisMs = new MemoryStream(vorbisData, 0, vorbisData.Length, false); result[i] = new VorbisWaveReader(vorbisMs); continue; } WaveFormat format = ReadWaveFormat(waveHeader); if (format == null) continue; _input.SetPosition(waveHeader.DataOffset); byte[] data = _input.EnsureRead(waveHeader.DataLength); MemoryStream ms = new MemoryStream(data, 0, data.Length, false); result[i] = new RawSourceWaveStream(ms, format); } return result; }
private bool Load(string fileName, string id, SoundType type) { if (type == SoundType.MUSIC) { NAudio.Vorbis.VorbisWaveReader soundPlayer = new NAudio.Vorbis.VorbisWaveReader(fileName); if (soundPlayer != null) { _musicMap.Add(id, soundPlayer); return(true); } } if (type == SoundType.SOUND) { NAudio.Vorbis.VorbisWaveReader soundPlayer = new NAudio.Vorbis.VorbisWaveReader(fileName); if (soundPlayer != null) { _soundMap.Add(id, soundPlayer); return(true); } } return(false); }
private void playSound(Stream stream, CancellationToken token) { try { using(VorbisWaveReader vorbisStream = new VorbisWaveReader(stream)) { using(WaveOutEvent waveOut = new WaveOutEvent()) { waveOut.Init(vorbisStream); waveOut.Play(); while(waveOut.PlaybackState == PlaybackState.Playing) { if (token.IsCancellationRequested) break; Task.Delay(100, token); } } } } catch(TaskCanceledException) { } catch(OperationCanceledException) { } catch(Exception ex) { messenger.SendUi(new ApplicationErrorMessage { Exception = ex, HeaderText = "Error Playing Audio" }); } finally { stream.Close(); } }