Tkinter slider not properly controlling Pygame channel volume

Question:

In my code, I am using the Pygame mixer to play different parts simultaneously, and I am testing adding a slider to only one of the tracks. I used the code below, but the volume is constant throughout the slider, only turning off when it hits zero. How do I make it that the volume declines gradually?

# Things to note
from tkinter import *
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from pygame import mixer

window = Tk()

# Pygame mixer code    
mixer.init()
mixer.set_num_channels(5)

channel1 = mixer.Channel(0)
channel1Sound = mixer.Sound("bass.wav")
channel1.play(channel1Sound)

channel2 = mixer.Channel(1)
channel2Sound = mixer.Sound("drums.wav")
channel2.play(channel2Sound)

channel3 = mixer.Channel(2)
channel3Sound = mixer.Sound("other.wav")
channel3.play(channel3Sound)

channel4 = mixer.Channel(3)
channel4Sound = mixer.Sound("piano.wav")
channel4.play(channel4Sound)

channel5 = mixer.Channel(4)
channel5Sound = mixer.Sound("vocals.wav")
channel5.play(channel5Sound)

# Slider code
def volume(x):
    channel5.set_volume(vocalsSlider.get())

vocalsSlider = Scale(window, from_ = 0, to=100, orient=HORIZONTAL, command=volume)
vocalsSlider.set(100)
vocalsSlider.pack()
Asked By: Matthew McDermott

||

Answers:

Per pygame documentation on set_volume:

The value argument is between 0.0 and 1.0.

Divide the value from get() by the max to have a float that will work.

channel5.set_volume(vocalsSlider.get()/100.0)

Answered By: Abel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.