Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

python - Pyaudio How to get sound on only one speaker

I'm using pyaudio in a school project and I'm trying to get the sound to play on only one speaker at a time. My code is like this:

import pyaudio

p = pyaudio.PyAduio()

def play_wave(stream, wave):
    chunks = []
    chunks.append(wave)
    chunk = concatenate(chunks)*0.1
    stream.write(chunk.astype(np.float32).tostring())

def play_sound(freq, t, A=0.2):
    wave, A = wavefunc(t, freq, A=A)
    S = sigmoid(t)
    wave = wave*S
    stream = p.open(channels=1, rate=44100, format=pyaudio.paFloat32, output=True)
    play_wave(stream,wave)
    stream.close()

where wavefunc just generates a wave.

Does anybody know what to do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Right now you are using channels=1, i.e., a mono audio stream. You need to use two channels for stereo and generate the data for the left and right channel separately.

Here's a short tutorial on how to create stereo data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...