[英]Raspberry pi: generate and play tone from python code (with sox)
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. 我正在使用python 3用Raspberry,TKinter和sox构建一个简单的GUI。我想每次按下GUI中的按钮时都会播放一种动态生成的音调。 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
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
If I try the same command ('play -n -c1 synth 3 sine 500') it works as expected. 如果我尝试相同的命令(“ play -n -c1 synth 3 sine 500”),则可以正常运行。
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. 我搜索了几个小时,然后尝试使用subprocess解决方案,该解决方案会返回相同的问题,以及与播放文件有关的解决方案,而我需要当场生成音调,因为将来会随机生成这些音调。
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? 我的问题归结为:1)为什么在终端中运行的命令在python脚本中不起作用2)我如何使其工作以便可以直接从脚本生成音调? 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. 但是我不会。
EDIT: I have an HiFiberry DAC+ pro sound card installed, which is automatically set to be the default one (instead of the vc4-hdmi) 编辑:我安装了一个HiFiberry DAC + pro声卡,它自动设置为默认声卡(而不是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
Thanks Ant 谢谢蚂蚁
Found the solution. 找到了解决方案。
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')
into this 进入这个
os.system("AUDIODRIVER=alsa AUDIODEV=hw:1,0 play -n -c1 synth 3 sine 500")
where the AUDIODEV=hw:1,0 is the number of my sound card derived from aplay -l
其中AUDIODEV = hw:1,0是我从aplay -l
导出的声卡的编号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.