Python Forum
update imhow in callback from pyaudio - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: update imhow in callback from pyaudio (/thread-27174.html)



update imhow in callback from pyaudio - markB - May-28-2020

Hi All,

I am trying to process some audio parallel with taking some images.
The idea is to grab a data from the micropohone using pyaudio, then
Process that audio in a callback furntion, then
take an image from the webcam and plot this together with some information from the audio stream.

I am having trouble in producing the image during the pyaudio loop.
During running the figure remains blank, sometimes when i press "stop current command" in spyder it start to run.
The latter makes me think I must be close, can someone assist?

Thanks,

import pyaudio
import numpy as np
import matplotlib.pyplot as plt
import cv2
import time

CHUNK = 1024 * 10
WINDOW = 5 * CHUNK
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
THRESH = 100

cap = cv2.VideoCapture(0)
p = pyaudio.PyAudio()



fig, ax = plt.subplots()
image = ax.imshow(np.zeros((480,460)),animated=False,cmap='gray', vmin=0, vmax=255)


def callback(in_data, frame_count, time_info, status):
    data = np.frombuffer(in_data, dtype=np.int16)
    # proces audio
    

    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    image.set_array(gray)
    # fig.canvas.draw()
    
    # ax.plot([0,0],[max(data),max(data)],c='white')
    # fig.canvas.flush_events()
    # flag = pyaudio.paContinue

    return (in_data, flag)

stream = p.open(
    format = FORMAT,
    channels = CHANNELS,
    rate = RATE,
    input = True,
    input_device_index = 0,
    output = False,
    frames_per_buffer = CHUNK,
    stream_callback=callback) 

stream.start_stream()

while stream.is_active():
    time.sleep(0.1)
    
cap.release()
cv2.destroyAllWindows()
    
stream.stop_stream()
stream.close()