[2] PyAudio

Processing digital audio con PyAudio.

1. Install

MaOS X

brew install portaudio
pip install pyaudio

Debians

sudo apt-get install portaudio19-dev
pip install pyaudio

2. Use

A real-time audio processor.


In [ ]:
import pyaudio
import sys

CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 50

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                frames_per_buffer=CHUNK)

print("* recording")

try:
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        stream.write(data, CHUNK)
        print('.', end='')
        sys.stdout.flush()

except KeyboardInterrupt:
    print("* done")
    stream.stop_stream()
    stream.close()
    p.terminate()