7

I need to change my volume on my USB sound card (which is the default card) via the command line. I know alsamixer will do that but I need it to be a one line command so I can use it in a python script, e.g., os.system("volume up 1"), or something like that. Is there any way to do this?

goldilocks
60.4k17 gold badges117 silver badges236 bronze badges
asked Oct 9, 2015 at 17:12
1
  • I haven't used it personally, but if you want to do this programmatically there's python bindings for alsa lib. That should be available as a package on Raspbian -- I don't have a Raspbian pi running right now but I'd guess the package is pyalsaaudio (check with apt-cache search alsa | grep python). That will mean reading the API docs a bit, so not as easy as os.system(). Commented Oct 9, 2015 at 17:49

1 Answer 1

5
  1. Create a Bash file with the following code:

    amixer scontrols
    amixer sset 'Master' 50%
    
  2. Make the Bash file executable using chmod +x filename

  3. In Python, use the following:

    import os
    os.system("directorytobashfile/file")
    
Bex
2,9293 gold badges27 silver badges34 bronze badges
answered Oct 9, 2015 at 17:17
5
  • alsamixer is a GUI, not a one line command to change volume. Commented Oct 9, 2015 at 17:27
  • @PatrickCook I think I got alsamixer mixed up with amixer Commented Oct 9, 2015 at 17:33
  • @PatrickCook added the correct code for alsamixer Commented Oct 9, 2015 at 17:37
  • amixer scontrols just prints the list of controls. And why don't you execute amixer directly? Commented Oct 10, 2015 at 8:09
  • @CL. Sorry I just do that to make sure I have the right command. I don't execute it directly because this way he can add to the bash file. You could do it directly too! Commented Oct 10, 2015 at 15:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.