Python pyaudio.paInt32() Examples

The following are 5 code examples of pyaudio.paInt32(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyaudio , or try the search function .
Example #1
Source File: recorder.py    From TranscriptBot with MIT License 5 votes vote down vote up
def __init__(self,
                 audio_device=0,
                 audio_format=pyaudio.paInt32,
                 rate=44100,
                 chunk_size=4096,
                 pause_threshold_seconds=1.0,
                 include_before_seconds=0.5,
                 include_after_seconds=0.5,
                 add_silence_seconds=0.5,
                 init_energy_threshold=1000000,
                 energy_damping=0.7):
        self.audio_device = audio_device
        self.audio_format = audio_format
        self.rate = rate
        self.chunk_size = chunk_size
        self.session = pyaudio.PyAudio()
        self.sample_width = self.session.get_sample_size(audio_format)
        self.add_silence_seconds = add_silence_seconds
        self.seconds_per_buffer = float(chunk_size)/rate
        self.include_before = include_before_seconds/self.seconds_per_buffer
        self.include_before = int(math.ceil(self.include_before))
        self.include_after = include_after_seconds/self.seconds_per_buffer
        self.include_after = int(math.ceil(self.include_after))
        self.pause_threshold = pause_threshold_seconds/self.seconds_per_buffer
        self.pause_threshold = int(math.ceil(self.pause_threshold))
        self.energy_threshold = init_energy_threshold
        self.energy_damping = energy_damping 
Example #2
Source File: pyaudio.py    From synthesizer with GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, samplerate: int = 0, samplewidth: int = 0, nchannels: int = 0, frames_per_chunk: int = 0) -> None:
        super().__init__(samplerate, samplewidth, nchannels, frames_per_chunk, 0)
        self.initialize()
        thread_ready = threading.Event()

        def audio_thread() -> None:
            audio = pyaudio.PyAudio()       # type: ignore
            try:
                mixed_chunks = self.mixer.chunks()
                audio_format = audio.get_format_from_width(self.samplewidth) \
                    if self.samplewidth != 4 else pyaudio.paInt32     # type: ignore
                output_device = None if playback.default_audio_device < 0 else playback.default_audio_device
                stream = audio.open(format=audio_format, channels=self.nchannels, rate=self.samplerate, output=True,
                                    output_device_index=output_device, input_device_index=output_device)
                thread_ready.set()
                try:
                    silence = b"\0" * self.chunksize
                    while True:
                        data = next(mixed_chunks) or silence
                        if isinstance(data, memoryview):
                            data = data.tobytes()   # PyAudio stream can't deal with memoryview
                        stream.write(data)
                        if len(data) < self.chunksize:
                            stream.write(silence[len(data):])
                        if self.playing_callback:
                            sample = Sample.from_raw_frames(data, self.samplewidth, self.samplerate, self.nchannels)
                            self.playing_callback(sample)
                except StopIteration:
                    pass
                finally:
                    stream.close()
            finally:
                audio.terminate()

        self.output_thread = threading.Thread(target=audio_thread, name="audio-pyaudio", daemon=True)
        self.output_thread.start()
        thread_ready.wait() 
Example #3
Source File: friday.py    From Friday with MIT License 5 votes vote down vote up
def _apiai_stt(self):
        from math import log
        import audioop
        import pyaudio
        import time
        resampler = apiai.Resampler(source_samplerate=settings['RATE'])
        request = self.ai.voice_request()
        vad = apiai.VAD()

        def callback(in_data, frame_count):
            frames, data = resampler.resample(in_data, frame_count)
            if settings.show_decibels:
                decibel = 20 * log(audioop.rms(data, 2) + 1, 10)
                click.echo(decibel)
            state = vad.processFrame(frames)
            request.send(data)
            state_signal = pyaudio.paContinue if state == 1 else pyaudio.paComplete
            return in_data, state_signal

        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paInt32, input=True, output=False, stream_callback=callback,
                        channels=settings['CHANNELS'], rate=settings['RATE'], frames_per_buffer=settings['CHUNK'])
        stream.start_stream()
        click.echo("Speak!")
        while stream.is_active():
            time.sleep(0.1)
        stream.stop_stream()
        stream.close()
        p.terminate() 
Example #4
Source File: pyaudio_source.py    From voice-engine with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, rate=16000, frames_size=None, channels=None, device_name=None, bits_per_sample=16):
        """Setup a pyaudio callback stream to record audio

        Args:
            rate: sample rate
            frames_size: number of each channel's frames per chunk
            channels: channels' number
            device_name: device name to search
            bits_per_sample: sample width - 8, 16, 24 or 32
        """
        super(Source, self).__init__()

        self.rate = rate
        self.frames_size = frames_size if frames_size else rate / 100
        self.channels = channels if channels else 1

        self.pyaudio_instance = pyaudio.PyAudio()

        formats = [pyaudio.paInt8, pyaudio.paInt16, pyaudio.paInt24, pyaudio.paInt32]
        width = formats[bits_per_sample / 8 - 1]

        # Search device by name
        if device_name:
            for i in range(self.pyaudio_instance.get_device_count()):
                dev = self.pyaudio_instance.get_device_info_by_index(i)
                name = dev['name'].encode('utf-8')
                logger.info('{}:{} with {} input channels'.format(i, name, dev['maxInputChannels']))
                if name.find(device_name) >= 0:
                    logger.info('Use {}'.format(name))
                    device_index = i
                    break
        else:
            device_index = self.pyaudio_instance.get_default_input_device_info()['index']

        if device_index is None:
            raise ValueError('Can not find an input device {}'.format(device_name))

        self.stream = self.pyaudio_instance.open(
            start=False,
            format=width,
            input_device_index=device_index,
            channels=self.channels,
            rate=int(self.rate),
            frames_per_buffer=int(self.frames_size),
            stream_callback=self._callback,
            input=True
        ) 
Example #5
Source File: pyaudio_source.py    From HermesLedControl with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, rate=16000, frames_size=None, channels=None, device_name=None, bits_per_sample=16):
        """Setup a pyaudio callback stream to record audio

        Args:
            rate: sample rate
            frames_size: number of each channel's frames per chunk
            channels: channels' number
            device_name: device name to search
            bits_per_sample: sample width - 8, 16, 24 or 32
        """
        super(Source, self).__init__()

        self.rate = rate
        self.frames_size = frames_size if frames_size else rate / 100
        self.channels = channels if channels else 1

        self.pyaudio_instance = pyaudio.PyAudio()

        formats = [pyaudio.paInt8, pyaudio.paInt16, pyaudio.paInt24, pyaudio.paInt32]
        width = formats[bits_per_sample / 8 - 1]

        # Search device by name
        if device_name:
            for i in range(self.pyaudio_instance.get_device_count()):
                dev = self.pyaudio_instance.get_device_info_by_index(i)
                name = dev['name'].encode('utf-8')
                logger.info('{}:{} with {} input channels'.format(i, name, dev['maxInputChannels']))
                if name.find(device_name) >= 0:
                    logger.info('Use {}'.format(name))
                    device_index = i
                    break
        else:
            device_index = self.pyaudio_instance.get_default_input_device_info()['index']

        if device_index is None:
            raise ValueError('Can not find an input device {}'.format(device_name))

        self.stream = self.pyaudio_instance.open(
            start=False,
            format=width,
            input_device_index=device_index,
            channels=self.channels,
            rate=int(self.rate),
            frames_per_buffer=int(self.frames_size),
            stream_callback=self._callback,
            input=True
        )