将WAV文件转换为频谱图

时间:2013-06-20 12:31:37

标签: c# naudio spectrogram

嗨,我对这件事很新,所以请耐心等待。我正在尝试将WAV文件转换为频谱图但不确定如何开始。我读了一些关于读取PCM数据(我认为是我的WAV文件)并将其存储在WavReader类中的数组中的内容,然后对其应用FFT并将其转换为GUI。我目前正在使用Naudio来实现这一点,但找不到任何显示如何将WAV文件转换为频谱图的内容。感谢

修改: 我发现有关使用Naudio将PCM转换为FFT并且卡住了。

     using (var reader = new AudioFileReader("test1.wav"))
        {
          // test1.wav is my file to process
          // test0.wav is my temp file

            IWaveProvider stream16 = new WaveFloatTo16Provider(reader);
            using (WaveFileWriter converted = new WaveFileWriter("test0.wav", stream16.WaveFormat))
            {
                // buffer length needs to be a power of 2 for FFT to work nicely
                // however, make the buffer too long and pitches aren't detected fast enough
                // successful buffer sizes: 8192, 4096, 2048, 1024
                // (some pitch detection algorithms need at least 2048)
                byte[] buffer = new byte[8192];
                int bytesRead;
                do
                {
                    bytesRead = stream16.Read(buffer, 0, buffer.Length);
                    converted.WriteData(buffer, 0, bytesRead);
                } while (bytesRead != 0 && converted.Length < reader.Length);
            }
        }

编辑:我还想知道是否可以通过编程方式比较2个不同文件的2个频谱图。

1 个答案:

答案 0 :(得分:8)

您还可以使用本地提供所有这些功能的BASS.NET库,并且该库是免费的。

Visuals.CreateSpectrum3DVoicePrint Method正是如此。

如果您在使用它时遇到困难,请随时寻求帮助。

编辑:这是一个快速而肮脏的样本

enter image description here

public partial class Form1 : Form
{
    private int _handle;
    private int _pos;
    private BASSTimer _timer;
    private Visuals _visuals;

    public Form1()
    {
        InitializeComponent();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        bool spectrum3DVoicePrint = _visuals.CreateSpectrum3DVoicePrint(_handle, pictureBox1.CreateGraphics(),
                                                                        pictureBox1.Bounds, Color.Cyan, Color.Green,
                                                                        _pos, false, true);
        _pos++;
        if (_pos >= pictureBox1.Width)
        {
            _pos = 0;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string file = "..\\..\\mysong.mp3";
        if (Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Handle))
        {
            _handle = Bass.BASS_StreamCreateFile(file, 0, 0, BASSFlag.BASS_DEFAULT);

            if (Bass.BASS_ChannelPlay(_handle, false))
            {
                _visuals = new Visuals();
                _timer = new BASSTimer((int) (1.0d/10*1000));
                _timer.Tick += timer_Tick;
                _timer.Start();
            }
        }
    }
}

编辑2

您可以提供文件名,但您也可以使用接受IntPtr的其他重载提供您自己的音频数据,或者使用Bass.BASS_StreamCutData使用Bass.BASS_StreamCreatePush。

关于比较光谱图,您可以执行以下操作:

  • 将图像调整为较小的尺寸,通过将图像抖动到8位来减少信息(但是使用了一个好的算法)
  • 比较两张图片

然而,为了比较音频数据,我强烈建议您使用指纹,它大致是这样做的,但比我的建议更强大。

这是一个可以免费使用的指纹库:

http://www.codeproject.com/Articles/206507/Duplicates-detector-via-audio-fingerprinting

不完全确定它适用于小样本。

编辑3

我恐怕无法找到我已经阅读过的链接,但这就是他们所做的事情:减少数据和比较图像,例如下面的示例(最后一张图片) :

(注意:不要与图像1进行比较,它只是为了说明为什么使用较低分辨率可能会提高产量)

enter image description here

(来自http://blog.echonest.com/post/545323349/the-echo-nest-musical-fingerprint-enmfp

现在是对该过程的一个非常基本的解释:

比较来源A:

enter image description here

比较源B :(我刚刚更改了A的区域)

enter image description here

比较结果:

(通过将以前的图像添加为图层并将第二层混合设置为差异而不是正常而使用Paint.Net完成)

enter image description here

如果指纹相同,则所得图像将完全变黑。

通过将数据减少为8位图像,您可以简化比较过程,但请记住,您需要一个良好的抖动算法。

这是一个非常好的:

http://www.codeproject.com/Articles/66341/A-Simple-Yet-Quite-Powerful-Palette-Quantizer-in-C

它与Photoshop或Hypersnap的一个不相上下(IMO很特别),但这可能足以完成任务。

并且不惜一切代价避免Floyd–Steinberg dithering或做出错误扩散的事情。

以下是创建抖动算法的一些尝试:http://bisqwit.iki.fi/story/howto/dither/jy/

请谨慎对待,因为我不是该领域的专家,但大概是如何完成的。

转到https://dsp.stackexchange.com/并在那里问几个问题,你可能会得到有用的提示。

相关问题