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?
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.
Instead of PyAudio, you could use http://python-sounddevice.rtfd.org/.
The play() function accepts a mapping argument to select the channel(s) to play back on.
You could try this to play a mono NumPy array on the right channel:
import sounddevice as sd
sd.play(signal, samplerate=44100, mapping=[2])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With