One of the few (mostly) true stories presented in Peter Schaffer‘s play and movie Amadeus was when Emperor Joseph II told Mozart that his latest work (Der Entführung aus dem Serail ) had “too many notes.”
How many notes is too many? Mozart had a great answer.
What would a Note class look like? Here is a first draft. The essential attributes are the note’s pitch (frequency), the time when it starts and the time when it finishes. Let’s ignore many other attributes such as loudness etc. for now.
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Note { private int pitch; private double startBeat; private double duration; // in beats public Note() { this(60); } public Note(int pitch) { this.setStartBeat(1d); this.duration = 1d; this.pitch = pitch; } ... |
For simple applications it would be easy to represent the pitch of a Note as its MIDI number (middle C – 261.626 Hz – is 60). But if you are going to use different temperaments besides equal temperament you will not be able to represent those pitches with a single int variable. A MIDI device expects an integer (actually a series of messages) to be transmitted to it for pitch. A computer program such as CSound uses other pitch formats such as frequency in Hz, “octave point pitchclass” etc. The point is a human musician will immediately understand “middle c” but would have to work out what 261.626 means whereas a computer would “prefer” the frequency rather than having to parse the human name to arrive at the frequency.
There are also many operations (e.g. transpose) you can execute on a pitch so it pretty much screams out to be a class.
To simplify things let’s create a Pitch class that uses the integer midiNumber instead of a floating point frequency. In real life I’d probably create a Map containing pre-calculated frequencies for any temperament I care to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Pitch implements Serializable, Comparable<Pitch> { private int midiNumber; private String preferredSpelling; // e.g. F# or Gb private short pitchBend; // amount of MIDI pitch bend public Pitch() { this(0); } public Pitch(int midiNumber) { if (n < 0 || n > 127) { throw new IllegalArgumentException("MIDI number parameter is out of range " + n); } this.midiNumber = midiNumber; } ... } |
So the Note will now instantiate a Pitch instead of simply setting an int.
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Note { private Pitch pitch; private double startBeat; private double duration; // in beats public Note() { this(new Pitch(60)); } public Note(Pitch pitch) { this.setStartBeat(1d); this.duration = 1d; this.pitch = pitch; } ... |
Finally getting to the point
Question: how many times do you hear middle C in Mozart’s Jupiter Symphony?
Answer: I don’t know but it’s a lot.
If you use these classes to create a representation of the Jupiter, how many Note/Pitch instances would you be creating? Right now there is a one to one relationship between Note and Pitch. Since we are defining a Note as a pitch that occurs at a specified time for a specified duration, they are likely to be unique. But are the actual Pitches unique? Actually no. Middle C is Middle C no matter when or how long it’s played. Can we somehow reuse Pitch instances? I’m glad you asked.

Don't be this guy
PitchFactory.getPitch(Pitch.C5); |
Then we simply ask our factory for that pitch.
1 2 3 4 5 6 7 8 9 10 11 12 | public class Note { private Pitch pitch; private double startBeat; private double duration; public Note() { this(PitchFactory.getPitch(Pitch.C5)); } public Note(Pitch pitch) { this.setStartBeat(1d); this.duration = 1d; this.pitch = pitch; } |
A simple implementation of the Factory would be something like this, where the Pitch instances are lazily instantiated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class PitchFactory { private static Map<Integer, Pitch> pitches = new HashMap<Integer, Pitch>(); public static Pitch getPitch(int midiNumber) { Integer i = new Integer(midiNumber); Pitch p = null; if (pitches.containsKey(i)) { p = pitches.get(i); } else { p = new Pitch(midiNumber); pitches.put(i, p); } return p; } ... |
The result is that we have many Notes (but not too many!) sharing fewer Pitches. Emperor Joseph II would finally be happy.










