2

我需要做的是计算麦克风输入的频率。我正在使用IWaveProvider它并实现它Read()。缓冲区的大小始终为 8820 个元素,从字节数组到浮点数组的转换似乎也出了问题(FloatBuffer属性部分)。

这里有一些重要的部分......

这是我开始录制的地方:

private void InitializeSoundRecording()
{
    WaveIn waveIn = new WaveIn();
    waveIn.DeviceNumber = 0;   
    waveIn.DataAvailable += (s, e) => this.waveIn_DataAvailable(s, e); 
    waveIn.RecordingStopped += (s, e) => this.waveIn_RecordingStopped(s, e);
    waveIn.WaveFormat = new WaveFormat(44100, 1);
    waveIn.StartRecording();
}

当调用 DataAvailable 事件处理程序时,将执行以下操作:

private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
    WaveBuffer wb = new WaveBuffer(e.Buffer.Length);

    IWaveProvider iWaveProvider = new PitchDetector(new WaveInProvider(sender as WaveIn), new WaveBuffer(e.Buffer));
    iWaveProvider.Read(wb, 0, e.Buffer.Length);

    PitchDetector pd = iWaveProvider as PitchDetector;

    this.ShowPitch(pd.Pitch);
}

最后,这是“实际”的重要部分:

private const int FLOAT_BUFFER_SIZE = 8820;
private IWaveProvider source;
private WaveBuffer waveBuffer;
private int sampleRate;
private float[] fftBuffer;
private float[] prevBuffer;
public float Pitch { get; private set; }

public WaveFormat WaveFormat { get { return this.source.WaveFormat; } }

internal PitchDetector(IWaveProvider waveProvider, WaveBuffer waveBuffer = null)
{
    this.source = waveProvider;
    this.sampleRate = waveProvider.WaveFormat.SampleRate;
    this.waveBuffer = waveBuffer;
}

/// <summary>
/// UNSAFE METHOD! 
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private unsafe float[] ByteArrayToFloatArray(byte[] input)
{
    float[] fb = new float[FLOAT_BUFFER_SIZE];
    unsafe
    {
        fixed (byte* ptrBuffer = input)
        {
            float* ptrFloatBuffer = (float*)ptrBuffer;
            for (int i = 0; i < FLOAT_BUFFER_SIZE; i++)
            {
                fb[i] = *ptrFloatBuffer;
                ptrFloatBuffer++;
            }
        }
    }
    return fb;
}

public int Read(byte[] buffer, int offset = 0, int count = 0)
{
    if (this.waveBuffer == null || this.waveBuffer.MaxSize < count)
        this.waveBuffer = new WaveBuffer(count);

    int readBytes = this.source.Read(this.waveBuffer, 0, count);

    if (readBytes > 0) readBytes = count;

    int frames = readBytes / sizeof(float);

    this.Pitch = this.DeterminePitch(this.waveBuffer.FloatBuffer, frames);

    return frames * 4;
}

奇怪的是,当它进入构造函数时,waveBuffer 包含一些数据(255、1、0 等),但是当我检查 的“buffer”参数时Read(),它完全是 0。每个元素。

出于好奇,为什么Read()有一个缓冲区参数,但实际上根本没有在方法中使用(我从你的一篇文章中得到了那段代码)?

任何解决此问题的帮助将不胜感激!我已经在这方面做了很长一段时间了,但对此毫无意义。

谢谢, 阿兰

4

2 回答 2

0

不清楚你指的是什么文章,我对这个库也不熟悉。但是,该Read方法显然是在读取您的“时间序列”/或其他数据。由此,buffer您所说的参数很可能是您想要放置在数据集任一端的填充长度。

这种填充被称为“零填充”,它用零填充您录制的信号(在信号的任一端放置 n 个零,其中 n 根据使用的基数设置)。这允许人们使用更长的 FFT,这将产生更长的 FFT 结果向量。

较长的 FFT 结果具有更多频率间隔更紧密的频率仓。但它们本质上将提供与原始数据的较短非零填充 FFT 的高质量 Sinc 插值相同的结果。

在没有进一步插值的情况下绘制时,这可能会导致看起来更平滑的频谱。

有关更多信息,请参阅

https://dsp.stackexchange.com/questions/741/why-should-i-zero-pad-a-signal-before-taking-the-fourier-transform

我希望这有帮助。

于 2013-04-08T08:12:57.817 回答
0

这并不是您问题的真正答案,但我safe为您的数组转换函数编写了一个通用替代方法。

using System;
using System.Runtime.InteropServices;

public static class Extensions
{
    public staitc TDestination[] Transform<TSource, TDestination>(
        this TSource[] source)
        where TSource : struct
        where TDestination : struct
    {
        if (source.Length == 0)
        {
            return new TDestination[0];
        }

        var sourceSize = Marshal.SizeOf(typeof(TSource));
        var destinationSize = Marshal.SizeOf(typeof(TDestination));

        var byteLength = source.Length * sourceSize;

        int remainder;
        var destinationLength = Math.DivRem(
            byteLength,
            destinationSize,
            out remainder);
        if (remainder > 0)
        {
            destinationLength++;
        }

        var destination = new TDestination[destinationLength];
        Buffer.BlockCopy(source, 0, destination, 0, byteLength);
        return destination;
    }
}

显然,您可以使用 like

var bytes = new byte[] { 1, 1, 2, 3, 5, 8, 13, 21 };
var floats = bytes.Transform<byte, float>();
于 2013-04-08T08:44:48.937 回答