public VoicePool(XAudio2 xAudio2, Voice masterVoice) { _objects = new ConcurrentDictionary <WaveFormat, ConcurrentBag <SourceVoiceEx> >(); _objectGenerator = format => { var newVoice = new SourceVoice(xAudio2, format, VoiceFlags.None, 3.0f, true); newVoice.SetOutputVoices(new[] { new VoiceSendDescriptor(masterVoice) }); var newId = Interlocked.Increment(ref _nextOperationId); var sourceVoiceEx = new SourceVoiceEx(newVoice, Guid.NewGuid(), format, newId); return(sourceVoiceEx); }; }
public override Task <ISoundPlayerBuilder <IStorageFileEx> > BuildAsync() { DisposeInternally(); _dataStream = new DataStream(_currentSampleBuffer.Length, true, true); _overLapDataStream = new DataStream(_currentSampleBuffer.Length, true, true); _dataStream.Read(_currentSampleBuffer, 0, _currentSampleBuffer.Length); _dataStream.Seek(0, 0); _overLapDataStream.Read(_currentSampleBuffer, 0, _currentSampleBuffer.Length); _overLapDataStream.Seek(0, 0); SourceVoice = VoicePool.GetVoice(WaveFormat); _overlapSourceVoice = VoicePool.GetVoice(WaveFormat); SourceVoice.PlayWith(_dataStream); _overlapSourceVoice.PlayWith(_overLapDataStream); return(Task.FromResult <ISoundPlayerBuilder <IStorageFileEx> >(this)); }
public void PutVoice(SourceVoiceEx item, WaveFormat waveFormat) { item.Id = Guid.Empty; item.SourceVoice.Stop(XAudio2.CommitNow); ConcurrentBag <SourceVoiceEx> bag; if (_objects.TryGetValue(waveFormat, out bag) && bag != null) { bag.Add(item); return; } bag = new ConcurrentBag <SourceVoiceEx> { item }; _objects.TryAdd(waveFormat, bag); }
public bool IsPooled(SourceVoiceEx voice) { var pooled = _objects.Values.SelectMany(exs => exs).Any(ex => ReferenceEquals(voice, ex)); return(pooled); }