当前位置: 首页>>代码示例>>Python>>正文


Python pyaudio.PyAudio方法代码示例

本文整理汇总了Python中pyaudio.PyAudio方法的典型用法代码示例。如果您正苦于以下问题:Python pyaudio.PyAudio方法的具体用法?Python pyaudio.PyAudio怎么用?Python pyaudio.PyAudio使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyaudio的用法示例。


在下文中一共展示了pyaudio.PyAudio方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __enter__

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def __enter__(self):
        self._audio_interface = pyaudio.PyAudio()
        self._audio_stream = self._audio_interface.open(
            # format=pyaudio.paInt16,
            format=pyaudio.paFloat32,
            # The API currently only supports 1-channel (mono) audio
            # https://goo.gl/z757pE
            channels=1,
            rate=self._rate,
            input=True,
            frames_per_buffer=self._chunk,
            input_device_index=self._device,
            # Run the audio stream asynchronously to fill the buffer object.
            # This is necessary so that the input device's buffer doesn't
            # overflow while the calling thread makes network requests, etc.
            stream_callback=self._fill_buffer,
        )

        self.closed = False

        return self 
开发者ID:pytorch,项目名称:audio,代码行数:23,代码来源:vad.py

示例2: play_audio_file

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def play_audio_file(fname=DETECT_DING):
    """Simple callback function to play a wave file. By default it plays
    a Ding sound.

    :param str fname: wave file name
    :return: None
    """
    ding_wav = wave.open(fname, 'rb')
    ding_data = ding_wav.readframes(ding_wav.getnframes())
    audio = pyaudio.PyAudio()
    stream_out = audio.open(
        format=audio.get_format_from_width(ding_wav.getsampwidth()),
        channels=ding_wav.getnchannels(),
        rate=ding_wav.getframerate(), input=False, output=True)
    stream_out.start_stream()
    stream_out.write(ding_data)
    time.sleep(0.2)
    stream_out.stop_stream()
    stream_out.close()
    audio.terminate() 
开发者ID:warchildmd,项目名称:google-assistant-hotword-raspi,代码行数:22,代码来源:snowboydecoder.py

示例3: valid_input_devices

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def valid_input_devices(self):
        """
        See which devices can be opened for microphone input.
        call this when no PyAudio object is loaded.
        """
        mics=[]
        for device in range(self.p.get_device_count()):
            if self.valid_test(device):
                mics.append(device)
        if len(mics)==0:
            print("no microphone devices found!")
        else:
            print("found %d microphone devices: %s"%(len(mics),mics))
        return mics

    ### SETUP AND SHUTDOWN 
开发者ID:swharden,项目名称:Python-GUI-examples,代码行数:18,代码来源:SWHear.py

示例4: play_wav

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def play_wav(fname, chunk=CHUNK):
    # create an audio object
    wf = wave.open(fname, 'rb')
    p = pyaudio.PyAudio()

    # open stream based on the wave object which has been input.
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True)

    # read data (based on the chunk size)
    data = wf.readframes(chunk)

    # play stream (looping from beginning of file to the end)
    while len(data) > 0:
        # writing to the stream is what *actually* plays the sound.
        stream.write(data)
        data = wf.readframes(chunk)

    # cleanup stuff
    stream.close()
    p.terminate() 
开发者ID:gigagenie,项目名称:ai-makers-kit,代码行数:25,代码来源:_audio.py

示例5: play_file

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def play_file(fname):
	# create an audio object
	wf = wave.open(fname, 'rb')
	p = pyaudio.PyAudio()
	chunk = 1024

	# open stream based on the wave object which has been input.
	stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
					channels=wf.getnchannels(),
					rate=wf.getframerate(),
					output=True)

	# read data (based on the chunk size)
	data = wf.readframes(chunk)

	# play stream (looping from beginning of file to the end)
	while len(data) > 0:
		# writing to the stream is what *actually* plays the sound.
		stream.write(data)
		data = wf.readframes(chunk)

		# cleanup stuff.
	stream.close()
	p.terminate() 
开发者ID:gigagenie,项目名称:ai-makers-kit,代码行数:26,代码来源:ex1_kwstest.py

