【问题标题】:Read in a wav file and get amplitudes读入 wav 文件并获取振幅
【发布时间】:2013-11-22 15:39:28
【问题描述】:

我需要提取 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

【问题讨论】:

    标签: audio


    【解决方案1】:

    这是我使用 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 个频道。
    【解决方案2】:

    这是我想出如何使用 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;
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      这是在 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()
      

      另见

      【讨论】:

        猜你喜欢
        • 2015-04-17
        • 2012-09-05
        • 2017-01-11
        • 2020-05-29
        • 1970-01-01
        • 2011-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多