Time to mix it up

Your best friend is an ultra hip DJ with a problem: his vinyl collection is now so large that he can't carry it all around from club to club anymore. He's decided to digitize his entire collection, put it on his laptop, and mix his sounds from there. His problem is that commercial mixing software costs a fortune and he doesn't like any of the free alternatives. He has his own ideas for the mixing software he wants.

What with spending all his time mixing music, he's never learned how to program his computer... and that's where you come in. If you help him to write the software he needs, he promises to showcase your work at the upcoming World Music Mixing Expo.

Let's build the mixing software bit-by-bit based on the DJ's requirements.

I'm dumping vinyl and going digital. Can you help me write my software? I need to be able to start and stop a track.

Code Magnets

A couple of buttons on a GUI ought to do it. Here's the code to a small tkinter program that starts and stops a sound file. Rearrange the code magnets to make the program:

stop_button = Button(app, command = track_stop, text = "Stop") stop_button.pack(side = RIGHT)

app.mainloopQ

def track_stop(): track.stop()

def track_stop(): track.stop()

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

start and stop

Code Magnets Solution

A couple of buttons on a GUI ought to do it. Here's the code to a small tkinter program that starts and stops a sound file. You were asked to rearrange the code magnets to make the program:

from tkinter import * import pygame.mixer app = Tk() app. title("Head First Mix") app.geometry('250x100+200+100' )

„port "the weired libraries.

-Create the GUI application Wmdo*.

Identify the DJ's track. £

Start the sound system.

mixer = pygame.mixer mixer.init()

The "track_stop()" function .. will respond to the "Stop" button-click event.

Create a button for "Start" and "Stop," then ■ I Connect each of them their event handlers.

Start the GUI event loop mixer = pygame.mixer mixer.init()

The "track_stop()" function .. will respond to the "Stop" button-click event.

track = mixer.Sound(sound_file)

The "track_startO" function will respond to the "Start" button-click event.

The "loops = -1" parameter to "playO" peats the track until you stop rt track = mixer.Sound(sound_file)

-Load up the track sound file.

start_button.pack fci^a =

command = track start

LEFT)

start_button.pack fci^a =

command = track start

LEFT)

stop_button = Button(app, command = track stop, text = "Stop") W stop_button.pack(side = RIGHT)

[ app■mainloop(

Download the sound tracks for this chapter from the Head First Programming website. Be sure to put the sound files in the same directory/folder as your code.

Was this article helpful?

0 0

Post a comment