在 Raspberry Pi(Python 和套接字)上播放 .wav 文件时出现问题
Having issues playing a .wav file on Raspberry Pi (Python & Socket)
所以我的代码是通过引用 and this post 来构建的,用于 wave 文件发送,这似乎是关于该主题的最流行的 Whosebug 帖子,但我似乎无法解决问题。所以,我的代码可以发送音频文件,但在 Pi 上播放时只有一半时间有效。首先,一些细节:
我正在尝试将在我的笔记本电脑上生成的 .wav 音频文件发送到 Raspberry Pi 运行 最新的默认设置 Raspberry Pi OS,然后通过连接的 USB 扬声器播放音频。
我发现它实际上是我用来播放音频的代码。有时它播放整个文件,但有时它只播放四分之一或最后几秒。我该如何解决这个问题?
我尝试了多种不同的音频播放器,例如 pygame、pyttsx3 和下面显示的播放器,这段代码在所有播放器中效果最好,即使有错误。
这是我的代码:
注意:服务器在树莓派上,我的电脑作为客户端发送.wav到服务器
服务器:
def audio_reciever(): # this is the audio receiver
port = 65432 # Reserve a port for your service.
s = socket.socket() # Create a socket object
host = 'this is the pi ip'
chunk = 1024
# Get local machine name
s.bind((host, port))
f = open('voice.wav', 'w+b')
# Bind to the port
s.listen() # Now wait for client connection.
while True:
conn, addr = s.accept() # Establish connection with client.
l = conn.recv(1024)
while (l):
f.write(l)
l = conn.recv(1024)
f.close()
conn.send(b'Audio_Recieved')
break
conn.close()
return
Pi 的音频播放器:
def audio_player():
chunk = 1024
wf = wave.open('voice.wav', 'rb')
# create an audio object
p = pyaudio.PyAudio()
# open stream based on the wave object which has been input.
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# read data (based on the chunk size)
data = wf.readframes(chunk)
# play stream (looping from beginning of file to the end)
while data != b'':
# writing to the stream is what *actually* plays the sound.
stream.write(data)
data = wf.readframes(chunk)
stream.stop_stream() # "Stop Audio Recording
stream.close() # "Close Audio Recording
p.terminate() # "Audio System Close
wf.close()
return
客户:
def audio_sender(self,command):
command = "command goes here"
print(command)
engine.save_to_file(command, 'voice.wav')
engine.runAndWait()
engine.stop()
arr = array('B') # create binary array to hold the wave file
result = stat("voice.wav") # sample file is in the same folder
f = open("voice.wav", 'rb')
arr.fromfile(f, result.st_size) # using file size as the array length
HOST = 'this is the pi ip'
PORT = 65432
print("actually sending")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.send(arr)
print('Finished sending...')
s.close()
f.close()
os.remove('voice.wav')
print('done.')
如果您能帮助我弄清楚为什么它有时会中断,我将不胜感激,因为我需要这段代码每次都能按预期工作,因为需要为个人项目重复调用这段代码。还有,有什么办法可以让小派播放音频文件的速度稍微慢一点吗?
感谢您的宝贵时间。
我最终使用 PyGame 模块来播放音频。这是代码:
def playSound(filename):
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
def audio_player():
pygame.init()
playSound('voice.wav')
while pygame.mixer.music.get_busy() == True:
continue
return
所以我的代码是通过引用
我正在尝试将在我的笔记本电脑上生成的 .wav 音频文件发送到 Raspberry Pi 运行 最新的默认设置 Raspberry Pi OS,然后通过连接的 USB 扬声器播放音频。
我发现它实际上是我用来播放音频的代码。有时它播放整个文件,但有时它只播放四分之一或最后几秒。我该如何解决这个问题?
我尝试了多种不同的音频播放器,例如 pygame、pyttsx3 和下面显示的播放器,这段代码在所有播放器中效果最好,即使有错误。
这是我的代码:
注意:服务器在树莓派上,我的电脑作为客户端发送.wav到服务器
服务器:
def audio_reciever(): # this is the audio receiver
port = 65432 # Reserve a port for your service.
s = socket.socket() # Create a socket object
host = 'this is the pi ip'
chunk = 1024
# Get local machine name
s.bind((host, port))
f = open('voice.wav', 'w+b')
# Bind to the port
s.listen() # Now wait for client connection.
while True:
conn, addr = s.accept() # Establish connection with client.
l = conn.recv(1024)
while (l):
f.write(l)
l = conn.recv(1024)
f.close()
conn.send(b'Audio_Recieved')
break
conn.close()
return
Pi 的音频播放器:
def audio_player():
chunk = 1024
wf = wave.open('voice.wav', 'rb')
# create an audio object
p = pyaudio.PyAudio()
# open stream based on the wave object which has been input.
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# read data (based on the chunk size)
data = wf.readframes(chunk)
# play stream (looping from beginning of file to the end)
while data != b'':
# writing to the stream is what *actually* plays the sound.
stream.write(data)
data = wf.readframes(chunk)
stream.stop_stream() # "Stop Audio Recording
stream.close() # "Close Audio Recording
p.terminate() # "Audio System Close
wf.close()
return
客户:
def audio_sender(self,command):
command = "command goes here"
print(command)
engine.save_to_file(command, 'voice.wav')
engine.runAndWait()
engine.stop()
arr = array('B') # create binary array to hold the wave file
result = stat("voice.wav") # sample file is in the same folder
f = open("voice.wav", 'rb')
arr.fromfile(f, result.st_size) # using file size as the array length
HOST = 'this is the pi ip'
PORT = 65432
print("actually sending")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.send(arr)
print('Finished sending...')
s.close()
f.close()
os.remove('voice.wav')
print('done.')
如果您能帮助我弄清楚为什么它有时会中断,我将不胜感激,因为我需要这段代码每次都能按预期工作,因为需要为个人项目重复调用这段代码。还有,有什么办法可以让小派播放音频文件的速度稍微慢一点吗?
感谢您的宝贵时间。
我最终使用 PyGame 模块来播放音频。这是代码:
def playSound(filename):
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
def audio_player():
pygame.init()
playSound('voice.wav')
while pygame.mixer.music.get_busy() == True:
continue
return