Pyaudio How to get sound on only one speaker

限于喜欢 提交于 2019-12-02 11:35:59

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.

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()

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])

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()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!