Python Forum
How to change the sound volume with python ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to change the sound volume with python ? (/thread-215.html)



How to change the sound volume with python ? - Lightningwalrus - Sep-30-2016

Hello, I'm new here !

My hobby is microcontrollers and now I'm doing a project where I could change the sound volume with buttons on my breadboard. I tried a program which can open a website by pushing a button on the breadboard which I took from the book that I read.


Here is that program from the book called "Make: AVR programming" by Elliot Williams

## Simple demo
## Sits forever listening to serial port
## When you press button, opens website of your choosing.
## Extend this to many buttons and you'll have a physical
##  web-launcher.  

BOSS_SITE = "http://animal-dream.com/data_images/badger/badger1.jpg"
## or perhaps more topical...
XKCD = "http://xkcd.com/353/"

SERIAL_PORT = "COM5"
BAUD_RATE = 9600

import serial
import webbrowser

sp = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout = 5)
sp.flush()
print ("Boss Button")

while(1):                       # Sit and wait forever
    response = sp.read(1)       # get one byte
    if response == "O":
        print "Got OK Byte.  Waiting for button press."
    elif response == "X":
        print "Got Boss Byte!  Alarm!"
        webbrowser.open(BOSS_SITE)
    else:
        print "Got nothing.  Still waiting."
How do I change this code so I can change the sound volume on my computer ? I use windows 7.


RE: How to change the sound volume with python ? - nilamo - Sep-30-2016

This isn't as simple as you might initially assume. Each audio device has it's own volume setting, and you need to set the volume level for each device individually. You could accomplish this using ctypes to interact with the Windows API, or you could use something like this: https://github.com/AndreMiras/pycaw


RE: How to change the sound volume with python ? - Lightningwalrus - Sep-30-2016

Now I'm not sure if I can pull it off at all. This seem very complicated :O


RE: How to change the sound volume with python ? - snippsat - Sep-30-2016

(Sep-30-2016, 07:33 PM)Lightningwalrus Wrote: How do I change this code so I can change the sound volume on my computer ? I use windows 7.
Now I'm not sure if I can pull it off at all. This seem very complicated :O
The library nilamo posted  should solve it.
Test controlling volume on my laptop Win-10.
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume

devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
   IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))

# Control volume
#volume.SetMasterVolumeLevel(-0.0, None) #max
#volume.SetMasterVolumeLevel(-5.0, None) #72%
volume.SetMasterVolumeLevel(-10.0, None) #51%



RE: How to change the sound volume with python ? - sparkz_alot - Sep-30-2016

You might try pywin32, it offers a lot of hooks to the windows API (as an example, I used it to access the 'speech' in the 'text to speech' feature  :) )


RE: How to change the sound volume with python ? - wavic - Sep-30-2016

Sound of what? The system sound volume, a running application volume?


import pyglet

player = pyglet.media.Player()
source = pyglet.media.load('DJ ODAWA Tokio - Innovaderz remix.mp3', streaming=False)
player.queue(source)
player.play()
player.volume = 0.2
player.volume = 0.5
Seems I didn't scroll the whole post. Mine is not related to the question