我有一个USB麦克风和覆盆子pi。这就是我想要做的。
我有一个“ music.wav”文件,其中的音轨设置为左声道,并且我有USB麦克风连接到了树莓派。
当我播放“ music.wav”并用麦克风讲话时,我可以听到左扬声器中的音乐,而左,右扬声器中的麦克风声音。
我尝试了以下代码,并尝试了不同的方法,无法实现将声音限制在正确的扬声器中。
有人可以帮助我如何使用python语言限制仅左讲话者的音乐和仅右讲话者的语音吗?
以下两个扬声器中来自麦克风的语音代码。 仅需要限制在正确的扬声器中。请帮忙!
import pyaudio
import os
import numpy as np
chunk=2800
RATE=44100
p=pyaudio.PyAudio()
#input stream setup
stream=p.open(format = pyaudio.paInt16,rate=RATE,channels=1,
input_device_index = 1, input=True, frames_per_buffer=chunk)
#the code below is from the pyAudio library documentation referenced
#below
#output stream setup
player=p.open(format = pyaudio.paInt16,rate=RATE,channels=1,
output=True, frames_per_buffer=chunk)
while True: #Used to continuously stream audio
try:
data=np.fromstring(stream.read(chunk,exception_on_overflow
=False),dtype=np.int16)
player.write(data,chunk)
except IOError:
continue
#closes streams
stream.stop_stream()
stream.close()
p.terminate
更新: 经过多次尝试,我执行了以下代码:现在声音不清晰,我仍然在同时听两个扬声器...我也将“ player”(输出)的输出通道更改为2.。不走运!
import pyaudio
import os
import numpy as np
chunk=2800
RATE=44100
p=pyaudio.PyAudio()
#input stream setup
stream=p.open(format = pyaudio.paInt16,rate=RATE,channels=1,
input_device_index = 1, input=True, frames_per_buffer=chunk)
player=p.open(format = pyaudio.paInt16,rate=RATE,channels=1,
output=True,
frames_per_buffer=chunk) #tried changing channels=2
while True: #Used to continuously stream audio
try:
data=np.fromstring(stream.read(chunk,exception_on_overflow =
False),dtype=np.int16)
chunk_length = int(len(data)/2)
result = np.reshape(data, (chunk_length, 2),order='f')
#result = np.reshape(data, (chunk_length, 2))
print(result)
rightchannel=result[:, 1] #I need right
#leftchannel=result[:, 0]
print(rightchannel)
player.write(rightchannel,chunk_length)
#player.write(result,chunk)
except IOError:
continue
更新2:
stream=p.open(format = pyaudio.paInt16,rate=RATE,channels=1,
input_device_index = 1, input=True, frames_per_buffer=chunk)
player=p.open(format = pyaudio.paInt16,rate=RATE,channels=2,
output=True,
frames_per_buffer=chunk)
while True: #Used to continuously stream audio
try:
data=np.fromstring(stream.read(chunk,exception_on_overflow =
False),dtype=np.int16)
chunk_length = int(len(data)/2)
result = np.reshape(data, (chunk_length, 2))
print(result)
rightchannel=result[:, 1] #I need right
#leftchannel=result[:, 0]
print(rightchannel)
player.write(rightchannel,chunk)
except IOError:
continue
**ERROR**>>>>
[[185 179]
[183 175]
[190 197]
...,
[156 156]
[149 144]
[145 146]]
[179 175 197 ..., 156 144 146]
Traceback (most recent call last):
File
"/home/pi/RKR_MainAudioBase/Audio_GUI_RKR/MicFuncFiles/recorder.py",
line 25, in <module>
player.write(rightchannel,chunk)
File "/usr/lib/python3/dist-packages/pyaudio.py", line 586, in write
exception_on_underflow)
ValueError: ndarray is not C-contiguous
答案 0 :(得分:0)
经过大量研究和发现,我得出的解决方案:
当您将输入数组通道设为1时,它起作用。输入数据= [1 2 3 4 5 6]例如,从流中读取此单声道,以使立体声仅使输出播放器channel = 2并将数据输入dataout = [1 1 2 2 3 3 4 4 5 5 6 6]此格式为[L0 R0 L1 R1 ... etc]。然后是“ player.write(dataout,chunk)”。
要控制右声道,请将第2、4、6等位置元素设为0 ex:dataout = [1 0 2 0 3 0 4 0 5 0 6 0] 并为< strong>左声道将第1、3、5个元素设为0。dataout = [0 1 0 2 0 3 0 4 0 5 0 6]
这工作得很好!!!!