I have an in memory WAV stream that I am able to play using
mStrm.Seek(0, SeekOrigin.Begin);
new System.Media.SoundPlayer(mStrm).Play();
I am trying to see how i can use the same stream and play it in a UWP application. I tried the following code
MediaElement soundPlayer = new MediaElement();
var soundSource = mStrm.AsRandomAccessStream();
soundSource.Seek(0);
soundPlayer.SetSource(soundSource, "audio/wav");
soundPlayer.Play();
But I am receiving the following error
MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED : HRESULT - 0x80000013
Adding the code for the audio synthesis that I have taken from web.
public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383)
{
var mStrm = new MemoryStream();
BinaryWriter writer = new BinaryWriter(mStrm);
const double TAU = 2 * Math.PI;
int formatChunkSize = 16;
int headerSize = 8;
short formatType = 1;
short tracks = 1;
int samplesPerSecond = 44100;
short bitsPerSample = 16;
short frameSize = (short)(tracks * ((bitsPerSample + 7) / 8));
int bytesPerSecond = samplesPerSecond * frameSize;
int waveSize = 4;
int 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); // = encoding.GetBytes("data")
writer.Write(dataChunkSize);
{
double theta = frequency * TAU / (double)samplesPerSecond;
double amp = volume >> 2;
for (int step = 0; step < samples; step++)
{
short s = (short)(amp * Math.Sin(theta * (double)step));
writer.Write(s);
}
}
mStrm.Seek(0, SeekOrigin.Begin);
MediaElement soundPlayer = new MediaElement();
var soundSource = mStrm.AsRandomAccessStream();
soundSource.Seek(0);
soundPlayer.SetSource(soundSource, "audio/wav");
soundPlayer.Play();
writer.Dispose();
mStrm.Dispose();
}
And I have am passing the different note frequencies for different octaves in a for loop and the duration is 1000milliseconds.
Any help on this is appreciated.
Thanks, Teja
User contributions licensed under CC BY-SA 3.0