Python Forum
How to change the sound volume with python ?
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change the sound volume with python ?
#1
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.
Reply
#2
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
Reply
#3
Now I'm not sure if I can pull it off at all. This seem very complicated :O
Reply
#4
(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%
Reply
#5
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  :) )
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mute spotify by the mixer of volume garze23 0 249 Feb-27-2024, 05:42 PM
Last Post: garze23
  Image to Sound Python Project ankitdixit 1 2,597 Jul-26-2021, 08:20 PM
Last Post: snippsat
  Real Time Audio Processing with Python Sound-Device not working Slartybartfast 2 3,945 Mar-14-2021, 07:20 PM
Last Post: Slartybartfast
  Play fixed frequency sound in python 3 jpezz 2 2,755 Feb-07-2021, 08:21 PM
Last Post: jpezz
  Python 3.7, Windows 10, pyinstaller, winsound, no sound in executable kmarien 3 3,627 Nov-30-2020, 04:10 PM
Last Post: buran
  Create a 3D volume with some properties. Rosendo 0 1,407 Jul-18-2020, 08:20 PM
Last Post: Rosendo
  Windows Volume Control using python Arun 1 4,726 May-17-2019, 02:50 PM
Last Post: Larz60+
  Working with large volume of data (RAM is not enough) evonevo 6 4,083 Oct-21-2018, 09:24 PM
Last Post: Larz60+
  Change Windows Sound Output Device with Python delfar 1 10,288 Sep-15-2017, 12:11 AM
Last Post: Larz60+
  I want to know how I can determine pitch and volume from my computer microphone. notalentgeek 1 6,856 Nov-16-2016, 08:38 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020