using Amphora;
using System;
namespace Susurrus.XAudio2.UnitTests
{
///
/// Makes sounds with nothing in them, so a test can start a voice without anyone hearing it.
///
public static class SilentSounds
{
public static DecodedSound Of(
int frameCount, int channelCount = 1, int sampleRate = 48000, SampleFormat format = SampleFormat.Pcm16)
{
int byteCount = frameCount * channelCount * DecodedSound.GetBytesPerSample(format);
return new DecodedSound(new ByteView(new byte[byteCount]), sampleRate, channelCount, format);
}
///
/// Returns a WAV file of 16-bit silence.
///
public static byte[] Wav(int frameCount, int channelCount, int sampleRate = 48000)
{
int dataBytes = frameCount * channelCount * 2;
byte[] returnMe = new byte[44 + dataBytes];
Put(returnMe, 0, "RIFF");
Put(returnMe, 4, 36 + dataBytes);
Put(returnMe, 8, "WAVE");
Put(returnMe, 12, "fmt ");
Put(returnMe, 16, 16);
Put(returnMe, 20, (short)1);
Put(returnMe, 22, (short)channelCount);
Put(returnMe, 24, sampleRate);
Put(returnMe, 28, sampleRate * channelCount * 2);
Put(returnMe, 32, (short)(channelCount * 2));
Put(returnMe, 34, (short)16);
Put(returnMe, 36, "data");
Put(returnMe, 40, dataBytes);
return returnMe;
}
private static void Put(byte[] bytes, int index, string text)
{
System.Text.Encoding.ASCII.GetBytes(text, 0, text.Length, bytes, index);
}
private static void Put(byte[] bytes, int index, int value)
{
BitConverter.GetBytes(value).CopyTo(bytes, index);
}
private static void Put(byte[] bytes, int index, short value)
{
BitConverter.GetBytes(value).CopyTo(bytes, index);
}
///
/// Makes a sound that lasts about ten milliseconds at 48000 frames per second.
///
public static DecodedSound Short(int channelCount = 1)
{
return Of(frameCount: 480, channelCount);
}
///
/// Makes a sound long enough to still be playing when a test has finished with it.
///
public static DecodedSound Long(int channelCount = 1)
{
return Of(frameCount: 48000 * 30, channelCount);
}
}
}