Faster way to convert from 24-bit wav pcm format to float?

I need to read data from a wav file in 24 bit format in pcm format and convert to float. I am using Python 2.7.2.

The wave packet reads the data as a string, so I tried:

import wave import numpy as np import array import struct f = wave.open('filename.wav') # read in entire wav file wdata = f.readframes(nFrames) f.close() # unpack into signed integers and convert to float data = array.array('f') for i in range(0,nFrames*3,3): data.append(float(struct.unpack('<i', '\x00'+ wdata[i:i+3])[0])) # normalize sample values data = np.array(data) data = data / 0x800000 

This is slightly faster than my previous approaches, but still pretty slow. Can anyone suggest a more efficient method?

+2
source share
2 answers

This seems pretty fast, it processes 24-bit values, and it performs normalization:

 from scikits.audiolab import Sndfile import numpy as np f = Sndfile(fname, 'r') data = np.array(f.read_frames(f.nframes), dtype=np.float64) f.close() return data 
+1
source
 import sndhdr, wave, struct if sndhdr.what(fname)[0] != 'wav': raise Exception("file doesn't have wav header") with wave.open(fname) as wav: params = (nchannels,sampwidth,rate,nframes,comp,compname) = wav.getparams() frames = wav.readframes(nframes*nchannels) out = struct.unpack_from("%dh" % nframes*nchannels, frames) 
0
source

Source: https://habr.com/ru/post/1482428/


All Articles