Raspberry pi:从python代码生成和播放音调(使用sox) [英] Raspberry pi: generate and play tone from python code (with sox)
问题描述
我正在使用python 3使用Raspberry,TKinter和sox构建一个简单的GUI. 每次按下GUI中的按钮时,我都想播放一种动态产生的音调.这是代码:
I am building a simple GUI with Raspberry, TKinter, and sox, using python 3. I want to play a tone, generated on the fly, every time a button in the GUI is pressed. Here's the code:
from Tkinter import Tk, Label, Button
import os
class MyFirstGUI:
def __init__(self, master):
self.master = master
master.title("Random Tone Generator")
self.label = Label(master, text="Press Generate and enjoy")
self.label.pack()
self.generate_button = Button(master, text="Generate", command=self.generate)
self.generate_button.pack()
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.pack()
def generate(self):
os.system('play -n -c1 synth 3 sine 500')
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
这是终端到此脚本的呼叫
Here is the call from terminal to this script
sudo python /home/pi/Desktop/soundtest.py
在这里,如果我尝试按"Generate"按钮,就会收到错误消息
And here the error I get if I try to push the button "Generate"
play FAIL formats: can't open output file `default': select_format error: Operation not permitted
如果我尝试相同的命令('play -n -c1 synth 3 sine 500'),它将按预期工作.
If I try the same command ('play -n -c1 synth 3 sine 500') it works as expected.
我搜索了几个小时,然后尝试使用 subprocess 解决方案,该解决方案会返回相同的问题,以及与播放文件有关的解决方案,而我需要当场发出提示音,因为将来它们将是随机生成的.
I searched for few hours now and I tried solutions using subprocess, which give back the same problem, and solutions related to playing files, while I need to generate tones on the spot because in the future they will be randomly generated.
我的问题归结为: 1)为什么在终端中运行的命令在python脚本中不起作用 2)如何使其起作用,以便可以直接从脚本生成音调? 我在某个地方读到了我找不到的东西,可能在从脚本进行调用的过程中可能需要指定音频驱动程序.但是我不知道怎么办.
My question boils down to: 1) why the command that works in the terminal does not work within the python script 2) how do I make it work so that I can generate tones directly from the script? I read somewhere I can't find anymore that one might need to specify the audio driver during the call from the script. But I wouldn't know how.
我安装了HiFiberry DAC + pro声卡,该声卡自动设置为默认声卡(而不是vc4-hdmi)
I have an HiFiberry DAC+ pro sound card installed, which is automatically set to be the default one (instead of the vc4-hdmi)
**** List of PLAYBACK Hardware Devices ****
card 0: vc4hdmi [vc4-hdmi], device 0: MAI PCM vc4-hdmi-hifi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: sndrpihifiberry [snd_rpi_hifiberry_dacplus], device 0: HiFiBerry DAC+ Pro HiFi pcm512x-hifi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
谢谢 蚂蚁
推荐答案
找到了解决方案.
我需要指定要使用的声卡,并且通过更改线路来做到这一点
I needed to specify the sound card to use and I did so by changing the line
os.system('play -n -c1 synth 3 sine 500')
进入此
os.system("AUDIODRIVER=alsa AUDIODEV=hw:1,0 play -n -c1 synth 3 sine 500")
其中 AUDIODEV = hw:1,0 是我的声卡的编号,
aplay -l
where the AUDIODEV=hw:1,0 is the number of my sound card derived from
aplay -l
这篇关于Raspberry pi:从python代码生成和播放音调(使用sox)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!