<jupyter_start><jupyter_text>Reading and Writing Audio Files with audioread[back to overview page](index.ipynb)The module `audioread` can be found there: https://github.com/beetbox/audioreadThis isn't really an option because it has the same disadvantages as the wave module (and more) and it has to be installed manually (but at least it's available as Debian package).Advantages:* WAVEX seems to work* seems to work for both Python 2 and 3Disadvantages:* always 16-bit values are returned (even if file has different data, e.g. pcm32, float32)* manual de-interleaving and conversion is necessary* ~~the number of channels is wrong in some cases (it should work for 1 and 2-channel files though)~~* no error/warning messages if something goes wrongInstallation:    python3 -m pip install audioread  ReadingI don't recommend the module `audioread`, but you can have a look how it is supposed to work, anyway.First, let's enable inline plotting and load the NumPy stuff:<jupyter_code>import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Following code should read a 16-bit WAV file.To convert it to floating point values and to normalize to a range from -1 to 1, we're using the function `pcm2float()`, located in the file [utility.py](utility.py).<jupyter_code>import audioread
import utility

temp = bytearray()
    
with audioread.audio_open('data/test_wav_pcm16.wav') as f:
    framerate = f.samplerate
    duration = f.duration
    nchannels = f.channels
    print("sampling rate = {framerate} Hz, duration = {duration} seconds, channels = {nchannels}".format(**locals()))

    # chunk size can be specified with 'block_samples' (default 1024):
    for chunk in f.read_data():
        temp.extend(chunk)

sig = np.frombuffer(temp, dtype='<i2').reshape(-1, nchannels)

normalized = utility.pcm2float(sig, np.float32)
plt.plot(normalized);<jupyter_output>sampling rate = 44100 Hz, duration = 0.0003401360544217687 seconds, channels = 7<jupyter_text>WritingTODO  Version Info<jupyter_code>import sys, numpy, IPython
print("Versions: NumPy = {}; IPython = {}".format(numpy.__version__, IPython.__version__))

print("Python interpreter:")
print(sys.version)

print("audioread version =", audioread.__version__)<jupyter_output>Versions: NumPy = 1.16.2; IPython = 7.5.0.dev
Python interpreter:
3.7.4 (default, Jul 11 2019, 10:43:21) 
[GCC 8.3.0]
audioread version = 2.1.8
{'fraction_non_alphanumeric': 0.0987961809879618, 'fraction_numerical': 0.033623910336239106, 'mean_word_length': 5.195372750642673, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '515132', 'license': 'CC0-1.0', 'n_tokens_mistral': 818, 'n_tokens_neox': 742, 'n_words': 266, 'path': 'audio-files/audio-files-with-audioread.ipynb', 'repo_name': 'mgeier/python-audio', 'size': '2409'}