11-28-2024, 12:37 AM
I have a Pi Zero 2W with a USB DAC sending an analogue audio signal to a Max9744 amplifier which then amplifiers the audio signal to power some 20W speakers; the max9744 is connected to the Pi via I2C and can provide digital volume control.
I have found from trial and error that my best sound quality comes when the Pi is outputting 100% volume and then I adjust volume through I2C to adjust the amplifier.
Below is my python code (thanks AI for writing it!) that lets me adjust volume by: "python SetAmpVolume.py vol" where "vol" is the volume.
Is there some means for me to have moode use this command to execute volume changes and keep mpc volume 100?
I tried editing vol.sh but it didn't work (thanks again for AI helping me find that file and trying this) - is there some master call to setting volume somewhere? Is this shenanigans possible?
On the plus side, I'm loving the built in rotary encoder feature for volume - worked first time! Alas it changes mpc volume and not via i2c to a max9744
I have found from trial and error that my best sound quality comes when the Pi is outputting 100% volume and then I adjust volume through I2C to adjust the amplifier.
Below is my python code (thanks AI for writing it!) that lets me adjust volume by: "python SetAmpVolume.py vol" where "vol" is the volume.
Is there some means for me to have moode use this command to execute volume changes and keep mpc volume 100?
I tried editing vol.sh but it didn't work (thanks again for AI helping me find that file and trying this) - is there some master call to setting volume somewhere? Is this shenanigans possible?
On the plus side, I'm loving the built in rotary encoder feature for volume - worked first time! Alas it changes mpc volume and not via i2c to a max9744
Code:
import smbus
import sys
# Initialize I2C bus
bus = smbus.SMBus(1)
MAX9744_ADDR = 0x4B # I2C address of MAX9744
def set_volume(volume):
"""Set volume level on MAX9744 (0-63)"""
if volume < 0:
volume = 0
elif volume > 63:
volume = 63
try:
bus.write_byte(MAX9744_ADDR, volume)
return True
except:
print("Error communicating with MAX9744")
return False
# Check if volume was provided as command line argument
if len(sys.argv) > 1:
try:
volume = int(sys.argv[1])
if 0 <= volume <= 63:
if set_volume(volume):
print(f"Volume set to {volume}")
else:
print("Failed to set volume")
else:
print("Volume must be between 0 and 63")
except ValueError:
print("Please provide a valid number between 0 and 63")
else:
# Get volume input from user if no command line argument
while True:
try:
volume = input("Enter volume level (0-63) or 'x' to exit: ")
if volume.lower() == 'x':
sys.exit()
volume = int(volume)
if 0 <= volume <= 63:
if set_volume(volume):
print(f"Volume set to {volume}")
break
else:
print("Failed to set volume")
else:
print("Volume must be between 0 and 63")
except ValueError:
print("Please enter a valid number")