4

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?

sCuper
  • 91
  • 2
  • 8

4 Answers4

4

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.

cfh
  • 4,576
  • 1
  • 24
  • 34
2

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])
Matthias
  • 4,524
  • 2
  • 31
  • 50
1

I also found a method myself. I only changed the play_sound function to

def play_sound(freq, t, A=0.2):
    wave, A = wavefunc(t, freq, A=A)
    S = sigmoid(t)
    signal = wave*S
    stereo_signal = zeros([len(signal), 2])   #these two lines are new
    stereo_signal[:, 1] = signal[:]     #1 for right speaker, 0 for  left
    stream = p.open(channels=2, 
                rate=44100, 
                format=pyaudio.paFloat32, 
                output=True)
    play_wave(stream,stereo_signal)
    stream.close()
sCuper
  • 91
  • 2
  • 8
1

Here is a sample code for another simple and easy to use library for stereo playing.

import numpy as np
import simpleaudio as sa
import matplotlib.pyplot as plt
# calculate note frequencies Hertz
A_freq = 120
Csh_freq = A_freq * 2 ** (4 / 12)

E_freq = A_freq * 2 ** (7 / 12)
print(Csh_freq)
print(E_freq)
# get timesteps for each sample, T is note duration in seconds
sample_rate = 44100
T = 0.25
t = np.linspace(0, T, T * sample_rate, False)
#print(t)
# generate sine wave notes
A_note = np.sin(A_freq * t * 2 * np.pi)
Csh_note = np.sin(Csh_freq * t * 2 * np.pi)
E_note = np.sin(E_freq * t * 2 * np.pi)

plt.subplot(211)
plt.plot(A_note)
# concatenate notes
audio = np.hstack((A_note, Csh_note,E_note))
#print(audio)
# normalize to 16-bit range
audio *= 32767 / 1 * np.max(np.abs(audio))
plt.subplot(212)
#print(audio)
# convert to 16-bit data
audio = audio.astype(np.int16)

# create stereo signal with zeros
stereo_signal = np.zeros([int(sample_rate*T*3),2],dtype=np.int16)   
stereo_signal[:,1] = audio[:]
print(stereo_signal)

#print(audio2)
plt.plot(audio)
# start playback
play_obj = sa.play_buffer(stereo_signal, 2, 2, sample_rate)

#plt.show()
# wait for playback to finish before exiting
play_obj.wait_done()