adding in callback for reading chunks
This commit is contained in:
+9
-6
@@ -23,7 +23,15 @@ class Audio:
|
||||
channels=RESPEAKER_CHANNELS,
|
||||
input=True,
|
||||
input_device_index=RESPEAKER_INDEX,
|
||||
start=False,)
|
||||
start=False,
|
||||
stream_callback=self.readCallback,)
|
||||
|
||||
def readCallback(self, out_data, frame_count, time_info, status):
|
||||
print("reading chunk")
|
||||
self.frames.append(out_data)
|
||||
# If len(data) is less than requested frame_count, PyAudio automatically
|
||||
# assumes the stream is finished, and the stream stops.
|
||||
return (out_data, pyaudio.paContinue)
|
||||
|
||||
def isRecording(self):
|
||||
return self.recordStream.is_active()
|
||||
@@ -33,11 +41,6 @@ class Audio:
|
||||
self.recordStream.start_stream()
|
||||
self.frames = []
|
||||
|
||||
def readChunk(self):
|
||||
print("reading chunk")
|
||||
data = self.recordStream.read(CHUNK)
|
||||
self.frames.append(data)
|
||||
|
||||
def stopRecord(self, play=True, save=False):
|
||||
print("* done recording")
|
||||
self.recordStream.stop_stream()
|
||||
|
||||
@@ -18,15 +18,12 @@ GPIO.setup(BUTTON, GPIO.IN)
|
||||
audioInstance = audio.Audio()
|
||||
|
||||
while True:
|
||||
if audioInstance.isRecording():
|
||||
audioInstance.readChunk()
|
||||
|
||||
state = GPIO.input(BUTTON)
|
||||
|
||||
if state:
|
||||
print("button: OFF")
|
||||
if audioInstance.isRecording():
|
||||
audioInstance.stopRecord()
|
||||
audioInstance.stopRecord(play=False)
|
||||
else:
|
||||
print("button: ON")
|
||||
if not audioInstance.isRecording():
|
||||
|
||||
Reference in New Issue
Block a user