我在学校项目中使用pyaudio,我试图让声音一次只能在一个扬声器上播放。我的代码是这样的:
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()
wavefunc只会生成一个wave。
有人知道该怎么办吗?
答案 0 :(得分:3)
现在您正在使用channels=1
,即单声道音频流。您需要使用两个立体声通道并分别为左右声道生成数据。
Here's a short tutorial关于如何创建立体声数据。
答案 1 :(得分:1)
我自己也找到了一种方法。我只将play_sound函数更改为
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()
答案 2 :(得分:0)
您可以使用http://python-sounddevice.rtfd.org/。
代替PyAudio play()函数接受CREATE TABLE MEMBERS_TBL
(
IDM NUMBER(9) NOT NULL ,
SNAME VARCHAR2(20) ,
FNAME VARCHAR2(20) ,
DBIRTH VARCHAR2(15) ,
IDDBL NUMBER(10) ,
FLAG SMALLINT ,
DBLCRIT SMALLINT
);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (125,'BOLOREY','JEANNE','11/05/1955',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (126,'BOLLOREY','JEANNE','11/05/1955',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (153,'BALORE','GIANNE','11/05/1955',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (223,'ABOLLOREYY','JEANNE','11/05/1955',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (225,'FIABLE','MARINE','25/04/1963',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (228,'FAIBLE','MARYANE','25/04/1963',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (230,'FAIBLE','MARINE','25/04/1963',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (235,'LIPARK','JACQLINE','20/12/1939',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (236,'ILIPARC','JACQUELYNE','20/12/1939',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (237,'LIPARC','JACQUELHINE','20/12/1939',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (240,'RINTET','MALIKA','07/08/1954',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (241,'GRISION','RAUGER','26/10/1931',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (242,'GRISION','ROGER','26/10/1931',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (245,'GRESION','RAUJER','26/10/1931',NULL,NULL,NULL);
参数来选择要重播的频道。
您可以尝试在右侧通道上播放单声道NumPy阵列:
mapping
答案 3 :(得分:0)
这是另一个简单易用的立体声播放库的示例代码。
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()