Pump up the volume

To make the program more useful, the DJ wants to be able to dynamically and interactively control the volume of the track as it plays. Basically, as the track plays, the DJ wants to fiddle with the volume.

To make the program more useful, the DJ wants to be able to dynamically and interactively control the volume of the track as it plays. Basically, as the track plays, the DJ wants to fiddle with the volume.

In the physical world, most devices that have a volume control provide either a large, circular knob or a slider on a scale:

Because a slider is much easier to manipulate with a click and drag of a mouse than a knob, the slider on a scale is used in lots of modern GUIs. It's the classic choice for showing a volume control.

-<*• the Control look like this, 'w™ >i up to the fft down t, the

-<*• the Control look like this, 'w™ >i up to the fft down t, the

Look closely at the slider. What do you need to model?

Model a slider on a scale

There's quite a bit going on with a slider on a scale. It's a simple control that everyone's familiar with, but the simplicity masks quite a bit of complexity. The devil is most definitely in the details:

Here's the standard representation of a slider on a scale that we all know and love-

There's a bunch of markings on the device that indicate where on the scale you currently are-

This end of the stale av-ks a

There's a bunch of markings on the device that indicate where on the scale you currently are-

]We is always something that you ¿an grab onto a»d slide fvom left to right

parpen your pencil

Write your four answers here-

Look at the volume control shown above and identify four key characteristics of the volume control.

sound before beauty

You were to look at the volume control on the previous page and identify four key characteristics of the volume control.

1. There's a scale that goes from a low value to a high value.

2. The scale has a fixed set of intervals.

3. The volume control "slider" moves from left to right.

4. Moving the "slider" dynamically adjusts the volume based on its current position on the scale.

Start with the volume

Before you start worrying about creating the appropriate GUI interface element to actually model the slider, you first need to know how to adjust the volume of a track.

Once you know how to adjust the volume, you can then start to worry about linking the volume to the slider, with the current position of the slider dictating the current volume setting.

Then you can allow your user to move the slider which has the effect of dynamically and interactively adjusting the volume.

Sounds easy, eh?

Use pygame to set the volume

Turns out pygame has this functionality built right into its library code via the set volume () method.

Take a look at this small example program:

import pygame.mixer from time import sleep mixer = pygame.mixer mixer.init()

track = mixer.SoundC50459_M_RED_Nephlimizer.wav") print("Play it LOUD, man!") track.play(loops = -1)

track.set_volume(0.9) tf setting sleep(2)

print("Softly does it ... ") track.set volume(0.1) 4-- Set the volume to sleep(2) track.stop()

a very low setting.

Louder, dude, loudev/

Tuvn that racket down!

Louder, dude, loudev/

When you set the track's volume to a high value using set volume (), it's the equivalent of cranking up the volume by moving the slider to the right. When you set it to a low value, that's like moving the slider to the left.

Tuvn that racket down!

create the slider

Use tkinter for everything else

The tkinter library has a graphical interface element called Scale that lives to help you create a slider. Take a look at this example code and let's see how it works:

Create a tkinter DoubleVar variable- Like IntVar and Str ingVar, the DoubleVar variable stores a "magic" value, this time a floaty

The tkinter Scale Widget creates a -> slider-

volume = DoubleVar() volume scale = Scale(app, variable

The slider can be linked to a m agic" variable-

= volume, from_ = 0.0, to = 1.0, resolution = 0.1, command = change volume, label = "Volume", orient = HORIZONTAL) volume scale.pack(side = RIGHT)

Specify the lowest and -the HIGHEST values on the scale-

Provide a nice, human-friendly label for the slider-

The slider can be linked to a m agic" variable-

Specify the resolution value, which indicates the interval.

Connect the widget to an event handler.

Specify the resolution value, which indicates the interval.

Connect the widget to an event handler.

|ndicate whether the slidev runs across the screen (HORIZONTAL) or up the screen (VERTICAL).

|ndicate whether the slidev runs across the screen (HORIZONTAL) or up the screen (VERTICAL).

The Scale () element is your most complex tkinter widget yet. But, despite this, it is not hard to work out what's going on here. The graphical interface element is linked to a tkinter DoubleVar (called variable), the lowest/ highest slider values are provided (to and from_), and an interval between them (resolution) is specified. The event handler is assocated with an event handler (command), a descriptive label is supplied (label), and, finally, the orientiation of the slider is specified (orient). There's a lot going on here, but none of it is that hard to understand.

ijiereisre no

Dumb Questions