示例6: __enter__

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def __enter__(self):
		self._audio_interface = pyaudio.PyAudio()
		self._audio_stream = self._audio_interface.open(
			format=pyaudio.paInt16,
			channels=1, rate=self._rate,
			input=True, frames_per_buffer=self._chunk,
			# Run the audio stream asynchronously to fill the buffer object.
			# This is necessary so that the input device's buffer doesn't
			# overflow while the calling thread makes network requests, etc.
			stream_callback=self._fill_buffer,
		)

		self.closed = False

		return self

	#def __exit__(self, type, value, traceback): 
开发者ID:gigagenie,项目名称:ai-makers-kit,代码行数:19,代码来源:proj2_yt_mvp.py

示例7: play_file

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def play_file(fname):
    # create an audio object
    wf = wave.open(fname, 'rb')
    p = pyaudio.PyAudio()
    chunk = 1024

    # open stream based on the wave object which has been input.
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True)

    # read data (based on the chunk size)
    data = wf.readframes(chunk)

    # play stream (looping from beginning of file to the end)
    while len(data) > 0:
        # writing to the stream is what *actually* plays the sound.
        stream.write(data)
        data = wf.readframes(chunk)

        # cleanup stuff.
    stream.close()
    p.terminate() 
开发者ID:gigagenie,项目名称:ai-makers-kit,代码行数:26,代码来源:ex4_getText2VoiceStream.py

示例8: _play_audio

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def _play_audio(path, delay):
        try:
            time.sleep(delay)
            wf = wave.open(path, 'rb')
            p = pyaudio.PyAudio()
            stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                            channels=wf.getnchannels(),
                            rate=wf.getframerate(),
                            output=True)
            
            data = wf.readframes(TextToSpeech.CHUNK)
            
            while data:
                stream.write(data)
                data = wf.readframes(TextToSpeech.CHUNK)
        
            stream.stop_stream()
            stream.close()

            p.terminate()
            return
        except:
            pass 
开发者ID:junzew,项目名称:HanTTS,代码行数:25,代码来源:main.py

示例9: play

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def play(self, file_):
        wf = wave.open(file_, 'rb')
        p = pyaudio.PyAudio()
        stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                        channels=wf.getnchannels(),
                        rate=wf.getframerate(),
                        output=True)

        data = wf.readframes(self.CHUNK)

        while data != '':
            stream.write(data)
            data = wf.readframes(self.CHUNK)

        stream.stop_stream()
        stream.close()

        p.terminate() 
开发者ID:namco1992,项目名称:voicetools,代码行数:20,代码来源:utils.py

示例10: __init__

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def __init__(self, config, verbose, logger):
        """Initialize an MQTT client.

        Args:
            config (:class:`.ServerConfig`): The configuration of
                the MQTT client.
            verbose (bool): Whether or not the MQTT client runs in verbose
                mode.
            logger (:class:`logging.Logger`): The Logger object for logging
                messages.
        """
        self.config = config
        self.verbose = verbose
        self.logger = logger
        self.mqtt = Client()
        self.logger.debug('Using %s', pyaudio.get_portaudio_version_text())
        self.logger.debug('Creating PyAudio object...')
        self.audio = pyaudio.PyAudio()

        self.initialize()

        self.mqtt.on_connect = self.on_connect
        self.mqtt.on_disconnect = self.on_disconnect
        self.connect() 
开发者ID:koenvervloesem,项目名称:hermes-audio-server,代码行数:26,代码来源:mqtt.py

示例11: __init__

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def __init__(self):
        self.interrupted = False
        self.detector = None
        rpack = RosPack()
        # UMDL or PMDL file paths along with audio files
        pkg_path = rpack.get_path('dialogflow_ros')
        self.model_path = pkg_path + '/scripts/snowboy/resources/jarvis.umdl'
        ding_path = pkg_path + '/scripts/snowboy/resources/ding.wav'
        # Setup df
        self.df_client = None
        # Setup audio output
        ding = wave.open(ding_path, 'rb')
        self.ding_data = ding.readframes(ding.getnframes())
        self.audio = pyaudio.PyAudio()
        self.stream_out = self.audio.open(
                format=self.audio.get_format_from_width(ding.getsampwidth()),
                channels=ding.getnchannels(), rate=ding.getframerate(),
                input=False, output=True)
        self.last_contexts = []
        rospy.loginfo("HOTWORD_CLIENT: Ready!") 
