public void CanPlayWithStreamingSourceVoice() { for (int i = 0; i < 20; i++) { using (var masteringVoice = _xaudio2.CreateMasteringVoice()) using (var source = GlobalTestConfig.TestWav2S()) using (var pool = new StreamingSourceVoiceListener()) using (var streamingSourceVoice = StreamingSourceVoice.Create(_xaudio2, source)) { var stoppedEvent = new ManualResetEvent(false); streamingSourceVoice.Stopped += (s, e) => stoppedEvent.Set(); streamingSourceVoice.Start(); pool.Add(streamingSourceVoice); Debug.WriteLine("All queued."); stoppedEvent.WaitOne(); pool.Remove(streamingSourceVoice); } Debug.WriteLine("All removed."); } }
static void Main(string[] args) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = CodecFactory.SupportedFilesFilterEn; if (openFileDialog.ShowDialog() == DialogResult.OK) { using (var source = CodecFactory.Instance.GetCodec(openFileDialog.FileName)) { using (var xaudio2 = XAudio2.CreateXAudio2()) using (var masteringVoice = xaudio2.CreateMasteringVoice()) //ALWAYS create at least one masteringVoice. using (var streamingSourceVoice = StreamingSourceVoice.Create(xaudio2, source)) { StreamingSourceVoiceListener.Default.Add(streamingSourceVoice); //add the streamingSourceVoice to the default sourcevoicelistener which processes the data requests. streamingSourceVoice.Start(); Console.WriteLine("Press any key to exit."); Console.ReadKey(); StreamingSourceVoiceListener.Default.Remove(streamingSourceVoice); streamingSourceVoice.Stop(); } } } }
public void OpenFile(string filename) { Stop(); Vector3 center = new Vector3(0); _waveSource = CodecFactory.Instance.GetCodec(filename).ToMono(); _masteringVoice = _xaudio2.CreateMasteringVoice(XAudio2.DefaultChannels, XAudio2.DefaultSampleRate); _streamingSourceVoice = StreamingSourceVoice.Create(_xaudio2, _waveSource, 150); object defaultDevice = _xaudio2.DefaultDevice; ChannelMask channelMask; if (_xaudio2.Version == XAudio2Version.XAudio2_7) { var xaudio27 = (XAudio2_7)_xaudio2; var deviceDetails = xaudio27.GetDeviceDetails((int)defaultDevice); channelMask = deviceDetails.OutputFormat.ChannelMask; _destinationChannels = deviceDetails.OutputFormat.Channels; } else { channelMask = _masteringVoice.ChannelMask; _destinationChannels = _masteringVoice.VoiceDetails.InputChannels; } _sourceChannels = _waveSource.WaveFormat.Channels; _x3daudio = new X3DAudioCore(channelMask); _listener = new Listener() { Position = center, OrientFront = new Vector3(0, 0, 1), OrientTop = new Vector3(0, 1, 0), Velocity = new Vector3(0, 0, 0) }; _emitter = new Emitter() { ChannelCount = _sourceChannels, CurveDistanceScaler = float.MinValue, OrientFront = new Vector3(0, 0, 1), OrientTop = new Vector3(0, 1, 0), Position = new Vector3(0, 0, 0), Velocity = new Vector3(0, 0, 0) }; StreamingSourceVoiceListener.Default.Add(_streamingSourceVoice); _streamingSourceVoice.Start(); _isPlaying = true; }
/// <summary> /// Plays the wave source. /// </summary> /// <param name="waveSource">The WaveSource.</param> internal void Play(IWaveSource waveSource) { var panSource = new PanSource(waveSource); _panSource = panSource; _currentWaveSource = panSource.ToWaveSource(); _sourceVoice = StreamingSourceVoice.Create(_xaudio2, _currentWaveSource); StreamingSourceVoiceListener.Default.Add(_sourceVoice); _playbackState = PlaybackState.Playing; IsPlaying = true; if (PlaybackChanged != null) { PlaybackChanged(this, EventArgs.Empty); } }