Hear
Boilerplate for Jack and PyAudio
Why?
PyAudio offers a binding for the old good PortAudio, it works everywhere but is quite low-level and if you are using Jack can be painful (ok, it is supported, but I was not able to create a client, nor to find working code by someone else).
Jack-Client is way better, but, obviously, works only with an active Jack server.
I want both.
Usage
To read audio from the microphone:
from hear import hear
def callback(data):
left = data[0]
right = data[1]
# do stuff
hear(callback)
where data
is a NumPy array.
If you have an active Jack server running the callback is processed by a Jack client, otherwise by PyAudio.
hear
takes also other arguments:
def hear(callback, channels=2, body=None,
jack_client="Hear",
rate=44100, frames_per_buffer=1024):
...
body
is the action (a function) performed while the client/pa-stream is
running, by default:
def body():
try:
while True:
sleep(0.1)
except KeyboardInterrupt:
print("Interrupted by user")
jack_client
, Jack only, is the client name.
rate
and frames_per_buffer
are PyAudio only, if you want to reuse the
same code switching between the two backends make sure to be using the same
values in your Jack configuration.
Installing
pip install hear --allow-unverified pyaudio
Known Issues
- If you change the Jack state you have to reload the whole application. (To check the available backends Hear uses PyAudio, and the C part inizializes everything just one time storing the state in some global variables)