private void Play(WaveStream stream, SpeakerState state) { // Stop all stream from playing for 1s if (DateTime.Now - StopTimeout < TimeSpan.FromMilliseconds(1000)) { Log("Stop next speech"); state.playing = false; stream.Dispose(); return; } state.start = DateTime.Now; using (WaveChannel32 volume = new WaveChannel32(stream)) { volume.Volume = ConfigManager.GetInstance().Find("speaker.volume", 100) / 100f; waveOut.Init(volume); waveOut.Play(); while (stream.CurrentTime < stream.TotalTime && (DateTime.Now - state.start) < state.timeout) { Thread.Sleep(500); } waveOut.Stop(); state.playing = false; } stream.Dispose(); }
/** * Play the given stream with given id * @param id of the stream * @param stream the generated tts * @param async boolean true/false * @param format int FORMAT_WAV or FORMAT_MP3 */ public void PlayWAV(String id, Stream stream, bool async) { var state = new SpeakerState(id); SpeakerStates[id] = state; PlayWAV(stream, async, state); }
/** * Play the given file path * @param path the file to play * @param async boolean true/false */ public void Play(String path, bool async) { var id = path; if (!File.Exists(path)) { path = "AddOns/speaker/medias/" + path; } if (!File.Exists(path)) { return; } int format = path.ToLower().EndsWith(".wav") || path.ToLower().EndsWith(".wma") ? FORMAT_WAV : FORMAT_MP3; if (format == FORMAT_WAV) { using (var stream = new FileStream(path, FileMode.Open)) { PlayWAV(id, stream, async); } } else { var stream = (WaveStream) new Mp3FileReader(path); SpeakerStates[id] = new SpeakerState(id); Play(stream, SpeakerStates[id]); } }
private void Play(WaveStream stream, bool async, SpeakerState state) { if (async) { Task.Factory.StartNew(() => Play(stream, state)); return; } Play(stream, state); }
private void PlayWAV(Stream stream, bool async, SpeakerState state) { var memory = new MemoryStream(); stream.Position = 0; stream.CopyTo(memory); memory.Position = 0; var wav = WaveFormatConversionStream.CreatePcmStream(new WaveFileReader(memory)); Play(wav, async, state); }
/** * Play the given file path * @param path the file to play * @param async boolean true/false */ public void Play(String path, bool async) { var id = path; if (!File.Exists(path)) { path = "AddOns/speaker/medias/" + path; } if (!File.Exists(path)) { return; } int format = path.ToLower().EndsWith(".wav") || path.ToLower().EndsWith(".wma") ? FORMAT_WAV : FORMAT_MP3; if (format == FORMAT_WAV) { using (var stream = new FileStream(path, FileMode.Open)) { PlayWAV(id, stream, async); } } else { var stream = (WaveStream)new Mp3FileReader(path); SpeakerStates[id] = new SpeakerState(id); Play(stream, SpeakerStates[id]); } }