private void StartPlayingThisClip(AudioClipToPlay clip, double curSec) { cxzxc(string.Format("start of {0} {1}-{2}", clip.Filename, clip.SecFileStart, clip.SecFileEnd)); // create the device IWavePlayer waveOutDevice = new WaveOut(); var deviceAndShit = new DeviceAndItsData(waveOutDevice, clip); lock (this) { devicesPlaying.Add(deviceAndShit); } Thread ttt = new Thread(() => { using (var reader = OpenAudioFile(clip.Filename)) { deviceAndShit.WaveReader = reader; // init the playback stuff waveOutDevice.Init(reader); var syncOffsetSec = curSec - clip.SecOffset; // would be 0 if curSec == clip.SecOffset var clipSecStart = clip.SecFileStart + syncOffsetSec; var clipSecEnd = clip.SecFileEnd; reader.CurrentTime = TimeSpan.FromSeconds(clipSecStart + NAUDIO_SYNC_OFFSET); var endTs = TimeSpan.FromSeconds(clipSecEnd + NAUDIO_SYNC_OFFSET); var sleepTs = TimeSpan.FromSeconds(NAUDIO_SLEEP_FRAME_TIME); deviceAndShit.PlaybackStateRequest = PlaybackState.Playing; waveOutDevice.Play(); while (reader.CurrentTime < endTs) { Thread.Sleep(sleepTs); if (waveOutDevice.PlaybackState == PlaybackState.Playing && deviceAndShit.PlaybackStateRequest == PlaybackState.Paused) { waveOutDevice.Pause(); } if (waveOutDevice.PlaybackState == PlaybackState.Paused && deviceAndShit.PlaybackStateRequest == PlaybackState.Playing) { waveOutDevice.Play(); } if (deviceAndShit.StopHasBeenInvoked) { break; } if (waveOutDevice.PlaybackState == PlaybackState.Stopped) { break; } } waveOutDevice.Stop(); // no harm calling stop 2x right? lock (this) { devicesPlaying.Remove(deviceAndShit); devicesPaused.Remove(deviceAndShit); } cxzxc(string.Format("stop of {0} {1}-{2}", clip.Filename, clip.SecFileStart, clip.SecFileEnd)); } }); ttt.Start(); }
public DeviceAndItsData(IWavePlayer waveOutDevice, AudioClipToPlay clip) { this.Wave = waveOutDevice; this.WhatIAmPlaying = clip; }
private void StartPlayingThisClip(AudioClipToPlay clip, double curSec) { cxzxc(string.Format("start of {0} {1}-{2}", clip.Filename, clip.SecFileStart, clip.SecFileEnd)); // create the device IWavePlayer waveOutDevice = new WaveOut(); var deviceAndShit = new DeviceAndItsData(waveOutDevice, clip); lock (this) { devicesPlaying.Add(deviceAndShit); } Thread ttt = new Thread(() => { using (var reader = OpenAudioFile(clip.Filename)) { deviceAndShit.WaveReader = reader; // init the playback stuff waveOutDevice.Init(reader); var syncOffsetSec = curSec - clip.SecOffset; // would be 0 if curSec == clip.SecOffset var clipSecStart = clip.SecFileStart + syncOffsetSec; var clipSecEnd = clip.SecFileEnd; reader.CurrentTime = TimeSpan.FromSeconds(clipSecStart + NAUDIO_SYNC_OFFSET); var endTs = TimeSpan.FromSeconds(clipSecEnd + NAUDIO_SYNC_OFFSET); var sleepTs = TimeSpan.FromSeconds(NAUDIO_SLEEP_FRAME_TIME); deviceAndShit.PlaybackStateRequest = PlaybackState.Playing; waveOutDevice.Play(); while (reader.CurrentTime < endTs) { Thread.Sleep(sleepTs); if (waveOutDevice.PlaybackState == PlaybackState.Playing && deviceAndShit.PlaybackStateRequest == PlaybackState.Paused) waveOutDevice.Pause(); if (waveOutDevice.PlaybackState == PlaybackState.Paused && deviceAndShit.PlaybackStateRequest == PlaybackState.Playing) waveOutDevice.Play(); if (deviceAndShit.StopHasBeenInvoked) break; if (waveOutDevice.PlaybackState == PlaybackState.Stopped) break; } waveOutDevice.Stop(); // no harm calling stop 2x right? lock (this) { devicesPlaying.Remove(deviceAndShit); devicesPaused.Remove(deviceAndShit); } cxzxc(string.Format("stop of {0} {1}-{2}", clip.Filename, clip.SecFileStart, clip.SecFileEnd)); } }); ttt.Start(); }