Q/ The Scale() code on the previous page has a variable called from_, that is, the word "from" together with a trailing underscore. Was that a typo?

A" No, it wasn't a tyop, eh, typo. The reason for the underscore has to do with the fact that Python uses "from" as a reserved word. These are words that Python uses for its own special purposes, which means you cannot name one of your variables after a reserved word. As using the word "from" makes a lot of sense when taking about a scale, the authors of tkinter decided to tack on the underscore so that the meaning of the variable would be clear, while allowing for the variable name not to clash with a reserved word.

Are there other reserved words?

A" Yes, a few. And every programming language has its own list. In Python, words like "while", "for", "if", "def", "class", and "pass" are all reserved words.

What happens if I use one anyway?

A." Python will most likely complain with a syntax error.

Where can I find a list?

A" Any good Python reference book will contain a list, and it's also included as part of the Python documentation that's installed with Python on your computer and available on the web at the main Python website.

Q/ How do I know which graphical interface element to use and when?

A." This is really a matter of experience. However, a lot of platforms go to great lengths to specify exactly when each of the elements should be used and for what purpose. Of them all, the Macintosh is the leader of the pack. Apple's engineers have worked hard to strictly enforce consistent usage of the Mac GUI among programmers.

Q/ So, it's a case of anything goes with the other operating systems?

A." No. That's not what we are saying. The Apple folks are very strict with their rules and regulations, and the others are less so. But there are still standards that you should try as much as possible to adhere to. One of the reasons for using a GUI is that your users will expect your program to work in a standard way. This makes your program immediately familiar to new users and lets them become productive with your program more quickly.

Q/ So there are no badly designed GUI apps?

A." No. There are plenty of howlers out there... and they tend to be harder to use than necessary, due to the fact that the programmers responsible for creating them did not conform to established interface standards and practices. When it comes to GUI programs, conformance is a good thing.

Q/ So does tkinter work well on all platforms?

A." The latest version of tkinter (which comes with Python 3) is pretty good. If you run your tkinter program on a Mac, it looks like a Mac OS X program, whereas on Windows it looks like a Windows application, and on Linux it takes on the look and feel of the graphical environment you happen to be using (there are a few choices on Linux).

Q/ Other than tkinter, what other graphical toolkits does Python support, and should I learn any of them?

A." Python supports lots of other toolkits on lots of operating systems. For now, tkinter is all you really need, and you shouldn't worry about the other choices until you are in a situation where learning how to use them becomes a necessity.

control the volume

1oh£ ExiddSe

Take the pygame and tkinter code and combine it to support a volume control. Then, complete the next version of your program.

from tkinter import * import pygame.mixer app = Tk()

app.title("Head First Mix") app.geometry('250x100+200+100')

sound_file = "50459_M_RED_Nephlimizer.wav"

mixer = pygame.mixer mixer.init()

def track toggle():

track.stop()

Add a function here that the track currently plays at.

that irrfle™en-b the volwc ¿ontvol hev-e-

track = mixer.Sound(sound file) track playing = IntVar() track button = Checkbutton(app, track button.pack(side = LEFT)

variable = track_playing, command = track_toggle, text = sound file)

def shutdown(): track.stop() app.destroy()

app.protocol("WM_DELETE_WINDOW", shutdown) app.mainloop()

volume controlled m

Exercise

You were to take the pygame and tkinter code and combine it to support a volume control, then complete the next version of your program.

from tkinter import * import pygame.mixer app = Tk()

app.title("Head First Mix") app.geometry('250x100+200+100')

Things are starting to —"J

get Crowded on the GUI so let's have tkinter automatically decide on the geometry for us. Remove the "app.. geometryO" call from the code. mixer. init ()

sound_file = "50459_M_RED_Nephlimizer.wav"

mixer = pygame.mixer def track toggle():

track.stop()

Put the pygame code s

tratk.set_volume(volumeget())

track = mixer.Sound(sound file) track playing = IntVar()

track button = Checkbutton(app, variable = track playing, command = track_toggle, text = sound file)

track button.pack(side = LEFT)

put the tkintev--^ volume = DoubleVarO

volumeset(track.get_volume())

volume_scale = Scale(variable = volume, from_ = 0-0, to = 1-0, resolution = 0-1, command = change_volume, label = "Volume", orient = HORIZONTAL) volume_scale-pack(side = RIGHT)

def shutdown(): track.stop() app.destroy()

app.protocol("WM_DELETE_WINDOW", shutdown) app.mainloop()

Was this article helpful?

0 0

Post a comment