Changing volume in pygame.mixer (almost) no effect

Meaty picture Meaty · Mar 26, 2017 · Viewed 17.1k times · Source

I'm trying to make an application in tkinter that has a number of buttons you can assign sound on and play it later. The click of the button itself only calls play() method, so loading of the sound is done beforehand.

I tried making some kind of volume control with sliders (tk.Scale) and I noticed there is no noticeable difference between most volume values until I get very close to zero (take into consideration that slider resolution is 0.01 from 0.0 to 0.1). At around 0.02 I think I notice the sound volume is significantly lower and if I get to zero, the sound is muted. Please note that this happens if I move the slider while no sounds are playing.

The interesting thing is, if I try playing a sound that is long enough to let me move the slider while it's playing, I can notice the difference right away, but if the sound stops playing and I try playing it again, it goes to the "default" volume again.

Since I divided my application into multiple scripts according to what they do (recording sound, playing sound, GUI) I thought it could be the problem that I haven't directly initialized pygame mixer, but rather from the imported module, so I made a new python script and typed this code in:

import pygame
import time
pygame.mixer.pre_init(frequency=44100, size=-16, channels=1, buffer=512)
pygame.mixer.init()

sound1=pygame.mixer.Sound("sound.wav")
sound1.set_volume(1.0)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())
sound1.set_volume(0.5)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())
sound1.set_volume(0.08)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())

The output is the following: 1.0,0.5,0.078125 (one below the other) confirming that the volume has indeed been set (I hope properly).

The only time I can notice the difference is the third case, which is not that noticeable really, I want the volume increase to be linear, this is far from it.

I tried the same thing with a channel:

sound1=pygame.mixer.Sound("sound.wav")
channel=pygame.mixer.find_channel(True)
channel.set_volume(1.0)
channel.play(sound1)
time.sleep(sound1.get_length()/2)
channel.set_volume(0.5)
print "Volume set"
time.sleep(sound1.get_length()/2)

No luck, the same thing happens here too.

I spent all day googling "pygame mixer volume problem" "pygame mixer volume set problem" and similar phrases, but no luck. Hopefully someone here can be of help, considering my diploma depends on a python method. :)

Thanks in advance.

Answer

Meaty picture Meaty · Mar 26, 2017

I found the answer (thank you Gummbum from PyGame IRC).

The problem is not in Python or Pygame itself, but rather in Windows. It seems sound enhancements are somehow fiddling with the way the sound my script is playing (or any other Pygame script for that matter).

I'm on Windows 10 and this is how I did it:

  1. Right click on the speaker icon in the taskbar

  2. Select Playback Devices

  3. Select Speakers and Properties
  4. Go to Enhancements tab and uncheck Equalizer and Loudness Equalization

That's it.