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

3
我有一些包含正弦样本的缓冲区,我想从内存流周期性地播放它。有没有有效的方法可以在时间上不断断续续地播放?我试图制作自己的信号生成器(我知道有一些库提供了这个功能,但我想要自己生成)。
平台是Windows Phone 8.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);
            }
        }


    }

你说的“随机播放”是什么意思?能给我们展示一下你的代码吗? - Luaan
我更新了我的帖子,加入了代码。 我的意思是随机播放信号,直到我告诉它停止。 - axcelenator
那么你真的是指“定期地”吗?例如,没有间断地持续发出一种音调? - Luaan
1
你可以使用 PlayLooping 方法。只需要确保流中的波形实际上是周期性的 - 否则在循环时会出现爆裂和类似的噪音。 - Luaan
谢谢!我会尝试的。 - axcelenator
显示剩余4条评论
1个回答

1

这是我的做法 - 只需创建一个新的SoundEffectInstance对象,并将其设置为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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接