Skip to content

Commit

Permalink
Handle MIDI track name and copyright meta message
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Jan 2, 2025
1 parent 2e70c66 commit 7326094
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

public class MidiDefinitions {

public static final int SET_TEMPO = 0x51;
public static final int META_COPYRIGHT_NOTICE = 0x02;
public static final int META_TRACK_NAME = 0x03;
public static final int META_SET_TEMPO = 0x51;

public static final int PERCUSSION_CHANNEL = 9;
public static final int VOLUME_CONTROL_MSB = 0x07;
public static final int PAN_CONTROL_MSB = 0x0A;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.sound.midi.*;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static javax.sound.midi.ShortMessage.*;
Expand Down Expand Up @@ -119,10 +120,14 @@ public static MidiSong readSong(final InputStream is, final String fileName) thr
}
} else if (message instanceof MetaMessage) {
final MetaMessage metaMessage = (MetaMessage) message;
if (metaMessage.getType() == SET_TEMPO && metaMessage.getData().length == 3) {
if (metaMessage.getType() == META_SET_TEMPO && metaMessage.getData().length == 3) {
final int newMpq = ((metaMessage.getData()[0] & 0xFF) << 16) | ((metaMessage.getData()[1] & 0xFF) << 8) | (metaMessage.getData()[2] & 0xFF);
final double microsPerTick = (double) newMpq / sequence.getResolution();
song.getTempoEvents().setTempo((int) event.getTick(), (float) (1_000_000D / microsPerTick));
} else if (metaMessage.getType() == META_COPYRIGHT_NOTICE) {
song.setAuthor(new String(metaMessage.getData(), StandardCharsets.US_ASCII));
} else if (metaMessage.getType() == META_TRACK_NAME) {
song.setTitle(new String(metaMessage.getData(), StandardCharsets.US_ASCII));
}
}
}
Expand Down

0 comments on commit 7326094

Please sign in to comment.