Skip to content

Instantly share code, notes, and snippets.

@robclewley
Created August 19, 2018 19:08
Show Gist options
  • Save robclewley/968def77c8dfcdd4c303e4e699ecf559 to your computer and use it in GitHub Desktop.
Save robclewley/968def77c8dfcdd4c303e4e699ecf559 to your computer and use it in GitHub Desktop.
Some pyaudio resources
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://stackoverflow.com/questions/46768459/python-recording-and-playing-microphone-input"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"\"\"\"\n",
"PyAudio Example: Make a wire between input and output (i.e., record a\n",
"few samples and play them back immediately).\n",
"\"\"\"\n",
"\n",
"import pyaudio\n",
"\n",
"CHUNK = 1024\n",
"WIDTH = 2\n",
"CHANNELS = 2\n",
"RATE = 44100\n",
"RECORD_SECONDS = 5\n",
"\n",
"p = pyaudio.PyAudio()\n",
"\n",
"stream = p.open(format=p.get_format_from_width(WIDTH),\n",
" channels=CHANNELS,\n",
" rate=RATE,\n",
" input=True,\n",
" output=True,\n",
" frames_per_buffer=CHUNK)\n",
"\n",
"print(\"* recording\")\n",
"\n",
"for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):\n",
" data = stream.read(CHUNK) #read audio stream\n",
" stream.write(data, CHUNK) #play back audio stream\n",
"\n",
"print(\"* done\")\n",
"\n",
"stream.stop_stream()\n",
"stream.close()\n",
"\n",
"p.terminate()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also: https://gist.github.com/ZWMiller/53232427efc5088007cab6feee7c6e4c"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"Modified from https://github.com/jpihl/pyclapper/blob/master/clapper.py\n",
"\n",
"The MIT License (MIT)\n",
"Copyright (c) 2016 Jeppe Pihl\n",
"\"\"\"\n",
"import time\n",
"import array\n",
"import pyaudio\n",
"import sys\n",
"\n",
"CHUNK_SIZE = 512 #1024\n",
"MIN_VOLUME = 12000\n",
"\n",
"def clapper(stop_check, clap_callback):\n",
" p = pyaudio.PyAudio()\n",
" stream = p.open(\n",
" format=pyaudio.paInt16,\n",
" channels=2,\n",
" rate=44100,\n",
" input=True,\n",
" frames_per_buffer=1024)\n",
" last = 0\n",
" while True:\n",
" if stop_check():\n",
" stream.stop_stream()\n",
" stream.close()\n",
" p.terminate()\n",
" break\n",
" chunk = array.array('h', stream.read(CHUNK_SIZE))\n",
" volume = max(chunk)\n",
" if volume >= MIN_VOLUME:\n",
" now = time.time()\n",
" diff = now - last\n",
" if diff > 0.2: # and diff < 1.0:\n",
" clap_callback()\n",
" last = now\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def stopper():\n",
" # return True based on some outside-of-thread condition (like a file)\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"clapper(stopper, lambda : print(\"CLAP\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment