Python Forum
update imhow in callback from pyaudio
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
update imhow in callback from pyaudio
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyaudio seems to randomly halt input. elpidiovaldez5 2 314 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Not getting response from pyaudio OceansBlue 1 2,585 Jul-03-2021, 06:22 AM
Last Post: OceansBlue
  help with PyAudio Leo12143 1 1,888 Jan-18-2021, 09:56 AM
Last Post: DT2000
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,628 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  pyAudio playing variable Talking2442 3 2,959 Dec-01-2020, 06:20 PM
Last Post: Talking2442
  Pyaudio Souls99 7 3,465 Oct-05-2020, 04:06 PM
Last Post: Larz60+
  PyAudio [Errorno -9999] Unanticipated Host Error iMuny 5 5,562 Sep-21-2020, 06:58 PM
Last Post: jefsummers
  PyAudio Buffer Data Calculation MclarenF1 0 2,103 Aug-21-2020, 10:55 AM
Last Post: MclarenF1
  How to update component props every time when a Dash callback returns? sguzunov 0 2,475 Jul-27-2020, 07:11 AM
Last Post: sguzunov
  PyAudio throwing Input overflowed anthares 3 4,689 Jun-14-2020, 03:37 PM
Last Post: anthares

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020