Boilerplate for Jack and PyAudio
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.
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"):
...
jack_client
is the client name, and 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")
- 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)