简体   繁体   English

使用 Python 更改 Raspberry Pi 音量

[英]Changing Raspberry Pi volume with Python

Hardware硬件

  • Arduino Uno Arduino 一诺
  • Potentiometer电位器
  • Raspberry Pi 3树莓派 3
  • Raspberry Pi Os树莓派操作系统
  • Hifiberry DAC Pro2海纤DAC Pro2

Setup设置

Potentiometer is running on the Arduino, I'm sending the signal through the USB Serial port to the Raspberry Pi.电位器在 Arduino 上运行,我通过 USB 串行端口向 Raspberry Pi 发送信号。 The Code is complete and working on the Arduino and I'm able to get the signal and print it using a python script on the Pi.代码已完成并可在 Arduino 上运行,我能够获取信号并使用 Pi 上的 python 脚本打印它。

I have remapped the signal range from 0-100 on the Arduino side and I'm looking to use this value to control the volume on the raspberry Pi for a music box I am creating.我已经在 Arduino 端重新映射了 0-100 的信号范围,我希望使用这个值来控制我正在创建的音乐盒的树莓派上的音量。

Issue问题

I'm currently using this answer found here as a guide.我目前正在使用此处找到的答案作为指南。 However, when I run the python script I get the following error in the terminal.但是,当我运行 python 脚本时,我在终端中收到以下错误。

" amixer: Unable to find simple control 'Master',0 " amixer:无法找到简单的控件‘Master’,0

But, when I run the command (amixer scontrols) I show a Master, 0 listed.但是,当我运行命令 (amixer scontrols) 时,我显示了一个 Master,列出了 0。 See below见下文

" pi@raspberrypi:~ $ amixer scontrols Simple mixer control 'Master',0 Simple mixer control 'Capture',0 " " pi@raspberrypi:~ $ amixer scontrols 简单混音器控制 'Master',0 简单混音器控制 'Capture',0 "

Why is the script not seeing the hardware or what am I doing wrong?为什么脚本看不到硬件或我做错了什么? Here is my script in it's current form.这是我当前形式的脚本。 Once I can get the volume to change I'll work on making the loop, I'm new to python and trying to learn something, but I've been stuck on this for a few weeks.一旦我可以改变音量,我就会开始制作循环,我是 python 的新手并试图学习一些东西,但我已经坚持了几个星期。 Any help is greatly appreciated.任何帮助是极大的赞赏。

#import serial adapter library and asla audio library
import subprocess
import serial
import alsaaudio

#Variables
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.flush()

volume = ser
command = ["amixer", "sset", "Master", "{}%".format(volume)]
subprocess.Popen(command)


#Imports serial value and prints to monitor
while True:  
    read_serial=ser.readline()
    print(read_serial)

The problem comes from the 'sudo'问题来自'sudo'

If you try amixer sset Master 100% in you're shell, it's going to work如果您在 shell 中尝试amixer sset Master 100% ,它将起作用

But if you try sudo amixer sset Master 100% , you will have amixer: Unable to find simple control 'Master',0但是如果你尝试sudo amixer sset Master 100% ,你将有amixer: Unable to find simple control 'Master',0

My advice: don't write sudo python volcon.py but rather python volcon.py to execute your code我的建议:不要写sudo python volcon.py而是python volcon.py来执行你的代码

If it doesn't work, or if you really need permissions by sudo, you may use other command.如果它不起作用,或者如果你真的需要 sudo 的权限,你可以使用其他命令。 For example amixer cset numid=1 100%例如amixer cset numid=1 100%

PS: the only thing I'm sure is that amixer sset Master 100% has a different response with a 'sudo', for the solution you can try what I adviced but I'm not sure about the result PS:我唯一确定的是amixer sset Master 100%对“sudo”有不同的响应,对于解决方案,您可以尝试我的建议,但我不确定结果

Edit:编辑:

Please try amixer scontrols and sudo amixer scontrols .请尝试amixer scontrolssudo amixer scontrols If the results are different, select the one from 'sudo...' On my Raspberry, I have如果结果不同,select 来自“sudo ...”的那个在我的树莓派上,我有

Simple mixer control 'Master',0
Simple mixer control 'Capture',0

for the first one and对于第一个和

Simple mixer control 'Headphone',0

for the second one第二个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM