Commit 0b761239 authored by Ricardo Cebada Fuentes's avatar Ricardo Cebada Fuentes 👦🏽

Implementación en Jupyter

parent 497c4972
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pyaudio\n",
"import os\n",
"import struct\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy.fftpack import fft\n",
"import time\n",
"from tkinter import TclError\n",
"\n",
"\n",
"%matplotlib tk\n",
"\n",
"\n",
"CHUNK = 1024 * 2 \n",
"FORMAT = pyaudio.paInt16 \n",
"CHANNELS = 1 \n",
"RATE = 44100 "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"stream started\n",
"stream stopped\n",
"average frame rate = 11 FPS\n"
]
}
],
"source": [
"\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(2, figsize=(15, 7))\n",
"\n",
"\n",
"p = pyaudio.PyAudio()\n",
"\n",
"\n",
"stream = p.open(\n",
" format=FORMAT,\n",
" channels=CHANNELS,\n",
" rate=RATE,\n",
" input=True,\n",
" output=True,\n",
" frames_per_buffer=CHUNK\n",
")\n",
"\n",
"x = np.arange(0, 2 * CHUNK, 2) \n",
"xf = np.linspace(0, RATE, CHUNK) \n",
"\n",
"\n",
"line, = ax1.plot(x, np.random.rand(CHUNK), '-', lw=2)\n",
"\n",
"\n",
"line_fft, = ax2.semilogx(xf, np.random.rand(CHUNK), '-', lw=2)\n",
"\n",
"\n",
"ax1.set_title('AUDIO WAVEFORM')\n",
"ax1.set_xlabel('samples')\n",
"ax1.set_ylabel('volume')\n",
"ax1.set_ylim(0, 255)\n",
"ax1.set_xlim(0, 2 * CHUNK)\n",
"plt.setp(ax1, xticks=[0, CHUNK, 2 * CHUNK], yticks=[0, 128, 255])\n",
"\n",
"\n",
"ax2.set_xlim(20, RATE / 2)\n",
"\n",
"print('stream started')\n",
"\n",
"\n",
"frame_count = 0\n",
"start_time = time.time()\n",
"\n",
"while True:\n",
" \n",
"\n",
" data = stream.read(CHUNK) \n",
" \n",
" \n",
" data_int = struct.unpack(str(2 * CHUNK) + 'B', data)\n",
" \n",
"\n",
" data_np = np.array(data_int, dtype='b')[::2] + 128\n",
" \n",
" line.set_ydata(data_np)\n",
" \n",
"\n",
" yf = fft(data_int)\n",
" line_fft.set_ydata(np.abs(yf[0:CHUNK]) / (128 * CHUNK))\n",
" \n",
"\n",
" try:\n",
" fig.canvas.draw()\n",
" fig.canvas.flush_events()\n",
" frame_count += 1\n",
" \n",
" except TclError:\n",
" \n",
"\n",
" frame_rate = frame_count / (time.time() - start_time)\n",
" \n",
" print('stream stopped')\n",
" print('average frame rate = {:.0f} FPS'.format(frame_rate))\n",
" break"
]
},
{
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment