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 = new StreamingSourceVoice(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(); } } } }
private void WorkerProc(object o) { const int waitTimeout = 10; WaitHandle[] waitHandles = {}; StreamingSourceVoice[] itemsCopy = {}; while (!_shutDown) { lock (_lockObject) { if (_itemsChanged) { itemsCopy = _items.ToArray(); waitHandles = itemsCopy.Select(x => x.BufferEndWaitHandle).Cast <WaitHandle>().ToArray(); _itemsChanged = false; } } if (waitHandles.Length == 0) { Thread.Sleep(waitTimeout); continue; } int index = WaitHandle.WaitAny(waitHandles, waitTimeout); if (index == WaitHandle.WaitTimeout) { continue; } StreamingSourceVoice item = itemsCopy[index]; //todo: make sure that we've got the right item item.Refill(); } }
/// <summary> /// Removes a <see cref="StreamingSourceVoice" /> from the <see cref="StreamingSourceVoiceListener" />. /// </summary> /// <param name="streamingSourceVoice"> /// The <see cref="StreamingSourceVoice" /> instance to remove from the /// <see cref="StreamingSourceVoiceListener" />. /// </param> public void Remove(StreamingSourceVoice streamingSourceVoice) { if (_items.Contains(streamingSourceVoice)) { lock (_lockObject) { _items.Remove(streamingSourceVoice); _itemsChanged = true; } } }
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 = new StreamingSourceVoice(_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> /// Adds a <see cref="StreamingSourceVoice" /> to the <see cref="StreamingSourceVoiceListener" />. /// </summary> /// <param name="streamingSourceVoice"> /// The <see cref="StreamingSourceVoice" /> instance to add to the /// <see cref="StreamingSourceVoiceListener" />. /// </param> public void Add(StreamingSourceVoice streamingSourceVoice) { if (!_items.Contains(streamingSourceVoice)) { if (_items.Count > MaxItems) //64 = Max waithandles { throw new NotSupportedException("The maximum number of items is limited to 64."); } lock (_lockObject) { _items.Add(streamingSourceVoice); _itemsChanged = true; TryStart(); } } }
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 = new StreamingSourceVoice(_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."); } }
/// <summary> /// Adds a <see cref="StreamingSourceVoice" /> to the <see cref="StreamingSourceVoiceListener" />. /// </summary> /// <param name="streamingSourceVoice"> /// The <see cref="StreamingSourceVoice" /> instance to add to the /// <see cref="StreamingSourceVoiceListener" />. /// </param> public void Add(StreamingSourceVoice streamingSourceVoice) { if (!_items.Contains(streamingSourceVoice)) { if (_items.Count > MaxItems) //64 = Max waithandles throw new NotSupportedException("The maximum number of items is limited to 64."); lock (_lockObject) { _items.Add(streamingSourceVoice); _itemsChanged = true; TryStart(); } } }
/// <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); }