public static void decode_to_int() { Assert.Equal(0, VariableLengthUtil.decode_to_int(new ByteList() { 0x00 })); Assert.Equal(0x40, VariableLengthUtil.decode_to_int(new ByteList() { 0x40 })); Assert.Equal(0x7F, VariableLengthUtil.decode_to_int(new ByteList() { 0x7F })); Assert.Equal(0x80, VariableLengthUtil.decode_to_int(new ByteList() { 0x81, 0x00 })); Assert.Equal(0x2000, VariableLengthUtil.decode_to_int(new ByteList() { 0xC0, 0x00 })); Assert.Equal(0x3FFF, VariableLengthUtil.decode_to_int(new ByteList() { 0xFF, 0x7F })); Assert.Equal(0x1FFFFF, VariableLengthUtil.decode_to_int(new ByteList() { 0xFF, 0xFF, 0x7F })); Assert.Equal(0x08000000, VariableLengthUtil.decode_to_int(new ByteList() { 0xC0, 0x80, 0x80, 0x00 })); Assert.Equal(0x0FFFFFFF, VariableLengthUtil.decode_to_int(new ByteList() { 0xFF, 0xFF, 0xFF, 0x7F })); }
public static void decode() { Assert.Equal(new ByteList() { 0x00 }, VariableLengthUtil.decode(new ByteList() { 0x00 })); Assert.Equal(new ByteList() { 0x40 }, VariableLengthUtil.decode(new ByteList() { 0x40 })); Assert.Equal(new ByteList() { 0x7F }, VariableLengthUtil.decode(new ByteList() { 0x7F })); Assert.Equal(new ByteList() { 0x80 }, VariableLengthUtil.decode(new ByteList() { 0x81, 0x00 })); Assert.Equal(new ByteList() { 0x20, 0x00 }, VariableLengthUtil.decode(new ByteList() { 0xC0, 0x00 })); Assert.Equal(new ByteList() { 0x3F, 0xFF }, VariableLengthUtil.decode(new ByteList() { 0xFF, 0x7F })); Assert.Equal(new ByteList() { 0x1F, 0xFF, 0xFF }, VariableLengthUtil.decode(new ByteList() { 0xFF, 0xFF, 0x7F })); Assert.Equal(new ByteList() { 0x08, 0x00, 0x00, 0x00 }, VariableLengthUtil.decode(new ByteList() { 0xC0, 0x80, 0x80, 0x00 })); Assert.Equal(new ByteList() { 0x0F, 0xFF, 0xFF, 0xFF }, VariableLengthUtil.decode(new ByteList() { 0xFF, 0xFF, 0xFF, 0x7F })); }
private static MIDIEvent_Length_Tuple next_event(ByteEnumerable track_data, int start_index, byte last_midi_channel) { var i = start_index - 1; MidiEvent midi_event = null; { var delta_time = 0; { var length_temp = new ByteList(); do { i += 1; length_temp.Add(track_data.ElementAt(i)); } while (track_data.ElementAt(i) > 0x7F); delta_time = VariableLengthUtil.decode_to_int(length_temp); } i += 1; var event_type_value = track_data.ElementAt(i); // MIDI Channel Events if ((event_type_value & 0xF0) < 0xF0) { var midi_channel_event_type = (byte)(event_type_value & 0xF0); var midi_channel = (byte)(event_type_value & 0x0F); i += 1; var parameter_1 = track_data.ElementAt(i); var parameter_2 = (byte)0x00; // One or two parameter type switch (midi_channel_event_type) { // One parameter types case 0xC0: midi_event = new ProgramChangeEvent(delta_time, midi_channel, parameter_1); last_midi_channel = midi_channel; break; case 0xD0: midi_event = new ChannelAftertouchEvent(delta_time, midi_channel, parameter_1); last_midi_channel = midi_channel; break; // Two parameter types case 0x80: i += 1; parameter_2 = track_data.ElementAt(i); midi_event = new NoteOffEvent(delta_time, midi_channel, parameter_1, parameter_2); last_midi_channel = midi_channel; break; case 0x90: i += 1; parameter_2 = track_data.ElementAt(i); midi_event = new NoteOnEvent(delta_time, midi_channel, parameter_1, parameter_2); last_midi_channel = midi_channel; break; case 0xA0: i += 1; parameter_2 = track_data.ElementAt(i); midi_event = new NoteAftertouchEvent(delta_time, midi_channel, parameter_1, parameter_2); last_midi_channel = midi_channel; break; case 0xB0: i += 1; parameter_2 = track_data.ElementAt(i); midi_event = new ControllerEvent(delta_time, midi_channel, parameter_1, parameter_2); last_midi_channel = midi_channel; break; case 0xE0: i += 1; parameter_2 = track_data.ElementAt(i); midi_event = new PitchBendEvent(delta_time, midi_channel, parameter_1, parameter_2); last_midi_channel = midi_channel; break; // Might be a Control Change Messages LSB default: midi_event = new ControllerEvent(delta_time, last_midi_channel, event_type_value, parameter_1); break; } i += 1; } // Meta Events else if (event_type_value == 0xFF) { i += 1; var meta_event_type = track_data.ElementAt(i); i += 1; var meta_event_length = track_data.ElementAt(i); i += 1; var meta_event_data = Enumerable.Range(i, meta_event_length).Select(b => track_data.ElementAt(b)).ToArray(); switch (meta_event_type) { case 0x00: midi_event = new SequenceNumberEvent(BitConverter.ToUInt16(meta_event_data.Reverse().ToArray <byte>(), 0)); break; case 0x01: midi_event = new TextEvent(delta_time, stringEncoder.GetString(meta_event_data)); break; case 0x02: midi_event = new CopyrightNoticeEvent(stringEncoder.GetString(meta_event_data)); break; case 0x03: midi_event = new SequenceOrTrackNameEvent(stringEncoder.GetString(meta_event_data)); break; case 0x04: midi_event = new InstrumentNameEvent(delta_time, stringEncoder.GetString(meta_event_data)); break; case 0x05: midi_event = new LyricsEvent(delta_time, stringEncoder.GetString(meta_event_data)); break; case 0x06: midi_event = new MarkerEvent(delta_time, stringEncoder.GetString(meta_event_data)); break; case 0x07: midi_event = new CuePointEvent(delta_time, stringEncoder.GetString(meta_event_data)); break; case 0x20: midi_event = new MIDIChannelPrefixEvent(delta_time, meta_event_data[0]); break; case 0x2F: midi_event = new EndOfTrackEvent(delta_time); break; case 0x51: var tempo = (meta_event_data[2] & 0x0F) + ((meta_event_data[2] & 0xF0) * 16) + ((meta_event_data[1] & 0x0F) * 256) + ((meta_event_data[1] & 0xF0) * 4096) + ((meta_event_data[0] & 0x0F) * 65536) + ((meta_event_data[0] & 0xF0) * 1048576); midi_event = new SetTempoEvent(delta_time, tempo); break; case 0x54: midi_event = new SMPTEOffsetEvent(delta_time, meta_event_data[0], meta_event_data[1], meta_event_data[2], meta_event_data[3], meta_event_data[4]); break; case 0x58: midi_event = new TimeSignatureEvent(delta_time, meta_event_data[0], meta_event_data[1], meta_event_data[2], meta_event_data[3]); break; case 0x59: midi_event = new KeySignatureEvent(delta_time, meta_event_data[0], meta_event_data[1]); break; case 0x7F: midi_event = new SequencerSpecificEvent(delta_time, meta_event_data); break; } i += meta_event_length; } // System Exclusive Events else if (event_type_value == 0xF0 || event_type_value == 0xF7) { var event_length = 0; { var length_temp = new ByteList(); do { i += 1; length_temp.Add(track_data.ElementAt(i)); } while (track_data.ElementAt(i) > 0x7F); event_length = VariableLengthUtil.decode_to_int(length_temp); } i += 1; var event_data = Enumerable.Range(i, event_length).Select(b => track_data.ElementAt(b)); midi_event = new SysexEvent(delta_time, event_type_value, event_data); i += event_length; } } switch (midi_event != null) { case true: return(new MIDIEvent_Length_Tuple(new SomeMidiEvent(midi_event), i - start_index, last_midi_channel)); } return(new MIDIEvent_Length_Tuple(new NoMidiEvent(), i - start_index, last_midi_channel)); }