adding in callback for reading chunks
This commit is contained in:
+9
-6
@@ -23,7 +23,15 @@ class Audio:
|
|||||||
channels=RESPEAKER_CHANNELS,
|
channels=RESPEAKER_CHANNELS,
|
||||||
input=True,
|
input=True,
|
||||||
input_device_index=RESPEAKER_INDEX,
|
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):
|
def isRecording(self):
|
||||||
return self.recordStream.is_active()
|
return self.recordStream.is_active()
|
||||||
@@ -33,11 +41,6 @@ class Audio:
|
|||||||
self.recordStream.start_stream()
|
self.recordStream.start_stream()
|
||||||
self.frames = []
|
self.frames = []
|
||||||
|
|
||||||
def readChunk(self):
|
|
||||||
print("reading chunk")
|
|
||||||
data = self.recordStream.read(CHUNK)
|
|
||||||
self.frames.append(data)
|
|
||||||
|
|
||||||
def stopRecord(self, play=True, save=False):
|
def stopRecord(self, play=True, save=False):
|
||||||
print("* done recording")
|
print("* done recording")
|
||||||
self.recordStream.stop_stream()
|
self.recordStream.stop_stream()
|
||||||
|
|||||||
@@ -18,15 +18,12 @@ GPIO.setup(BUTTON, GPIO.IN)
|
|||||||
audioInstance = audio.Audio()
|
audioInstance = audio.Audio()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if audioInstance.isRecording():
|
|
||||||
audioInstance.readChunk()
|
|
||||||
|
|
||||||
state = GPIO.input(BUTTON)
|
state = GPIO.input(BUTTON)
|
||||||
|
|
||||||
if state:
|
if state:
|
||||||
print("button: OFF")
|
print("button: OFF")
|
||||||
if audioInstance.isRecording():
|
if audioInstance.isRecording():
|
||||||
audioInstance.stopRecord()
|
audioInstance.stopRecord(play=False)
|
||||||
else:
|
else:
|
||||||
print("button: ON")
|
print("button: ON")
|
||||||
if not audioInstance.isRecording():
|
if not audioInstance.isRecording():
|
||||||
|
|||||||
Reference in New Issue
Block a user