读入一个wav文件并获得振幅

时间:2013-11-10 22:37:02

标签: audio

我需要提取wav文件的幅度,并希望将其作为一个简单的命令行应用程序。有什么简单的方法可以做到这一点?跨平台会很棒。至少需要在Windows上工作。

你可以download an audio file for testing from soundcloud(或从https://dl.dropboxusercontent.com/u/313647/hosted/one_pos_to_neg_crossing.wav直到soundcloud处理它。)

一些潜在用途的图书馆:

.NET

的Python

3 个答案:

答案 0 :(得分:2)

这是我使用Accord.NET提出的解决方案:

public IEnumerable<float> AmplitudesFromFile(string fileName)
{
        Accord.Audio.Formats.WaveDecoder sourceDecoder = new Accord.Audio.Formats.WaveDecoder(fileName);
        Signal sourceSignal = sourceDecoder.Decode();
        for (int i = 0; i < sourceSignal.Samples; i++) yield return sourceSignal.GetSample(1, i);            
}

项目页面:http://accord-framework.net/

文档:http://accord-framework.net/docs/Index.html

答案 1 :(得分:0)

这是我想出如何用NAudio做的第一种方式。

输出

>SoundToAmplitudes.exe w:\materials\audio\one_pos_to_neg_crossing.wav
0.04284668
-0.005615234
-0.1177368

代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;

namespace SoundToAmplitudes {
    class Program {
        private static int Main(string[] args) {
#if DEBUG
            Console.WriteLine(String.Join(", ", args));
            args = new[] { @"W:\materials\audio\one_pos_to_neg_crossing.wav" };
#endif
            return Cli(args);
        }

        static int Cli(string[] args) {
            string fileName = args[0];
            var soundFile = new FileInfo(fileName);
            foreach (float s in AmplitudesFromFile(soundFile)) {
                Console.WriteLine(s);
            }
            //Console.WriteLine();
#if DEBUG
            Console.Read();
#endif
            return 0;
        }

        public static IEnumerable<float> AmplitudesFromFile(FileInfo soundFile) {
            var reader = new AudioFileReader(soundFile.FullName);
            int count = 4096; // arbitrary
            float[] buffer = new float[count];
            int offset = 0;
            int numRead = 0;
            while ((numRead = reader.Read(buffer, offset, count)) > 0) {
                foreach (float amp in buffer.Take(numRead)) {
                    yield return amp;
                }
            }
        }
    }
}

答案 2 :(得分:0)

这是在python中使用scipy)的一种方法,虽然我假设wav文件是16位。

输出

W:\>python amplitudes.py w:\materials\audio\one_pos_to_neg_crossing.wav
0.0428479873043
-0.00561540574358
-0.117740409558

代码

"""Usage:
    amplitudes WAV_FILE

    Returns the (linear) amplitude of the signal inside the wav file, sample by sample.
"""
from __future__ import division
import docopt

import scipy.io.wavfile

MAX_WAV16_AMP = 32767  # = 2**15-1  # because wav16 is signed (wav8 isn't)


def main():
    args = docopt.docopt(__doc__)

    one_pos_to_neg_crossing_path = r"W:\materials\audio\one_pos_to_neg_crossing.wav"
    if False: args['WAV_FILE'] = one_pos_to_neg_crossing_path

    rate, amp_arr = scipy.io.wavfile.read(args['WAV_FILE'])
    for amp in (amp_arr / MAX_WAV16_AMP):
        print amp

if __name__ == '__main__':
    main()

另见