diff --git a/audio/audio.py b/audio/audio.py index 94ca028..93651dd 100644 --- a/audio/audio.py +++ b/audio/audio.py @@ -10,7 +10,6 @@ RESPEAKER_WIDTH = 2 # run getDeviceInfo.py to get index RESPEAKER_INDEX = 1 # refer to input device id CHUNK = 1024 -RECORD_SECONDS = 5 WAVE_OUTPUT_FILENAME = "output.wav" @@ -27,23 +26,31 @@ class Audio: start=False,) def startRecord(self): + print("* recording stream started") + self.recordStream.start_stream() + self.readChunk() + + def isRecording(self): + return self.recordStream.is_active() + + def readChunk(self): self.frames = [] - print("* recording") - for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)): - data = self.recordStream.read(CHUNK) - print("read a chunk") - self.frames.append(data) + print("reading chunk") + data = self.recordStream.read(CHUNK) + self.frames.append(data) def stopRecord(self, play=True, save=False): print("* done recording") print(self.frames) self.recordStream.stop_stream() self.stream.close() - self.pyaudio.terminate() if save: self._saveFile() + def terminate(self): + self.pyaudio.terminate() + def _saveFile(self): wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(RESPEAKER_CHANNELS) @@ -67,4 +74,3 @@ class Audio: # cleanup stuff self.playbackStream.close() - self.pyaudio.terminate() diff --git a/push_to_record_and_play.py b/push_to_record_and_play.py index fad6756..e6cd4ab 100644 --- a/push_to_record_and_play.py +++ b/push_to_record_and_play.py @@ -1,2 +1,26 @@ # this program will record audio while the button is pressed # it will then play the audio back to the user from memory + +import audio +import RPi.GPIO as GPIO + +BUTTON = 17 + +GPIO.setmode(GPIO.BCM) +GPIO.setup(BUTTON, GPIO.IN) + +audioInstance = audio.Audio() + +while True: + state = GPIO.input(BUTTON) + if audioInstance.isRecording(): + audioInstance.readChunk() + + if state: + print("button: OFF") + if audioInstance.isRecording(): + audioInstance.stopRecord() + else: + print("button: ON") + if not audioInstance.isRecording(): + audioInstance.startRecord()