Merged outfile/fifo logic in `sound.stream_recording`.

This commit is contained in:
Fabio Manganiello 2023-06-12 12:33:14 +02:00
parent e238fcb6e4
commit a415c5b231
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 20 additions and 14 deletions

View File

@ -442,6 +442,7 @@ class SoundPlugin(Plugin):
self, self,
device: Optional[str] = None, device: Optional[str] = None,
fifo: Optional[str] = None, fifo: Optional[str] = None,
outfile: Optional[str] = None,
duration: Optional[float] = None, duration: Optional[float] = None,
sample_rate: Optional[int] = None, sample_rate: Optional[int] = None,
dtype: str = 'float32', dtype: str = 'float32',
@ -456,8 +457,10 @@ class SoundPlugin(Plugin):
:param device: Input device (default: default configured device or :param device: Input device (default: default configured device or
system default audio input if not configured) system default audio input if not configured)
:param fifo: Path of the FIFO that will be used to exchange audio :param fifo: Path of a FIFO that will be used to exchange audio frames
samples (default: /tmp/inputstream) with other consumers.
:param outfile: If specified, the audio data will be persisted on the
specified audio file too.
:param duration: Recording duration in seconds (default: record until :param duration: Recording duration in seconds (default: record until
stop event) stop event)
:param sample_rate: Recording sample rate (default: device default rate) :param sample_rate: Recording sample rate (default: device default rate)
@ -487,8 +490,18 @@ class SoundPlugin(Plugin):
if blocksize is None: if blocksize is None:
blocksize = self.input_blocksize blocksize = self.input_blocksize
if not fifo: if fifo:
fifo = os.devnull fifo = os.path.expanduser(fifo)
if os.path.exists(fifo) and stat.S_ISFIFO(os.stat(fifo).st_mode):
self.logger.info('Removing previous input stream FIFO %s', fifo)
os.unlink(fifo)
os.mkfifo(fifo, 0o644)
outfile = fifo
elif outfile:
outfile = os.path.expanduser(outfile)
outfile = outfile or fifo or os.devnull
def audio_callback(audio_converter: ConverterProcess): def audio_callback(audio_converter: ConverterProcess):
# _ = frames # _ = frames
@ -522,8 +535,8 @@ class SoundPlugin(Plugin):
latency=latency, latency=latency,
blocksize=blocksize, blocksize=blocksize,
), open( ), open(
fifo, 'wb' outfile, 'wb'
) as audio_queue: ) as f:
self.start_recording() self.start_recording()
get_bus().post(SoundRecordingStartedEvent()) get_bus().post(SoundRecordingStartedEvent())
self.logger.info('Started recording from device [%s]', device) self.logger.info('Started recording from device [%s]', device)
@ -549,7 +562,7 @@ class SoundPlugin(Plugin):
if not data: if not data:
continue continue
audio_queue.write(data) f.write(data)
if redis_queue: if redis_queue:
get_redis().publish(redis_queue, data) get_redis().publish(redis_queue, data)
@ -559,13 +572,6 @@ class SoundPlugin(Plugin):
self.stop_recording() self.stop_recording()
get_bus().post(SoundRecordingStoppedEvent()) get_bus().post(SoundRecordingStoppedEvent())
if os.path.exists(fifo):
if stat.S_ISFIFO(os.stat(fifo).st_mode):
self.logger.info('Removing previous input stream FIFO %s', fifo)
os.unlink(fifo)
else:
os.mkfifo(fifo, 0o644)
Thread(target=streaming_thread).start() Thread(target=streaming_thread).start()
@action @action