【发布时间】:2017-11-24 02:58:05
【问题描述】:
我刚刚开始使用pyaudio,我编写了一个简单的函数来播放一个音符。但是,根据我使用的 Python 版本,注释听起来会有所不同:
from __future__ import division
import math
import pyaudio
BITS_PER_BYTE = 8 # for clarity
SAMPLE_BIT_DEPTH = 8 # i.e. each sample is 1 byte
SAMPLES_PER_SECOND = 16000
NOTE_TIME_SECONDS = 1
MIDDLE_C_HZ = 523.3
CYCLES_PER_SECOND = SAMPLES_PER_SECOND / MIDDLE_C_HZ
NUM_SAMPLES = SAMPLES_PER_SECOND * NOTE_TIME_SECONDS
def play_note():
audio = pyaudio.PyAudio()
stream = audio.open(
format=audio.get_format_from_width(SAMPLE_BIT_DEPTH / BITS_PER_BYTE),
channels=1,
rate=SAMPLES_PER_SECOND,
output=True,
)
byte_string = str()
for i in range(NUM_SAMPLES):
# calculate the amplitude for this frame as a float between -1 and 1
frame_amplitude = math.sin(i / (CYCLES_PER_SECOND / math.pi))
# scale the amplitude to an integer between 0 and 255 (inclusive)
scaled_amplitude = int(frame_amplitude * 127 + 128)
# convert amplitude to byte string (ascii value)
byte_string += chr(scaled_amplitude)
stream.write(byte_string)
stream.close()
audio.terminate()
if __name__ == '__main__':
play_note()
在 Python 2.7.13 中,我听到了正确、清晰的音调。在3.6.2 中听起来很粗糙,像方波。
为什么会这样,我将如何解决这个问题(或至少开始调试)?
我在 OSX v10.11.6 上使用 portaudio v19.6.0。
【问题讨论】:
标签: python python-3.x python-2.7 audio pyaudio