How to get the maximum signed short integer in Python (i.e. SHRT_MAX in C limits.h) ?
I want to normalize samples from one channel of a file *.wav, so instead of a bunch of 16-bit integers, I want a bunch of floats from 1 to -1. Here is what I have (the corresponding code is in the function normalized_samples()):
def samples(clip, chan_no = 0):
STRUCT_FMT = { 1 : 'B',
2 : 'h' }
for i in range(clip.getnframes()):
yield struct.unpack(STRUCT_FMT[clip.getsampwidth()] * clip.getnchannels(),
clip.readframes(1))[chan_no]
def normalized_samples(clip, chan_no = 0):
for sample in samples(clip, chan_no):
yield float(sample) / float(32767)
source
share