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,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
  Raspberry PI - PyAudio - Streaming to Web Page ultimatecodewarrior 2 1,002 Dec-25-2024, 10:54 AM
Last Post: pintailscratchy
  pyaudio seems to randomly halt input. elpidiovaldez5 2 1,486 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Not getting response from pyaudio OceansBlue 1 3,796 Jul-03-2021, 06:22 AM
Last Post: OceansBlue
  help with PyAudio Leo12143 1 2,956 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 4,096 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  pyAudio playing variable Talking2442 3 4,145 Dec-01-2020, 06:20 PM
Last Post: Talking2442
  Pyaudio Souls99 7 5,425 Oct-05-2020, 04:06 PM
Last Post: Larz60+
  PyAudio [Errorno -9999] Unanticipated Host Error iMuny 5 8,666 Sep-21-2020, 06:58 PM
Last Post: jefsummers
  PyAudio Buffer Data Calculation MclarenF1 0 3,081 Aug-21-2020, 10:55 AM
Last Post: MclarenF1
  How to update component props every time when a Dash callback returns? sguzunov 0 3,887 Jul-27-2020, 07:11 AM
Last Post: sguzunov

Forum Jump:

User Panel Messages

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