adding in loop for button press record

This commit is contained in:
talksik
2023-04-27 13:58:56 -07:00
parent 1a1b49a958
commit 6a177cfb61
2 changed files with 38 additions and 8 deletions
+14 -8
View File
@@ -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()
+24
View File
@@ -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()