COM1100 Keyboard Project

October 1997

Overview
In this program you will create a small playable keyboard in the drawing window. The basic keyboard will offer one chromatic octave starting at middle C. The black notes are the same size and shape as the white notes.


What You Need to Know about Music:
Middle C is Midi Note 60. It will be easier for us to think of the keys as numbered from 0 to NumKeys and then add 60 to the note numbers when we play the notes.

C = 0, C # = D b = 1, D = 2, D # = E b = 3, E = 4, F = 5, F # = G b = 6, G = 7,
G # = A b = 8, A = 9, A # = B b= 10, B = 11, hiC = 12

Those are all the notes on our basic keyboard.
The black keys are the # and b keys: 1, 3, 6, 8, 10

Activities
The project folder Keyboard contains a working application as well as a shell project, cp file for this program, and SoundChannels.h.

Run the application program to see what your final program should do.

Writing the Code:
Look at the keyboard.cp file. The declarations

	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

(0) Add your Name and ID number to the Credits function.

(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 DrawMaxY 
are 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.

(3) Implement the Done function.
The returned value should be "true" if and only if (x, y) is in the QUIT box.

(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:


(5a) Use the following pair of while- statements to get the mouse position.
		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 .. 255
Note: This function call looks different from other functions calls we've made.
SoundChannel is a class.
ChannelA is an object of the SoundChannel class.
Play is a member function of the SoundChannel class.
In object-oriented terminology, the call
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

Turn in:
Printout of your source code
Diskette with your source code, project and application program

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

Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1100/HW/Keyboard.html