platypush/plugins/sound/__init__.py
def recordplay(self, duration=None, input_device=None, output_device=None,
                   sample_rate=None, blocksize=None, latency=0, channels=1, dtype=None):
        """
        Records audio and plays it on an output sound device (audio pass-through)

        :param duration: Recording duration in seconds (default: record until stop event)
        :type duration: float

        :param input_device: Input device (default: default configured device or system default audio input if not configured)
        :type input_device: int or str

        :param output_device: Output device (default: default configured device or system default audio output if not configured)
        :type output_device: int or str

        :param sample_rate: Recording sample rate (default: device default rate)
        :type sample_rate: int

        :param blocksize: Audio block size (default: configured `output_blocksize` or 2048)
        :type blocksize: int

        :param latency: Device latency in seconds (default: 0)
        :type latency: float

        :param channels: Number of channels (default: 1)
        :type channels: int

        :param dtype: Data type for the recording - see `Soundfile docs - Recording <https://python-sounddevice.readthedocs.io/en/0.3.12/_modules/sounddevice.html#rec>`_ for available types (default: input device default)
        :type dtype: str
        """

        import sounddevice as sd

        self.recording_paused_changed.clear()

        if input_device is None:
            input_device = self.input_device
        if input_device is None:
            input_device = self._get_default_device('input')

        if output_device is None:
            output_device = self.output_device
        if output_device is None:
            output_device = self._get_default_device('output')

        if sample_rate is None:
            dev_info = sd.query_devices(input_device, 'input')
            sample_rate = int(dev_info['default_samplerate'])

        if blocksize is None:
            blocksize = self.output_blocksize

        # noinspection PyUnusedLocal
        def audio_callback(indata, outdata, frames, time, status):
            while self._get_recording_state() == RecordingState.PAUSED:
                self.recording_paused_changed.wait()

            if status:
                self.logger.warning('Recording callback status: {}'.format(
                    str(status)))

            outdata[:] = indata

        stream_index = None

        try:
            import soundfile as sf
            import numpy

            stream_index = self._allocate_stream_index()
            stream = sd.Stream(samplerate=sample_rate, channels=channels,
                               blocksize=blocksize, latency=latency,
                               device=(input_device, output_device),
                               dtype=dtype, callback=audio_callback)
            self.start_recording()
            self._start_playback(stream_index=stream_index,
                                 stream=stream)

            self.logger.info('Started recording pass-through from device ' +
                             '[{}] to sound device [{}]'.
                             format(input_device, output_device))

            recording_started_time = time.time()

            while self._get_recording_state() != RecordingState.STOPPED \
                    and (duration is None or
                         time.time() - recording_started_time < duration):
                while self._get_recording_state() == RecordingState.PAUSED:
                    self.recording_paused_changed.wait()

                time.sleep(0.1)

        except queue.Empty:
            self.logger.warning('Recording timeout: audio callback failed?')
        finally:
            self.stop_playback([stream_index])
            self.stop_recording()

        Records audio and plays it on an output sound device (audio pass-through)

        :param duration: Recording duration in seconds (default: record until stop event)
        :type duration: float

        :param input_device: Input device (default: default configured device or system default audio input if not configured)
        :type input_device: int or str

        :param output_device: Output device (default: default configured device or system default audio output if not configured)
        :type output_device: int or str

        :param sample_rate: Recording sample rate (default: device default rate)
        :type sample_rate: int

        :param blocksize: Audio block size (default: configured `output_blocksize` or 2048)
        :type blocksize: int

        :param latency: Device latency in seconds (default: 0)
        :type latency: float

        :param channels: Number of channels (default: 1)
        :type channels: int

        :param dtype: Data type for the recording - see `Soundfile docs - Recording <https://python-sounddevice.readthedocs.io/en/0.3.12/_modules/sounddevice.html#rec>`_ for available types (default: input device default)
        :type dtype: str
        
Records audio and plays it on an output sound device (audio pass-through)
Records audio and plays it on an output sound device ( audio pass - through )
def recordplay(self, duration=None, input_device=None, output_device=None,
                   sample_rate=None, blocksize=None, latency=0, channels=1, dtype=None):
        import sounddevice as sd
        self.recording_paused_changed.clear()
        if input_device is None:
            input_device = self.input_device
        if input_device is None:
            input_device = self._get_default_device('input')
        if output_device is None:
            output_device = self.output_device
        if output_device is None:
            output_device = self._get_default_device('output')
        if sample_rate is None:
            dev_info = sd.query_devices(input_device, 'input')
            sample_rate = int(dev_info['default_samplerate'])
        if blocksize is None:
            blocksize = self.output_blocksize
        def audio_callback(indata, outdata, frames, time, status):
            while self._get_recording_state() == RecordingState.PAUSED:
                self.recording_paused_changed.wait()
            if status:
                self.logger.warning('Recording callback status: {}'.format(
                    str(status)))
            outdata[:] = indata
        stream_index = None
        try:
            import soundfile as sf
            import numpy
            stream_index = self._allocate_stream_index()
            stream = sd.Stream(samplerate=sample_rate, channels=channels,
                               blocksize=blocksize, latency=latency,
                               device=(input_device, output_device),
                               dtype=dtype, callback=audio_callback)
            self.start_recording()
            self._start_playback(stream_index=stream_index,
                                 stream=stream)
            self.logger.info('Started recording pass-through from device ' +
                             '[{}] to sound device [{}]'.
                             format(input_device, output_device))
            recording_started_time = time.time()
            while self._get_recording_state() != RecordingState.STOPPED \
                    and (duration is None or
                         time.time() - recording_started_time < duration):
                while self._get_recording_state() == RecordingState.PAUSED:
                    self.recording_paused_changed.wait()
                time.sleep(0.1)
        except queue.Empty:
            self.logger.warning('Recording timeout: audio callback failed?')
        finally:
            self.stop_playback([stream_index])
            self.stop_recording()
