Overview
const NumKeys = 13; short KeyWidth;are made before main. They are globals and are available to all the functions in the program. KeyWidth should be computed in main after the windows are opened.
SoundChannel ChannelA;is declared in the body of PlayKeyboard as it is the only function that plays notes.
Your job is to add the bodies of the four functions:
void DrawKeyboard(); // Draw the keyboard in the Graphics window bool Done(short x, short y); // True if (x, y) is on QUIT box bool OnKeys(short x, short y); // True if (x, y) is on keys void PlayKeyboard(); // Play notes until Quit is selected
(1) Compute KeyWidth so that the keys are as wide as possible given NumKeys and the horizontal bounds on the Drawing Window: DrawMinX, DrawMaxX.
The variables
DrawMinX, DrawMaxX, DrawMinY, and DrawMaxYare defined in SWindows.h and their values are set automatically when a drawing window is opened.
(2) Implement the DrawKeyboard function.
(2a) Draw the keys.
Use a for-statement to draw the keys.
First draw all the keys white to make sure you have the right number of keys and
that they don't overlap.
Add an if-else
-statement to decide whether a key is white or black.
See the What You Need to Know about Music
section above.
Use FrameRect for the white keys and PaintRect for the black keys.
(2b) Write "QUIT" in the Quit box.
Use MoveTo to get to the center of the Quit box then call:
ShowString ("QUIT", Center); // The word "QUIT" will be centered // about the starting point.
(4) Implement the OnKeys function.
The returned value should be "true" if and only if (x, y) is in the on the keys.
(5) Implement the PlayKeyboard
function.
Use a do{ }while(); loop to keep playing notes until the user clicks in the Quit Box. Inside the loop:
while (!Button()); while (Button()) GetMouse(x, y);Note: x and y must be declared as short.
(5b) The Midi note number is given by the scaling function
NoteNum := 60 + x/ KeyWidth;(5c) To play the note, call
ChannelA.Play(100, 100, NoteNum);// volume 0 .. 255Note: This function call looks different from other functions calls we've made.
ChannelA.Play(100, 100, NoteNum);is said to "send the message Play, with its parameters, to the object ChannelA". You will learn how to write your own class definitions in COM1101. This is just an introduction and a chance to get a little object experience.
It is possible to declare two or more SoundChannel objects, each with its own ability to Play. You can then play harmonies.
This is what you need to know about the play function
// volume 0 .. 255 // duration in half-milliseconds (2000 = 1 second // midiNote = midi # = 1 .. 127; middle C = 60, C# = 61, D = 62, ... void Play (short volume, short duration, long midiNote)Due Date: Monday, October 27, 1997
Extra Credit Possibilities:
(2 points max)
Make a better looking keyboard, similar to a real piano.
Make a "piano-forte" Use the y position of the mouse-click to regulate the volume,
e.g. quieter higher up, louder lower down.
Warning: You might want the sound to stay on as long as the button is down. This Play function is not really suited to this functionality since you must pass the duration when you call Play. At best, you will get a warbley sound caused by repeated calls to Play. The Play function is not reliable with duration values less than 10. Erratic behavior and even crashes are possible.
Last Updated: October 11, 1997 11:51 am by