-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi-parser.dart
51 lines (49 loc) · 1.5 KB
/
midi-parser.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// note: requires flutter_midi_command
import 'package:flutter_midi_command/flutter_midi_command_messages.dart';
MidiMessage parseMidiMessage(List<int> data) {
int status = data[0];
assert(status & 0x80 > 0, 'no STATUS byte');
if (status & 0xf0 == 0x90) {
return NoteOnMessage(
channel: status & 0x0f,
note: data[1],
velocity: data[2],
);
}
if (status & 0xf0 == 0x80) {
return NoteOffMessage(
channel: status & 0x0f,
note: data[1],
velocity: data[2],
);
}
if (status & 0xf0 == 0xc0) {
return PCMessage(
channel: status & 0x0f,
program: data[1],
);
}
if (status & 0xf0 == 0xb0) {
return CCMessage(
channel: status & 0x0f,
controller: data[0],
value: data[1],
);
}
throw FormatException("Unknown status byte 0x${status.toRadixString(16)}");
}
String prettyPrintMidiMessage(MidiMessage message) {
if (message is NoteOnMessage) {
return "(Note ${message.note} on channel ${message.channel} with velocity ${message.velocity} on)";
}
if (message is NoteOffMessage) {
return "(Note ${message.note} on channel ${message.channel} with velocity ${message.velocity} off)";
}
if (message is PCMessage) {
return "(Instrument ${message.program} on channel ${message.channel} selected)";
}
if (message is CCMessage) {
return "(Controller ${message.controller} on channel ${message.channel} set to value ${message.value})";
}
return message.data.map((e) => e.toRadixString(16)).toString();
}