def recordplay ( self , duration = None , input_device = None , output_device = None , sample_rate = None , blocksize = None , latency = 0 , channels = 1 , dtype = None ) : import sounddevice as sd self . recording_paused_changed . clear ( ) if input_device is None : input_device = self . input_device if input_device is None : input_device = self . _get_default_device ( 'input' ) if output_device is None : output_device = self . output_device if output_device is None : output_device = self . _get_default_device ( 'output' ) if sample_rate is None : dev_info = sd . query_devices ( input_device , 'input' ) sample_rate = int ( dev_info [ 'default_samplerate' ] ) if blocksize is None : blocksize = self . output_blocksize def audio_callback ( indata , outdata , frames , time , status ) : while self . _get_recording_state ( ) == RecordingState . PAUSED : self . recording_paused_changed . wait ( ) if status : self . logger . warning ( 'Recording callback status: {}' . format ( str ( status ) ) ) outdata [ : ] = indata stream_index = None try : import soundfile as sf import numpy stream_index = self . _allocate_stream_index ( ) stream = sd . Stream ( samplerate = sample_rate , channels = channels , blocksize = blocksize , latency = latency , device = ( input_device , output_device ) , dtype = dtype , callback = audio_callback ) self . start_recording ( ) self . _start_playback ( stream_index = stream_index , stream = stream ) self . logger . info ( 'Started recording pass-through from device ' + '[{}] to sound device [{}]' . format ( input_device , output_device ) ) recording_started_time = time . time ( ) while self . _get_recording_state ( ) != RecordingState . STOPPED and ( duration is None or time . time ( ) - recording_started_time < duration ) : while self . _get_recording_state ( ) == RecordingState . PAUSED : self . recording_paused_changed . wait ( ) time . sleep ( 0.1 ) except queue . Empty : self . logger . warning ( 'Recording timeout: audio callback failed?' ) finally : self . stop_playback ( [ stream_index ] ) self . stop_recording ( )
Records audio and plays it on an output sound device (audio pass-through)
Records audio and plays it on an output sound device ( audio pass - through )
"""
        Records audio and plays it on an output sound device (audio pass-through)

        :param duration: Recording duration in seconds (default: record until stop event)
        :type duration: float

        :param input_device: Input device (default: default configured device or system default audio input if not configured)
        :type input_device: int or str

        :param output_device: Output device (default: default configured device or system default audio output if not configured)
        :type output_device: int or str

        :param sample_rate: Recording sample rate (default: device default rate)
        :type sample_rate: int

        :param blocksize: Audio block size (default: configured `output_blocksize` or 2048)
        :type blocksize: int

        :param latency: Device latency in seconds (default: 0)
        :type latency: float

        :param channels: Number of channels (default: 1)
        :type channels: int

        :param dtype: Data type for the recording - see `Soundfile docs - Recording <https://python-sounddevice.readthedocs.io/en/0.3.12/_modules/sounddevice.html#rec>`_ for available types (default: input device default)
        :type dtype: str
        """ # noinspection PyUnusedLocal
self
duration
Recording duration in seconds (default: record until stop event)
Recording duration in seconds ( default : record until stop event )
input_device
Input device (default: default configured device or system default audio input if not configured)
Input device ( default : default configured device or system default audio input if not configured )
output_device
Output device (default: default configured device or system default audio output if not configured)
Output device ( default : default configured device or system default audio output if not configured )
sample_rate
Recording sample rate (default: device default rate)
Recording sample rate ( default : device default rate )
blocksize
Audio block size (default: configured `output_blocksize` or 2048)
Audio block size ( default : configured ` output_blocksize ` or 2048 )
latency
Device latency in seconds (default: 0)
Device latency in seconds ( default : 0 )
channels
Number of channels (default: 1)
Number of channels ( default : 1 )
dtype
Data type for the recording - see `Soundfile docs - Recording `_ for available types (default: input device default)
Data type for the recording - see ` Soundfile docs - Recording ` _ for available types ( default : input device default )