有没有人举例说明如何将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);
}
从缓冲区分割通道数据可能会出现问题吗?
答案 0 :(得分:0)
我不熟悉BPMDetect
课程,但是如何将数据输入其中有一些问题:
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检测器应该能够使用的样本。