how to convert wav file to floating amplitude

so I asked everything in the title:

I have a WAV file (written by PyAudio from input audio) and I want to convert it to floating point data corresponding to sound level (amplitude) in order to do some Fourier transforms, etc ...

Does anyone have an idea to convert WAV data to floating?

+7
source share
4 answers

I have identified two worthy ways to do this.

Method 1: use the wavefile module

Use this method if you don't mind installing some additional libraries that included a bit of clutter on my Mac, but it was easy on my Ubuntu server.

https://github.com/vokimon/python-wavefile

import wavefile # returns the contents of the wav file as a double precision float array def wav_to_floats(filename = 'file1.wav'): w = wavefile.load(filename) return w[1][0] signal = wav_to_floats(sys.argv[1]) print "read "+str(len(signal))+" frames" print "in the range "+str(min(signal))+" to "+str(min(signal)) 

Method 2: use the wave module

Use this method if you need less problems installing modules.

It reads a wav file from the file system and converts it to a float in the range -1 to 1. It works with 16-bit files, and if they are> 1 channel, they will alternate the samples in the same way as they are found in the file. For other bit depths, change the "h" in the argument to struct.unpack according to the table at the bottom of this page:

https://docs.python.org/2/library/struct.html

It will not work for 24-bit files, since there is no data type that is 24 bits, so there is no way to tell struct.unpack what to do.

 import wave import struct import sys def wav_to_floats(wave_file): w = wave.open(wave_file) astr = w.readframes(w.getnframes()) # convert binary chunks to short a = struct.unpack("%ih" % (w.getnframes()* w.getnchannels()), astr) a = [float(val) / pow(2, 15) for val in a] return a # read the wav file specified as first command line arg signal = wav_to_floats(sys.argv[1]) print "read "+str(len(signal))+" frames" print "in the range "+str(min(signal))+" to "+str(min(signal)) 
+10
source

Most wave files are in 16-bit integer PCM format.

What would you like:

  • Parse the header to see what format it is (check the link from Xophmeister)
  • Read the data, take integer values ​​and convert them to float

Integer values ​​range from -32768 to 32767, and you need to convert the values ​​from -1.0 to 1.0 to floating points.

I don't have code in python, however in C ++, here is a code snippet if the PCM data is a 16-bit integer and converts it to a float (32-bit):

 short* pBuffer = (short*)pReadBuffer; const float ONEOVERSHORTMAX = 3.0517578125e-5f; // 1/32768 unsigned int uFrameRead = dwRead / m_fmt.Format.nBlockAlign; for ( unsigned int i = 0; i < uFrameCount * m_fmt.Format.nChannels; ++i ) { short i16In = pBuffer[i]; out_pBuffer[i] = (float)i16In * ONEOVERSHORTMAX; } 

Be careful with stereo files as PCM stereo data in wave files alternate, which means the data looks like LRLRLRLRLRLRLRLRLR (instead of LLLLLLLLRRRRRRRR). You may or may not need to interleave, depending on what you do with the data.

+5
source

I spent hours trying to find the answer to this question. The solution turned out to be very simple: struct.unpack is what you are looking for. The final code will look something like this:

 rawdata=stream.read() # The raw PCM data in need of conversion from struct import unpack # Import unpack -- this is what does the conversion npts=len(rawdata) # Number of data points to be converted formatstr='%ih' % npts # The format to convert the data; use '%iB' for unsigned PCM int_data=unpack(formatstr,rawdata) # Convert from raw PCM to integer tuple 

Most loans relate to Interpretation of WAV data . The only trick is to get the format for unpacking: it must be the correct number of bytes and the correct format (signed or unsigned).

+5
source

The Microsoft WAVE format is well documented. See https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ , for example. It wouldn’t be so much to write a file analyzer, to open and interpret the data, to get the information you need ... However, it was almost certainly done before, so I'm sure someone will give a “easier” answer ; )

0
source

All Articles