Skip to content

Instantly share code, notes, and snippets.

@bastibe
Created March 27, 2013 13:18
Show Gist options
  • Save bastibe/5254089 to your computer and use it in GitHub Desktop.
Save bastibe/5254089 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Audio processing in Python"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Audio processing in Python\n",
"\n",
"we use the Package `PyAudio` and some standard libraries"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sys\n",
"import time\n",
"from pyaudio import PyAudio, paFloat32\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Initialize PyAudio\n",
"pa = PyAudio()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Get some information about the default audio hardware\n",
"default_device = pa.get_default_input_device_info()\n",
"fs = int(default_device['defaultSampleRate'])\n",
"channels = default_device['maxInputChannels']"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def easy_read(stream, num_samples, num_channels):\n",
" \"\"\" \n",
" Read samples from stream and convert them to a numpy array\n",
"\n",
" stream is the stream to read from\n",
" num_samples is the number of samples to read\n",
" num_channels is the number of channels to read\n",
" \"\"\"\n",
" data = stream.read(num_samples)\n",
" data = np.fromstring(data, dtype=np.float32)\n",
" data = np.reshape(data, (len(data)/num_channels,num_channels)).T\n",
" return data"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def rms(arr):\n",
" \"\"\" calculates the rms power of an array \"\"\"\n",
" return np.sqrt(np.mean(np.power(arr, 2)))\n",
"\n",
"def dB(power): \n",
" \"\"\" calculates the logarithmic dB value of a power \"\"\"\n",
" return 20*np.log10(power)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# open an input stream using the default audio device\n",
"print('Open Stream on %s' % default_device['name'])\n",
"stream = pa.open(rate=fs,\n",
" channels=channels,\n",
" format=paFloat32,\n",
" input=True)\n",
"\n",
"# run for three seconds\n",
"t_start = time.time()\n",
"while time.time()-t_start < 3:\n",
" # read some audio data from the sound card\n",
" data = easy_read(stream, int(fs/10), channels)\n",
" for i in range(channels):\n",
" print(' %5.2f' % dB(rms(data[i,:])), end='')\n",
" print(' dB FS')\n",
"\n",
"stream.stop_stream()\n",
"stream.close()\n",
"print('Stream Closed')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Open Stream on Built-in Microph\n",
" -80.31"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -80.31 dB FS\n",
" -83.32"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -83.32 dB FS\n",
" -84.52"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -84.52 dB FS\n",
" -85.42"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -85.42 dB FS\n",
" -84.31"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -84.31 dB FS\n",
" -84.54"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -84.54 dB FS\n",
" -83.95"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -83.95 dB FS\n",
" -83.29"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -83.29 dB FS\n",
" -83.85"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -83.85 dB FS\n",
" -81.96"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -81.96 dB FS\n",
" -58.30"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -58.30 dB FS\n",
" -77.62"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -77.62 dB FS\n",
" -80.63"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -80.63 dB FS\n",
" -84.42"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -84.42 dB FS\n",
" -83.95"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -83.95 dB FS\n",
" -85.04"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -85.04 dB FS\n",
" -84.94"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -84.94 dB FS\n",
" -84.45"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -84.45 dB FS\n",
" -82.53"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -82.53 dB FS\n",
" -82.50"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -82.50 dB FS\n",
" -84.29"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -84.29 dB FS\n",
" -84.71"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -84.71 dB FS\n",
" -83.29"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" -83.29 dB FS\n",
"Stream Closed"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment