python pyaudio_Python pyaudio.PyAudio方法代碼示例

本文汇总了Python中PyAudio模块的用法,包括播放音频文件、录音设备选择、音频输入输出流的创建等。通过多个示例展示了如何使用PyAudio进行音频处理,如播放wav文件、录音、检查可用输入设备等。适用于需要处理音频的Python开发者。
摘要由CSDN通过智能技术生成

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

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

示例1: __enter__

​點讚 7

# 需要導入模塊: 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,

示例2: play_audio_file

​點讚 6

# 需要導入模塊: 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,

示例3: valid_input_devices

​點讚 6

# 需要導入模塊: 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,

示例4: play_wav

​點讚 6

# 需要導入模塊: 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值