Pat
Pat

Reputation: 16891

Read in a wav file and get amplitudes

I need to extract the amplitudes of a wav file and want to do it as a simple commandline app. What's an easy way to do that? Cross-platform would be great. Needs to at least work on windows.

You can download an audio file for testing from soundcloud (or from https://dl.dropboxusercontent.com/u/313647/hosted/one_pos_to_neg_crossing.wav until soundcloud processes it).

Some libraries for potential use:

.NET

Python

Upvotes: 0

Views: 5405

Answers (3)

Blairg23
Blairg23

Reputation: 12045

Here is a solution I came up with using 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);            
}

Project Page: http://accord-framework.net/

Documentation: http://accord-framework.net/docs/Index.html

Upvotes: 2

Pat
Pat

Reputation: 16891

Here's one way to do it in python (using scipy), although I assume that the wav file is 16-bit.

Output

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

Code

"""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()

See also

Upvotes: 0

Pat
Pat

Reputation: 16891

Here's the first way I figured out how to do it with NAudio.

Output

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

Code

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;
                }
            }
        }
    }
}

Upvotes: 1

Related Questions