/**
 * A note in a song
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Note extends AbstractLineElement
{
    private boolean   sharp;
    private char      pitch;


    /**
     * Default Constructor
     *
     * Constructs a whole note at middle C
     */
    public Note()
    {
	this('C', false, 0, 1, false);
    }




    /**
     * Explicit Value Constructor
     *
     * @param pitch    The pitch ('A','B','C','D','E','F', 'G' or 'R')
     * @param sharp    true for a sharp and false for a natural
     * @param octave   The octave (relative to middle C)
     * @param duration 1 for whole notes, 2 for half notes, etc...
     * @param dotted   Whether the note is dotted
     */
    public Note(char pitch,   boolean sharp, int octave, 
                int duration, boolean dotted)
    {
	int     durationInMillis, midiBase, midiNumber;

	// Store the pitch and duration
	this.pitch    = Character.toUpperCase(pitch);
        
	// Correct for possible "mistakes" (B# and E#)
        // (Alternatively, we could treat a B# as a C and an
        // E# as an F)
	if ((pitch == 'B') || (pitch == 'E')) this.sharp = false;
	else                                  this.sharp = sharp;

	// Calculate the MIDI value
	midiBase = 60;
	if      (pitch == 'A') midiNumber = midiBase +  9;
	else if (pitch == 'B') midiNumber = midiBase + 11;
	else if (pitch == 'C') midiNumber = midiBase +  0;
	else if (pitch == 'D') midiNumber = midiBase +  2;
	else if (pitch == 'E') midiNumber = midiBase +  4;
	else if (pitch == 'F') midiNumber = midiBase +  5;
	else if (pitch == 'G') midiNumber = midiBase +  7;
        else                   midiNumber = -1;             // Rest        

	if (this.sharp) midiNumber = midiNumber + 1;

	midiNumber = midiNumber + (octave * 12);

        setValue(""+midiNumber);
        setDuration(duration, dotted);        
    }

}
