C#带有SoundTouch BPMDetect的NAudio WaveIn示例

时间:2018-05-23 13:39:22

标签: c# naudio soundtouch

有没有人举例说明如何将BPMDetect类与naudio WaveIn一起使用?

我得到的是0 BPM。

这是我到目前为止所做的:

        const int ConstWaveInSampleRate = 44100;

    private BufferedWaveProvider bufferedWaveProvider;

    private WaveIn m_WaveIn;
    private WaveOut m_WaveOut;

    private BPMDetect m_BpmDetect;
    public NAudioBpmDetect()
    {
        int deviceNumber = 0;
        m_WaveIn = new WaveIn();
        m_WaveIn.DeviceNumber = deviceNumber;
        int channels = NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels;
        m_WaveIn.WaveFormat = new WaveFormat(ConstWaveInSampleRate, 16, 1);
        m_WaveOut = new WaveOut();

        m_BpmDetect = new BPMDetect(channels, ConstWaveInSampleRate);
        m_WaveIn.DataAvailable += new EventHandler<WaveInEventArgs>(WaveIn_DataAvailable);

        bufferedWaveProvider = new BufferedWaveProvider(m_WaveIn.WaveFormat);
        bufferedWaveProvider.DiscardOnBufferOverflow = true;

        m_WaveOut.Init(bufferedWaveProvider);
        m_WaveIn.StartRecording();
        m_WaveOut.Play();

    }

    void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
        byte[] buffer = e.Buffer.Where((value, index) => index % 2 == 0).ToArray();
        var waveBuffer = new WaveBuffer(buffer);
        uint count = (uint)waveBuffer.FloatBuffer.Count();
        m_BpmDetect.PutSamples(waveBuffer.FloatBuffer, count);
        Console.WriteLine(m_BpmDetect.Bpm);
    }

从缓冲区分割通道数据可能会出现问题吗?

1 个答案:

答案 0 :(得分:0)

我不熟悉BPMDetect课程,但是如何将数据输入其中有一些问题:

  • 您正在以单声道录制,因此您无需拆分频道。
  • 您可能正在使用2个频道初始化BPMDetect,这里不正确。
  • 即使你是用立体声录音,每个样本都是两个字节,所以你不能只取每隔一个字节。
  • WaveBuffer仅执行重新演绎。它不会将16位整数样本转换为32位浮点样本。

您需要做的是获取捕获的缓冲区中的每对字节,将其解释为Int16(例如使用BitConverter),然后将其转换为float中的public class MyEventIdProvider : IEventIdProvider { public ushort ComputeEventId(LogEvent logEvent) { //your implementation here } } Log.Logger = new LoggerConfiguration() .WriteTo.EventLog("Sample App", manageEventSource: true, eventIdProvider: new MyEventIdProvider()) .CreateLogger(); 范围+/- 1.0除以32768f。这将为您提供BPM检测器应该能够使用的样本。