#!/usr/bin/env python # coding: utf-8 # # Reading and Writing Audio Files with wave # # [back to overview page](index.ipynb) # # The `wave` module is part of the Python standard library. # # Documentation: # # * http://docs.python.org/2/library/wave.html (Python 2.x) # * http://docs.python.org/3/library/wave.html (Python 3.x) # # Audio data is handled with the Python type `str` (Python 2.x) or `bytes` (Python 3.x). # # Advantages: # # * part of the standard library, no further dependencies # * 24-bit files can be used (but manual conversion is necessary) # * partial reading is possible # * works with both Python 2 and 3 # # Disadvantages: # # * 32-bit float not supported # * WAVEX doesn't work # * manual de-interleaving and conversion is necessary # ## Reading # # Reading a 16-bit WAV file into a NumPy array is not hard, but it requires a few lines of code: # In[1]: import wave import numpy as np import utility with wave.open('data/test_wav_pcm16.wav') as w: channels = w.getnchannels() assert w.getsampwidth() == 2 data = w.readframes(w.getnframes()) sig = np.frombuffer(data, dtype=' # # CC0 # #
# To the extent possible under law, # the person who associated CC0 # with this work has waived all copyright and related or neighboring # rights to this work. #