#!/usr/bin/env python # coding: utf-8 # # Reading and Writing Audio Files with ewave # # [back to overview page](index.ipynb) # # https://github.com/melizalab/py-ewave # # Advantages: # # * "pure Python" (plus NumPy!) # * floating-point files can be used # * WAVEX is supported # * files can be read partially # * uses [numpy.memmap](http://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html) # # Disadvantages: # # * no 24-bit PCM # * function for rescaling has to be invoked separately (but at least it's available) # # Installation: # # python3 -m pip install ewave # ## Reading # In[ ]: import ewave # In[ ]: with ewave.open('data/test_wav_pcm16.wav') as w: print("samplerate = {0.sampling_rate} Hz, length = {0.nframes} samples, " "channels = {0.nchannels}, dtype = {0.dtype!r}".format(w)) data = w.read() # In[ ]: data # In[ ]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt import numpy as np # In[ ]: plt.plot(data); # Obviously, the returned samples have the `dtype` `'int16'` (as stored in the file). # To be able to do something useful, we'll have to convert it to floating point and normalize it to a range from -1 to 1. # Luckily, there is a function especially made for this: # In[ ]: np.set_printoptions(precision=4) ewave.rescale(data, 'float32') # Note: until version 1.0.4 this was broken, but now it works (see https://github.com/melizalab/py-ewave/issues/4). # # Files with floating point data can be used, WAVEX is supported: # In[ ]: with ewave.open('data/test_wav_float32.wav') as w: data = w.read() plt.plot(data); # Looking good! # In[ ]: with ewave.open('data/test_wavex_pcm16.wav') as w: data = w.read() plt.plot(data); # In[ ]: with ewave.open('data/test_wavex_float32.wav') as w: data = w.read() plt.plot(data); # Opening a 24-bit PCM file fails with a not very verbose error message: # In[ ]: import traceback try: ewave.open('data/test_wav_pcm24.wav') except: traceback.print_exc() else: print("It works (unexpectedly)!") # ## Writing # In[ ]: # TODO! # ## Version Info # In[ ]: print("ewave:", ewave.__version__) import numpy, IPython, sys print("NumPy: {}; IPython: {}".format(numpy.__version__, IPython.__version__)) print("Python interpreter:") print(sys.version) #
#
#
#
#
# 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.
#