getting saved file name and saving it

This commit is contained in:
talksik
2023-04-28 15:35:17 -07:00
parent e45be75f9b
commit ef0d71930e
2 changed files with 21 additions and 17 deletions
+16 -7
View File
@@ -39,7 +39,8 @@ class Audio:
input_device_index=RESPEAKER_INDEX,
start=False,
frames_per_buffer=CHUNK,
stream_callback=self.readCallback,)
stream_callback=self.readCallback,
)
self.recordStream.start_stream()
self._isRecording = True
@@ -49,21 +50,28 @@ class Audio:
self.recordStream.close()
self._isRecording = False
savedFileName = None
if save:
self._saveFile()
savedFileName = self._saveFile()
if play:
self._playFrames()
self.frames = []
return savedFileName if save else self.frames
def _saveFile(self):
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf = wave.open(WAVE_OUTPUT_FILENAME, "wb")
wf.setnchannels(RESPEAKER_CHANNELS)
wf.setsampwidth(self.pyaudio.get_sample_size(
self.pyaudio.get_format_from_width(RESPEAKER_WIDTH)))
wf.setsampwidth(
self.pyaudio.get_sample_size(
self.pyaudio.get_format_from_width(RESPEAKER_WIDTH)
)
)
wf.setframerate(RESPEAKER_RATE)
wf.writeframes(b''.join(self.frames))
wf.writeframes(b"".join(self.frames))
wf.close()
return WAVE_OUTPUT_FILENAME
def _playFrames(self):
print("* playing recorded audio")
@@ -74,7 +82,8 @@ class Audio:
channels=RESPEAKER_CHANNELS,
rate=RESPEAKER_RATE,
output=True,
output_device_index=RESPEAKER_INDEX)
output_device_index=RESPEAKER_INDEX,
)
# loop through self.frames and play audio
for frame in self.frames:
+5 -10
View File
@@ -1,16 +1,10 @@
# this program will record audio while the button is pressed
# and then will transcibe it by hitting our api
# and then will transcribe it by hitting our api
import RPi.GPIO as GPIO
# importing sys
import sys
# adding Folder_2/subfolder to the system path
sys.path.insert(0, '/home/talksik/rpi-random-projects/audio')
import audio
import audio.audio
from simple_recorder.apa102 import apa102
BUTTON = 17
@@ -41,7 +35,8 @@ while True:
if state:
if audioInstance.isRecording():
turnOffLED()
audioInstance.stopRecord()
savedFileName = audioInstance.stopRecord(save=True, play=False)
print("saved file: " + savedFileName)
else:
if not audioInstance.isRecording():
turnOnLED()