From 1b3c83671fc647cea7c8828a1c1e4c26b840d3c4 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Wed, 27 Sep 2023 20:16:07 +0700 Subject: [PATCH] some improvements/fixes to the MIDI and NBS converter --- .../songplayer/conversion/MidiConverter.java | 27 +++++++++++++++++++ .../songplayer/conversion/NBSConverter.java | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) 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;