raspberry pi python 如何实现左喇叭播放音乐,右喇叭播放麦克风声音?
How to play music in left speaker and microphone voice in right speaker with raspberry pi python?
我有一个 USB 麦克风和 raspberry pi。这是我正在尝试做的事情。
我有“music.wav”文件,其中音轨设置为左声道,并且我将 USB 麦克风连接到 raspberry pi。
当我播放“music.wav”并用麦克风说话时,我可以听到左扬声器的音乐和左右扬声器的麦克风声音。
我尝试了下面的代码并尝试了不同的方法,无法实现将声音限制在右扬声器中。
有人可以帮助我如何使用 python 语言限制 仅左扬声器 的音乐和 仅右扬声器 的声音?
下面是来自两个扬声器的麦克风声音的代码。
只需要限制在右边speaker.Please求助!!
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
更新:
经过多次尝试,我执行了下面的 code:Now 声音不清晰,我仍然进入两个扬声器...我还更改了“播放器”——输出通道为 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
我经过多次研究和发现得到的解决方案:
当您将输入数组通道设为 1 时,它正在工作,输入数据=[1 2 3 4 5 6] 例如,从流读取的单声道,要制作立体声只需使输出播放器通道=2 并制作data into dataout=[ 1 1 2 2 3 3 4 4 5 5 6 6] 这种格式也就是[L0 R0 L1 R1...etc]。然后是“player.write(数据输出,块)”。
并控制右声道使每个第2、4、6等位置元素为0 ex:dataout=[ 1 0 2 0 3 0 4 0 5 0 6 0] 和 左声道使第 1、3、5 个元素为 0。 dataout=[ 0 1 0 2 0 3 0 4 0 5 0 6]
这工作得很好!!!!
我有一个 USB 麦克风和 raspberry pi。这是我正在尝试做的事情。
我有“music.wav”文件,其中音轨设置为左声道,并且我将 USB 麦克风连接到 raspberry pi。
当我播放“music.wav”并用麦克风说话时,我可以听到左扬声器的音乐和左右扬声器的麦克风声音。
我尝试了下面的代码并尝试了不同的方法,无法实现将声音限制在右扬声器中。
有人可以帮助我如何使用 python 语言限制 仅左扬声器 的音乐和 仅右扬声器 的声音?
下面是来自两个扬声器的麦克风声音的代码。 只需要限制在右边speaker.Please求助!!
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
更新: 经过多次尝试,我执行了下面的 code:Now 声音不清晰,我仍然进入两个扬声器...我还更改了“播放器”——输出通道为 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
我经过多次研究和发现得到的解决方案:
当您将输入数组通道设为 1 时,它正在工作,输入数据=[1 2 3 4 5 6] 例如,从流读取的单声道,要制作立体声只需使输出播放器通道=2 并制作data into dataout=[ 1 1 2 2 3 3 4 4 5 5 6 6] 这种格式也就是[L0 R0 L1 R1...etc]。然后是“player.write(数据输出,块)”。
并控制右声道使每个第2、4、6等位置元素为0 ex:dataout=[ 1 0 2 0 3 0 4 0 5 0 6 0] 和 左声道使第 1、3、5 个元素为 0。 dataout=[ 0 1 0 2 0 3 0 4 0 5 0 6]
这工作得很好!!!!