Raspberry pi:从 python 代码生成和播放音调(使用 sox) [英] Raspberry pi: generate and play tone from python code (with sox)

查看:49
本文介绍了Raspberry pi:从 python 代码生成和播放音调(使用 sox)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Raspberry、TKinter 和 sox 构建一个简单的 GUI,使用 python 3.每次按下 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

这里是我尝试按下生成"按钮时得到的错误

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