import pyaudio
import numpy as np
p = pyaudio.PyAudio()
volume = 0.5 # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = 1.0 # in seconds, may be float
f = 440.0 # sine frequency, Hz, may be float
# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
# play. May repeat with different volume values (if done interactively)
stream.write(volume*samples)
stream.stop_stream()
stream.close()
p.terminate()
Nguồn
2015-01-16 07:06:39
nền tảng gì (s) được bạn nhắm mục tiêu? –
Chủ yếu là MacOS X - Tôi nên xác định điều này, xin lỗi! – astrofrog
Câu hỏi này tương tự như http://stackoverflow.com/questions/307305/play-a-sound-with-python và http://stackoverflow.com/questions/260738/play-audio-with-python –