Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/protocol/midi.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 14 апр. 2020 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/status.h>
23 #include <lsp-plug.in/protocol/midi.h>
24 #include <lsp-plug.in/test-fw/utest.h>
25 #include <lsp-plug.in/test-fw/helpers.h>
26 #include <lsp-plug.in/test-fw/ByteBuffer.h>
27
28 namespace lsp
29 {
30 static const uint8_t message[]=
31 {
32 0x9c, 0x3e, 0x3c, // Note on: channel = 0x0c, note = 0x3e, velocity = 0x3c
33 0x83, 0x5a, 0x45, // Note off: channel = 0x03, note = 0x5a, velocity = 0x45
34 0xb2, 0x08, 0x7f, // Controller: channel = 0x02, balance msb = 0x7f
35 0xb2, 0x28, 0x7e, // Controller: channel = 0x02, balance lsb = 0x7e
36 0xb4, 0x40, 0x0f, // Controller: channel = 0x04, sustain = 0x0f
37 0xf1, 0x5c, // MTC Quarter: type = 0x05, value = 0x0c
38 0xae, 0x40, 0x44, // Aftertourch: channel = 0x0e, note = 0x40, velocity = 0x44
39 0xc3, 0x63, // Program change: channel = 0x03, program = 0x63
40 0xd8, 0x55, // Channel pressure: channel = 0x08, pressure = 0x55
41 0xe7, 0x3c, 0x22, // Pitch bend: channel = 0x07, bend = 0x113c
42 0xf2, 0x1e, 0x22, // Song position select: position = 0x111e
43 0xf3, 0x42, // Song select: song = 0x42
44 0xf8, // MIDI Clock
45 0x00 // Invalid message
46 };
47 }
48
49 UTEST_BEGIN("runtime.protocol", midi)
50 void test_decode()
51 {
52 midi::event_t ev;
53 const uint8_t *b = message;
54
55 UTEST_ASSERT(midi::decode(&ev, b) == 3);
56 UTEST_ASSERT((ev.type == midi::MIDI_MSG_NOTE_ON));
57 UTEST_ASSERT((ev.channel == 0x0c) && (ev.note.pitch == 0x3e) && (ev.note.velocity == 0x3c));
58 b += 3;
59
60 UTEST_ASSERT(midi::decode(&ev, b) == 3);
61 UTEST_ASSERT((ev.type == midi::MIDI_MSG_NOTE_OFF));
62 UTEST_ASSERT((ev.channel == 0x03) && (ev.note.pitch == 0x5a) && (ev.note.velocity == 0x45));
63 b += 3;
64
65 UTEST_ASSERT(midi::decode(&ev, b) == 3);
66 UTEST_ASSERT((ev.type == midi::MIDI_MSG_NOTE_CONTROLLER));
67 UTEST_ASSERT((ev.channel == 0x02) && (ev.ctl.control == midi::MIDI_CTL_MSB_BALANCE) && (ev.ctl.value == 0x7f));
68 b += 3;
69
70 UTEST_ASSERT(midi::decode(&ev, b) == 3);
71 UTEST_ASSERT((ev.type == midi::MIDI_MSG_NOTE_CONTROLLER));
72 UTEST_ASSERT((ev.channel == 0x02) && (ev.ctl.control == midi::MIDI_CTL_LSB_BALANCE) && (ev.ctl.value == 0x7e));
73 b += 3;
74
75 UTEST_ASSERT(midi::decode(&ev, b) == 3);
76 UTEST_ASSERT((ev.type == midi::MIDI_MSG_NOTE_CONTROLLER));
77 UTEST_ASSERT((ev.channel == 0x04) && (ev.ctl.control == midi::MIDI_CTL_SUSTAIN) && (ev.ctl.value == 0x0f));
78 b += 3;
79
80 UTEST_ASSERT(midi::decode(&ev, b) == 2);
81 UTEST_ASSERT((ev.type == midi::MIDI_MSG_MTC_QUARTER));
82 UTEST_ASSERT((ev.mtc.type == 0x05) && (ev.mtc.value == 0x0c));
83 b += 2;
84
85 UTEST_ASSERT(midi::decode(&ev, b) == 3);
86 UTEST_ASSERT((ev.type == midi::MIDI_MSG_NOTE_PRESSURE));
87 UTEST_ASSERT((ev.channel == 0x0e) && (ev.atouch.pitch == 0x40) && (ev.atouch.pressure = 0x44));
88 b += 3;
89
90 UTEST_ASSERT(midi::decode(&ev, b) == 2);
91 UTEST_ASSERT((ev.type == midi::MIDI_MSG_PROGRAM_CHANGE));
92 UTEST_ASSERT((ev.channel == 0x03) && (ev.program == 0x63));
93 b += 2;
94
95 UTEST_ASSERT(midi::decode(&ev, b) == 2);
96 UTEST_ASSERT((ev.type == midi::MIDI_MSG_CHANNEL_PRESSURE));
97 UTEST_ASSERT((ev.channel == 0x08) && (ev.chn.pressure = 0x55));
98 b += 2;
99
100 UTEST_ASSERT(midi::decode(&ev, b) == 3);
101 UTEST_ASSERT((ev.type == midi::MIDI_MSG_PITCH_BEND));
102 UTEST_ASSERT((ev.channel == 0x07) && (ev.bend = 0x113c));
103 b += 3;
104
105 UTEST_ASSERT(midi::decode(&ev, b) == 3);
106 UTEST_ASSERT((ev.type == midi::MIDI_MSG_SONG_POS));
107 UTEST_ASSERT((ev.beats = 0x111e));
108 b += 3;
109
110 UTEST_ASSERT(midi::decode(&ev, b) == 2);
111 UTEST_ASSERT((ev.type == midi::MIDI_MSG_SONG_SELECT));
112 UTEST_ASSERT((ev.song = 0x42));
113 b += 2;
114
115 UTEST_ASSERT(midi::decode(&ev, b) == 1);
116 UTEST_ASSERT((ev.type == midi::MIDI_MSG_CLOCK));
117 b += 1;
118
119 UTEST_ASSERT(midi::decode(&ev, b) == -STATUS_CORRUPTED);
120 }
121
122 void test_encode()
123 {
124 uint8_t buf[0x100];
125
126 midi::event_t ev;
127 uint8_t *b = buf;
128
129 ev.type = midi::MIDI_MSG_NOTE_ON;
130 ev.channel = 0x0c;
131 ev.note.pitch = 0x3e;
132 ev.note.velocity = 0x3c;
133 UTEST_ASSERT(midi::size_of(&ev) == 3);
134 UTEST_ASSERT(midi::encode(b, &ev) == 3);
135 b += 3;
136
137 ev.type = midi::MIDI_MSG_NOTE_OFF;
138 ev.channel = 0x03;
139 ev.note.pitch = 0x5a;
140 ev.note.velocity = 0x45;
141 UTEST_ASSERT(midi::size_of(&ev) == 3);
142 UTEST_ASSERT(midi::encode(b, &ev) == 3);
143 b += 3;
144
145 ev.type = midi::MIDI_MSG_NOTE_CONTROLLER;
146 ev.channel = 0x02;
147 ev.ctl.control = midi::MIDI_CTL_MSB_BALANCE;
148 ev.ctl.value = 0x7f;
149 UTEST_ASSERT(midi::size_of(&ev) == 3);
150 UTEST_ASSERT(midi::encode(b, &ev) == 3);
151 b += 3;
152
153 ev.type = midi::MIDI_MSG_NOTE_CONTROLLER;
154 ev.channel = 0x02;
155 ev.ctl.control = midi::MIDI_CTL_LSB_BALANCE;
156 ev.ctl.value = 0x7e;
157 UTEST_ASSERT(midi::size_of(&ev) == 3);
158 UTEST_ASSERT(midi::encode(b, &ev) == 3);
159 b += 3;
160
161 ev.type = midi::MIDI_MSG_NOTE_CONTROLLER;
162 ev.channel = 0x04;
163 ev.ctl.control = midi::MIDI_CTL_SUSTAIN;
164 ev.ctl.value = 0x0f;
165 UTEST_ASSERT(midi::size_of(&ev) == 3);
166 UTEST_ASSERT(midi::encode(b, &ev) == 3);
167 b += 3;
168
169 ev.type = midi::MIDI_MSG_MTC_QUARTER;
170 ev.mtc.type = 0x05;
171 ev.mtc.value = 0x0c;
172 UTEST_ASSERT(midi::size_of(&ev) == 2);
173 UTEST_ASSERT(midi::encode(b, &ev) == 2);
174 b += 2;
175
176 ev.type = midi::MIDI_MSG_NOTE_PRESSURE;
177 ev.channel = 0x0e;
178 ev.atouch.pitch = 0x40;
179 ev.atouch.pressure = 0x44;
180 UTEST_ASSERT(midi::size_of(&ev) == 3);
181 UTEST_ASSERT(midi::encode(b, &ev) == 3);
182 b += 3;
183
184 ev.type = midi::MIDI_MSG_PROGRAM_CHANGE;
185 ev.channel = 0x03;
186 ev.program = 0x63;
187 UTEST_ASSERT(midi::size_of(&ev) == 2);
188 UTEST_ASSERT(midi::encode(b, &ev) == 2);
189 b += 2;
190
191 ev.type = midi::MIDI_MSG_CHANNEL_PRESSURE;
192 ev.channel = 0x08;
193 ev.chn.pressure = 0x55;
194 UTEST_ASSERT(midi::size_of(&ev) == 2);
195 UTEST_ASSERT(midi::encode(b, &ev) == 2);
196 b += 2;
197
198 ev.type = midi::MIDI_MSG_PITCH_BEND;
199 ev.channel = 0x07;
200 ev.bend = 0x113c;
201 UTEST_ASSERT(midi::size_of(&ev) == 3);
202 UTEST_ASSERT(midi::encode(b, &ev) == 3);
203 b += 3;
204
205 ev.type = midi::MIDI_MSG_SONG_POS;
206 ev.beats = 0x111e;
207 UTEST_ASSERT(midi::size_of(&ev) == 3);
208 UTEST_ASSERT(midi::encode(b, &ev) == 3);
209 b += 3;
210
211 ev.type = midi::MIDI_MSG_SONG_SELECT;
212 ev.song = 0x42;
213 UTEST_ASSERT(midi::size_of(&ev) == 2);
214 UTEST_ASSERT(midi::encode(b, &ev) == 2);
215 b += 2;
216
217 ev.type = midi::MIDI_MSG_CLOCK;
218 UTEST_ASSERT(midi::size_of(&ev) == 1);
219 UTEST_ASSERT(midi::encode(b, &ev) == 1);
220 b += 1;
221
222 if (::memcmp(buf, message, b-buf) != 0)
223 {
224 ByteBuffer b1(message, b-buf);
225 ByteBuffer b2(buf, b-buf);
226
227 b1.dump("msg");
228 b2.dump("buf");
229 UTEST_FAIL_MSG("Failed comparison of byte buffers");
230 }
231 }
232
233 UTEST_MAIN
234 {
235 #define CALL(v) printf("Executing " #v "...\n"); v();
236
237 CALL(test_decode);
238 CALL(test_encode);
239 }
240 UTEST_END;
241
242
243