This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmidi.zig
123 lines (107 loc) · 3.11 KB
/
midi.zig
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const std = @import("std");
const mem = std.mem;
const midi = @This();
pub const decode = @import("midi/decode.zig");
pub const encode = @import("midi/encode.zig");
pub const file = @import("midi/file.zig");
pub const File = file.File;
test {
_ = @import("midi/test.zig");
std.testing.refAllDecls(@This());
}
pub const Message = struct {
status: u7,
values: [2]u7,
pub fn kind(message: Message) Kind {
const _kind: u3 = @truncate(message.status >> 4);
const _channel: u4 = @truncate(message.status);
return switch (_kind) {
0x0 => Kind.NoteOff,
0x1 => Kind.NoteOn,
0x2 => Kind.PolyphonicKeyPressure,
0x3 => Kind.ControlChange,
0x4 => Kind.ProgramChange,
0x5 => Kind.ChannelPressure,
0x6 => Kind.PitchBendChange,
0x7 => switch (_channel) {
0x0 => Kind.ExclusiveStart,
0x1 => Kind.MidiTimeCodeQuarterFrame,
0x2 => Kind.SongPositionPointer,
0x3 => Kind.SongSelect,
0x6 => Kind.TuneRequest,
0x7 => Kind.ExclusiveEnd,
0x8 => Kind.TimingClock,
0xA => Kind.Start,
0xB => Kind.Continue,
0xC => Kind.Stop,
0xE => Kind.ActiveSensing,
0xF => Kind.Reset,
0x4, 0x5, 0x9, 0xD => Kind.Undefined,
},
};
}
pub fn channel(message: Message) ?u4 {
const _kind = message.kind();
const _channel: u4 = @truncate(message.status);
switch (_kind) {
// Channel events
.NoteOff,
.NoteOn,
.PolyphonicKeyPressure,
.ControlChange,
.ProgramChange,
.ChannelPressure,
.PitchBendChange,
=> return _channel,
// System events
.ExclusiveStart,
.MidiTimeCodeQuarterFrame,
.SongPositionPointer,
.SongSelect,
.TuneRequest,
.ExclusiveEnd,
.TimingClock,
.Start,
.Continue,
.Stop,
.ActiveSensing,
.Reset,
=> return null,
.Undefined => return null,
}
}
pub fn value(message: Message) u14 {
// TODO: Is this the right order according to the midi spec?
return @as(u14, message.values[0]) << 7 | message.values[1];
}
pub fn setValue(message: *Message, v: u14) void {
message.values = .{
@truncate(v >> 7),
@truncate(v),
};
}
pub const Kind = enum {
// Channel events
NoteOff,
NoteOn,
PolyphonicKeyPressure,
ControlChange,
ProgramChange,
ChannelPressure,
PitchBendChange,
// System events
ExclusiveStart,
MidiTimeCodeQuarterFrame,
SongPositionPointer,
SongSelect,
TuneRequest,
ExclusiveEnd,
TimingClock,
Start,
Continue,
Stop,
ActiveSensing,
Reset,
Undefined,
};
};