{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading and Writing Audio Files with ewave\n", "\n", "[back to overview page](index.ipynb)\n", "\n", "https://github.com/melizalab/py-ewave\n", "\n", "Advantages:\n", "\n", "* \"pure Python\" (plus NumPy!)\n", "* floating-point files can be used\n", "* WAVEX is supported\n", "* files can be read partially\n", "* uses [numpy.memmap](http://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html)\n", "\n", "Disadvantages:\n", "\n", "* no 24-bit PCM\n", "* function for rescaling has to be invoked separately (but at least it's available)\n", "\n", "Installation:\n", "\n", " python3 -m pip install ewave" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ewave" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with ewave.open('data/test_wav_pcm16.wav') as w:\n", " print(\"samplerate = {0.sampling_rate} Hz, length = {0.nframes} samples, \"\n", " \"channels = {0.nchannels}, dtype = {0.dtype!r}\".format(w))\n", " data = w.read()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(data);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Obviously, the returned samples have the `dtype` `'int16'` (as stored in the file).\n", "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.\n", "Luckily, there is a function especially made for this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.set_printoptions(precision=4)\n", "ewave.rescale(data, 'float32')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: until version 1.0.4 this was broken, but now it works (see https://github.com/melizalab/py-ewave/issues/4).\n", "\n", "Files with floating point data can be used, WAVEX is supported:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with ewave.open('data/test_wav_float32.wav') as w:\n", " data = w.read()\n", " \n", "plt.plot(data);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looking good!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with ewave.open('data/test_wavex_pcm16.wav') as w:\n", " data = w.read()\n", " \n", "plt.plot(data);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with ewave.open('data/test_wavex_float32.wav') as w:\n", " data = w.read()\n", " \n", "plt.plot(data);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Opening a 24-bit PCM file fails with a not very verbose error message:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import traceback\n", "try:\n", " ewave.open('data/test_wav_pcm24.wav')\n", "except:\n", " traceback.print_exc()\n", "else:\n", " print(\"It works (unexpectedly)!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# TODO!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Version Info" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"ewave:\", ewave.__version__)\n", "\n", "import numpy, IPython, sys\n", "print(\"NumPy: {}; IPython: {}\".format(numpy.__version__, IPython.__version__))\n", "\n", "print(\"Python interpreter:\")\n", "print(sys.version)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n",
" \n",
"
\n",
" \n",
"
\n",
" To the extent possible under law,\n",
" the person who associated CC0\n",
" with this work has waived all copyright and related or neighboring\n",
" rights to this work.\n",
"