Running recording in a separate thread so it doesn't block the receiving backend and added playback/recording start/pause/stop events
This commit is contained in:
parent
cfbbff19c1
commit
2d2db499be
2 changed files with 149 additions and 59 deletions
65
platypush/message/event/sound.py
Normal file
65
platypush/message/event/sound.py
Normal file
|
@ -0,0 +1,65 @@
|
|||
from platypush.message.event import Event
|
||||
|
||||
|
||||
class SoundEvent(Event):
|
||||
""" Base class for sound events """
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class SoundPlaybackStartedEvent(SoundEvent):
|
||||
"""
|
||||
Event triggered when a new sound playback starts
|
||||
"""
|
||||
|
||||
def __init__(self, filename=None, *args, **kwargs):
|
||||
super().__init__(*args, filename=filename, **kwargs)
|
||||
|
||||
|
||||
class SoundPlaybackStoppedEvent(SoundEvent):
|
||||
"""
|
||||
Event triggered when the sound playback stops
|
||||
"""
|
||||
|
||||
def __init__(self, filename=None, *args, **kwargs):
|
||||
super().__init__(*args, filename=filename, **kwargs)
|
||||
|
||||
|
||||
class SoundPlaybackPausedEvent(SoundEvent):
|
||||
"""
|
||||
Event triggered when the sound playback pauses
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class SoundRecordingStartedEvent(SoundEvent):
|
||||
"""
|
||||
Event triggered when a new recording starts
|
||||
"""
|
||||
|
||||
def __init__(self, filename=None, *args, **kwargs):
|
||||
super().__init__(*args, filename=filename, **kwargs)
|
||||
|
||||
|
||||
class SoundRecordingStoppedEvent(SoundEvent):
|
||||
"""
|
||||
Event triggered when a sound recording stops
|
||||
"""
|
||||
|
||||
def __init__(self, filename=None, *args, **kwargs):
|
||||
super().__init__(*args, filename=filename, **kwargs)
|
||||
|
||||
|
||||
class SoundRecordingPausedEvent(SoundEvent):
|
||||
"""
|
||||
Event triggered when a sound recording pauses
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
|
||||
# vim:sw=4:ts=4:et:
|
|
@ -13,6 +13,13 @@ from enum import Enum
|
|||
from threading import Thread, Event, RLock
|
||||
|
||||
from .core import Sound, Mix
|
||||
|
||||
from platypush.context import get_bus
|
||||
from platypush.message.event.sound import SoundPlaybackStartedEvent, \
|
||||
SoundPlaybackPausedEvent, SoundPlaybackStoppedEvent, \
|
||||
SoundRecordingStartedEvent, SoundRecordingPausedEvent, \
|
||||
SoundRecordingStoppedEvent
|
||||
|
||||
from platypush.plugins import Plugin, action
|
||||
|
||||
|
||||
|
@ -32,6 +39,15 @@ class SoundPlugin(Plugin):
|
|||
"""
|
||||
Plugin to interact with a sound device.
|
||||
|
||||
Triggers:
|
||||
|
||||
* :class:`platypush.message.event.sound.SoundPlaybackStartedEvent` on playback start
|
||||
* :class:`platypush.message.event.sound.SoundPlaybackStoppedEvent` on playback stop
|
||||
* :class:`platypush.message.event.sound.SoundPlaybackPausedEvent` on playback pause
|
||||
* :class:`platypush.message.event.sound.SoundRecordingStartedEvent` on recording start
|
||||
* :class:`platypush.message.event.sound.SoundRecordingStoppedEvent` on recording stop
|
||||
* :class:`platypush.message.event.sound.SoundRecordingPausedEvent` on recording pause
|
||||
|
||||
Requires:
|
||||
|
||||
* **sounddevice** (``pip install sounddevice``)
|
||||
|
@ -384,13 +400,13 @@ class SoundPlugin(Plugin):
|
|||
|
||||
|
||||
@action
|
||||
def record(self, file=None, duration=None, device=None, sample_rate=None,
|
||||
def record(self, outfile=None, duration=None, device=None, sample_rate=None,
|
||||
blocksize=None, latency=0, channels=1, subtype='PCM_24'):
|
||||
"""
|
||||
Records audio to a sound file (support formats: wav, raw)
|
||||
|
||||
:param file: Sound file (default: the method will create a temporary file with the recording)
|
||||
:type file: str
|
||||
:param outfile: Sound file (default: the method will create a temporary file with the recording)
|
||||
:type outfile: str
|
||||
|
||||
:param duration: Recording duration in seconds (default: record until stop event)
|
||||
:type duration: float
|
||||
|
@ -414,19 +430,22 @@ class SoundPlugin(Plugin):
|
|||
:type subtype: str
|
||||
"""
|
||||
|
||||
def recording_thread(outfile, duration, device, sample_rate, blocksize,
|
||||
latency, channels, subtype):
|
||||
import sounddevice as sd
|
||||
|
||||
self.recording_paused_changed.clear()
|
||||
|
||||
if file:
|
||||
file = os.path.abspath(os.path.expanduser(file))
|
||||
if outfile:
|
||||
outfile = os.path.abspath(os.path.expanduser(outfile))
|
||||
else:
|
||||
file = tempfile.mktemp(prefix='platypush_recording_', suffix='.wav',
|
||||
dir='')
|
||||
outfile = tempfile.NamedTemporaryFile(
|
||||
prefix='recording_', suffix='.wav', delete=False,
|
||||
dir=tempfile.gettempdir()).name
|
||||
|
||||
if os.path.isfile(file):
|
||||
self.logger.info('Removing existing audio file {}'.format(file))
|
||||
os.unlink(file)
|
||||
if os.path.isfile(outfile):
|
||||
self.logger.info('Removing existing audio file {}'.format(outfile))
|
||||
os.unlink(outfile)
|
||||
|
||||
if device is None:
|
||||
device = self.input_device
|
||||
|
@ -457,14 +476,15 @@ class SoundPlugin(Plugin):
|
|||
import soundfile as sf
|
||||
import numpy
|
||||
|
||||
with sf.SoundFile(file, mode='x', samplerate=sample_rate,
|
||||
with sf.SoundFile(outfile, mode='x', samplerate=sample_rate,
|
||||
channels=channels, subtype=subtype) as f:
|
||||
with sd.InputStream(samplerate=sample_rate, device=device,
|
||||
channels=channels, callback=audio_callback,
|
||||
latency=latency, blocksize=blocksize):
|
||||
self.start_recording()
|
||||
get_bus().post(SoundRecordingStartedEvent(filename=outfile))
|
||||
self.logger.info('Started recording from device [{}] to [{}]'.
|
||||
format(device, file))
|
||||
format(device, outfile))
|
||||
|
||||
recording_started_time = time.time()
|
||||
|
||||
|
@ -489,6 +509,11 @@ class SoundPlugin(Plugin):
|
|||
self.logger.warning('Recording timeout: audio callback failed?')
|
||||
finally:
|
||||
self.stop_recording()
|
||||
get_bus().post(SoundRecordingStoppedEvent(filename=outfile))
|
||||
|
||||
Thread(target=recording_thread, args=(
|
||||
outfile, duration, device, sample_rate, blocksize, latency, channels,
|
||||
subtype)).start()
|
||||
|
||||
|
||||
@action
|
||||
|
|
Loading…
Reference in a new issue