using System;
namespace Susurrus.XAudio2.UnitTests
{
///
/// An engine with a mastering voice, which needs a machine that has XAudio2 and an audio device.
///
public class TestDevice : IDisposable
{
public const int SampleRate = 44100;
public XAudio2Engine Engine { get; }
public MasteringVoice Master { get; }
private TestDevice(XAudio2Engine engine, MasteringVoice master)
{
this.Engine = engine;
this.Master = master;
}
///
/// Returns the device, or null on a machine that can't play sound.
///
public static TestDevice Open()
{
var engine = XAudio2Engine.Create();
if (!engine.IsOk)
{
return null;
}
var master = engine.Value.CreateMasteringVoice();
if (!master.IsOk)
{
engine.Value.Dispose();
return null;
}
return new TestDevice(engine.Value, master.Value);
}
public static WaveFormatEx MonoFormat()
{
return WaveFormatEx.Of(SampleFormat.Pcm16, channelCount: 1, sampleRate: SampleRate);
}
public SourceVoice MonoVoice(float maxFrequencyRatio = 2f)
{
WaveFormatEx format = MonoFormat();
return Engine.CreateSourceVoice(format, maxFrequencyRatio).Value;
}
public void Dispose()
{
Master.Destroy();
Engine.Dispose();
}
}
}