I am using the following code to dynamically create a frequency tone in memory and play the tone asynchronously:
public static void PlayTone(UInt16 frequency, int msDuration, UInt16 volume = 16383)
{
using (var mStrm = new MemoryStream())
{
using (var writer = new BinaryWriter(mStrm))
{
const double tau = 2*Math.PI;
const int formatChunkSize = 16;
const int headerSize = 8;
const short formatType = 1;
const short tracks = 1;
const int samplesPerSecond = 44100;
const short bitsPerSample = 16;
const short frameSize = (short) (tracks*((bitsPerSample + 7)/8));
const int bytesPerSecond = samplesPerSecond*frameSize;
const int waveSize = 4;
var samples = (int) ((decimal) samplesPerSecond*msDuration/1000);
int dataChunkSize = samples*frameSize;
int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;
writer.Write(0x46464952);
writer.Write(fileSize);
writer.Write(0x45564157);
writer.Write(0x20746D66);
writer.Write(formatChunkSize);
writer.Write(formatType);
writer.Write(tracks);
writer.Write(samplesPerSecond);
writer.Write(bytesPerSecond);
writer.Write(frameSize);
writer.Write(bitsPerSample);
writer.Write(0x61746164);
writer.Write(dataChunkSize);
double theta = frequency*tau/samplesPerSecond;
double amp = volume >> 2;
for (int step = 0; step < samples; step++)
{
writer.Write((short) (amp*Math.Sin(theta*step)));
}
mStrm.Seek(0, SeekOrigin.Begin);
using (var player = new System.Media.SoundPlayer(mStrm))
{
player.Play();
}
}
}
}
The code is working fine. The only issue is, how do I know when the tone has stopped playing? There doesn't appear to be a Completed event on the SoundPlayer class that I can subscribe to.