-
Notifications
You must be signed in to change notification settings - Fork 1
/
MicrophoneThread.py
56 lines (50 loc) · 2.01 KB
/
MicrophoneThread.py
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
import numpy as np
import pyaudio
import threading
from queue import Queue
class AudioProcessor:
"""
An audio processor class for capturing and processing audio data.
Args:
audio_queue (Queue): A queue to store captured audio data.
toggle_whisper (threading.Event): An event to control audio playback.
chunk_size (int): The size of audio chunks to capture.
fs (int): The sampling frequency of the audio.
Attributes:
audio_queue (Queue): A queue to store captured audio data.
toggle_whisper (threading.Event): An event to control audio playback.
chunk_size (int): The size of audio chunks to capture.
fs (int): The sampling frequency of the audio.
"""
def __init__(self, audio_queue: Queue, toggle_whisper: threading.Event, chunk_size: int, fs: int):
self.audio_queue = audio_queue
self.toggle_whisper = toggle_whisper
self.chunk_size = chunk_size
self.fs = fs
def fetch_signal(self):
"""
Start capturing audio data and store it in the audio queue.
This method opens an audio stream and continuously captures audio data,
which is then put into the audio_queue. It also plays back the audio if
the toggle_whisper event is set.
"""
p = pyaudio.PyAudio()
stream = p.open(
format=pyaudio.paInt16,
channels=1,
rate=self.fs,
input=True,
output=True,
frames_per_buffer=self.chunk_size
)
try:
while True:
data = stream.read(self.chunk_size, exception_on_overflow=True)
audio_data = np.frombuffer(data, dtype=np.int16)
if self.toggle_whisper.is_set() and stream.get_write_available() > 0: # Check if the speaker is on
stream.write(audio_data.tobytes())
self.audio_queue.put(audio_data)
finally:
stream.stop_stream()
stream.close()
p.terminate()