當前位置: 首頁>>代碼示例>>Python>>正文


Python pyaudio.paInt32方法代碼示例

本文整理匯總了Python中pyaudio.paInt32方法的典型用法代碼示例。如果您正苦於以下問題:Python pyaudio.paInt32方法的具體用法?Python pyaudio.paInt32怎麽用?Python pyaudio.paInt32使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pyaudio的用法示例。


在下文中一共展示了pyaudio.paInt32方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: import pyaudio [as 別名]
# 或者: from pyaudio import paInt32 [as 別名]
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 
開發者ID:agermanidis,項目名稱:TranscriptBot,代碼行數:29,代碼來源:recorder.py

示例2: __init__

# 需要導入模塊: import pyaudio [as 別名]
# 或者: from pyaudio import paInt32 [as 別名]
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() 
開發者ID:irmen,項目名稱:synthesizer,代碼行數:39,代碼來源:pyaudio.py

示例3: _apiai_stt

# 需要導入模塊: import pyaudio [as 別名]
# 或者: from pyaudio import paInt32 [as 別名]
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() 
開發者ID:Zenohm,項目名稱:Friday,代碼行數:31,代碼來源:friday.py

示例4: __init__

# 需要導入模塊: import pyaudio [as 別名]
# 或者: from pyaudio import paInt32 [as 別名]
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
        ) 
開發者ID:voice-engine,項目名稱:voice-engine,代碼行數:49,代碼來源:pyaudio_source.py


注:本文中的pyaudio.paInt32方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。