diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java index 764967d..b4bfea5 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java @@ -16,6 +16,7 @@ import java.nio.file.Paths; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -141,6 +142,32 @@ public class MidiConverter { break; } } + + if (instrument == null) { +// instrument = instrumentList[0]; + + // we are finding the closest instrument offset here and use that + // closest instrument as the instrument + + final Integer[] offsets = Arrays.stream(instrumentList).map(ins -> ins.offset).toArray(Integer[]::new); + + // https://stackoverflow.com/questions/13318733/get-closest-value-to-a-number-in-array + int distance = Math.abs(offsets[0] - midiPitch); + int idx = 0; + for (int c = 1; c < offsets.length; c++) { + int cdistance = Math.abs(offsets[c] - midiPitch); + if (cdistance < distance) { + idx = c; + distance = cdistance; + } + } + + final int closest = offsets[idx]; + + instrument = Arrays.stream(instrumentList) + .filter(ins -> ins.offset == closest) + .toArray(Instrument[]::new)[0]; + } } if (instrument == null) { diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java index add90cb..11bad46 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java @@ -153,7 +153,7 @@ public class NBSConverter { layerVolume = nbsLayers.get(note.layer).volume; } - int key = note.key + SongHandler.getInstance().pitch; + int key = (note.key + (note.pitch / 100)) + SongHandler.getInstance().pitch; while (key < 33) key += 12; while (key > 57) key -= 12;