Audio 读取wav文件并获取振幅

Audio 读取wav文件并获取振幅,audio,Audio,我需要提取一个wav文件的振幅,并想做一个简单的命令行应用程序。做这件事的简单方法是什么?跨平台就好了。至少需要在windows上工作 您可以(或者在soundcloud处理之前) 一些潜在用途的库: .NET python 这是我找到的第一种使用NAudio的方法 输出 代码 使用系统; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用N

我需要提取一个wav文件的振幅,并想做一个简单的命令行应用程序。做这件事的简单方法是什么?跨平台就好了。至少需要在windows上工作

您可以(或者在soundcloud处理之前)

一些潜在用途的库:

.NET
python

这是我找到的第一种使用NAudio的方法

输出 代码
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用NAudio.波;
名称空间声音振幅{
班级计划{
私有静态int Main(字符串[]args){
#如果调试
Console.WriteLine(String.Join(“,”,args));
args=new[]{@“W:\materials\audio\one_pos_to_neg_crossing.wav”};
#恩迪夫
返回Cli(args);
}
静态int Cli(字符串[]args){
字符串文件名=args[0];
var soundFile=新文件信息(文件名);
foreach(在AmplitudesFromFile(声音文件)中浮动s){
控制台。写入线(s);
}
//Console.WriteLine();
#如果调试
Console.Read();
#恩迪夫
返回0;
}
公共静态IEnumerable AmplitudesFromFile(FileInfo声音文件){
var reader=newaudiofilereader(soundFile.FullName);
int count=4096;//任意
float[]缓冲区=新的float[计数];
整数偏移=0;
int numRead=0;
而((numRead=reader.Read(缓冲区、偏移量、计数))>0){
foreach(缓冲区中的浮点放大器。取数(numRead)){
收益率返回放大器;
}
}
}
}
}

这是我找到的第一种使用NAudio的方法

输出 代码
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用NAudio.波;
名称空间声音振幅{
班级计划{
私有静态int Main(字符串[]args){
#如果调试
Console.WriteLine(String.Join(“,”,args));
args=new[]{@“W:\materials\audio\one_pos_to_neg_crossing.wav”};
#恩迪夫
返回Cli(args);
}
静态int Cli(字符串[]args){
字符串文件名=args[0];
var soundFile=新文件信息(文件名);
foreach(在AmplitudesFromFile(声音文件)中浮动s){
控制台。写入线(s);
}
//Console.WriteLine();
#如果调试
Console.Read();
#恩迪夫
返回0;
}
公共静态IEnumerable AmplitudesFromFile(FileInfo声音文件){
var reader=newaudiofilereader(soundFile.FullName);
int count=4096;//任意
float[]缓冲区=新的float[计数];
整数偏移=0;
int numRead=0;
而((numRead=reader.Read(缓冲区、偏移量、计数))>0){
foreach(缓冲区中的浮点放大器。取数(numRead)){
收益率返回放大器;
}
}
}
}
}

这是我找到的第一种使用NAudio的方法

输出 代码
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用NAudio.波;
名称空间声音振幅{
班级计划{
私有静态int Main(字符串[]args){
#如果调试
Console.WriteLine(String.Join(“,”,args));
args=new[]{@“W:\materials\audio\one_pos_to_neg_crossing.wav”};
#恩迪夫
返回Cli(args);
}
静态int Cli(字符串[]args){
字符串文件名=args[0];
var soundFile=新文件信息(文件名);
foreach(在AmplitudesFromFile(声音文件)中浮动s){
控制台。写入线(s);
}
//Console.WriteLine();
#如果调试
Console.Read();
#恩迪夫
返回0;
}
公共静态IEnumerable AmplitudesFromFile(FileInfo声音文件){
var reader=newaudiofilereader(soundFile.FullName);
int count=4096;//任意
float[]缓冲区=新的float[计数];
整数偏移=0;
int numRead=0;
而((numRead=reader.Read(缓冲区、偏移量、计数))>0){
foreach(缓冲区中的浮点放大器。取数(numRead)){
收益率返回放大器;
}
}
}
}
}

这是我找到的第一种使用NAudio的方法

输出 代码
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用NAudio.波;
名称空间声音振幅{
班级计划{
私有静态int Main(字符串[]args){
#如果调试
Console.WriteLine(String.Join(“,”,args));
args=new[]{@“W:\materials\audio\one_pos_to_neg_crossing.wav”};
#恩迪夫
返回Cli(args);
}
静态int Cli(字符串[]args){
字符串文件名=args[0];
var soundFile=新文件信息(文件名);
foreach(在AmplitudesFromFile(声音文件)中浮动s){
控制台。写入线(s);
}
//Console.WriteLine();
#如果调试
Console.Read();
#恩迪夫
返回0;
}
公共静态IEnumerable AmplitudesFromFile(FileInfo声音文件){
var reader=newaudiofilereader(soundFile.FullName);
int count=4096;//任意
float[]缓冲区=新的float[计数];
整数偏移=0;
int numRead=0;
而((numRead=reader.Read(缓冲区、偏移量、计数))>0){
foreach(缓冲区中的浮点放大器。取数(numRead)){
收益率
>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;
                }
            }
        }
    }
}
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()
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);            
}