StanfordCPPLib
Public Types | Public Member Functions | List of all members
Note Class Reference

Each Note object represents a musical note or rest. More...

#include "note.h"

Public Types

enum  Accidental { SHARP, NATURAL, FLAT }
 Represents a musical accidental: sharp, natural, or flat. More...
 
enum  Pitch { A, B, C, D, E, F, G, R }
 Represents a musical pitch from A-G or rest. More...
 

Public Member Functions

 Note(string line)
 Constructs a Note with the information contained in the given line. More...
 
 Note(double duration=0.0, Pitch pitch=R, int octave=OCTAVE_MIN, Accidental accidental=NATURAL, bool repeat=false)
 Constructs a Note with the given information. More...
 
bool equals(const Note &note2) const
 Returns true if note2 has the same state as this Note object; otherwise false. More...
 
Accidental getAccidental() const
 Returns this Note's accidental value of SHARP, FLAT, or NATURAL. More...
 
double getDuration() const
 Returns this Note's duration in seconds. More...
 
int getOctave() const
 Returns this Note's octave. More...
 
Pitch getPitch() const
 Returns this Note's pitch value of A-G or R for a rest. More...
 
bool isRepeat() const
 Returns true if this Note is the start or end of a repeated section. More...
 
bool isRest() const
 Returns true if this Note is a rest. More...
 
void play() const
 Plays this note through the underlying audio system. More...
 
void setAccidental(Accidental accidental)
 Sets this Note's accidental value to be the given value: SHARP, FLAT, or NATURAL. More...
 
void setDuration(double duration)
 Sets this Note's duration in seconds to be the given value. More...
 
void setOctave(int octave)
 Sets this Note's octave to be the given value. More...
 
void setPitch(Pitch pitch)
 Sets this Note's pitch to be the given value. More...
 
void setRepeat(bool repeat)
 Sets this Note's repeat flag to be the given value. More...
 
string toString() const
 Returns a string representation of this note. More...
 

Detailed Description

Each Note object represents a musical note or rest.

A Note encapsulates a pitch (A-G), a duration in seconds, an octave, an accidental (sharp, flat, or natural), and a flag of whether it is the start/end of a repeated section or not. A song or melody can be thought of as a list or array of Note objects.

Member Enumeration Documentation

◆ Accidental

enum Accidental

Represents a musical accidental: sharp, natural, or flat.

Enumerator
SHARP 
NATURAL 
FLAT 

◆ Pitch

enum Pitch

Represents a musical pitch from A-G or rest.

Enumerator

Constructor & Destructor Documentation

◆ Note() [1/2]

Note ( string  line)

Constructs a Note with the information contained in the given line.

Parameters
linea line of input data such as "0.2 C 4 NATURAL false" or "0.4 R false" for a rest
Exceptions
anerror if line is empty.
anerror if duration is negative or octave is not between OCTAVE_MIN and OCTAVE_MAX inclusive.

◆ Note() [2/2]

Note ( double  duration = 0.0,
Pitch  pitch = R,
int  octave = OCTAVE_MIN,
Accidental  accidental = NATURAL,
bool  repeat = false 
)

Constructs a Note with the given information.

Parameters
durationNote's duration in seconds.
pitchNote's pitch from A through G, or R for a rest.
octaveNote's octave from OCTAVE_MIN through OCTAVE_MAX inclusive.
accidentalNote's accidental from SHARP, FLAT, or NATURAL.
repeattrue if this note starts/ends a repeated section.
Exceptions
anerror if any value passed is illegal.

Member Function Documentation

◆ equals()

bool equals ( const Note note2) const

Returns true if note2 has the same state as this Note object; otherwise false.

Parameters
othe object to compare against

◆ getAccidental()

Note::Accidental getAccidental ( ) const

Returns this Note's accidental value of SHARP, FLAT, or NATURAL.

The accidental value is meaningless for a rest; this method will return NATURAL by default if called on a rest.

Returns
this Note's accidental value of SHARP, FLAT, or NATURAL.

◆ getDuration()

double getDuration ( ) const

Returns this Note's duration in seconds.

Returns
this Note's duration in seconds.

◆ getOctave()

int getOctave ( ) const

Returns this Note's octave.

The octave value is meaningless for a rest; this method will return OCTAVE_MIN + 1 by default if called on a rest.

Returns
this Note's octave from OCTAVE_MIN to OCTAVE_MAX.

◆ getPitch()

Note::Pitch getPitch ( ) const

Returns this Note's pitch value of A-G or R for a rest.

Returns
this Note's pitch value of A-G or R for a rest.

◆ isRepeat()

bool isRepeat ( ) const

Returns true if this Note is the start or end of a repeated section.

Returns
true if this Note is the start or end of a repeated section, otherwise false.

◆ isRest()

bool isRest ( ) const

Returns true if this Note is a rest.

Equivalent to checking whether this note's pitch is Pitch.R. Provided for convenience.

Returns
true if this Note is a rest, otherwise false.

◆ play()

void play ( ) const

Plays this note through the underlying audio system.

Also may print a message to the system console for debugging. If the audio system is muted or paused, the note may not play.

◆ setAccidental()

void setAccidental ( Note::Accidental  accidental)

Sets this Note's accidental value to be the given value: SHARP, FLAT, or NATURAL.

The accidental value is meaningless for a rest, but the Note object still maintains an accidental value internally (initially NATURAL) which is ignored.

Parameters
accidentalNote's accidental from SHARP, FLAT, or NATURAL.
Exceptions
anerror if the accidental value passed is illegal.

◆ setDuration()

void setDuration ( double  duration)

Sets this Note's duration in seconds to be the given value.

Parameters
durationNote's duration in seconds.
Exceptions
anerror if duration is negative.

◆ setOctave()

void setOctave ( int  octave)

Sets this Note's octave to be the given value.

The octave value is meaningless for a rest, but the Note object still maintains an octave value internally (initially OCTAVE_MIN + 1) which is ignored.

Parameters
octaveNote's octave from OCTAVE_MIN through OCTAVE_MAX inclusive.
Exceptions
anerror if octave is not between OCTAVE_MIN and OCTAVE_MAX inclusive.

◆ setPitch()

void setPitch ( Note::Pitch  pitch)

Sets this Note's pitch to be the given value.

Parameters
pitchNote's pitch from A through G, or R for a rest.
Exceptions
anerror if pitch is invalid.

◆ setRepeat()

void setRepeat ( bool  repeat)

Sets this Note's repeat flag to be the given value.

Parameters
repeattrue to indicate that this note is the start/end of a repeated section, or false if not.

◆ toString()

string toString ( ) const

Returns a string representation of this note.

Returns
A string such as "0.4 C 5 NATURAL false".