Java Lecture 15

(Miami) Sound Machine
Do the Conga...or whatever
Lab09
• A hint about randomly selecting files for playback
• In lab09 you are required to randomly select between
19 different files
• They have been labelled “synthesizer*.wav”
• * is a number between 1 and 19
Lab Example
• Here you are given a choice between two songs
• The program will play whatever song that you have
chosen
• This is simply a case of testing two different boxes
separately
Test 1 ══►
Test 2 ══►
Piano Keys
• Life becomes more difficult when you need to draw
many more buttons
• In the case of piano keys, it would be silly to test each
key individually
• Fortunately, the keys are related in a particular way
• Although the relationship is not exactly simple...
Piano Keys
• If we look at the white and black keys separately, this
can make life a little easier
• There are five black keys, with a few “missing keys”
• The white keys are easy, they are just one after the
other
• First we should define variables for the location of our
piano keys
Stays the same
Changes
Playing music
• How would we go about making a noise when the user
clicked a piano key???
• Obviously we would test to see if the user had clicked
in the rectangle
• Since we have just drawn them, we can certainly test if
the mouse has been clicked in one of them
• But there are some problems to be dealt with...
Problems
Problems
• The black keys overlap the white keys
– What happens if the user clicks on a black key?
– How do we fix this?
• The sounds are suppose to be in increasing
order, but the keys are drawn separately
– The most obvious way to select a filename would
be to use “counter” and label files in order
– But the black keys are a problem
Problems
Solutions
The black keys overlap the white keys
• If the user HAS clicked on a black key, use
a boolean variable to signal this
• Do not process mouse clicks if this
variable is true
• Use an IF statement
Problems
Solutions
The sounds are suppose to be in increasing
order, but the keys are drawn separately
• Re-number/rename the files so we can just
use: “synthesizerBlack”+counter+”.wav”
• Use some complicated mathematical formula
to work out which number to use
• Use many IF statements to work it out
Playing Music with SoundMachine
• Playing midi/wav files with the
SoundMachine is easy – it was designed that
way
• However, designing a GUI for users is not
• Especially if we are doing things the hard
way
• We will start with simply getting play
buttons on the screen
Loading a midi file
• Loading a midi file is easy, much like a wav file
SoundMachine.play(“filename.mid”);
SoundMachine.play(“filename.kar”);
This will load and play a midi file
Staring and Stopping a midi file
• Not surprisingly…
SoundMachine.startMidi();
SoundMachine.stopMidi();
• To tell if a midi file is currently playing…
SoundMachine.isMidiPlaying();
This will return us a boolean
Interesting way to see if
the click was a dupe
Very bad code!!
Draw the GUI
testing for clicks
Et Tu Tempo
• Tempo is measured in beat per minute, just like
heart rate
• The average song is about 120
• Changing and displaying tempo are trivial...
Changing the temp of a song
• To tell the current tempo of a song
SoundMachine.getMidiTempo();
• To change the tempo
SoundMachine.setMidiTempo(double);
NB: Tempo is given as a double
Two lines of code??
Some Change is Good
• Ideally, we want faster/slower buttons for
out tempo changes
• In a real application you would use a slider
• How would you implement a “ghetto
slider” using only 111 code???
Set up GUI
Check for clicks...
Display – must come last...why??
Tracking Software
• Muting Tracks is quite hard
• You have too many to deal with
• However, they are MUCH easier than keys
on a keyboard!
• If you can do keys, you can do tracks
Altering Midi Tracks
• To find out how many tracks a midi song
has
SoundMachine.getNumberOfMidiTracks();
Muting Midi
• Muting a particular
SoundMachine.muteMidiTrack(int);
NB: Muting a track has a toggle (on/off) effect!
• To check if a track is muted…returns boolean
SoundMachine.isTrackMuted(int);
Loop for every track
Set up variables
Draw box...
Draw text...just a number
Clicked?
Don’t forget counter