public void LoadAudioFromFile(string filePath) { // MP3 if (filePath.EndsWith(".mp3")) { PCMStream = new AudioFileReader(filePath); } // WAV else if (filePath.EndsWith(".wav")) { PCMStream = new AudioFileReader(filePath); } // OGG or EGG else if (filePath.EndsWith(".ogg") || filePath.EndsWith(".egg")) { PCMStream = new VorbisWaveReader(filePath); } else if (PCMStream != null) { // Throw an error is the audio has more channels than stereo if (PCMStream.WaveFormat.Channels > 2) { throw new FormatException("Only Mono and Stereo are supported"); } InitialiseOutputDevice(); OnsetsFound = null; } else { throw new FormatException("Invalid audio file"); } }
/// <summary> /// Initialize a local audio player with given type /// </summary> /// <param name="fileName">the file name to play</param> /// <param name="waveOutType">the wave out type to use</param> public LocalAudioPlayer(string fileName, WaveOutType waveOutType) { this.fileName = fileName; PlaybackStopType = PlaybackStopTypes.PlaybackStoppedReachingEndOfFile; Debug.Assert(this.wavePlayer == null); this.wavePlayer = CreateWavePlayer(waveOutType); wavePlayer.PlaybackStopped += wavePlayer_PlaybackStopped; try { if (fileName.ToLowerInvariant().EndsWith(".ogg")) { vorbisReader = new VorbisWaveReader(fileName); wavePlayer.Init(vorbisReader); } else if (fileName.ToLowerInvariant().EndsWith(".flac")) { flacReader = new FlacReader(fileName); wavePlayer.Init(flacReader); } else { this.file = new AudioFileReader(fileName); this.wavePlayer.Init(file); } //this.wavePlayer.PlaybackStopped += wavePlayer_PlaybackStopped; } catch (Exception) { //throw; } //Play(); }
public static VorbisWaveReader WavToOgg(WaveStream wav) { using (var ogg = new VorbisWaveReader(wav)) { return(ogg); } }
public NAudioSoundEngineData(byte[] oggData) { OutputDevice = new WaveOutEvent(); WaveReader = new VorbisWaveReader(new MemoryStream(oggData)); OutputDevice.Init(WaveReader); }
public static void RegisterSound(string soundName) { var file = $"{SharpCraft.Instance.GameFolderDir}/assets/sharpcraft/sounds/{soundName}.ogg"; if (!File.Exists(file)) { Console.WriteLine($"ERROR: Couldn't find sound '{soundName}'"); return; } using (var wr = new VorbisWaveReader(file)) { byte[] data = new byte[wr.Length]; wr.Read(data, 0, data.Length); int buffer = AL.GenBuffer(); int source = AL.GenSource(); AL.BufferData(buffer, ALFormat.VorbisExt, data, data.Length, wr.WaveFormat.SampleRate); AL.Source(source, ALSourcei.Buffer, buffer); AL.BindBufferToSource(source, buffer); //Sounds.TryAdd(soundName, new ValueTuple<int, int>(source, buffer)); } }
public static VorbisWaveReader WavToOgg(string path) { using (var ogg = new VorbisWaveReader(path)) { return(ogg); } }
public static void PlaySound(Stream soundStream) { var vorbisWaveReader = new VorbisWaveReader(soundStream); SoundOut.Init(vorbisWaveReader); SoundOut.Play(); }
/// <summary> /// releases all the player related resources /// </summary> public void Dispose() { if (this.file != null) { this.file.Close(); this.file.Dispose(); this.file = null; } if (vorbisReader != null) { this.vorbisReader.Close(); this.vorbisReader.Dispose(); this.vorbisReader = null; } if (flacReader != null) { this.flacReader.Close(); this.flacReader.Dispose(); this.flacReader = null; } if (this.wavePlayer != null) { this.wavePlayer.PlaybackStopped -= wavePlayer_PlaybackStopped; this.wavePlayer.Dispose(); this.wavePlayer = null; } }
private void Play_Click(object sender, RoutedEventArgs e) { UndertaleEmbeddedAudio target = DataContext as UndertaleEmbeddedAudio; if (target.Data.Length > 4) { try { if (target.Data[0] == 'R' && target.Data[1] == 'I' && target.Data[2] == 'F' && target.Data[3] == 'F') { wavReader = new WaveFileReader(new MemoryStream(target.Data)); InitAudio(); waveOut.Init(wavReader); waveOut.Play(); } else if (target.Data[0] == 'O' && target.Data[1] == 'g' && target.Data[2] == 'g' && target.Data[3] == 'S') { oggReader = new VorbisWaveReader(new MemoryStream(target.Data)); InitAudio(); waveOut.Init(oggReader); waveOut.Play(); } else { mainWindow.ShowError("Failed to play audio!\r\nNot a WAV or OGG.", "Audio failure"); } } catch (Exception ex) { waveOut = null; mainWindow.ShowError("Failed to play audio!\r\n" + ex.Message, "Audio failure"); } } }
private void OnPlaybackStopped(object sender, StoppedEventArgs args) { outputDevice.Dispose(); outputDevice = null; audioFile.Dispose(); audioFile = null; }
public static void PlayOGGAudioFiles(int BusType, int BusNumber, int audioIndex) { VorbisWaveReader vorbisStream = new VorbisWaveReader(BGMfiles[audioIndex]); try { vorbisStream.Dispose(); waveOut.Dispose(); vorbisStream = new VorbisWaveReader(BGMfiles[audioIndex]); switch (BusType) { //BGM case 0: waveOut.Init(vorbisStream); waveOut.Play(); break; //SFX case 1: waveOut.Init(vorbisStream); waveOut.Play(); break; } } catch (Exception) { throw; } }
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(); } }
private void OnPlaybackStopped(object sender, EventArgs e) { _waveOut.PlaybackStopped -= OnPlaybackStopped; _waveOut = null; _vorbisReader.Dispose(); _vorbisReader = null; }
public AudioSource(string name, string path) { this.name = name; reader = new VorbisWaveReader(path); waveChannel = new WaveChannel32(reader); output.Init(waveChannel); }
private void btnCry_MouseUp(object sender, MouseEventArgs e) { string s; if (optionsList[0].CryVolume != 10) { s = "0." + optionsList[0].CryVolume.ToString(); } else { s = "1.0"; } float f; Single.TryParse(s, out f); if (viewMega) { if (!megax) { var CryOGG = new VorbisWaveReader(AppDomain.CurrentDomain.BaseDirectory + "Data\\Audio\\Empty.ogg"); try { CryOGG = new VorbisWaveReader(AppDomain.CurrentDomain.BaseDirectory + "Data\\Audio\\Cries\\" + pokeList[carryi].number + "-mega.ogg"); } catch { MessageBox.Show(pokeList[carryi].number + "-mega.ogg does not exist."); } var CryPlay = new WaveOut(); CryPlay.Init(CryOGG); CryPlay.Volume = f; CryPlay.Play(); } else { if (onMegaX) { var CryOGG = new VorbisWaveReader(AppDomain.CurrentDomain.BaseDirectory + "Data\\Audio\\Empty.ogg"); try { CryOGG = new VorbisWaveReader(AppDomain.CurrentDomain.BaseDirectory + "Data\\Audio\\Cries\\" + pokeList[carryi].number + "-mega-x.ogg"); } catch { MessageBox.Show(pokeList[carryi].number + "-mega-x.ogg does not exist."); } var CryPlay = new WaveOut(); CryPlay.Init(CryOGG); CryPlay.Volume = f; CryPlay.Play(); } else { var CryOGG = new VorbisWaveReader(AppDomain.CurrentDomain.BaseDirectory + "Data\\Audio\\Empty.ogg"); try { CryOGG = new VorbisWaveReader(AppDomain.CurrentDomain.BaseDirectory + "Data\\Audio\\Cries\\" + pokeList[carryi].number + "-mega-y.ogg"); } catch { MessageBox.Show(pokeList[carryi].number + "-mega-y.ogg does not exist."); } var CryPlay = new WaveOut(); CryPlay.Init(CryOGG); CryPlay.Volume = f; CryPlay.Play(); } } } else { var CryOGG = new VorbisWaveReader(AppDomain.CurrentDomain.BaseDirectory + "Data\\Audio\\Empty.ogg"); try { CryOGG = new VorbisWaveReader(AppDomain.CurrentDomain.BaseDirectory + "Data\\Audio\\Cries\\" + pokeList[carryi].number + ".ogg"); } catch { MessageBox.Show(pokeList[carryi].number + ".ogg does not exist."); } var CryPlay = new WaveOut(); CryPlay.Init(CryOGG); CryPlay.Volume = f; CryPlay.Play(); } }
public void PlayNotificationSound() { _vorbisReader = new VorbisWaveReader(BDMTConstants.WORKSPACE_PATH + BDMTConstants.NOTIFICATION_SOUND_FILE); _waveOut = new WaveOut(); _waveOut.PlaybackStopped += OnPlaybackStopped; _waveOut.Init(_vorbisReader); _waveOut.Volume = 0.65f; _waveOut.Play(); }
/// <summary>Reads sound data from a Ogg Vorbis file.</summary> /// <param name="fileName">The file name of the Ogg Vorbis file.</param> /// <returns>The raw sound data.</returns> private static Sound LoadFromFile(string fileName) { using (VorbisWaveReader reader = new VorbisWaveReader(fileName)) { int sampleCount = (int)reader.Length / (reader.WaveFormat.Channels * sizeof(float)); float[] dataFloats = new float[sampleCount * reader.WaveFormat.Channels]; // Convert Ogg Vorbis to raw 32-bit float n channels PCM. int floatsRead = reader.Read(dataFloats, 0, sampleCount * reader.WaveFormat.Channels); byte[] dataBytes = new byte[floatsRead * sizeof(short)]; byte[][] buffers = new byte[reader.WaveFormat.Channels][]; for (int i = 0; i < buffers.Length; i++) { buffers[i] = new byte[dataBytes.Length / buffers.Length]; } // Convert PCM bit depth from 32-bit float to 16-bit integer. using (MemoryStream stream = new MemoryStream(dataBytes)) using (BinaryWriter writer = new BinaryWriter(stream)) { for (int i = 0; i < floatsRead; i++) { float sample = dataFloats[i]; if (sample < -1.0f) { sample = -1.0f; } if (sample > 1.0f) { sample = 1.0f; } writer.Write((short)(sample * short.MaxValue)); } } // Separated for each channel. for (int i = 0; i < floatsRead / buffers.Length; i++) { for (int j = 0; j < buffers.Length; j++) { for (int k = 0; k < sizeof(short); k++) { buffers[j][i * sizeof(short) + k] = dataBytes[i * sizeof(short) * buffers.Length + sizeof(short) * j + k]; } } } return(new Sound(reader.WaveFormat.SampleRate, sizeof(short) * 8, buffers)); } }
/// <summary> /// saves vorbis ogg or flac tag information to file /// </summary> /// <param name="taglibFile"></param> public void SaveVorbisTag(VorbisComment taglibFile) { if (taglibFile != null) { //string fName = file.FileName; //AudioFileReader prev = file; if (flacReader != null) { //float curVol = file.Volume; wavePlayer.Stop(); string prevFile = this.fileName; this.flacReader.Close(); this.flacReader.Dispose(); this.flacReader = null; try { taglibFile.SaveMetadata(); } catch (UnauthorizedAccessException) { } catch (Exception) { } this.flacReader = new FlacReader(prevFile); wavePlayer.Init(this.flacReader); //SetVolume(curVol); } else if (vorbisReader != null) { wavePlayer.Stop(); string prevFile = this.fileName; this.vorbisReader.Close(); this.vorbisReader.Dispose(); this.vorbisReader = null; try { taglibFile.SaveMetadata(); } catch (UnauthorizedAccessException) { } catch (Exception) { } this.vorbisReader = new VorbisWaveReader(prevFile); wavePlayer.Init(this.vorbisReader); //SetVolume(curVol); } } }
/// <summary> /// Playing an .ogg audio stream /// </summary> /// <param name="audioStream"></param> private static void PlayAudio(Stream audioStream) { using (var vorbisStream = new VorbisWaveReader(audioStream)) using (var waveOut = new NAudio.Wave.WaveOutEvent()) { waveOut.Init(vorbisStream); waveOut.Volume = 0.5f; waveOut.Play(); Thread.Sleep(vorbisStream.TotalTime); } }
private void ResetOggPreview() { if (waveOut != null && waveOut.PlaybackState == PlaybackState.Playing) { waveOut.Stop(); waveReader.Close(); waveReader.Dispose(); } waveReader = null; btnOggPreview.Text = "Ogg Preview"; }
public static async void PlaySound(string sound, bool loop) { Logger.WriteLine($"attempting to play sound: {sound}"); try { // gc isn't happy rn // create wave out event and initialize it with the vorbis wave reader using (WaveOutEvent waveOut = new WaveOutEvent()) { // create vorbis wave reader using (VorbisWaveReader v = new VorbisWaveReader($"{Static.audioPath}\\{sound}.ogg")) { // also create a loop stream and initialize the wave out event with the loop stream instead of loop is true using (LoopStream loopStream = new LoopStream(v)) { if (loop) { waveOut.Init(loopStream); } else { waveOut.Init(v); } // flush and dispose the streams after playback stops void Dispose(object sender, StoppedEventArgs e) { v.Flush(); v.Dispose(); waveOut.Dispose(); loopStream.Flush(); loopStream.Dispose(); } waveOut.PlaybackStopped += Dispose; // play waveOut.Play(); // add the wave out event to the active audio list so it can be stopped manually activeAudio.Add(waveOut); // wait the duration of the sound await Task.Delay(v.TotalTime); } } } } catch (Exception ex) { ExceptionMessage.New(ex, true); } }
public void BeginPlayback(Stream dataStream) { Debug.Assert(wavePlayer == null); stream = dataStream; wavePlayer = new WaveOut(); vorbisWaveReader = new VorbisWaveReader(dataStream); wavePlayer.Volume = volumeSlider1.Volume; wavePlayer.Init(vorbisWaveReader); wavePlayer.PlaybackStopped += OnPlaybackStopped; wavePlayer.Play(); EnableButtons(true); timer1.Enabled = true; // timer for updating current time label }
public void SetAudio(Stream data) { CleanUp(); try { outputDevice = new WaveOutEvent(); vorbis = new VorbisWaveReader(data); outputDevice.Volume = 1.0f; outputDevice.Init(vorbis); } catch (Exception ex) { Debugger.Log(0, "[TankView.Sound.SetAudio]", $"Error setting audio! {ex.Message}\n"); // ignored } }
private void PlayMusic(String audioPath) { if (outputDevice == null) { outputDevice = new WaveOutEvent(); outputDevice.PlaybackStopped += OnPlaybackStopped; } if (audioFile == null) { audioFile = new VorbisWaveReader(audioPath); outputDevice.Init(audioFile); } outputDevice.Play(); }
private void CleanUp() { if (outputDevice != null) { outputDevice.Stop(); outputDevice.Dispose(); outputDevice = null; } if (vorbis != null) { vorbis.Dispose(); vorbis = null; } }
public void SetAudio(Stream data) { CleanUp(); try { outputDevice = new WaveOutEvent(); vorbis = new VorbisWaveReader(data); outputDevice.Volume = 1.0f; outputDevice.Init(vorbis); _progressWorker.ReportProgress(0, $"00:00/{new DateTime(vorbis.TotalTime.Ticks):mm:ss}"); } catch (Exception ex) { Debugger.Log(0, "[TankView.Sound.SetAudio]", $"Error setting audio! {ex.Message}\n"); // ignored } }
public void StopSong() { if (audioFile != null) { audioFile.Dispose(); audioFile = null; } if (oggReader != null) { oggReader.Dispose(); oggReader = null; } audioOutput.Stop(); }
public void SetAudio(Stream data) { CleanUp(); try { outputDevice = new WaveOutEvent(); vorbis = new VorbisWaveReader(data); outputDevice.Volume = 1.0f; outputDevice.Init(vorbis); } catch { } }
// SOUND // Play Sound public void playSound(HKSound sound) { if (waveOut != null) { debugLog("[Sound] Disposed"); waveOut.Dispose(); } waveOut = new WaveOut(); waveOut.DeviceNumber = selectedDevice; checkSoundState.Stop(); debugLog("[Action] Sound " + sound.filename); // VorbisWaveReader is not in AudioFileReader but is a ISampleProvider if (sound.extension == ".ogg") { if (currentOGG != null) { currentOGG.Dispose(); } currentOGG = new VorbisWaveReader(sound.completeFilename); waveOut.Init(currentOGG); waveOut.Play(); waveOut.Volume = sound.volume; checkSoundState.Start(); currentSound = sound; debugLog("[Sound] Volume : " + sound.volume); debugLog("[Sound] OGG Start"); } else if (".mp3.mp4.wav".Contains(sound.extension)) { if (audioFile != null) { audioFile.Dispose(); } audioFile = new AudioFileReader(sound.completeFilename); waveOut.Init(audioFile); waveOut.Play(); waveOut.Volume = sound.volume; checkSoundState.Start(); currentSound = sound; debugLog("[Sound] Volume : " + sound.volume); debugLog("[Sound] MP3/MP4/WAV Start"); } else { debugLog("[Sound] Not MP3, MP4 nor WAV"); } }
private void audioList_AfterSelect(object sender, TreeViewEventArgs e) { forceStop = true; if (audioList.SelectedNode.Parent != null) { try { if (wavOutput != null) { wavOutput.Stop(); wavOutput.Dispose(); } if (wavFile != null) { wavFile.Dispose(); } if (vorbisFile != null) { vorbisFile.Dispose(); } if (Directory.Exists(@"C:\gmetemp")) { Directory.Delete(@"C:\gmetemp", true); } Directory.CreateDirectory(@"C:\gmetemp"); loadedAudio = AudioGroupList[audioList.SelectedNode.Parent.Index].files[audioList.SelectedNode.Index]; wavOutput = new WaveOutEvent(); wavOutput.PlaybackStopped += WavOutput_PlaybackStopped; if (BitConverter.ToUInt32(loadedAudio, 0) == 0x5367674F) { File.WriteAllBytes(@"C:\gmetemp\temp.ogg", loadedAudio); vorbisFile = new VorbisWaveReader(@"C:\gmetemp\temp.ogg"); wavOutput.Init(vorbisFile); isOgg = true; } else if (BitConverter.ToUInt32(loadedAudio, 0) == 0x46464952) { File.WriteAllBytes(@"C:\gmetemp\temp.wav", loadedAudio); wavFile = new AudioFileReader(@"C:\gmetemp\temp.wav"); wavOutput.Init(wavFile); isOgg = false; } playPause.Text = "Play"; filelabel.Text = "File: " + audioList.SelectedNode.Parent.Text + "\\" + audioList.SelectedNode.Text; } catch { } } }