2023-08-01 18:22:49 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace MoonWorks.Audio
|
|
|
|
{
|
|
|
|
internal class SourceVoicePool
|
|
|
|
{
|
|
|
|
private AudioDevice Device;
|
|
|
|
|
2023-08-02 21:10:48 +00:00
|
|
|
Dictionary<(System.Type, Format), Queue<SourceVoice>> VoiceLists = new Dictionary<(System.Type, Format), Queue<SourceVoice>>();
|
2023-08-01 18:22:49 +00:00
|
|
|
|
|
|
|
public SourceVoicePool(AudioDevice device)
|
|
|
|
{
|
|
|
|
Device = device;
|
|
|
|
}
|
|
|
|
|
2023-08-02 21:10:48 +00:00
|
|
|
public T Obtain<T>(Format format) where T : SourceVoice, IPoolable<T>
|
2023-08-01 18:22:49 +00:00
|
|
|
{
|
2023-08-02 21:10:48 +00:00
|
|
|
if (!VoiceLists.ContainsKey((typeof(T), format)))
|
2023-08-01 18:22:49 +00:00
|
|
|
{
|
2023-08-02 21:10:48 +00:00
|
|
|
VoiceLists.Add((typeof(T), format), new Queue<SourceVoice>());
|
2023-08-01 18:22:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:10:48 +00:00
|
|
|
var list = VoiceLists[(typeof(T), format)];
|
2023-08-01 18:22:49 +00:00
|
|
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
{
|
2023-08-02 21:10:48 +00:00
|
|
|
list.Enqueue(T.Create(Device, format));
|
2023-08-01 18:22:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:10:48 +00:00
|
|
|
return (T) list.Dequeue();
|
2023-08-01 18:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Return(SourceVoice voice)
|
|
|
|
{
|
2023-08-02 21:10:48 +00:00
|
|
|
var list = VoiceLists[(voice.GetType(), voice.Format)];
|
2023-08-01 18:22:49 +00:00
|
|
|
list.Enqueue(voice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|