n音讯播放正弦波对于x毫秒使用C# [英] NAudio playing a sine wave for x milliseconds using C#

查看:452
本文介绍了n音讯播放正弦波对于x毫秒使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 n音讯发挥给定频率的正弦波作为博客文章的 < A HREF =htt​​p://mark-dot-net.blogspot.com/2009/10/playback-of-sine-wave-in-naudio.html相对=nofollow>正弦波,n音讯回放 的。我只是想播放声音()对于x毫秒,然后停止。



我试过了Thread.Sleep,但声音停止通俗易懂。我尝试了计时器,但当waveout的布置有一个跨线程异常。



我试过这个代码,但是当我叫嘟嘟程序冻结。

 公共类蜂鸣
{
公共蜂鸣(INT频率,诠释毫秒)
{
SineWaveProvider32 sineWaveProvider =新SineWaveProvider32();
sineWaveProvider.Amplitude = 0.25f;
sineWaveProvider.Frequency =频率;

NAudio.Wave.WaveOut waveout的=新NAudio.Wave.WaveOut(WaveCallbackInfo.FunctionCallback());
waveOut.Init(sineWaveProvider);
waveOut.Play();
Thread.sleep代码(MS);
waveOut.Stop();
waveOut.Dispose();
}
}

公共类SineWaveProvider32:NAudio.Wave.WaveProvider32
{
INT样本;

公共SineWaveProvider32()
{
频率= 1000;
振幅= 0.25f; //我们不会伤害我们的耳朵
}

公众持股量的频率{搞定;组; }
公众持股量的振幅{搞定;组; }

公共覆盖INT读(浮动[]缓冲区,诠释抵消,诠释sampleCount)
{
INT采样率= WaveFor​​mat.SampleRate;
为(INT N = 0; N< sampleCount; N ++)
{
缓冲区[N +偏移量=(浮点)(振幅* Math.Sin((2 * Math.PI *样品*频率)/采样率));
样品++;
如果(样品> =采样率)$​​ B $ B样品= 0;
}
}


解决方案

的SineWaveProvider32类并不需要无限期地提供音频。如果你想嘟嘟有第二(说)的最长期限,那么对于单声道44.1&NBSP;千赫,您需要提供44,100个样本。 Read方法应返回0时,它已经没有更多的数据源。



为使您的GUI线程不会阻止,你需要摆脱的Thread.Sleep,waveout的的。停止和处置,并简单地开始播放音频(您可能会发现窗口的回调函数相比更可靠)。



然后,当音频播放完毕,您可以关闭和清理waveout的对象。


I am using NAudio to play a sinewave of a given frequency as in the blog post Playback of Sine Wave in NAudio. I just want the sound to play() for x milliseconds and then stop.

I tried a thread.sleep, but the sound stops straightaway. I tried a timer, but when the WaveOut is disposed there is a cross-thread exception.

I tried this code, but when I call beep the program freezes.

public class Beep
{
    public Beep(int freq, int ms)
    {
        SineWaveProvider32 sineWaveProvider = new SineWaveProvider32();
        sineWaveProvider.Amplitude = 0.25f;
        sineWaveProvider.Frequency = freq;

        NAudio.Wave.WaveOut waveOut = new NAudio.Wave.WaveOut(WaveCallbackInfo.FunctionCallback());
        waveOut.Init(sineWaveProvider);
        waveOut.Play();
        Thread.Sleep(ms);
        waveOut.Stop();
        waveOut.Dispose();
    }
}

public class SineWaveProvider32 : NAudio.Wave.WaveProvider32
{
    int sample;

    public SineWaveProvider32()
    {
        Frequency = 1000;
        Amplitude = 0.25f; // Let's not hurt our ears
    }

    public float Frequency { get; set; }
    public float Amplitude { get; set; }

    public override int Read(float[] buffer, int offset, int sampleCount)
    {
        int sampleRate = WaveFormat.SampleRate;
        for (int n = 0; n < sampleCount; n++)
        {
            buffer[n + offset] = (float)(Amplitude * Math.Sin((2 * Math.PI * sample * Frequency) / sampleRate));
            sample++;
            if (sample >= sampleRate)
                sample = 0;
        }
   }

解决方案

The SineWaveProvider32 class doesn't need to indefinitely provide audio. If you want the beep to have a maximum duration of a second (say), then for mono 44.1 kHz, you need to provide 44,100 samples. The Read method should return 0 when it has no more data to supply.

To make your GUI thread not block, you need to get rid of Thread.Sleep, waveOut.Stop and Dispose, and simply start playing the audio (you may find window callbacks more reliable than function).

Then, when the audio has finished playing, you can close and clean up the WaveOut object.

这篇关于n音讯播放正弦波对于x毫秒使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