import wave fp = wave.open("passport.wav", 'r') print fp print fp.getframerate(), fp.getnchannels(), fp.getnframes(), fp.getsampwidth(), fp.getcomptype(), fp.getcompname() fp.getparams() bytes = fp.readframes(8) print bytes for byte in bytes: print ord(byte), for byte in bytes: print bin(ord(byte)), import struct struct.unpack("hhhhhhhh", bytes) fp.rewind() samples = [] for i in range(fp.getnframes()): s = fp.readframes(1) samples.append(struct.unpack('h', s)) plot(samples) samples = np.array(samples) samplesnorm = samples/2**15 plot(samplesnorm) plot(samplesnorm) xlim((0, 100)) samplesnorm = samples/2.0**15 plot(samplesnorm) print samples.dtype print samplesnorm.dtype outfile = wave.open('testout.wav','w') outfile.getframerate() sr = 44100 outfile.setframerate(sr) outfile.getnchannels() outfile.setnchannels(1) outfile.getnchannels() outfile.getsampwidth() outfile.setsampwidth(2) outfile.getsampwidth() dur = 1.0 phase = linspace(0, 2*pi, dur*sr) out = sin(phase)*2**15 for sample in out: outfile.writeframes(struct.pack('h', int(sample))) outfile.close() outfile = wave.open('testout2.wav','w') sr = 44100 outfile.setframerate(sr) outfile.setnchannels(1) outfile.setsampwidth(2) freq = 440 dur = 1.0 phase = linspace(0, 2*pi, dur*sr) out = sin(phase*freq)*2**15 for sample in out: outfile.writeframes(struct.pack('h', int(sample))) outfile.close() # Great audio player for ipython notebook I found at http://nbviewer.ipython.org/github/Carreau/posts/blob/master/07-the-sound-of-hydrogen.ipynb # The original source is here: https://github.com/Carreau/posts import StringIO import base64 import struct from IPython.core.display import HTML def wavPlayer(data, rate): """ will display html 5 player for compatible browser The browser need to know how to play wav through html5. there is no autoplay to prevent file playing when the browser opens Adapted from SciPy.io. """ buffer = StringIO.StringIO() buffer.write(b'RIFF') buffer.write(b'\x00\x00\x00\x00') buffer.write(b'WAVE') buffer.write(b'fmt ') if data.ndim == 1: noc = 1 else: noc = data.shape[1] bits = data.dtype.itemsize * 8 sbytes = rate*(bits // 8)*noc ba = noc * (bits // 8) buffer.write(struct.pack('' or (data.dtype.byteorder == '=' and sys.byteorder == 'big'): data = data.byteswap() buffer.write(data.tostring()) size = buffer.tell() buffer.seek(4) buffer.write(struct.pack(' """.format(base64=base64.encodestring(val)) display(HTML(src)) from IPython.display import Audio Audio(url="http://www.nch.com.au/acm/8k16bitpcm.wav") rate = 44100 #44.1 khz duration = 2.5 # in sec def normedsin(f,t): return 2**14*sin(2*pi*f*t) time = np.linspace(0,duration, num=rate*duration) wavPlayer(normedsin(440, time).astype(np.int16), rate) rate = 44100 #44.1 khz duration = 1 # in sec time = np.linspace(0,duration, num=rate*duration) wavPlayer(normedsin(880, time).astype(np.int16), rate) wavPlayer(samples.astype(np.int16), fp.getframerate()) wavPlayer(samples.astype(np.int16), 48000) from scipy.io import wavfile sr, frames = wavfile.read('passport.wav') print sr plot(frames) frames.dtype wavPlayer(frames, sr) from essentia.standard import AudioLoader loader = AudioLoader(filename='passport.wav') audio, sr, nchnls = loader() plot(audio); audio.shape, audio.dtype wavPlayer(audio.astype(int16), sr) wavPlayer((audio*2**15).astype(int16), sr) wavPlayer((audio[:,0]*2**15).astype(int16), sr) loader = AudioLoader(filename='/home/andres/Music/Jose Vicente Asuar/Obra Electroacustica I/01. Variaciones espctrales (1959).flac') audio, sr, nchnls = loader() plot(audio[:44100]) wavPlayer(audio, sr) wavPlayer((audio[:444100]*2.0**15).astype(int16), sr)