rspeyer / soundtouch

SoundTouch library compiled for iOS http://www.surina.net/soundtouch/index.html
GNU Lesser General Public License v2.1
313 stars 88 forks source link

C# BPMDetect and NAudio WaveIn example #6

Closed ghost closed 5 years ago

ghost commented 6 years ago

May you provide me an example how to use the BPMDetect with naudio WaveIn Class with C#.

I've tested it, but the BPM is always 0.

This is my code:

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

Thanks