Skip to content

Instantly share code, notes, and snippets.

@andilabs
Last active December 3, 2022 01:12
Show Gist options
  • Select an option

  • Save andilabs/f90122f1f4cfd9edb1db34ea62fdf323 to your computer and use it in GitHub Desktop.

Select an option

Save andilabs/f90122f1f4cfd9edb1db34ea62fdf323 to your computer and use it in GitHub Desktop.
tutorial1_audio_intro.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/andilabs/f90122f1f4cfd9edb1db34ea62fdf323/tutorial1_audio_intro.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"# SOURCE of this notebook is https://publish.illinois.edu/augmentedlistening/tutorials/music-processing/tutorial-1-introduction-to-audio-processing-in-python/\n",
"# Illinois Augmented Listening Laboratory - research team based in the Coordinated Science Laboratory at the University of Illinois at Urbana-Champaign."
],
"metadata": {
"id": "JC9v4FRKfTsg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "-PEpfBcqcrwD"
},
"source": [
"## Import Libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"id": "yJs6wJsIcrwH"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy.io.wavfile import read, write\n",
"from IPython.display import Audio\n",
"from numpy.fft import fft, ifft\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"source": [
"# Determine where you are"
],
"metadata": {
"id": "kzPaxOeDhFZ5"
}
},
{
"cell_type": "code",
"source": [
"!pwd\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yamiwLY2dCZp",
"outputId": "69ced076-1918-4ffe-b4ca-0b00917d2efc"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/content\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import os\n",
"os.getcwd()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "lnO4HQdJdGK-",
"outputId": "6b342193-dfd1-41b5-f707-de9ae69c6d5f"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'/content'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "markdown",
"source": [
"# Mount Google Drive"
],
"metadata": {
"id": "cJhnsaUMhL4P"
}
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "K39zN-2OeBog",
"outputId": "6133e901-7ee7-4a9f-dac4-7bfa1b16385d"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Navigate to right place"
],
"metadata": {
"id": "Pyz08jXGhQDB"
}
},
{
"cell_type": "code",
"source": [
"os.chdir('/content/drive/My Drive/Colab Notebooks/python-audio-processing')\n"
],
"metadata": {
"id": "PgEBEmLRdW0s"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# List files inside the directory"
],
"metadata": {
"id": "-W3Ol8SGhTbo"
}
},
{
"cell_type": "code",
"source": [
"!ls -l"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "RHPpujeRdFPr",
"outputId": "6475fe27-96d3-4901-f6e3-954a62028a96"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"total 4296\n",
"-rw------- 1 root root 1008044 Dec 3 00:53 output.wav\n",
"-rw------- 1 root root 2016078 May 7 2019 test.wav\n",
"-rw------- 1 root root 1374156 Dec 3 00:53 tutorial1_audio_intro.ipynb\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0Up7AwiUcrwJ"
},
"source": [
"## Read Audio Input"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zGKXdydhcrwK"
},
"source": [
"You can use scipy.io.wavefile to read and write a wav file. \n",
"Usually audio file contains two channels (left and right). We will only keep one channel for simplicity. In the code below, \"data = data[:,0]\" will keep channel 0."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_V2AWpZgcrwL",
"outputId": "3988e38b-b20e-4bcf-d252-f7f804d8225c"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Sampling Frequency is 48000\n"
]
}
],
"source": [
"Fs, data = read('test.wav')\n",
"\n",
"data = data[:,0]\n",
"\n",
"print(\"Sampling Frequency is\", Fs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m6nL-REUcrwM"
},
"source": [
"## Play Audio"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d-UI_IP8crwO"
},
"source": [
"You can use IPython.display.Audio to play audio."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2WW9vizRcrwO",
"outputId": "93f91e78-b4fd-47ca-8b66-69f540c40771"
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <audio controls=\"controls\" >\n",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment