Skip to content

Commit

Permalink
halfly implemented import from .mid files. staff still raspidorashiva…
Browse files Browse the repository at this point in the history
…etsja a bit for some reason (you cant playback on high tempo, concurrent modification pops up and pointer for some reason visually skips some chords)
  • Loading branch information
klesun committed Sep 5, 2015
1 parent eaa2c2a commit b817b1f
Show file tree
Hide file tree
Showing 70 changed files with 3,220 additions and 740 deletions.
Binary file modified shmidusic.jar
100644 → 100755
Binary file not shown.
1 change: 0 additions & 1 deletion src/org/jm
Submodule jm deleted from f69110
34 changes: 31 additions & 3 deletions src/org/klesun_model/Explain.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/** my implementation of Optional XD */
public class Explain<C> {

final private Boolean success;
Expand All @@ -25,12 +27,17 @@ public Explain(String explanation, Exception exc) {
this(false, explanation + " " + exc.getClass() + " " + exc.getMessage());
}

// TODO: it should be (failurePredicate, explanation) it will make much more sense
public Explain(Boolean success, String explanationIfFail) {
this.data = null;
this.success = success;
this.explanation = success ? null : explanationIfFail;
}

public static Explain butIf(Boolean failureCondition, String explanation) {
return new Explain(!failureCondition, explanation);
}

// if "implicit" is true, alert wont appear even if isSuccess() == false
public Explain<C> setImplicit(Boolean value) {
this.implicit = value;
Expand All @@ -57,11 +64,27 @@ public <T> Explain<T> ifSuccess(Function<C, Explain<T>> lambda) {
return this.isSuccess() ? lambda.apply(this.getData()) : new Explain<T>(false, explanation);
}

public Explain whenSuccess(Consumer<C> lambda) {
return ifSuccess(c -> { lambda.accept(c); return new Explain(true); });
public <T> Explain<T> andThen(Supplier<Explain<T>> lambda) {
return this.isSuccess() ? lambda.get() : new Explain<T>(false, this.explanation);
}

public Explain<C> andIf(Predicate<C> failPred, String explanation) {
return failPred.test(getData()) ? new Explain<>(false, explanation) : this;
}

public Explain<C> whenSuccess(Consumer<C> lambda) {
return ifSuccess(c -> { lambda.accept(c); return this; });
}

public Explain runIfSuccess(Runnable lambda) {
public Explain<C> whenFailure(Runnable onFailure) {
if (!this.isSuccess()) {
onFailure.run();
}
return this;

}

public Explain<C> runIfSuccess(Runnable lambda) {
lambda.run();
return this;
}
Expand Down Expand Up @@ -94,4 +117,9 @@ public static <T, R> Function<T, Explain<R>> mkPred(Predicate<T> pred, Function<
? new Explain<>(func.apply(e))
: new Explain<>(false, "lox");
}

public Explain<C> whileIf(Supplier<Boolean> cond, Function<C, Explain<C>> iteration)
{
return cond.get() ? iteration.apply(this.data).whileIf(cond, iteration) : this;
}
}
2 changes: 1 addition & 1 deletion src/org/klesun_model/field/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Field(String name, Class<E> cls, Boolean isFinal, IModel owner) {
this(name, cls, isFinal, owner, a -> a);
}

private Field(String name, Class<E> cls, Boolean isFinal, IModel owner, Function<E, E> normalizeLambda) {
public Field(String name, Class<E> cls, Boolean isFinal, IModel owner, Function<E, E> normalizeLambda) {
checkValueClass(cls);
this.elemClass = cls;
this.owner = owner;
Expand Down
3 changes: 1 addition & 2 deletions src/org/shmidusic/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.shmidusic;

//import com.sun.deploy.util.SystemUtils;
import org.shmidusic.stuff.Midi.DeviceEbun;
import org.shmidusic.stuff.Midi.PlaybackTimer;
import org.shmidusic.stuff.midi.DeviceEbun;
import org.shmidusic.stuff.tools.Logger;

public class Main
Expand Down
17 changes: 14 additions & 3 deletions src/org/shmidusic/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import org.shmidusic.sheet_music.SheetMusicComponent;
import org.shmidusic.sheet_music.staff.StaffComponent;
import org.shmidusic.sheet_music.staff.StaffHandler;
import org.shmidusic.stuff.Midi.DumpReceiver;
import org.shmidusic.stuff.midi.DumpReceiver;
import org.shmidusic.stuff.OverridingDefaultClasses.Scroll;
import org.shmidusic.stuff.tools.Fp;

import java.awt.*;
import java.text.DecimalFormat;

import javax.swing.*;

Expand All @@ -36,6 +34,12 @@ public MainPanel() {

this.add(sheetScroll, BorderLayout.CENTER);
sheetScroll.getVerticalScrollBar().setUnitIncrement(Staff.SISDISPLACE);
// removing stupid built-ins
InputMap im = sheetScroll.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke("UP"), "none");
im.put(KeyStroke.getKeyStroke("DOWN"), "none");
im.put(KeyStroke.getKeyStroke("PAGE_UP"), "none");
im.put(KeyStroke.getKeyStroke("PAGE_DOWN"), "none");

northPanel.add(pianoLayoutPanel = new PianoLayoutPanel(this), BorderLayout.CENTER);

Expand All @@ -50,8 +54,15 @@ public void replaceSheetMusic(SheetMusic sheetMusic)
sheetScroll.getVerticalScrollBar().setUnitIncrement(Staff.SISDISPLACE);
sheetContainer.requestFocus();
DumpReceiver.eventHandler = (StaffHandler) sheetContainer.getFocusedChild().getHandler();
// removing stupid built-ins
InputMap im = sheetScroll.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke("UP"), "none");
im.put(KeyStroke.getKeyStroke("DOWN"), "none");
im.put(KeyStroke.getKeyStroke("PAGE_UP"), "none");
im.put(KeyStroke.getKeyStroke("PAGE_DOWN"), "none");

this.revalidate();
this.repaint();
}

public void chordChanged() {
Expand Down
2 changes: 1 addition & 1 deletion src/org/shmidusic/MajesticListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.shmidusic;

import org.shmidusic.stuff.Midi.DeviceEbun;
import org.shmidusic.stuff.midi.DeviceEbun;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
Expand Down
4 changes: 1 addition & 3 deletions src/org/shmidusic/MajesticWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.shmidusic.sheet_music.SheetMusic;
import org.shmidusic.sheet_music.SheetMusicComponent;
import org.shmidusic.sheet_music.staff.StaffComponent;
import org.shmidusic.stuff.Midi.DumpReceiver;
import org.shmidusic.stuff.midi.DumpReceiver;
import org.shmidusic.stuff.graphics.ImageStorage;
import org.klesun_model.Combo;
import org.klesun_model.ContextAction;
Expand All @@ -20,8 +20,6 @@
import javax.swing.*;

import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.*;
import java.util.List;

Expand Down
4 changes: 1 addition & 3 deletions src/org/shmidusic/PianoLayoutPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import org.klesun_model.Combo;
import org.shmidusic.sheet_music.staff.Staff;
import org.shmidusic.sheet_music.staff.chord.Chord;
import org.shmidusic.sheet_music.staff.staff_config.KeySignature;
import org.shmidusic.stuff.graphics.Constants;
import org.shmidusic.stuff.graphics.ImageStorage;
import org.apache.commons.math3.fraction.Fraction;
import org.shmidusic.stuff.graphics.Settings;
import org.shmidusic.stuff.tools.Fp;
import org.shmidusic.stuff.tools.jmusic_integration.INota;
import org.shmidusic.stuff.tools.INota;

import javax.swing.*;
import java.awt.*;
Expand Down
35 changes: 18 additions & 17 deletions src/org/shmidusic/sheet_music/SheetMusicComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ public LinkedHashMap<Combo, ContextAction> getMyClassActionMap() {
// .p(new Combo(ctrl, k.VK_P), mkAction(SheetMusicComponent::triggerPlayback).setCaption("Play/Stop"))
.p(new Combo(0, k.VK_SPACE), mkAction(SheetMusicComponent::triggerPlayback).setCaption("Play/Stop"))
// File
.p(new Combo(ctrl, k.VK_S), mkFailableAction(FileProcessor::saveMusicPanel).setCaption("Save midi.json"))
.p(new Combo(ctrl, k.VK_S), mkFailableAction(FileProcessor::saveMusicPanel).setCaption("Save mid.js"))
.p(new Combo(ctrl, k.VK_M), mkFailableAction(FileProcessor::saveMidi).setCaption("Save midi"))
.p(new Combo(ctrl, k.VK_O), mkFailableAction(FileProcessor::openSheetMusic).setCaption("Open"))
/** @legacy */
.p(new Combo(ctrl, k.VK_U), mkFailableAction(FileProcessor::openStaffOld).setCaption("Open Old (When Staff Coul Be Only One)"))
.p(new Combo(ctrl, k.VK_I), mkFailableAction(FileProcessor::openMidi).setCaption("Open midi"))

.p(new Combo(ctrl, k.VK_E), mkFailableAction(FileProcessor::savePNG).setCaption("Export png"))
;
Expand All @@ -70,20 +69,22 @@ public void triggerPlayback() {
/** creating two staffs from one: to pointer pos and from pointer pos */
public Explain splitFocusedStaff()
{
Staff s = getFocusedChild().staff;
return new Explain(s.getFocusedIndex() > 0, "im not plitting here").runIfSuccess(() -> {
Staff newStaff = this.sheetMusic.addNewStaffAfter(s);

List<Chord> staff2Chords = s.getChordList().subList(s.getFocusedIndex(), s.getChordList().size());

staff2Chords.stream().forEach(c -> {
s.remove(c);
newStaff.addNewAccord().reconstructFromJson(c.getJsonRepresentation());
});
this.revalidate();

newStaff.getConfig().reconstructFromJson(newStaff.getJsonRepresentation());
});
return new Explain(false, "Implementation not ready yet");

// Staff s = getFocusedChild().staff;
// return new Explain(s.getFocusedIndex() > 0, "im not plitting here").runIfSuccess(() -> {
// Staff newStaff = this.sheetMusic.addNewStaffAfter(s);
//
// List<Chord> staff2Chords = s.getChordList().subList(s.getFocusedIndex(), s.getChordList().size());
//
// staff2Chords.stream().forEach(c -> {
// s.remove(c);
// newStaff.addNewAccord().reconstructFromJson(c.getJsonRepresentation());
// });
// this.revalidate();
//
// newStaff.getConfig().reconstructFromJson(newStaff.getJsonRepresentation());
// });
}

private int getFocusedSystemY() {
Expand Down
84 changes: 7 additions & 77 deletions src/org/shmidusic/sheet_music/staff/Staff.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.shmidusic.sheet_music.staff.staff_config.StaffConfig;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.shmidusic.MainPanel;
Expand All @@ -21,7 +20,7 @@


import org.shmidusic.stuff.tools.Fp;
import org.shmidusic.stuff.tools.jmusic_integration.INota;
import org.shmidusic.stuff.tools.INota;
import org.apache.commons.math3.fraction.Fraction;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -127,17 +126,13 @@ public Staff reconstructFromJson(JSONObject jsObject) throws JSONException {
JSONObject configJson = jsObject.getJSONObject("staffConfig");
this.getConfig().reconstructFromJson(configJson);

if (jsObject.has("chordList")) {
toStream(jsObject.getJSONArray("chordList")).forEach(
childJs -> this.addNewAccord().reconstructFromJson(childJs));
Stream<JSONObject> jsChords = jsObject.has("chordList")
? toStream(jsObject.getJSONArray("chordList"))
: toStream(jsObject.getJSONArray("tactList"))
.map(t -> toStream(t.getJSONArray("chordList")))
.flatMap(s -> s);

} else if (jsObject.has("tactList")) {
toStream(jsObject.getJSONArray("tactList")).forEach(
childJs -> toStream(childJs.getJSONArray("chordList")).forEach(
a -> addNewAccord().reconstructFromJson(a)));
} else {
throw new JSONException("Staff Json Has No Valid Children! It got only: " + Arrays.toString(jsObject.keySet().toArray()));
}
jsChords.forEach(childJs -> this.addNewAccord().reconstructFromJson(childJs));

return this;
}
Expand Down Expand Up @@ -213,71 +208,6 @@ public Staff setFocusedIndex(int value)

// action handles

/** @return - nota that we just put */
public Nota putAt(Fraction desiredPos, INota nota)
{
// TODO: it's broken somehow. Bakemonogatari can be opent with Noteworthy, but cant with midiana
// and yu-no, maybe move this method into some specialized class? it seems to be something serious
// what this method does, yes it's definitely should be move into some MidiSheetMusicCalculator or so...

Fraction curPos = new Fraction(0);
for (int i = 0; i < getChordList().size(); ++i) {

if (curPos.equals(desiredPos)) {
Chord chord = getChordList().get(i);

Fraction wasAccordLength = chord.getFraction();
Nota newNota = chord.addNewNota(nota);

if (!wasAccordLength.equals(chord.getFraction())) {
// putting filler in case when chord length became smaller to preserve timing
Fraction dl = wasAccordLength.subtract(chord.getFraction());
this.addNewAccord(i + 1).addNewNota(0, 0).setLength(dl);
}
return newNota;
} else if (curPos.compareTo(desiredPos) > 0) {

Chord chord = getChordList().get(i - 1);
Fraction offset = new Fraction(curPos.doubleValue() - desiredPos.doubleValue());
Fraction onset = new Fraction(chord.getFraction().doubleValue() - offset.doubleValue());

/** @debug */
// if (onset.equals(new Fraction(0))) {
// Logger.fatal("How came ?! " + chord.getFraction() + " " + offset + " " + curPos + " " + desiredPos);
// }

chord.addNewNota(0, 0).setLength(onset);

Chord newChord = this.addNewAccord(i);
Nota newNota = newChord.addNewNota(nota);
if (newNota.getLength().compareTo(offset) > 0) {
// TODO: maybe if last chord in org.shmidusic.staff then no need
// put nota with onset length into newNota's chord to preserve timing
newChord.addNewNota(0, 0).setLength(offset);
} else if (newNota.getLength().compareTo(offset) < 0) {
// TODO: maybe if last chord in org.shmidusic.staff then no need
// put an empty nota after and set it's length(onset - newNota.getLength())
this.addNewAccord(i + 1).addNewNota(0, 0).setLength(offset.subtract(newNota.getLength()));
}

return newNota;
}

Fraction accordFraction = getChordList().get(i).getFraction();

curPos = new Fraction(curPos.doubleValue() + accordFraction.doubleValue());
}

Fraction rest = desiredPos.subtract(curPos);
if (!rest.equals(new Fraction(0))) {
this.addNewAccord().addNewNota(0,0).setLength(rest);
}

// TODO: we don't handle here pause prefix! (i.e when desired start is more than end) !!!
// if not returned already
return this.addNewAccord().addNewNota(nota);
}

public static class TactMeasurer {

final private Fraction tactSize;
Expand Down
5 changes: 2 additions & 3 deletions src/org/shmidusic/sheet_music/staff/StaffComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import org.shmidusic.sheet_music.staff.chord.Chord;
import org.shmidusic.sheet_music.staff.chord.ChordComponent;
import org.shmidusic.sheet_music.staff.chord.ChordHandler;
import org.shmidusic.stuff.Midi.DeviceEbun;
import org.shmidusic.stuff.Midi.Playback;
import org.shmidusic.stuff.midi.DeviceEbun;
import org.shmidusic.stuff.musica.Playback;
import org.shmidusic.stuff.graphics.Settings;
import org.shmidusic.Main;
import org.shmidusic.stuff.musica.PlayMusThread;
import org.shmidusic.stuff.tools.Logger;

Expand Down
2 changes: 1 addition & 1 deletion src/org/shmidusic/sheet_music/staff/StaffHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.klesun_model.ContextAction;
import org.klesun_model.Explain;
import org.shmidusic.sheet_music.staff.staff_config.StaffConfig;
import org.shmidusic.stuff.Midi.DeviceEbun;
import org.shmidusic.stuff.midi.DeviceEbun;
import org.shmidusic.sheet_music.staff.chord.Chord;
import org.shmidusic.sheet_music.staff.chord.nota.Nota;
import org.shmidusic.stuff.OverridingDefaultClasses.TruMap;
Expand Down
2 changes: 1 addition & 1 deletion src/org/shmidusic/sheet_music/staff/StaffPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.apache.commons.math3.fraction.Fraction;
import org.shmidusic.sheet_music.staff.staff_config.StaffConfigComponent;
import org.shmidusic.stuff.graphics.ImageStorage;
import org.shmidusic.stuff.tools.jmusic_integration.INota;
import org.shmidusic.stuff.tools.INota;

import java.awt.*;

Expand Down
Loading

0 comments on commit b817b1f

Please sign in to comment.