platypush/plugins/sound/__init__.py
def stream_recording(self, device=None, fifo=None, duration=None, sample_rate=None,
                         dtype='float32', blocksize=None, latency=0, channels=1):
        """
        Return audio data from an audio source

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

        :param fifo: Path of the FIFO that will be used to exchange audio samples (default: /tmp/inputstream)
        :type fifo: str

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

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

        :param dtype: Data type for the audio samples. Supported types:
            'float64', 'float32', 'int32', 'int16', 'int8', 'uint8'. Default: float32
        :type dtype: str

        :param blocksize: Audio block size (default: configured `input_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
        """

        import sounddevice as sd

        self.recording_paused_changed.clear()

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

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

        if blocksize is None:
            blocksize = self.input_blocksize

        if not fifo:
            fifo = self._default_input_stream_fifo

        q = queue.Queue()

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

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

            q.put(indata.copy())

        def streaming_thread():
            try:
                with sd.InputStream(samplerate=sample_rate, device=device,
                                    channels=channels, callback=audio_callback,
                                    dtype=dtype, latency=latency, blocksize=blocksize):
                    with open(fifo, 'wb') as audio_queue:
                        self.start_recording()
                        get_bus().post(SoundRecordingStartedEvent())
                        self.logger.info('Started recording from device [{}]'.format(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()

                            get_args = {
                                'block': True,
                                'timeout': max(0, duration - (time.time() - recording_started_time)),
                            } if duration is not None else {}

                            data = q.get(**get_args)
                            if not len(data):
                                continue

                            audio_queue.write(data)
            except queue.Empty:
                self.logger.warning('Recording timeout: audio callback failed?')
            finally:
                self.stop_recording()
                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 {}'.format(fifo))
                os.unlink(fifo)
            else:
                raise RuntimeError('{} exists and is not a FIFO. Please remove it or rename it'.format(fifo))

        os.mkfifo(fifo, 0o644)
        Thread(target=streaming_thread).start()

        Return audio data from an audio source

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

        :param fifo: Path of the FIFO that will be used to exchange audio samples (default: /tmp/inputstream)
        :type fifo: str

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

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

        :param dtype: Data type for the audio samples. Supported types:
            'float64', 'float32', 'int32', 'int16', 'int8', 'uint8'. Default: float32
        :type dtype: str

        :param blocksize: Audio block size (default: configured `input_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
        
Return audio data from an audio source
Return audio data from an audio source
def stream_recording(self, device=None, fifo=None, duration=None, sample_rate=None,
                         dtype='float32', blocksize=None, latency=0, channels=1):
        import sounddevice as sd
        self.recording_paused_changed.clear()
        if device is None:
            device = self.input_device
        if device is None:
            device = self._get_default_device('input')
        if sample_rate is None:
            dev_info = sd.query_devices(device, 'input')
            sample_rate = int(dev_info['default_samplerate'])
        if blocksize is None:
            blocksize = self.input_blocksize
        if not fifo:
            fifo = self._default_input_stream_fifo
        q = queue.Queue()
        def audio_callback(indata, frames, time_duration, status):
            while self._get_recording_state() == RecordingState.PAUSED:
                self.recording_paused_changed.wait()
            if status:
                self.logger.warning('Recording callback status: {}'.format(str(status)))
            q.put(indata.copy())
        def streaming_thread():
            try:
                with sd.InputStream(samplerate=sample_rate, device=device,
                                    channels=channels, callback=audio_callback,
                                    dtype=dtype, latency=latency, blocksize=blocksize):
                    with open(fifo, 'wb') as audio_queue:
                        self.start_recording()
                        get_bus().post(SoundRecordingStartedEvent())
                        self.logger.info('Started recording from device [{}]'.format(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()
                            get_args = {
                                'block': True,
                                'timeout': max(0, duration - (time.time() - recording_started_time)),
                            } if duration is not None else {}
                            data = q.get(**get_args)
                            if not len(data):
                                continue
                            audio_queue.write(data)
            except queue.Empty:
                self.logger.warning('Recording timeout: audio callback failed?')
            finally:
                self.stop_recording()
                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 {}'.format(fifo))
                os.unlink(fifo)
            else:
                raise RuntimeError('{} exists and is not a FIFO. Please remove it or rename it'.format(fifo))
        os.mkfifo(fifo, 0o644)
        Thread(target=streaming_thread).start()
def stream_recording ( self , device = None , fifo = None , duration = None , sample_rate = None , dtype = 'float32' , blocksize = None , latency = 0 , channels = 1 ) : import sounddevice as sd self . recording_paused_changed . clear ( ) if device is None : device = self . input_device if device is None : device = self . _get_default_device ( 'input' ) if sample_rate is None : dev_info = sd . query_devices ( device , 'input' ) sample_rate = int ( dev_info [ 'default_samplerate' ] ) if blocksize is None : blocksize = self . input_blocksize if not fifo : fifo = self . _default_input_stream_fifo q = queue . Queue ( ) def audio_callback ( indata , frames , time_duration , status ) : while self . _get_recording_state ( ) == RecordingState . PAUSED : self . recording_paused_changed . wait ( ) if status : self . logger . warning ( 'Recording callback status: {}' . format ( str ( status ) ) ) q . put ( indata . copy ( ) ) def streaming_thread ( ) : try : with sd . InputStream ( samplerate = sample_rate , device = device , channels = channels , callback = audio_callback , dtype = dtype , latency = latency , blocksize = blocksize ) : with open ( fifo , 'wb' ) as audio_queue : self . start_recording ( ) get_bus ( ) . post ( SoundRecordingStartedEvent ( ) ) self . logger . info ( 'Started recording from device [{}]' . format ( 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 ( ) get_args = { 'block' : True , 'timeout' : max ( 0 , duration - ( time . time ( ) - recording_started_time ) ) , } if duration is not None else { } data = q . get ( ** get_args ) if not len ( data ) : continue audio_queue . write ( data ) except queue . Empty : self . logger . warning ( 'Recording timeout: audio callback failed?' ) finally : self . stop_recording ( ) 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 {}' . format ( fifo ) ) os . unlink ( fifo ) else : raise RuntimeError ( '{} exists and is not a FIFO. Please remove it or rename it' . format ( fifo ) ) os . mkfifo ( fifo , 0o644 ) Thread ( target = streaming_thread ) . start ( )
Return audio data from an audio source
Return audio data from an audio source
"""
        Return audio data from an audio source

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

        :param fifo: Path of the FIFO that will be used to exchange audio samples (default: /tmp/inputstream)
        :type fifo: str

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

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

        :param dtype: Data type for the audio samples. Supported types:
            'float64', 'float32', 'int32', 'int16', 'int8', 'uint8'. Default: float32
        :type dtype: str

        :param blocksize: Audio block size (default: configured `input_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
        """ # noinspection PyUnusedLocal
self
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 )
fifo
Path of the FIFO that will be used to exchange audio samples (default: /tmp/inputstream)
Path of the FIFO that will be used to exchange audio samples ( default : / tmp / inputstream )
duration
Recording duration in seconds (default: record until stop event)
Recording duration in seconds ( default : record until stop event )
sample_rate
Recording sample rate (default: device default rate)
Recording sample rate ( default : device default rate )
dtype
Data type for the audio samples.
Data type for the audio samples .
blocksize
Audio block size (default: configured `input_blocksize` or 2048)
Audio block size ( default : configured ` input_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 )