adding in loop for button press record
This commit is contained in:
+14
-8
@@ -10,7 +10,6 @@ RESPEAKER_WIDTH = 2
|
|||||||
# run getDeviceInfo.py to get index
|
# run getDeviceInfo.py to get index
|
||||||
RESPEAKER_INDEX = 1 # refer to input device id
|
RESPEAKER_INDEX = 1 # refer to input device id
|
||||||
CHUNK = 1024
|
CHUNK = 1024
|
||||||
RECORD_SECONDS = 5
|
|
||||||
WAVE_OUTPUT_FILENAME = "output.wav"
|
WAVE_OUTPUT_FILENAME = "output.wav"
|
||||||
|
|
||||||
|
|
||||||
@@ -27,23 +26,31 @@ class Audio:
|
|||||||
start=False,)
|
start=False,)
|
||||||
|
|
||||||
def startRecord(self):
|
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 = []
|
self.frames = []
|
||||||
print("* recording")
|
print("reading chunk")
|
||||||
for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
|
data = self.recordStream.read(CHUNK)
|
||||||
data = self.recordStream.read(CHUNK)
|
self.frames.append(data)
|
||||||
print("read a 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")
|
||||||
print(self.frames)
|
print(self.frames)
|
||||||
self.recordStream.stop_stream()
|
self.recordStream.stop_stream()
|
||||||
self.stream.close()
|
self.stream.close()
|
||||||
self.pyaudio.terminate()
|
|
||||||
|
|
||||||
if save:
|
if save:
|
||||||
self._saveFile()
|
self._saveFile()
|
||||||
|
|
||||||
|
def terminate(self):
|
||||||
|
self.pyaudio.terminate()
|
||||||
|
|
||||||
def _saveFile(self):
|
def _saveFile(self):
|
||||||
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
|
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
|
||||||
wf.setnchannels(RESPEAKER_CHANNELS)
|
wf.setnchannels(RESPEAKER_CHANNELS)
|
||||||
@@ -67,4 +74,3 @@ class Audio:
|
|||||||
|
|
||||||
# cleanup stuff
|
# cleanup stuff
|
||||||
self.playbackStream.close()
|
self.playbackStream.close()
|
||||||
self.pyaudio.terminate()
|
|
||||||
|
|||||||
@@ -1,2 +1,26 @@
|
|||||||
# this program will record audio while the button is pressed
|
# this program will record audio while the button is pressed
|
||||||
# it will then play the audio back to the user from memory
|
# 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()
|
||||||
|
|||||||
Reference in New Issue
Block a user