如何在
Python中获得最大有符号短整数(即C的limits.h中的SHRT_MAX)?
我想从* .wav文件的单个通道中标准化样本,所以我想要一堆介于1和-1之间的一堆浮点数,而不是一堆16位有符号整数.这是我得到的(相关代码在normalized_samples()函数中):
def samples(clip, chan_no = 0):
# *.wav files generally come in 8-bit unsigned ints or 16-bit signed ints
# python's wave module gives sample width in bytes, so STRUCT_FMT
# basically converts the wave.samplewidth into a struct fmt string
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) ### THIS IS WHERE I NEED HELP