From 674ce005385d50ce18b8626d58189a156e9a9d62 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Sun, 26 Feb 2012 12:48:34 -0500 Subject: [Bleeding] Allow sharping notes that aren't sharpable, and a factory method to create flat notes. Addresses BUKKIT-861 - Uses enharmonic equivalences to rewrite the note in the normalized form, E-sharp becomes F and A-flat becomes G-sharp --- src/main/java/org/bukkit/Note.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/bukkit/Note.java b/src/main/java/org/bukkit/Note.java index 3d5f1254..8e053fd7 100644 --- a/src/main/java/org/bukkit/Note.java +++ b/src/main/java/org/bukkit/Note.java @@ -127,10 +127,13 @@ public class Note { * * @param octave The octave where the note is in. Has to be 0 - 2. * @param tone The tone within the octave. If the octave is 2 the note has to be F#. - * @param sharped Set it the tone is sharped (e.g. for F#). + * @param sharped Set if the tone is sharped (e.g. for F#). */ public Note(int octave, Tone tone, boolean sharped) { - Validate.isTrue(!(sharped && !tone.isSharpable()), "This tone could not be sharped."); + if (sharped && !tone.isSharpable()) { + tone = tone == Tone.F ? Tone.G : Tone.values()[tone.ordinal() + 1]; + sharped = false; + } if (octave < 0 || octave > 2 || (octave == 2 && !(tone == Tone.F && sharped))) { throw new IllegalArgumentException("Tone and octave have to be between F#0 and F#2"); } @@ -138,6 +141,18 @@ public class Note { this.note = (byte) (octave * Tone.TONES_COUNT + tone.getId(sharped)); } + /** + * Creates a new note for a flat tone, such as A-flat. + * + * @param octave The octave where the note is in. Has to be 0 - 2. + * @param tone The tone within the octave. If the octave is 2 the note has to be F#. + * @return The new note. + */ + public static Note flat(int octave, Tone tone) { + tone = tone == Tone.G ? Tone.F : Tone.values()[tone.ordinal() - 1]; + return new Note(octave, tone, tone.isSharpable()); + } + /** * Returns the internal id of this note. * -- cgit v1.2.3