some improvements/fixes to the MIDI and NBS converter

This commit is contained in:
Chayapak 2023-09-27 20:16:07 +07:00
parent 7fcd8923fb
commit 1b3c83671f
2 changed files with 28 additions and 1 deletions

View file

@ -16,6 +16,7 @@ import java.nio.file.Paths;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -141,6 +142,32 @@ public class MidiConverter {
break; 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) { if (instrument == null) {

View file

@ -153,7 +153,7 @@ public class NBSConverter {
layerVolume = nbsLayers.get(note.layer).volume; 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 < 33) key += 12;
while (key > 57) key -= 12; while (key > 57) key -= 12;