/* -*- c-basic-offset: 2; indent-tabs-mode: nil; -*- */ /* * FreeDots -- MusicXML to braille music transcription * * Copyright 2008-2010 Mario Lang All Rights Reserved. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details (a copy is included in the LICENSE.txt file that * accompanied this code). * * You should have received a copy of the GNU General Public License * along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * This file is maintained by Mario Lang <mlang@delysid.org>. */ package freedots.musicxml; import freedots.music.TupletElement; import freedots.math.Fraction; import freedots.math.PowerOfTwo; public class Tuplet extends freedots.music.Tuplet { public Tuplet() { } /** Calcule NormalType with time-modification when it's not ambiguous */ protected Fraction getNormal() { if (getParent() == null && getFirst() instanceof Note) { final Note note = (Note)getFirst(); final Note.TimeModification timeModification = note.getTimeModification(); return new Fraction(timeModification.getNormalNotes() * timeModification.getNormalType().numerator(), timeModification.getNormalType().denominator()); } return null; } /** Calcule ActualType with time-modification when it's not ambiguous */ protected Fraction getActual() { if (getParent() == null && getFirst() instanceof Note) { final Note note = (Note)getFirst(); final Note.TimeModification timeModification = note.getTimeModification(); return new Fraction(timeModification.getActualNotes() * timeModification.getNormalType().numerator(), timeModification.getNormalType().denominator()); } return null; } public final boolean addNote(final Note note) { if (super.add(note)) { note.addTuplet(this); return true; } return false; } /** Check if tuplet is complete */ public boolean completed() { Fraction expectedFrac = this.getActualType().simplify(); Fraction sumFrac = Fraction.ZERO; for (TupletElement tE: this) { Fraction currentFrac = Fraction.ZERO; if (tE instanceof Tuplet) { Tuplet tuplet = (Tuplet)tE; currentFrac = tuplet.getNormalType(); } else { final Note note = (Note)tE; currentFrac = new Fraction(new PowerOfTwo(note.getAugmentedFraction().getPower())); } sumFrac = sumFrac.add(currentFrac); } return sumFrac.equals(expectedFrac); } }