@@ -91,44 +91,59 @@ public MidiData(final MidiHeader header, final InputStream is) throws InvalidMid
9191
9292 if (message instanceof ShortMessage ) {
9393 final ShortMessage shortMessage = (ShortMessage ) message ;
94- if (shortMessage .getCommand () == NOTE_ON ) {
95- final byte instrument = channelInstruments [shortMessage .getChannel ()];
96- final byte key = (byte ) shortMessage .getData1 ();
97- final byte velocity = (byte ) shortMessage .getData2 ();
98- final byte effectiveVelocity = (byte ) ((float ) velocity * channelVolumes [shortMessage .getChannel ()] / MAX_VELOCITY );
99- final byte pan = channelPans [shortMessage .getChannel ()];
100-
101- final MidiNote note ;
102- if (shortMessage .getChannel () == PERCUSSION_CHANNEL ) {
103- final PercussionMapping mapping = MidiMappings .PERCUSSION_MAPPINGS .get (key );
104- if (mapping == null ) continue ;
105-
106- note = new MidiNote (event .getTick (), mapping .getInstrument (), mapping .getKey (), effectiveVelocity , pan );
107- } else {
108- final InstrumentMapping mapping = MidiMappings .INSTRUMENT_MAPPINGS .get (instrument );
109- if (mapping == null ) continue ;
110-
111- final int transposedKey = key - NBS_KEY_OFFSET + KEYS_PER_OCTAVE * mapping .getOctaveModifier ();
112- final byte clampedKey = (byte ) Math .max (NBS_LOWEST_KEY , Math .min (transposedKey , NBS_HIGHEST_KEY ));
113- note = new MidiNote (event .getTick (), mapping .getInstrument (), clampedKey , effectiveVelocity , pan );
114- }
115-
116- this .notes .computeIfAbsent ((int ) Math .round (microTime * SONG_TICKS_PER_SECOND / 1_000_000D ), k -> new ArrayList <>()).add (note );
117- } else if (shortMessage .getCommand () == PROGRAM_CHANGE ) {
118- channelInstruments [shortMessage .getChannel ()] = (byte ) shortMessage .getData1 ();
119- } else if (shortMessage .getCommand () == CONTROL_CHANGE ) {
120- if (shortMessage .getData1 () == VOLUME_CONTROL_MSB ) {
121- channelVolumes [shortMessage .getChannel ()] = (byte ) shortMessage .getData2 ();
122- } else if (shortMessage .getData1 () == PAN_CONTROL_MSB ) {
123- channelPans [shortMessage .getChannel ()] = (byte ) shortMessage .getData2 ();
124- } else if (shortMessage .getData1 () == RESET_CONTROLS ) {
125- channelVolumes [shortMessage .getChannel ()] = MAX_VELOCITY ;
126- channelPans [shortMessage .getChannel ()] = CENTER_PAN ;
127- }
128- } else if (shortMessage .getCommand () == SYSTEM_RESET ) {
129- Arrays .fill (channelInstruments , (byte ) 0 );
130- Arrays .fill (channelVolumes , MAX_VELOCITY );
131- Arrays .fill (channelPans , CENTER_PAN );
94+ switch (shortMessage .getCommand ()) {
95+ case NOTE_ON :
96+ final byte instrument = channelInstruments [shortMessage .getChannel ()];
97+ final byte key = (byte ) shortMessage .getData1 ();
98+ final byte velocity = (byte ) shortMessage .getData2 ();
99+ final byte effectiveVelocity = (byte ) ((float ) velocity * channelVolumes [shortMessage .getChannel ()] / MAX_VELOCITY );
100+ final byte pan = channelPans [shortMessage .getChannel ()];
101+
102+ final MidiNote note ;
103+ if (shortMessage .getChannel () == PERCUSSION_CHANNEL ) {
104+ final PercussionMapping mapping = MidiMappings .PERCUSSION_MAPPINGS .get (key );
105+ if (mapping == null ) continue ;
106+
107+ note = new MidiNote (event .getTick (), mapping .getInstrument (), mapping .getKey (), effectiveVelocity , pan );
108+ } else {
109+ final InstrumentMapping mapping = MidiMappings .INSTRUMENT_MAPPINGS .get (instrument );
110+ if (mapping == null ) continue ;
111+
112+ final int transposedKey = key - NBS_KEY_OFFSET + KEYS_PER_OCTAVE * mapping .getOctaveModifier ();
113+ final byte clampedKey = (byte ) Math .max (NBS_LOWEST_KEY , Math .min (transposedKey , NBS_HIGHEST_KEY ));
114+ note = new MidiNote (event .getTick (), mapping .getInstrument (), clampedKey , effectiveVelocity , pan );
115+ }
116+
117+ this .notes .computeIfAbsent ((int ) Math .round (microTime * SONG_TICKS_PER_SECOND / 1_000_000D ), k -> new ArrayList <>()).add (note );
118+ break ;
119+ case NOTE_OFF :
120+ // Ignore note off events
121+ break ;
122+ case PROGRAM_CHANGE :
123+ channelInstruments [shortMessage .getChannel ()] = (byte ) shortMessage .getData1 ();
124+ break ;
125+ case CONTROL_CHANGE :
126+ switch (shortMessage .getData1 ()) {
127+ case VOLUME_CONTROL_MSB :
128+ channelVolumes [shortMessage .getChannel ()] = (byte ) shortMessage .getData2 ();
129+ break ;
130+ case PAN_CONTROL_MSB :
131+ channelPans [shortMessage .getChannel ()] = (byte ) shortMessage .getData2 ();
132+ break ;
133+ case RESET_CONTROLS :
134+ channelVolumes [shortMessage .getChannel ()] = MAX_VELOCITY ;
135+ channelPans [shortMessage .getChannel ()] = CENTER_PAN ;
136+ break ;
137+ }
138+ break ;
139+ case PITCH_BEND :
140+ // Ignore pitch bend events
141+ break ;
142+ case SYSTEM_RESET :
143+ Arrays .fill (channelInstruments , (byte ) 0 );
144+ Arrays .fill (channelVolumes , MAX_VELOCITY );
145+ Arrays .fill (channelPans , CENTER_PAN );
146+ break ;
132147 }
133148 }
134149 }
0 commit comments