diff --git a/TBbard/launch4j/TBbard.xml b/TBbard/launch4j/TBbard.xml index d13ca8c..8d07367 100644 --- a/TBbard/launch4j/TBbard.xml +++ b/TBbard/launch4j/TBbard.xml @@ -2,8 +2,8 @@ false gui - E:\Desktop\TBbard_v1.7d.jar - E:\Desktop\TBbard_v1.7d.exe + E:\Desktop\TBbard_v1.8.jar + E:\Desktop\TBbard_v1.8.exe . diff --git a/TBbard/src/bard/GUI.java b/TBbard/src/bard/GUI.java index dfc8697..eb15d60 100644 --- a/TBbard/src/bard/GUI.java +++ b/TBbard/src/bard/GUI.java @@ -450,19 +450,27 @@ public synchronized void actionPerformed(ActionEvent e) midi.getInstruments(filePath); //InstrumentSelector is = new InstrumentSelector(midi.instruments); - //Update instruments - cmbSelectedInstrument.removeAllItems(); - for(String instrument : midi.instruments){ - cmbSelectedInstrument.addItem(instrument); - } + //cmbSelectedInstrument = new JComboBox(midi.instruments); //int selectedInstrument = is.showDialogue(); System.out.println(cmbSelectedInstrument.getSelectedIndex()); midi.getNotes(filePath, cmbSelectedInstrument.getSelectedIndex(), cmbOctaveTargetCombo.getSelectedIndex()-1, holdCheckBox.isSelected()); - + //Update instruments + cmbSelectedInstrument.removeAllItems(); + for(String instrument : midi.instruments){ + cmbSelectedInstrument.addItem(instrument); + } + taText.setText(midi.getSheet(0, cmbOctaveTargetCombo.getSelectedIndex())); + + DefaultComboBoxModel model = new DefaultComboBoxModel<>(midi.getOctaveQuality(cmbSelectedInstrument.getSelectedIndex())); + cmbOctaveTargetCombo.setModel(model); + cmbOctaveTargetCombo.setSelectedIndex(midi.getHighestQualityOctave(cmbSelectedInstrument.getSelectedIndex())); + taText.setText(midi.getSheet(cmbSelectedInstrument.getSelectedIndex(), cmbOctaveTargetCombo.getSelectedIndex())); + + setOpenFile(frame, new File(filePath).getName()); } catch (Exception ex) { ex.printStackTrace(); @@ -484,11 +492,7 @@ public synchronized void drop(DropTargetDropEvent evt) { midi.getInstruments(filePath); //InstrumentSelector is = new InstrumentSelector(midi.instruments); - //Update instruments - cmbSelectedInstrument.removeAllItems(); - for(String instrument : midi.instruments){ - cmbSelectedInstrument.addItem(instrument); - } + //cmbSelectedInstrument = new JComboBox(midi.instruments); @@ -496,7 +500,20 @@ public synchronized void drop(DropTargetDropEvent evt) { System.out.println(cmbSelectedInstrument.getSelectedIndex()); midi.getNotes(filePath, cmbSelectedInstrument.getSelectedIndex(), cmbOctaveTargetCombo.getSelectedIndex()-1, holdCheckBox.isSelected()); + //Update instruments + cmbSelectedInstrument.removeAllItems(); + for(String instrument : midi.instruments){ + cmbSelectedInstrument.addItem(instrument); + } + taText.setText(midi.getSheet(0, cmbOctaveTargetCombo.getSelectedIndex())); + + DefaultComboBoxModel model = new DefaultComboBoxModel<>(midi.getOctaveQuality(cmbSelectedInstrument.getSelectedIndex())); + cmbOctaveTargetCombo.setModel(model); + cmbOctaveTargetCombo.setSelectedIndex(midi.getHighestQualityOctave(cmbSelectedInstrument.getSelectedIndex())); + taText.setText(midi.getSheet(cmbSelectedInstrument.getSelectedIndex(), cmbOctaveTargetCombo.getSelectedIndex())); + + setOpenFile(frame, new File(filePath).getName()); } catch (Exception ex) { @@ -510,6 +527,11 @@ public synchronized void drop(DropTargetDropEvent evt) { public void itemStateChanged(ItemEvent arg0) { System.out.println("Action: " + cmbSelectedInstrument.getSelectedIndex()); taText.setText(midi.getSheet(cmbSelectedInstrument.getSelectedIndex(), cmbOctaveTargetCombo.getSelectedIndex())); + if(cmbSelectedInstrument.getSelectedIndex() == -1) return; + DefaultComboBoxModel model = new DefaultComboBoxModel<>(midi.getOctaveQuality(cmbSelectedInstrument.getSelectedIndex())); + cmbOctaveTargetCombo.setModel(model); + cmbOctaveTargetCombo.setSelectedIndex(midi.getHighestQualityOctave(cmbSelectedInstrument.getSelectedIndex())); + taText.setText(midi.getSheet(cmbSelectedInstrument.getSelectedIndex(), cmbOctaveTargetCombo.getSelectedIndex())); } }); diff --git a/TBbard/src/bard/MidiParser.java b/TBbard/src/bard/MidiParser.java index 3e7e560..429cbe9 100644 --- a/TBbard/src/bard/MidiParser.java +++ b/TBbard/src/bard/MidiParser.java @@ -22,11 +22,15 @@ public class MidiParser { public static final int NOTE_OFF = 0x80; public static final String[] NOTE_NAMES = {"C", "C#", "D", "Eb", "E", "F", "F#", "G", "G#", "A", "Bb", "B"}; + public static final String []octaves = {"-1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; + String[] instruments; String[][] sheets = new String[16][11]; - + Integer[][] quality = new Integer[16][11]; + Integer[] totalNotes = new Integer[16]; + String filePath; - + String lastNote = ""; int lastOctave; @@ -36,19 +40,21 @@ public MidiParser(String fileName) { } public void parseFile(String filePath){ - + } public void getNotes(String filePath, int instrumentIndex, int octaveTarget, boolean includeHoldRelease) throws Exception{ includeHoldRelease = true; - + for(int ins = 0; ins < 16; ins++){ + totalNotes[ins] = 0; for(int oct = 0; oct < 11; oct++){ - sheets[ins][oct] = ""; + sheets[ins][oct] = ""; + quality[ins][oct] = 0; } } - + Sequence sequence = MidiSystem.getSequence(new File(filePath)); TickConverter converter = new TickConverter(sequence); @@ -56,47 +62,48 @@ public void getNotes(String filePath, int instrumentIndex, int octaveTarget, boo System.out.println("Sequence (MS): " + sequence.getMicrosecondLength()/1000); System.out.println("Sequence (ticks): " + sequence.getTickLength()); System.out.println("Target octave: " + octaveTarget); - + for(String i : instruments){ System.out.println(i); } //if(true) return ""; - - - int trackNumber = 0; + + for (Track track : sequence.getTracks()) { - trackNumber++; + //System.out.println("Track " + trackNumber + ": size = " + track.size()); long prevTick = 0; boolean firstLineDone = false; - + ShortMessage sm = null; for (int i=0; i < track.size(); i++) { MidiEvent event = track.get(i); if((i+1 < track.size()) && (track.get(i+1).getTick() - event.getTick()) < 2 && firstLineDone) continue; - - - + + + MidiMessage message = event.getMessage(); if (message instanceof ShortMessage) { - ShortMessage sm = (ShortMessage) message; + sm = (ShortMessage) message; //System.out.println("Channel: " + sm.getChannel()); - - - - + + + + if ((sm.getCommand() == NOTE_ON) && (sm.getData2() > 0)) { + //trackNumber = track.size(); + totalNotes[sm.getChannel()]++; //int tick = Math.round((event.getTick() - prevTick)*sequence.getTickLength()/(sequence.getMicrosecondLength()/1000)); String line = ""; - + if(firstLineDone)line += "w" + converter.ticksToMillis(event.getTick() - prevTick) + "\n"; prevTick = event.getTick(); @@ -113,84 +120,130 @@ public void getNotes(String filePath, int instrumentIndex, int octaveTarget, boo if(includeHoldRelease) line += "h"; line += noteName; -// if(octave < octaveTarget) sheets[sm.getChannel()] += "-1"; -// if(octave > octaveTarget) sheets[sm.getChannel()] += "+1"; + // if(octave < octaveTarget) sheets[sm.getChannel()] += "-1"; + // if(octave > octaveTarget) sheets[sm.getChannel()] += "+1"; firstLineDone = true; - + for(int o = 0; o < 11; o++){ sheets[sm.getChannel()][o] += line; if(noteName.equals("C") && octave > o+1){ sheets[sm.getChannel()][o] += "+2"; + quality[sm.getChannel()][o]++; } else { if(octave < o) { sheets[sm.getChannel()][o] += "-1"; + if(octave == o-1) quality[sm.getChannel()][o]++; } if(octave > o) { sheets[sm.getChannel()][o] += "+1"; + if(octave == o+1) quality[sm.getChannel()][o]++; } + if(octave == o) quality[sm.getChannel()][o]++; } } - + } else - - if (includeHoldRelease && (sm.getCommand() == NOTE_OFF || (sm.getData2() == 0))) { - - if(i+1 <= track.size() && track.get(i+1).getMessage() instanceof ShortMessage && ((ShortMessage)track.get(i+1).getMessage()).getCommand() == NOTE_ON && (track.get(i+1).getTick() - event.getTick()) < 2){ - continue; - } - - int key = sm.getData1(); - int octave = (key / 12)-1; - int note = key % 12; - String noteName = NOTE_NAMES[note]; - System.out.println("Tick: " + event.getTick() + " Note off, " + noteName + octave + " key=" + key); - System.out.println("Previous note was: " + lastNote); - if(lastNote.equals(noteName) && lastOctave == octave){ - System.out.println("MATCH! Adding hold and release."); - //addPrefixToPrevLine(sm.getChannel(), "h"); - String line = ""; - line += "w" + converter.ticksToMillis(event.getTick() - prevTick) + "\n"; - prevTick = event.getTick(); - for(int o = 0; o < 11; o++){ - sheets[sm.getChannel()][o] += "\n" + line; + + if (includeHoldRelease && (sm.getCommand() == NOTE_OFF || (sm.getData2() == 0))) { + + if(i+1 <= track.size() && track.get(i+1).getMessage() instanceof ShortMessage && ((ShortMessage)track.get(i+1).getMessage()).getCommand() == NOTE_ON && (track.get(i+1).getTick() - event.getTick()) < 2){ + continue; } - addStringToAllSheets(sm.getChannel(), "release"); + + int key = sm.getData1(); + int octave = (key / 12)-1; + int note = key % 12; + String noteName = NOTE_NAMES[note]; + System.out.println("Tick: " + event.getTick() + " Note off, " + noteName + octave + " key=" + key); + System.out.println("Previous note was: " + lastNote); + if(lastNote.equals(noteName) && lastOctave == octave){ + System.out.println("MATCH! Adding hold and release."); + //addPrefixToPrevLine(sm.getChannel(), "h"); + String line = ""; + line += "w" + converter.ticksToMillis(event.getTick() - prevTick) + "\n"; + prevTick = event.getTick(); + for(int o = 0; o < 11; o++){ + sheets[sm.getChannel()][o] += "\n" + line; + } + addStringToAllSheets(sm.getChannel(), "release"); + } + lastNote = ""; + lastOctave = -10; } - lastNote = ""; - lastOctave = -10; - } - + if (sm.getCommand() == NOTE_ON || (includeHoldRelease && sm.getCommand() == NOTE_OFF)) { //prevTick = event.getTick(); addStringToAllSheets(sm.getChannel(), "\n"); } - + } + } + //System.out.println(); + } for(int ins = 0; ins < 16; ins++){ + estimateOctaveQuality(ins); for(int oct = 0; oct < 11; oct++){ - sheets[ins][oct] = sheets[ins][oct].replaceAll("release\nw0\n", ""); - sheets[ins][oct] = sheets[ins][oct].replaceAll("\n+", "\n"); + sheets[ins][oct] = sheets[ins][oct].replaceAll("release\nw0\n", ""); + sheets[ins][oct] = sheets[ins][oct].replaceAll("\n+", "\n"); } } - + + + return; } - + + public String[] getOctaveQuality(int instrument){ + for(int q = 0; q < 11; q++){ + octaves[q] = (q-1) + " (quality: " + (quality[instrument][q]) + "%)"; + System.out.println("Octave quality " + (q-1) + ": " + quality[instrument][q]); + } + return octaves; + } + + private void estimateOctaveQuality(int instrument){ + if(sheets[instrument][0].equals("")) return; + System.out.println("Processing quality of instrument " + instrument); + boolean ignorePrint = true; + for(int q = 0; q < 11; q++){ + if(quality[instrument][q] != 0) ignorePrint = false; + } + + for(int q = 0; q < 11; q++){ + int qualityPercentage = 0; + if(quality[instrument][q] != 0) qualityPercentage = (int)(((float)quality[instrument][q]/(float)totalNotes[instrument])*100); + quality[instrument][q] = qualityPercentage; + octaves[q] = (q-1) + " (quality: " + (qualityPercentage) + "%)"; + if(!ignorePrint) { + System.out.println("Octave quality " + (q-1) + ": " + qualityPercentage); + } + } + //System.out.println("Highest quality is octave " + (getHighestQualityOctave()-1) + " (index=" + getHighestQualityOctave() + ")"); + } + + public int getHighestQualityOctave(int instrument){ + int highest = 6; //Octave 5 is the default + for(int q = 0; q < 11; q++){ + if(quality[instrument][q] > quality[instrument][highest]) highest = q; + } + return highest; + } + private void addPrefixToPrevLine(int instrument, String string) { - + for(int o = 0; o < 11; o++){ String lastNoteConverted = ""; lastNoteConverted += lastNote; if(lastOctave < o) lastNoteConverted += "-1"; if(lastOctave > o) lastNoteConverted += "+1"; - + //System.out.println("\n\n--- CURRENT STATE ---\n" + sheets[instrument][o]); System.out.println("Note converted to: " + lastNoteConverted); sheets[instrument][o] = sheets[instrument][o].substring(0, sheets[instrument][o].lastIndexOf(lastNoteConverted)) + @@ -204,21 +257,21 @@ private void addStringToAllSheets(int instrument, String s){ sheets[instrument][o] += s; } } - + private String convertLastNote(int instrument){ String out = ""; for(int o = 0; o < 11; o++){ out += lastNote; if(lastOctave < o) out += "-1"; if(lastOctave > o) out += "+1"; - + } System.out.println("Last note was converted to: " + out); return out; } public void getInstruments(String filePath) throws Exception { - + Sequence sequence = MidiSystem.getSequence(new File(filePath)); @@ -273,9 +326,11 @@ public void getInstruments(String filePath) throws Exception { } } } - + public String getSheet(int instrument, int octave){ if(instrument == -1 || sheets[instrument][octave] == null) return ""; else return sheets[instrument][octave]; } + + }