Convert 3-byte stereo WAV-file to numpy array
Asked Answered
I

3

5

I have been given a large WAV-file of continuous underwater recording which I would like to convert to a numpy array for analysis. I am struggling to do this.

So far I have:

import numpy as np
import scipy as sp
import wave as wv
import struct

wavefile = wv.open(filename,'r')
(nchannels,sampwidth,framerate,nframes,comptype,compname) = wavefile.getparams()

// read a sample as example

wavedata =wavefile.readframes(1)

The first frame looks like this: '\xcd\xbc\xff@\x01\x00'. I have tried to unpack it using struct but unpack whatever I do I get the following error: "str size does not match format". I guess this is related to the fact that Python struct cannot handle 24-bit data.

The parameter of the wave-file looks as following:

  • nchannels=2
  • sampwidth=3
  • framerate=48000
  • nframes=283516532L
  • comptype='NONE'
  • compname='not compressed'

Someone know hows to read a 24-bit stereo WAV-file into a numpy array?

Iconic answered 31/10, 2013 at 14:21 Comment(1)
I think scipy reads 24bit wav files into an array of 32 bit integers, but it doesn't write 24 bit wavs. You can always read the bytes one at a time, and them convert them into 24 bit values doing something like w24 = (w8_3 << 16) | (w8_2 << 8) | w8_1Payment
I
2

For those with similar issues I post my solution. Note that this converts a 24-bit wave file into a signed floating point numpy array. Leave the /int2float part out when only converting to integers.

frames = wavfile.readframes(nsamples)

ch1 = np.zeros(nsamples)
ch2 = np.zeros(nsamples)
int2float = (2**23)-1

for x in np.arange(int(nsamples)):
    ch1_24bit_sample = frames[x*6:x*6+3]
    ch2_24bit_sample = frames[x*6+3:x*6+6]
    ch1_32bit_sample = bit24_2_32(ch1_24bit_sample)
    ch2_32bit_sample = bit24_2_32(ch2_24bit_sample)
    ch1[x]=struct.unpack('i',ch_32bit_sample)[0]
    ch2[x]=struct.unpack('i',ch_32bit_sample)[0]
    ch1[x]=ch1[x]/int2float
    ch2[x]=ch2[x]/int2float

def bit24_2_32(strbytes):
    if strbytes[2] < '\x80':
       return strbytes+'\x00'
    else:
       return strbytes+'\xff'
Iconic answered 15/11, 2013 at 10:46 Comment(0)
E
6

Here's a loop that handles 2, 3, and 4 byte WAV files with arbitrary numbers of channels:

def dataFromWave(fname):
""" return list with interleaved samples """
    f = wave.open(fname, 'rb')
    chans = f.getnchannels()
    samps = f.getnframes()
    sampwidth = f.getsampwidth()
    if  sampwidth == 3: #have to read this one sample at a time
        s = ''
        for k in xrange(samps):
            fr = f.readframes(1)
            for c in xrange(0,3*chans,3):                
                s += '\0'+fr[c:(c+3)] # put TRAILING 0 to make 32-bit (file is little-endian)
    else:
        s = f.readframes(samps)
    f.close()
    unpstr = '<{0}{1}'.format(samps*chans, {1:'b',2:'h',3:'i',4:'i',8:'q'}[sampwidth])
    x = list(struct.unpack(unpstr, s))
    if sampwidth == 3:
        x = [k >> 8 for k in x] #downshift to get +/- 2^24 with sign extension
    return x
Euton answered 15/11, 2013 at 11:3 Comment(2)
Thanks. Just out of curiosity, what requires 2.6?Euton
I think string format was introduced in v2.6. It does not work with the v2.4.3 I am forced to use.Iconic
I
2

For those with similar issues I post my solution. Note that this converts a 24-bit wave file into a signed floating point numpy array. Leave the /int2float part out when only converting to integers.

frames = wavfile.readframes(nsamples)

ch1 = np.zeros(nsamples)
ch2 = np.zeros(nsamples)
int2float = (2**23)-1

for x in np.arange(int(nsamples)):
    ch1_24bit_sample = frames[x*6:x*6+3]
    ch2_24bit_sample = frames[x*6+3:x*6+6]
    ch1_32bit_sample = bit24_2_32(ch1_24bit_sample)
    ch2_32bit_sample = bit24_2_32(ch2_24bit_sample)
    ch1[x]=struct.unpack('i',ch_32bit_sample)[0]
    ch2[x]=struct.unpack('i',ch_32bit_sample)[0]
    ch1[x]=ch1[x]/int2float
    ch2[x]=ch2[x]/int2float

def bit24_2_32(strbytes):
    if strbytes[2] < '\x80':
       return strbytes+'\x00'
    else:
       return strbytes+'\xff'
Iconic answered 15/11, 2013 at 10:46 Comment(0)
A
1

This is an old question but if someone needs additional options and there is no restriction on using external modules, then you can probably use librosa

myNdArray = librosa.core.load(wav_path, sr=sample_rate)[0]
Area answered 3/5, 2018 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.