Skip to content

Commit fadcbda

Browse files
committed
Cleaned up and unified various data types
1 parent 6ec49e4 commit fadcbda

22 files changed

Lines changed: 398 additions & 347 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ NoteBlockLib.writeSong(newSong, new File("output.nbs"));
115115
Song mySong = new GenericSong();
116116
mySong.setTitle("My song");
117117
mySong.getTempoEvents().set(0, 10F); // set the tempo to 10 ticks per second
118-
mySong.getNotes().add(0, new Note().setInstrument(MinecraftInstrument.HARP).setNbsKey((byte) 46));
119-
mySong.getNotes().add(5, new Note().setInstrument(MinecraftInstrument.BASS).setNbsKey((byte) 60));
120-
mySong.getNotes().add(8, new Note().setInstrument(MinecraftInstrument.BIT).setNbsKey((byte) 84));
118+
mySong.getNotes().add(0, new Note().setInstrument(MinecraftInstrument.HARP).setNbsKey(46));
119+
mySong.getNotes().add(5, new Note().setInstrument(MinecraftInstrument.BASS).setNbsKey(60));
120+
mySong.getNotes().add(8, new Note().setInstrument(MinecraftInstrument.BIT).setNbsKey(84));
121121
Song nbsSong = NoteBlockLib.convertSong(mySong, SongFormat.NBS);
122122
NoteBlockLib.writeSong(nbsSong, new File("output.nbs"));
123123
```

src/main/java/net/raphimc/noteblocklib/data/MinecraftDefinitions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class MinecraftDefinitions {
6262
* @param note The note to clamp
6363
*/
6464
public static void clampNoteKey(final Note note) {
65-
note.setMidiKey(Math.max(MC_LOWEST_MIDI_KEY, Math.min(MC_HIGHEST_MIDI_KEY, note.getMidiKey())));
65+
note.setMidiKey(Math.max(MC_LOWEST_MIDI_KEY, Math.min(note.getMidiKey(), MC_HIGHEST_MIDI_KEY)));
6666
}
6767

6868
/**

src/main/java/net/raphimc/noteblocklib/data/MinecraftInstrument.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ public enum MinecraftInstrument implements Instrument {
3838
BANJO(14, 14, "block.note_block.banjo"),
3939
PLING(15, 15, "block.note_block.pling");
4040

41-
private final byte nbsId;
42-
private final byte mcId;
41+
private final int nbsId;
42+
private final int mcId;
4343
private final String mcSoundName;
4444

4545
MinecraftInstrument(final int nbsId, final int mcId, final String mcSoundName) {
46-
this.nbsId = (byte) nbsId;
47-
this.mcId = (byte) mcId;
46+
this.nbsId = nbsId;
47+
this.mcId = mcId;
4848
this.mcSoundName = mcSoundName;
4949
}
5050

51-
public static MinecraftInstrument fromNbsId(final byte nbsId) {
51+
public static MinecraftInstrument fromNbsId(final int nbsId) {
5252
for (final MinecraftInstrument instrument : MinecraftInstrument.values()) {
5353
if (instrument.nbsId == nbsId) {
5454
return instrument;
@@ -57,7 +57,7 @@ public static MinecraftInstrument fromNbsId(final byte nbsId) {
5757
return null;
5858
}
5959

60-
public static MinecraftInstrument fromMcId(final byte mcId) {
60+
public static MinecraftInstrument fromMcId(final int mcId) {
6161
for (final MinecraftInstrument instrument : MinecraftInstrument.values()) {
6262
if (instrument.mcId == mcId) {
6363
return instrument;
@@ -75,11 +75,11 @@ public static MinecraftInstrument fromMcSoundName(final String mcSoundName) {
7575
return null;
7676
}
7777

78-
public byte nbsId() {
78+
public int nbsId() {
7979
return this.nbsId;
8080
}
8181

82-
public byte mcId() {
82+
public int mcId() {
8383
return this.mcId;
8484
}
8585

src/main/java/net/raphimc/noteblocklib/format/futureclient/model/FutureClientNote.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public FutureClientNote setInstrument(final byte instrument) {
3333
return this;
3434
}
3535

36+
public FutureClientNote setInstrument(final int instrument) {
37+
if (instrument < Byte.MIN_VALUE || instrument > Byte.MAX_VALUE) {
38+
throw new IllegalArgumentException("Instrument must be between " + Byte.MIN_VALUE + " and " + Byte.MAX_VALUE);
39+
}
40+
return this.setInstrument((byte) instrument);
41+
}
42+
3643
public byte getKey() {
3744
return this.key;
3845
}
@@ -42,6 +49,13 @@ public FutureClientNote setKey(final byte key) {
4249
return this;
4350
}
4451

52+
public FutureClientNote setKey(final int key) {
53+
if (key < Byte.MIN_VALUE || key > Byte.MAX_VALUE) {
54+
throw new IllegalArgumentException("Key must be between " + Byte.MIN_VALUE + " and " + Byte.MAX_VALUE);
55+
}
56+
return this.setKey((byte) key);
57+
}
58+
4559
public FutureClientNote copy() {
4660
final FutureClientNote copyNote = new FutureClientNote();
4761
copyNote.setInstrument(this.getInstrument());

src/main/java/net/raphimc/noteblocklib/format/mcsp/McSpIo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public static McSpSong readSong(final InputStream is, final String fileName) {
5454
if (instrument == 0) continue;
5555

5656
final McSpNote note = new McSpNote();
57-
note.setInstrument((byte) (instrument - 1));
58-
note.setKey((byte) key);
57+
note.setInstrument(instrument - 1);
58+
note.setKey(key);
5959
noteArray[i] = note;
6060
}
6161
notes.put(tick, noteArray);

src/main/java/net/raphimc/noteblocklib/format/mcsp/model/McSpNote.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121

2222
public class McSpNote {
2323

24-
private byte instrument;
25-
private byte key;
24+
private int instrument;
25+
private int key;
2626

27-
public byte getInstrument() {
27+
public int getInstrument() {
2828
return this.instrument;
2929
}
3030

31-
public McSpNote setInstrument(final byte instrument) {
31+
public McSpNote setInstrument(final int instrument) {
3232
this.instrument = instrument;
3333
return this;
3434
}
3535

36-
public byte getKey() {
36+
public int getKey() {
3737
return this.key;
3838
}
3939

40-
public McSpNote setKey(final byte key) {
40+
public McSpNote setKey(final int key) {
4141
this.key = key;
4242
return this;
4343
}

src/main/java/net/raphimc/noteblocklib/format/mcsp2/McSp2Converter.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package net.raphimc.noteblocklib.format.mcsp2;
1919

20-
import net.raphimc.noteblocklib.data.MinecraftDefinitions;
20+
import com.google.common.collect.Sets;
2121
import net.raphimc.noteblocklib.data.MinecraftInstrument;
2222
import net.raphimc.noteblocklib.format.mcsp2.model.McSp2Layer;
2323
import net.raphimc.noteblocklib.format.mcsp2.model.McSp2Note;
@@ -27,12 +27,12 @@
2727
import net.raphimc.noteblocklib.model.Song;
2828
import net.raphimc.noteblocklib.util.SongResampler;
2929

30-
import java.util.Arrays;
3130
import java.util.List;
31+
import java.util.Set;
3232

3333
public class McSp2Converter {
3434

35-
private static final List<MinecraftInstrument> SUPPORTED_INSTRUMENTS = Arrays.asList(MinecraftInstrument.HARP, MinecraftInstrument.BASS, MinecraftInstrument.BASS_DRUM, MinecraftInstrument.SNARE, MinecraftInstrument.HAT);
35+
public static final Set<MinecraftInstrument> SUPPORTED_INSTRUMENTS = Sets.immutableEnumSet(MinecraftInstrument.HARP, MinecraftInstrument.BASS, MinecraftInstrument.BASS_DRUM, MinecraftInstrument.SNARE, MinecraftInstrument.HAT);
3636

3737
/**
3838
* Creates a new MCSP2 song from the general data of the given song (Also copies some format specific fields if applicable).
@@ -42,7 +42,7 @@ public class McSp2Converter {
4242
*/
4343
public static McSp2Song createSong(Song song) {
4444
song = song.copy();
45-
SongResampler.changeTickSpeed(song, Math.max(McSp2Definitions.MIN_TEMPO, Math.min(McSp2Definitions.MAX_TEMPO, Math.round(song.getTempoEvents().get(0)))));
45+
SongResampler.changeTickSpeed(song, Math.max(McSp2Definitions.MIN_TEMPO, Math.min(Math.round(song.getTempoEvents().get(0)), McSp2Definitions.MAX_TEMPO)));
4646

4747
final McSp2Song newSong = new McSp2Song();
4848
newSong.copyGeneralData(song);
@@ -52,10 +52,10 @@ public static McSp2Song createSong(Song song) {
5252
final List<Note> notes = song.getNotes().get(tick);
5353
for (int i = 0; i < notes.size(); i++) {
5454
final Note note = notes.get(i);
55-
if (note.getInstrument() instanceof MinecraftInstrument && SUPPORTED_INSTRUMENTS.contains((MinecraftInstrument) note.getInstrument()) && note.getVolume() > 0) {
55+
if (note.getInstrument() instanceof MinecraftInstrument && note.getVolume() > 0) {
5656
final McSp2Note mcSp2Note = new McSp2Note();
5757
mcSp2Note.setInstrument(((MinecraftInstrument) note.getInstrument()).nbsId());
58-
mcSp2Note.setKey((byte) Math.max(MinecraftDefinitions.MC_LOWEST_KEY, Math.min(MinecraftDefinitions.MC_HIGHEST_KEY, note.getMcKey())));
58+
mcSp2Note.setKey(note.getMcKey());
5959

6060
final McSp2Layer mcSp2Layer = newSong.getLayers().computeIfAbsent(i, k -> new McSp2Layer());
6161
mcSp2Layer.getNotes().put(tick, mcSp2Note);
@@ -66,15 +66,14 @@ public static McSp2Song createSong(Song song) {
6666
if (song instanceof McSp2Song) {
6767
final McSp2Song mcSp2Song = (McSp2Song) song;
6868
newSong.setAutoSaveInterval(mcSp2Song.getAutoSaveInterval());
69-
newSong.setAutoSaveInterval((byte) mcSp2Song.getAutoSaveInterval());
7069
newSong.setMinutesSpent(mcSp2Song.getMinutesSpent());
7170
newSong.setLeftClicks(mcSp2Song.getLeftClicks());
7271
newSong.setRightClicks(mcSp2Song.getRightClicks());
7372
newSong.setNoteBlocksAdded(mcSp2Song.getNoteBlocksAdded());
7473
newSong.setNoteBlocksRemoved(mcSp2Song.getNoteBlocksRemoved());
7574
} else if (song instanceof NbsSong) {
7675
final NbsSong nbsSong = (NbsSong) song;
77-
newSong.setAutoSaveInterval(nbsSong.isAutoSave() ? nbsSong.getAutoSaveInterval() : (byte) 0);
76+
newSong.setAutoSaveInterval(nbsSong.isAutoSave() ? nbsSong.getAutoSaveInterval() : 0);
7877
newSong.setMinutesSpent(nbsSong.getMinutesSpent());
7978
newSong.setLeftClicks(nbsSong.getLeftClicks());
8079
newSong.setRightClicks(nbsSong.getRightClicks());

src/main/java/net/raphimc/noteblocklib/format/mcsp2/model/McSp2Note.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,27 @@ public class McSp2Note {
2626
private byte instrument;
2727
private byte key;
2828

29-
public byte getInstrument() {
29+
public int getInstrument() {
3030
return this.instrument;
3131
}
3232

33-
public McSp2Note setInstrument(final byte instrument) {
34-
this.instrument = instrument;
33+
public McSp2Note setInstrument(final int instrument) {
34+
if (instrument < 0 || instrument > 4) {
35+
throw new IllegalArgumentException("Instrument must be between 0 and 4");
36+
}
37+
this.instrument = (byte) instrument;
3538
return this;
3639
}
3740

38-
public byte getKey() {
41+
public int getKey() {
3942
return this.key;
4043
}
4144

42-
public McSp2Note setKey(final byte key) {
43-
this.key = key;
45+
public McSp2Note setKey(final int key) {
46+
if (key < 0 || key > 24) {
47+
throw new IllegalArgumentException("Key must be between 0 and 24");
48+
}
49+
this.key = (byte) key;
4450
return this;
4551
}
4652

src/main/java/net/raphimc/noteblocklib/format/midi/MidiDefinitions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class MidiDefinitions {
2828
public static final int PAN_CONTROL_MSB = 0x0A;
2929
public static final int RESET_CONTROLS = 0x79;
3030

31+
public static final int KEY_COUNT = 128;
3132
public static final int CHANNEL_COUNT = 16;
3233
public static final int DEFAULT_TEMPO_MPQ = 500_000;
3334
public static final byte MAX_VELOCITY = 127;

src/main/java/net/raphimc/noteblocklib/format/midi/MidiIo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ public static MidiSong parseSong(final Sequence sequence, final String fileName)
7777

7878
final Note note = new Note();
7979
if (shortMessage.getChannel() == PERCUSSION_CHANNEL) {
80-
final PercussionMapping mapping = MidiMappings.PERCUSSION_MAPPINGS.get(key);
80+
final PercussionMapping mapping = MidiMappings.PERCUSSION_MAPPINGS[key];
8181
if (mapping == null) continue;
8282

8383
note.setInstrument(mapping.getInstrument());
8484
note.setNbsKey(mapping.getNbsKey());
8585
} else {
86-
final InstrumentMapping mapping = MidiMappings.INSTRUMENT_MAPPINGS.get(instrument);
86+
final InstrumentMapping mapping = MidiMappings.INSTRUMENT_MAPPINGS[instrument];
8787
if (mapping == null) continue;
8888

8989
note.setInstrument(mapping.getInstrument());
90-
note.setMidiKey(Math.max(NbsDefinitions.NBS_LOWEST_MIDI_KEY, Math.min(NbsDefinitions.NBS_HIGHEST_MIDI_KEY, key + Constants.KEYS_PER_OCTAVE * mapping.getOctaveModifier())));
90+
note.setMidiKey(Math.max(NbsDefinitions.NBS_LOWEST_MIDI_KEY, Math.min(key + Constants.KEYS_PER_OCTAVE * mapping.getOctaveModifier(), NbsDefinitions.NBS_HIGHEST_MIDI_KEY)));
9191
}
9292
note.setVolume(((float) velocity / MAX_VELOCITY) * (float) channelVolumes[shortMessage.getChannel()] / MAX_VELOCITY);
9393
note.setPanning((float) (pan - CENTER_PAN) / CENTER_PAN);

0 commit comments

Comments
 (0)