Not knowledgeable in programming but in following along with the wire and wire callback examples on the documentation website I am struggling to figure out how to access the stream.read(CHUNK) data in callback mode to be processed by audioop.rms() from the audioop library.
Below are two slightly modified examples. The former is the wire method in blocking mode and processed by the rms function successfully. The latter is the wire method in non-blocking mode which I do not know how to go about accessing the same data.
"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
"""
import pyaudio
import audioop # new
CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
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")
while True: # <-----------------------------------------
data = stream.read(CHUNK)
stream.write(data, CHUNK)
rms = audioop.rms(data, WIDTH) # new
print(rms) # computer audio power
print("* done")
stream.stop_stream()
stream.close()
p.terminate()
non-blocking
"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
This is the callback (non-blocking) version.
"""
import pyaudio
import time
import audioop # new
WIDTH = 2
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, status):
return in_data, pyaudio.paContinue
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active(): # <--------------------------------------------
time.sleep(0.1)
# data = stream.read(1024) # the docs say not to call this
data = stream.get_read_available() # not sure what to do
print(data) # new
rms = audioop.rms(data, WIDTH) # compute audio power
print(rms) # new
stream.stop_stream()
stream.close()
p.terminate()