Periodically play audio from C # memory stream

I have a buffer containing sine samples that I want to play periodically from a memory stream. Is there an efficient way to play it without having time gaps? I'm trying to create my own signal generator (I know some libraries provide this, but I want to create one myself).

The platform is Windows Phone 8.1 silverlight

Update: the code is taken from this forum somewhere

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 samplesPerSecond = 44100;

        {
            double theta = frequency * TAU / (double)samplesPerSecond;
            // 'volume' is UInt16 with range 0 thru Uint16.MaxValue ( = 65 535)
            // we need 'amp' to have the range of 0 thru Int16.MaxValue ( = 32 767)
            double amp = volume >> 2; // so we simply set amp = volume / 2
            for (int step = 0; step < samples; step++)
            {
                short s = (short)(amp * Math.Sin(theta * (double)step));
                writer.Write(s);
            }
        }


    }

      

+3


source to share


1 answer


This is how I did it - just create a new SoundEffectInstance object and set it to the return value SoundEffect.CreateInstance.

https://msdn.microsoft.com/en-us/library/dd940203.aspx



SoundEffect mySoundPlay = new SoundEffect(mStrm.ToArray(), 16000,AudioChannels.Mono);
SoundEffectInstance instance = mySoundPlay.CreateInstance();
instance.IsLooped = true;
instance.Play();

      

+1


source







All Articles