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
  Not getting response from pyaudio OceansBlue 1 271 Jul-03-2021, 06:22 AM
Last Post: OceansBlue
  help with PyAudio Leo12143 1 504 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 518 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  pyAudio playing variable Talking2442 3 663 Dec-01-2020, 06:20 PM
Last Post: Talking2442
  Pyaudio Souls99 7 1,053 Oct-05-2020, 04:06 PM
Last Post: Larz60+
  PyAudio [Errorno -9999] Unanticipated Host Error iMuny 5 1,399 Sep-21-2020, 06:58 PM
Last Post: jefsummers
  PyAudio Buffer Data Calculation MclarenF1 0 573 Aug-21-2020, 10:55 AM
Last Post: MclarenF1
  How to update component props every time when a Dash callback returns? sguzunov 0 652 Jul-27-2020, 07:11 AM
Last Post: sguzunov
  PyAudio throwing Input overflowed anthares 3 1,286 Jun-14-2020, 03:37 PM
Last Post: anthares
  Using pyaudio to stop recording under certain sound threshold Twanski94 2 1,330 Jun-13-2020, 11:35 AM
Last Post: Twanski94

Forum Jump:

User Panel Messages

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