Skip to content

Commit

Permalink
at last! no more lame getRealLength() method in Note, the tuplet deno…
Browse files Browse the repository at this point in the history
…minator was merged into the main fraction
  • Loading branch information
klesun authored and klesun committed Dec 30, 2015
1 parent f77980b commit 33a58ce
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/org/shmidusic/PianoLayoutPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ synchronized private Set<INote> getNoteSet()
sum = sum.add(chord.getFraction());

final Fraction finalSum = sum;
chord.noteStream(n -> n.getRealLength().compareTo(finalSum) > 0).forEach(result::add);
chord.noteStream(n -> n.getLength().compareTo(finalSum) > 0).forEach(result::add);

--index;
}
Expand Down
5 changes: 2 additions & 3 deletions src/org/shmidusic/sheet_music/staff/chord/Chord.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public int getShortestTime(int tempo) {
}

public Fraction getFraction() {
return getShortest().map(INote::getRealLength).orElse(new Fraction(0));
return getShortest().map(INote::getLength).orElse(new Fraction(0));
}

// field getters/setters
Expand All @@ -72,7 +72,6 @@ public Chord setExplicitLength(Fraction length) {

public Note addNewNote(INote source) {
Note newNote = addNewNote(source.getTune(), source.getChannel()).setLength(source.getLength());
newNote.isTriplet.set(source.isTriplet());
removeRedundantPauseIfAny();

return newNote;
Expand Down Expand Up @@ -100,7 +99,7 @@ public void removeRedundantPauseIfAny()
{
getShortest().ifPresent(
n -> noteList.get().stream()
.filter(k -> k.getRealLength().equals(n.getRealLength()) && !k.isPause())
.filter(k -> k.getLength().equals(n.getLength()) && !k.isPause())
.findAny().ifPresent(
k -> noteList.get().stream().filter(Note::isPause)
.collect(Collectors.toList())
Expand Down
26 changes: 13 additions & 13 deletions src/org/shmidusic/sheet_music/staff/chord/note/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ public class Note extends AbstractModel implements INote
// TODO: normalization rules maybe ???
final public Field<Integer> tune = new Field<>("tune", Integer.class, true, this, n -> limit(n, 0, 127));
final protected Field<Integer> channel = new Field<>("channel", Integer.class, true, this, n -> limit(n, 0, 15));
final public Field<Fraction> length = new Field<>("length", new Fraction(1, 4), this, INote::legnthNorm);
final public Field<Fraction> length = new Field<>("length", new Fraction(1, 4), this);

/** @TODO: forbid more than two dots and move tripletness into length */
final public Field<Boolean> isTriplet = new Field<>("isTriplet", false, this);
/** @unused */
final public Field<Boolean> isSharp = new Field<>("isSharp", false, this);
/** @unused */
final private Field<Boolean> isMuted = new Field<>("isMuted", false, this);
final public Field<Boolean> isLinkedToNext = new Field<>("isLinkedToNext", false, this);

final private static int MAX_DOT_COUNT = 3;
final private static int MAX_DOT_COUNT = 2; // 3 dots would screw triplets - use linking
final private static int PAUSE_POSITION = 3 * 7;

// </editor-fold>
Expand All @@ -42,7 +40,7 @@ public Note(int tuneValue, int channelValue) {

public long keydownTimestamp;

public Boolean isLongerThan(Note rival) { return getRealLength().compareTo(rival.getRealLength()) > 0; }
public Boolean isLongerThan(Note rival) { return getLength().compareTo(rival.getLength()) > 0; }

// <editor-fold desc="implementing abstract model">

Expand Down Expand Up @@ -71,7 +69,7 @@ public Note reconstructFromJson(JSONObject dict) {
// <editor-fold desc="getters">

public int getTimeMilliseconds(int tempo) {
return getTimeMilliseconds(getRealLength(), tempo);
return getTimeMilliseconds(getLength(), tempo);
}

// TODO: separate it into two parts. What we pass to SMF are not milliseconds, they are beats!
Expand Down Expand Up @@ -113,10 +111,11 @@ private int pow(int n, int e) {
return e == 0 ? 1 : n * pow(n, e - 1);
}

public Fraction getCleanLength() { // i.e. length without dots: 1/4, 1/2
return length.get().getDenominator() == 1
? new Fraction(length.get().getNumerator() * 2, length.get().getDenominator() + 1)
: new Fraction(length.get().getNumerator() + 1, length.get().getDenominator() * 2);
public Fraction getCleanLength() { // i.e. length without dots: 1/4, 1/2, 1/6
Fraction r = length.get();
return r.getDenominator() == 1
? new Fraction(r.getNumerator() * 2, 2) // i have doubts for this line. think about numberOfTrailingZeros()
: new Fraction(r.getNumerator() + 1, r.getDenominator() * 2);
}

// </editor-fold>
Expand Down Expand Up @@ -145,7 +144,7 @@ public int ivoryIndex(KeySignature siga) { // siga - key signature from staff co
public Integer getTune() { return tune.get(); }
public Integer getChannel() { return channel.get(); }
public Fraction getLength() { return length.get(); }
public Boolean isTriplet() { return isTriplet.get(); }
public Boolean isTriplet() { return getLength().getDenominator() % 3 == 0; }
public Boolean getIsSharp() { return isSharp.get(); }
public Boolean getIsMuted() { return isMuted.get(); }
public Boolean getIsLinkedToNext() { return isLinkedToNext.get(); }
Expand All @@ -158,7 +157,6 @@ public Note setTune(int value) {
/** @Bug - note is immutable, this will blow with fatal !!! */
public Note setChannel(int value) { this.channel.set(value); return this; }
public Note setIsSharp(Boolean value) { this.isSharp.set(value); return this; }
public Note setIsTriplet(Boolean value) { this.isTriplet.set(value); return this; }
public Note setIsMuted(Boolean value) { this.isMuted.set(value); return this; }

// </editor-fold>
Expand All @@ -175,7 +173,9 @@ public void triggerIsLinkedToNext() {
isLinkedToNext.set(!isLinkedToNext.get());
}
public Note triggerTupletDenominator() {
isTriplet.set(!isTriplet.get());
length.set(isTriplet()
? length.get().multiply(3)
: length.get().divide(3));
return this;
}

Expand Down
12 changes: 7 additions & 5 deletions src/org/shmidusic/sheet_music/staff/chord/note/NotePainter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.shmidusic.sheet_music.staff.chord.note;

import org.apache.commons.math3.fraction.Fraction;
import org.shmidusic.sheet_music.staff.staff_config.KeySignature;
import org.shmidusic.stuff.graphics.ImageStorage;
import org.klesun_model.AbstractPainter;
Expand Down Expand Up @@ -41,7 +42,8 @@ public void draw(KeySignature siga)
} else if (n.isTooLong()) {
tmpImg = ImageStorage.inst().getTooLongImage();
} else {
tmpImg = ImageStorage.inst().getNoteImg(n.getCleanLength(), colorChannel);
Fraction l = n.getCleanLength().multiply(n.isTriplet() ? 3 : 1);
tmpImg = ImageStorage.inst().getNoteImg(l, colorChannel);
}

if (n.getIsLinkedToNext()) {
Expand All @@ -50,15 +52,15 @@ public void draw(KeySignature siga)

drawImage(tmpImg, dx(), 0);

if (n.isTriplet.get()) {
if (n.isTriplet()) {
Rectangle rect = new Rectangle(dx() /4, 6 * dy(), dx() / 2, dy() * 2);
drawString("3", rect, ImageStorage.getColorByChannel(n.channel.get()));
}

for (int i = 0; i < n.getDotCount(); ++i) {
// TODO: for some reason it draws only one dot even for multidot hujot
int x = dx() * 5/3 + dx() * i / n.getDotCount();
drawDot(new Pnt(x, noteCenterY), dy() / 2, Color.BLACK);
double r = dy() / 2;
int x = (int)(dx() * 5/3 + r * 4 * i / n.getDotCount());
drawDot(new Pnt(x, noteCenterY), r, Color.BLACK);
}

if (n.ivoryIndex(siga) % 2 == 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/shmidusic/stuff/midi/IMidiScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IMidiScheduler
// read "final"
default void addNoteTask(Fraction when, INote note) {
addNoteOnTask(when, note.getTune(), note.getChannel());
addNoteOffTask(when.add(note.getRealLength()), note.getTune(), note.getChannel());
addNoteOffTask(when.add(note.getLength()), note.getTune(), note.getChannel());
}
void addNoteOnTask(Fraction when, int tune, int channel);
void addNoteOffTask(Fraction when, int tune, int channel);
Expand Down
19 changes: 4 additions & 15 deletions src/org/shmidusic/stuff/midi/NoteGuesser.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void putAt(Fraction desiredPos, INote note, Staff staff)
if (opt.isPresent()) {

Chord chord = opt.get();
Fraction pauseRest = chord.getFraction().subtract(note.getRealLength());
Fraction pauseRest = chord.getFraction().subtract(note.getLength());
chord.addNewNote(note);
staff.accordListChanged(-100);

Expand All @@ -201,7 +201,7 @@ private void putAt(Fraction desiredPos, INote note, Staff staff)
// putting note
staff.addNewAccord(index++).setExplicitLength(postRest).addNewNote(note);
// putting following pauses
putRest.apply(postRest.subtract(note.getRealLength()), index);
putRest.apply(postRest.subtract(note.getLength()), index);

} else {
// put enough pauses
Expand Down Expand Up @@ -325,25 +325,14 @@ public GuessingNote setDuration(int value) {

public Integer getTune() { return tune; }
public Integer getChannel() { return channel; }
public Fraction getLength() {
return isTriplet()
? getActualLength().multiply(3)
: getActualLength();
}
public Boolean isTriplet() {
return getActualLength().getDenominator() % 3 == 0;
}

private Fraction getActualLength() {
return guessLength(this.duration);
}
public Fraction getLength() { return guessLength(this.duration); }

public int getTime() {
return this.time;
}

public String strMe() {
return "time: " + time + "; tune: " + tune + "; channel: " + channel + "; duration: " + duration + "; length: " + getActualLength();
return "time: " + time + "; tune: " + tune + "; channel: " + channel + "; duration: " + duration + "; length: " + getLength();
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/org/shmidusic/stuff/musica/Playback.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

import org.shmidusic.sheet_music.staff.chord.Chord;
import org.shmidusic.sheet_music.staff.StaffComponent;
import org.shmidusic.Main;
import org.klesun_model.Explain;
import org.shmidusic.sheet_music.staff.Staff;
import org.apache.commons.math3.fraction.Fraction;
import org.shmidusic.sheet_music.staff.chord.ChordComponent;
import org.shmidusic.sheet_music.staff.chord.note.Note;
import org.shmidusic.sheet_music.staff.chord.note.NoteComponent;
import org.shmidusic.stuff.midi.DeviceEbun;
import org.shmidusic.stuff.midi.IMidiScheduler;
import org.shmidusic.stuff.midi.SmfScheduler;
import org.shmidusic.stuff.tools.INote;
import org.shmidusic.stuff.tools.Logger;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class Playback {

Expand Down Expand Up @@ -103,7 +97,7 @@ private void streamTo(IMidiScheduler scheduler, int startFrom, Consumer<Fraction
}

if (!note.isLinkedToNext.get()) {
scheduler.addNoteOffTask(noteStart.add(note.getRealLength()), note.tune.get(), note.getChannel());
scheduler.addNoteOffTask(noteStart.add(note.getLength()), note.tune.get(), note.getChannel());
openedLinks.remove(note);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/shmidusic/stuff/musica/PlaybackTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public KlesunthesizerTimer(StaffConfig config) {

@Override
public void addNoteTask(Fraction when, INote note) {
addTask(when, () -> Klesunthesizer.send(note.getTune(), (int) toMillis(note.getRealLength())));
addTask(when, () -> Klesunthesizer.send(note.getTune(), (int) toMillis(note.getLength())));
}
}
}
27 changes: 2 additions & 25 deletions src/org/shmidusic/stuff/tools/INote.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,9 @@

import java.util.Arrays;

public interface INote extends ISound {

public interface INote extends ISound
{
Fraction getLength();
Boolean isTriplet();

static Fraction legnthNorm(Fraction value) {
// Commented for now, but not sure that made right thing
// if (value.equals(new Fraction(0))) {
// Logger.fatal("zero length NO WAI");
// }
if (!isDotable(value) && !isTooShort(value) && !isTooLong(value)) {
Logger.warning("Not Dotable Fraction: " + value);
}

return value;
// return Helper.limit(value, new Fraction(1, 256), new Fraction(4)); // sometimes need to fill diff between triplet and straight
// return Helper.limit(value, new Fraction(1, 16), new Fraction(2));
}

default Boolean isDotable() {
return isDotable(getLength());
}

default Boolean isTooLong() {
return isTooLong(getLength());
Expand All @@ -37,10 +18,6 @@ default Boolean isTooShort() {
return isTooShort(getLength());
}

default Fraction getRealLength() { // that includes tuplet denominator
return getLength().divide(isTriplet() ? 3 : 1);
}

default Boolean isEbony() { return isEbony(getTune()); }

default int ivoryIndex(KeySignature siga) { return ivoryIndex(getTune(), siga); }
Expand Down

0 comments on commit 33a58ce

Please sign in to comment.