Chris@5: Chris@5: # To the extent possible under law, Chris Cannam and QMUL have waived Chris@5: # all copyright and related or neighboring rights to this file Chris@5: # (http://creativecommons.org/about/cc0) Chris@0: Chris@0: import wave Chris@0: import numpy as np Chris@0: Chris@0: def decode_to_mono_samples(rawdata, n_channels): Chris@0: """Given raw PCM16 interleaved WAV-format audio data as a binary Chris@0: string, decode and return as floating-point samples in range [-1,1)""" Chris@0: samples = np.fromstring(rawdata, 'Int16').astype(float) Chris@0: samples = samples / 32768 Chris@0: totals = sum(samples[c::n_channels] for c in range(0, n_channels)) Chris@0: return totals / n_channels Chris@0: Chris@0: def read_all_mono_samples_from_file(filename): Chris@0: """Read the whole of the given PCM16 WAV-format given audio file, Chris@0: mix down to mono by taking means across channels, and return as an Chris@0: array of floating-point sampless in range [-1,1)""" Chris@0: wavfile = wave.open(filename, 'r') Chris@0: n_channels = wavfile.getnchannels() Chris@0: rawdata = wavfile.readframes(-1) # read all data Chris@0: decoded = decode_to_mono_samples(rawdata, n_channels) Chris@0: return (decoded, wavfile.getframerate()) Chris@0: