.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/loading_saving/plot_read_and_write.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_loading_saving_plot_read_and_write.py: Read and write WAV and FLAC =========================== Saves a signal as 16-bit WAV and as FLAC, reads both back, and checks that the lossless files return the same samples. ``io.info`` reads the header alone. .. GENERATED FROM PYTHON SOURCE LINES 8-17 .. code-block:: Python import tempfile from pathlib import Path import numpy as np import audio_samples as aus from audio_samples import mpl .. GENERATED FROM PYTHON SOURCE LINES 18-20 Generate one second of a chirp and convert it to 16-bit integers, the sample type most WAV files use. .. GENERATED FROM PYTHON SOURCE LINES 20-28 .. code-block:: Python audio = aus.generation.chirp(200.0, 4000.0, 1.0, 22050).as_i16() folder = Path(tempfile.mkdtemp()) wav_path = folder / "chirp.wav" flac_path = folder / "chirp.flac" aus.io.save(str(wav_path), audio) aus.io.save(str(flac_path), audio) .. GENERATED FROM PYTHON SOURCE LINES 29-30 ``io.info`` reads the header without decoding the samples. .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python info = aus.io.info(str(flac_path)) print(info.sample_rate, info.channels, info.bits_per_sample, info.num_samples) .. rst-class:: sphx-glr-script-out .. code-block:: none 22050 1 16 22050 .. GENERATED FROM PYTHON SOURCE LINES 34-35 ``io.read`` keeps the file's sample type unless ``as_type`` asks for another. .. GENERATED FROM PYTHON SOURCE LINES 35-43 .. code-block:: Python from_wav = aus.io.read(str(wav_path)) from_flac = aus.io.read(str(flac_path)) assert from_wav.dtype == audio.dtype assert np.array_equal(from_wav.to_numpy(), audio.to_numpy()) assert np.array_equal(from_flac.to_numpy(), audio.to_numpy()) print(f"WAV {wav_path.stat().st_size} bytes, FLAC {flac_path.stat().st_size} bytes") .. rst-class:: sphx-glr-script-out .. code-block:: none WAV 44144 bytes, FLAC 34780 bytes .. GENERATED FROM PYTHON SOURCE LINES 44-45 ``as_type=SampleType.F32`` maps the integers to floats in [-1, 1]. .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: Python as_float = aus.io.read(str(flac_path), as_type=aus.SampleType.F32) assert abs(as_float.peak()) <= 1.0 fig, ax = mpl.plot_waveform(as_float, title="Read back from FLAC, as float32") .. image-sg:: /gallery/loading_saving/images/sphx_glr_plot_read_and_write_001.png :alt: Read back from FLAC, as float32 :srcset: /gallery/loading_saving/images/sphx_glr_plot_read_and_write_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 51-52 An ``AudioSamples`` shows a player in a notebook or on this page. .. GENERATED FROM PYTHON SOURCE LINES 52-53 .. code-block:: Python as_float .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration1.000 s (22050 samples per channel)
peak1.0000 (0.0 dBFS)
RMS0.7071 (-3.0 dBFS)


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.072 seconds) .. _sphx_glr_download_gallery_loading_saving_plot_read_and_write.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_read_and_write.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_read_and_write.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_read_and_write.zip `