drumkit-old/DrumKit/SoundPool.cs

83 lines
2.3 KiB
C#
Raw Normal View History

2013-11-18 18:03:00 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX;
using SharpDX.XAudio2;
using SharpDX.Multimedia;
namespace DrumKit
{
2013-11-18 18:11:53 +00:00
public class SoundPool
2013-11-18 18:03:00 +00:00
{
2013-11-18 18:11:53 +00:00
private XAudio2 Device { get; set; }
private MasteringVoice MasterVoice { get; set; }
private Queue<SourceVoice> Channels { get; set; }
2013-11-18 18:03:00 +00:00
2013-11-18 18:11:53 +00:00
/// <summary>
/// Gets or sets the master volume
/// </summary>
public float MasterVolume
2013-11-18 18:03:00 +00:00
{
2013-11-18 18:11:53 +00:00
get {
return this.MasterVoice.Volume;
}
2013-11-18 18:03:00 +00:00
2013-11-18 18:11:53 +00:00
set {
this.MasterVoice.SetVolume(value);
}
2013-11-18 18:03:00 +00:00
}
2013-11-18 18:11:53 +00:00
/// <summary>
/// Initializes a new sound pool.
/// </summary>
/// <param name="poly">How many sounds will be able to play simultaneously. Default is 64.</param>
public SoundPool(WaveFormat format, int poly = 64)
2013-11-18 18:03:00 +00:00
{
2013-11-18 18:11:53 +00:00
// Create and initialize device
this.Device = new XAudio2();
this.Device.StartEngine();
2013-11-18 18:03:00 +00:00
2013-11-18 18:11:53 +00:00
// Create voices
this.MasterVoice = new MasteringVoice(this.Device);
this.Channels = new Queue<SourceVoice>();
2013-11-18 18:03:00 +00:00
2013-11-18 18:11:53 +00:00
for (int i = 0; i < poly; i++)
2013-11-18 18:03:00 +00:00
{
2013-11-18 18:11:53 +00:00
SourceVoice voice = new SourceVoice(this.Device, format, true);
this.Channels.Enqueue(voice);
2013-11-18 18:03:00 +00:00
}
}
2013-11-18 18:11:53 +00:00
/// <summary>
/// Plays a sound buffer through one of the free channels.
/// </summary>
/// <param name="sound">The sound object</param>
public void PlayBuffer(Sound sound, float volumeL = 1.0f, float volumeR = 1.0f)
{
float[] volumes = { volumeL, volumeR };
SourceVoice top = this.Channels.Dequeue();
top.Stop();
top.FlushSourceBuffers();
top.SubmitSourceBuffer(sound.Buffer, sound.DecodedPacketsInfo);
top.SetChannelVolumes(2, volumes);
top.Start();
this.Channels.Enqueue(top);
}
/// <summary>
/// Cleans up used resources
/// </summary>
public void Dispose()
{
this.Channels.Clear();
this.MasterVoice.Dispose();
Device.StopEngine();
Device.Dispose();
}
2013-11-18 18:03:00 +00:00
}
}