Updated static interface

This commit is contained in:
Pünkösd Marcell 2020-09-30 03:12:25 +02:00
parent 4bf5d980d2
commit 087395e950
3 changed files with 30 additions and 9 deletions

View File

@ -93,3 +93,7 @@ class BirbnetesIoTPlatformStatusDriver:
@classmethod @classmethod
def enqueue_pattern(cls, target: str, pattern: list): def enqueue_pattern(cls, target: str, pattern: list):
cls.blinken_lights.enqueue_pattern(target, pattern) cls.blinken_lights.enqueue_pattern(target, pattern)
@classmethod
def cleanup(cls):
cls.blinken_lights.stop()

View File

@ -10,3 +10,7 @@ class BirbnetesIoTPlatformPlaybackDriver:
@classmethod @classmethod
def play_audio(cls, audiofile: str): def play_audio(cls, audiofile: str):
playsound(audiofile) playsound(audiofile)
@classmethod
def cleanup(cls):
pass

View File

@ -4,6 +4,7 @@ import tempfile
from queue import Queue, Empty from queue import Queue, Empty
from threading import Thread from threading import Thread
import wave import wave
import os
SAMPLE_RATE = 44100 SAMPLE_RATE = 44100
MICROPHONE = 'default' MICROPHONE = 'default'
@ -11,8 +12,7 @@ DEST_FOLDER = "/dev/shm/"
class SlicedRecorder(Thread): 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): def __init__(self, slice_size: float, sample_rate=SAMPLE_RATE, microphone=MICROPHONE, dest_folder=DEST_FOLDER):
super().__init__() super().__init__()
@ -26,7 +26,7 @@ class SlicedRecorder(Thread):
microphone, microphone,
channels=1, channels=1,
rate=sample_rate, rate=sample_rate,
format=alsaaudio.PCM_FORMAT_S16_LE, # = 2bytes format=alsaaudio.PCM_FORMAT_S16_LE, # = 2bytes
periodsize=512 periodsize=512
) )
self._active = True self._active = True
@ -54,13 +54,13 @@ class SlicedRecorder(Thread):
samples_append_to_this_slice = self._samples_per_slice - current_samples_saved 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() current_working_file.close()
self._output_queue.put(current_working_file_name) self._output_queue.put(current_working_file_name)
current_working_file_name, current_working_file = self._request_new_file() 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 current_samples_saved = length - samples_append_to_this_slice
else: # it is safe to append, the resulting file will be smaller than the 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: class BirbnetesIoTPlatformRecordDriver:
sliced_recorder = None
@classmethod @classmethod
def init(cls, sample_length_sec: float): def init(cls, sample_length_sec: float, sample_rate=SAMPLE_RATE, microphone=MICROPHONE, dest_folder=DEST_FOLDER):
pass cls.sliced_recorder = SlicedRecorder(sample_length_sec, sample_rate, microphone, dest_folder)
cls.sliced_recorder.start()
@classmethod @classmethod
def get_recording(cls, blocking: bool = False): def get_recording(cls, blocking: bool = False) -> Optional[str]:
pass 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