在 PyAudio `open` 方法中设置 `output=False` 有什么意义?
What is the point of setting `output=False` in the PyAudio `open` method?
下面是来自 the PyAudio documentation 的代码示例,展示了如何播放 .wav 文件。
我知道在 open
方法中设置 output=False
会阻止文件播放,但这有什么意义呢?这是为调试目的保留的吗?
"""PyAudio Example: Play a wave file."""
import pyaudio
import wave
import sys
CHUNK = 1024
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(CHUNK)
# play stream (3)
while len(data) > 0:
stream.write(data)
data = wf.readframes(CHUNK)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
您可以在 PyAudio
中输入 和 输出流,设置 output = False
(我认为这是默认设置)只是意味着它不是输出流。
输出流将用于(例如)将现有文件播放到声音子系统(根据您的代码片段)。
输入流可用于从声音子系统中提取信息以记录到文件。
我明白为什么有人会想知道为什么你会把输入 和 输出都设置为 false(事实上,我认为这是一个错误条件)但是 output
如果 input
为真,则可以为假。
TL;DR 流可以是输入、输出或两者。至少 output
或 input
必须是 True
.
PyAudio 声明 open
method of PyAudio
打开一个新流:
Open a new stream. See constructor for Stream.__init__()
for parameter details.
查看 Stream.__init__()
时,我们看到输入和输出都默认为 false:
input – Specifies whether this is an input stream. Defaults to False.
output – Specifies whether this is an output stream. Defaults to False.
但是有一个警告:
Raises:
ValueError – Neither input nor output are set True.
因此流可以是输入(从计算机声音系统到文件)或输出(从文件到计算机声音系统)或两者(回调)。
下面是来自 the PyAudio documentation 的代码示例,展示了如何播放 .wav 文件。
我知道在 open
方法中设置 output=False
会阻止文件播放,但这有什么意义呢?这是为调试目的保留的吗?
"""PyAudio Example: Play a wave file."""
import pyaudio
import wave
import sys
CHUNK = 1024
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(CHUNK)
# play stream (3)
while len(data) > 0:
stream.write(data)
data = wf.readframes(CHUNK)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
您可以在 PyAudio
中输入 和 输出流,设置 output = False
(我认为这是默认设置)只是意味着它不是输出流。
输出流将用于(例如)将现有文件播放到声音子系统(根据您的代码片段)。
输入流可用于从声音子系统中提取信息以记录到文件。
我明白为什么有人会想知道为什么你会把输入 和 输出都设置为 false(事实上,我认为这是一个错误条件)但是 output
如果 input
为真,则可以为假。
TL;DR 流可以是输入、输出或两者。至少 output
或 input
必须是 True
.
PyAudio 声明 open
method of PyAudio
打开一个新流:
Open a new stream. See constructor for
Stream.__init__()
for parameter details.
查看 Stream.__init__()
时,我们看到输入和输出都默认为 false:
input – Specifies whether this is an input stream. Defaults to False.
output – Specifies whether this is an output stream. Defaults to False.
但是有一个警告:
Raises:
ValueError – Neither input nor output are set True.
因此流可以是输入(从计算机声音系统到文件)或输出(从文件到计算机声音系统)或两者(回调)。