开发者ID:piraka9011,项目名称:dialogflow_ros,代码行数:22,代码来源:hotword_dialogflow.py

示例12: __init__

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def __init__(self):
        # Audio stream input setup
        FORMAT = pyaudio.paInt16
        CHANNELS = 1
        RATE = 16000
        self.CHUNK = 4096
        self.audio = pyaudio.PyAudio()
        self.stream = self.audio.open(format=FORMAT, channels=CHANNELS,
                                      rate=RATE, input=True,
                                      frames_per_buffer=self.CHUNK,
                                      stream_callback=self.get_data)
        self._buff = Queue.Queue()  # Buffer to hold audio data
        self.closed = False

        # ROS Text Publisher
        self.text_pub = rospy.Publisher('/google_client/text', String, queue_size=10)

        # Context clues in yaml file
        rospack = rospkg.RosPack()
        yamlFileDir = rospack.get_path('dialogflow_ros') + '/config/context.yaml'
        with open(yamlFileDir, 'r') as f:
            self.context = yaml.load(f) 
开发者ID:piraka9011,项目名称:dialogflow_ros,代码行数:24,代码来源:google_client.py

示例13: __init__

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def __init__(self):
        FORMAT = pyaudio.paInt16
        CHANNELS = 1
        RATE = 16000
        CHUNK = 4096
        self.audio = pyaudio.PyAudio()
        self.stream = self.audio.open(format=FORMAT, channels=CHANNELS, rate=RATE,
                                      input=True, frames_per_buffer=CHUNK,
                                      stream_callback=self._callback)
        self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.read_list = [self.serversocket]

        self._server_name = rospy.get_param('/dialogflow_client/server_name',
                                            '127.0.0.1')
        self._port = rospy.get_param('/dialogflow_client/port', 4444)

        rospy.loginfo("DF_CLIENT: Audio Server Started!") 
开发者ID:piraka9011,项目名称:dialogflow_ros,代码行数:19,代码来源:audio_server.py

示例14: _record

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def _record(self):
        # Start recording audio on the current thread until stop() is
        # called.
        p = pyaudio.PyAudio()
        channels, rate = self.config.CHANNELS, self.config.RATE
        frames_per_buffer = self.config.FRAMES_PER_BUFFER
        pa_format = pyaudio.get_format_from_width(self.config.SAMPLE_WIDTH)
        stream = p.open(input=True, format=pa_format, channels=channels,
                        rate=rate, frames_per_buffer=frames_per_buffer)

        # Start recognising in a loop
        stream.start_stream()
        while self._recording:
            with self._condition:
                self._buffers.append(stream.read(frames_per_buffer))

                # Notify waiting threads (if any).
                self._condition.notifyAll()

            # This improves the performance; we don't need to process as
            # much audio as the device can read.
            time.sleep(self.read_interval)

        stream.close()
        p.terminate() 
开发者ID:dictation-toolbox,项目名称:dragonfly,代码行数:27,代码来源:recording.py

示例15: _get_pa_instance

# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import PyAudio [as 别名]
def _get_pa_instance():
    # Suppress initial ALSA messages if using ALSA.
    # Got this from: https://stackoverflow.com/a/17673011/12157649
    try:
        asound = cdll.LoadLibrary('libasound.so')
        c_error_handler = ERROR_HANDLER_FUNC(
            lambda filename, line, function, err, fmt: None
        )
        asound.snd_lib_error_set_handler(c_error_handler)
    except:
        # We'll most likely get here if the Port Audio host API isn't ALSA.
        asound = None

    # Create the pa instance.
    pa = pyaudio.PyAudio()

    # If necessary, restore the original error handler.
    if asound:
        asound.snd_lib_error_set_handler(None)
    return pa 
开发者ID:dictation-toolbox,项目名称:dragonfly,代码行数:22,代码来源:action_playsound.py


注:本文中的pyaudio.PyAudio方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。