diff --git a/birbnetes_iot_platform_raspberry/led_stuff.py b/birbnetes_iot_platform_raspberry/led_stuff.py index 7c6a4cf..d917374 100644 --- a/birbnetes_iot_platform_raspberry/led_stuff.py +++ b/birbnetes_iot_platform_raspberry/led_stuff.py @@ -93,3 +93,7 @@ class BirbnetesIoTPlatformStatusDriver: @classmethod def enqueue_pattern(cls, target: str, pattern: list): cls.blinken_lights.enqueue_pattern(target, pattern) + + @classmethod + def cleanup(cls): + cls.blinken_lights.stop() diff --git a/birbnetes_iot_platform_raspberry/playback_stuff.py b/birbnetes_iot_platform_raspberry/playback_stuff.py index cc694ad..595af03 100644 --- a/birbnetes_iot_platform_raspberry/playback_stuff.py +++ b/birbnetes_iot_platform_raspberry/playback_stuff.py @@ -10,3 +10,7 @@ class BirbnetesIoTPlatformPlaybackDriver: @classmethod def play_audio(cls, audiofile: str): playsound(audiofile) + + @classmethod + def cleanup(cls): + pass diff --git a/birbnetes_iot_platform_raspberry/record_stuff.py b/birbnetes_iot_platform_raspberry/record_stuff.py index 19c280e..61d8d5e 100644 --- a/birbnetes_iot_platform_raspberry/record_stuff.py +++ b/birbnetes_iot_platform_raspberry/record_stuff.py @@ -4,6 +4,7 @@ import tempfile from queue import Queue, Empty from threading import Thread import wave +import os SAMPLE_RATE = 44100 MICROPHONE = 'default' @@ -11,8 +12,7 @@ DEST_FOLDER = "/dev/shm/" class SlicedRecorder(Thread): - - BYTES_PER_SAMPLE = 2 # 16bit + BYTES_PER_SAMPLE = 2 # 16bit def __init__(self, slice_size: float, sample_rate=SAMPLE_RATE, microphone=MICROPHONE, dest_folder=DEST_FOLDER): super().__init__() @@ -26,7 +26,7 @@ class SlicedRecorder(Thread): microphone, channels=1, rate=sample_rate, - format=alsaaudio.PCM_FORMAT_S16_LE, # = 2bytes + format=alsaaudio.PCM_FORMAT_S16_LE, # = 2bytes periodsize=512 ) self._active = True @@ -54,13 +54,13 @@ class SlicedRecorder(Thread): samples_append_to_this_slice = self._samples_per_slice - current_samples_saved - current_working_file.writeframes(data[:(samples_append_to_this_slice*self.BYTES_PER_SAMPLE)]) + current_working_file.writeframes(data[:(samples_append_to_this_slice * self.BYTES_PER_SAMPLE)]) current_working_file.close() self._output_queue.put(current_working_file_name) current_working_file_name, current_working_file = self._request_new_file() - current_working_file.writeframes(data[(samples_append_to_this_slice*self.BYTES_PER_SAMPLE):]) + current_working_file.writeframes(data[(samples_append_to_this_slice * self.BYTES_PER_SAMPLE):]) current_samples_saved = length - samples_append_to_this_slice else: # it is safe to append, the resulting file will be smaller than the slice @@ -78,11 +78,24 @@ class SlicedRecorder(Thread): class BirbnetesIoTPlatformRecordDriver: + sliced_recorder = None @classmethod - def init(cls, sample_length_sec: float): - pass + def init(cls, sample_length_sec: float, sample_rate=SAMPLE_RATE, microphone=MICROPHONE, dest_folder=DEST_FOLDER): + cls.sliced_recorder = SlicedRecorder(sample_length_sec, sample_rate, microphone, dest_folder) + cls.sliced_recorder.start() @classmethod - def get_recording(cls, blocking: bool = False): - pass + def get_recording(cls, blocking: bool = False) -> Optional[str]: + return cls.sliced_recorder.get_recording(blocking) + + @classmethod + def cleanup(cls): + cls.sliced_recorder.stop() + + while True: + fname = cls.sliced_recorder.get_recording(False) + if fname: + os.unlink(fname) + else: + break