定期从内存流播放音频 C#

play audio periodically from a memory stream c#

我有一些缓冲区,其中包含我想定期从内存流中播放的窦样本。有什么有效的方法可以在没有时间间隔的情况下播放它吗?我尝试制作我自己的信号发生器(我知道有一些库提供,但我想自己生成)。

平台是WindowsPhone8.1 silverlight

更新:代码取自本论坛某处

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);
            }
        }


    }

这是我的做法 - 只需创建一个新的 SoundEffectInstance 对象并将其设置为 SoundEffect.CreateInstance 的 return 值。

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();