Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyaudio How to get sound on only one speaker

Tags:

python

pyaudio

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?

like image 457
sCuper Avatar asked Oct 27 '25 01:10

sCuper


2 Answers

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.

like image 109
cfh Avatar answered Oct 29 '25 18:10

cfh


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])
like image 44
Matthias Avatar answered Oct 29 '25 17:10

Matthias