Fix last commit
[carla.git] / source / tests.old / EngineEvents.cpp
blob218108da2ba0c5595be8c7fb0be1441c27ced730
1 /*
2 * Carla Tests
3 * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaEngine.hpp"
19 #include "CarlaMIDI.h"
20 #include "CarlaUtils.hpp"
22 CARLA_BACKEND_USE_NAMESPACE
24 void testControlEventDump()
26 uint8_t size;
27 uint8_t data[3];
29 EngineControlEvent e;
31 // test null event
32 e.type = kEngineControlEventTypeNull;
33 e.dumpToMidiData(0, size, data);
34 assert(size == 0);
36 // test regular param event (half value)
37 e.type = kEngineControlEventTypeParameter;
38 e.param = MIDI_CONTROL_MODULATION_WHEEL;
39 e.value = 0.5f;
40 e.dumpToMidiData(0, size, data);
41 assert(size == 3);
42 assert(data[0] == MIDI_STATUS_CONTROL_CHANGE);
43 assert(data[1] == MIDI_CONTROL_MODULATION_WHEEL);
44 assert(data[2] == (MAX_MIDI_VALUE-1)/2);
46 // test regular param event (out of bounds positive)
47 e.value = 9.0f;
48 e.dumpToMidiData(0, size, data);
49 assert(data[2] == MAX_MIDI_VALUE-1);
51 // test regular param event (out of bounds negative)
52 e.value = -11.0f;
53 e.dumpToMidiData(0, size, data);
54 assert(data[2] == 0);
56 // test bad param as bank select
57 e.param = MIDI_CONTROL_BANK_SELECT;
58 e.value = 100;
59 e.dumpToMidiData(0, size, data);
60 assert(size == 3);
61 assert(data[1] == MIDI_CONTROL_BANK_SELECT);
62 assert(data[2] == 100);
64 // test bad param as bank select (out of bounds index positive)
65 e.value = 9999;
66 e.dumpToMidiData(0, size, data);
67 assert(data[2] == MAX_MIDI_VALUE-1);
69 // test bad param as bank select (out of bounds index negative)
70 e.value = -9999;
71 e.dumpToMidiData(0, size, data);
72 assert(data[2] == 0);
74 // test bad param (out of bounds index)
75 e.param = 999;
76 e.dumpToMidiData(0, size, data);
77 assert(size == 0);
79 // test regular midi-bank event
80 e.type = kEngineControlEventTypeMidiBank;
81 e.param = 11;
82 e.dumpToMidiData(0, size, data);
83 assert(size == 3);
84 assert(data[0] == MIDI_STATUS_CONTROL_CHANGE);
85 assert(data[1] == MIDI_CONTROL_BANK_SELECT);
86 assert(data[2] == 11);
88 // test bad midi-bank event (out of bounds)
89 e.param = 300;
90 e.dumpToMidiData(0, size, data);
91 assert(data[2] == MAX_MIDI_VALUE-1);
93 // test bad midi-bank event (out of bounds #2)
94 e.param = 32867;
95 e.dumpToMidiData(0, size, data);
96 assert(data[2] == MAX_MIDI_VALUE-1);
98 // test regular midi-program event
99 e.type = kEngineControlEventTypeMidiProgram;
100 e.param = 15;
101 e.dumpToMidiData(0, size, data);
102 assert(size == 2);
103 assert(data[0] == MIDI_STATUS_PROGRAM_CHANGE);
104 assert(data[1] == 15);
106 // test bad midi-program event (out of bounds)
107 e.param = 299;
108 e.dumpToMidiData(0, size, data);
109 assert(data[1] == MAX_MIDI_VALUE-1);
111 // test regular all-sound-off event
112 e.type = kEngineControlEventTypeAllSoundOff;
113 e.dumpToMidiData(0, size, data);
114 assert(size == 2);
115 assert(data[0] == MIDI_STATUS_CONTROL_CHANGE);
116 assert(data[1] == MIDI_CONTROL_ALL_SOUND_OFF);
118 // test regular all-notes-off event
119 e.type = kEngineControlEventTypeAllNotesOff;
120 e.dumpToMidiData(0, size, data);
121 assert(size == 2);
122 assert(data[0] == MIDI_STATUS_CONTROL_CHANGE);
123 assert(data[1] == MIDI_CONTROL_ALL_NOTES_OFF);
125 // test channel bit
126 e.dumpToMidiData(0, size, data);
127 assert(MIDI_GET_CHANNEL_FROM_DATA(data) == 0);
128 e.dumpToMidiData(1, size, data);
129 assert(MIDI_GET_CHANNEL_FROM_DATA(data) == 1);
130 e.dumpToMidiData(15, size, data);
131 assert(MIDI_GET_CHANNEL_FROM_DATA(data) == 15);
132 e.dumpToMidiData(16, size, data);
133 assert(MIDI_GET_CHANNEL_FROM_DATA(data) == 0);
134 e.dumpToMidiData(17, size, data);
135 assert(MIDI_GET_CHANNEL_FROM_DATA(data) == 1);
136 e.dumpToMidiData(31, size, data);
137 assert(MIDI_GET_CHANNEL_FROM_DATA(data) == 15);
140 void testEventMidiFill()
142 uint8_t size;
143 uint8_t data[25];
145 //EngineControlEvent c;
146 EngineEvent e;
147 e.time = 0;
149 // test safety first
150 e.fillFromMidiData(0, data);
151 assert(e.type == kEngineEventTypeNull);
152 assert(e.channel == 0);
153 e.fillFromMidiData(50, nullptr);
154 assert(e.type == kEngineEventTypeNull);
155 assert(e.channel == 0);
157 // test bad midi event
158 size = 3;
159 data[0] = 0x45;
160 data[1] = 100;
161 data[2] = 100;
162 e.fillFromMidiData(size, data);
163 assert(e.type == kEngineEventTypeNull);
164 assert(e.channel == 0);
166 // test simple midi event
167 size = 3;
168 data[0] = 0x82; // MIDI_STATUS_NOTE_OFF + 2
169 data[1] = 127;
170 data[2] = 127;
171 e.fillFromMidiData(size, data);
172 assert(e.type == kEngineEventTypeMidi);
173 assert(e.channel == 2);
174 assert(e.midi.size == size);
175 assert(e.midi.dataExt == nullptr);
176 assert(e.midi.data[0] == 0x80); // without channel bit
177 assert(e.midi.data[1] == data[1]);
178 assert(e.midi.data[2] == data[2]);
180 // test sysex midi event
181 size = 11;
182 data[ 0] = 0xF0;
183 data[ 1] = 0x41;
184 data[ 2] = 0x10;
185 data[ 3] = 0x42;
186 data[ 4] = 0x12;
187 data[ 5] = 0x40;
188 data[ 6] = 0x00;
189 data[ 7] = 0x7F;
190 data[ 8] = 0x00;
191 data[ 9] = 0x41;
192 data[10] = 0xF7;
193 e.fillFromMidiData(size, data);
194 assert(e.type == kEngineEventTypeMidi);
195 assert(e.channel == 0);
196 assert(e.midi.size == size);
197 assert(e.midi.dataExt == data);
198 assert(e.midi.data[0] == 0);
199 assert(e.midi.data[1] == 0);
200 assert(e.midi.data[2] == 0);
201 assert(e.midi.data[3] == 0);
203 // test all-sound-off event
204 size = 2;
205 data[0] = 0xB5; // MIDI_STATUS_CONTROL_CHANGE + 5
206 data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
207 e.fillFromMidiData(size, data);
208 assert(e.type == kEngineEventTypeControl);
209 assert(e.channel == 5);
210 assert(e.ctrl.type == kEngineControlEventTypeAllSoundOff);
211 assert(e.ctrl.param == 0);
212 assert(e.ctrl.value == 0.0f);
214 // test all-notes-off event
215 size = 2;
216 data[0] = 0xBF; // MIDI_STATUS_CONTROL_CHANGE + 15
217 data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
218 e.fillFromMidiData(size, data);
219 assert(e.type == kEngineEventTypeControl);
220 assert(e.channel == 15);
221 assert(e.ctrl.type == kEngineControlEventTypeAllNotesOff);
222 assert(e.ctrl.param == 0);
223 assert(e.ctrl.value == 0.0f);
225 // test midi-bank event
226 size = 3;
227 data[0] = 0xB1; // MIDI_STATUS_CONTROL_CHANGE + 1
228 data[1] = MIDI_CONTROL_BANK_SELECT;
229 data[2] = 123;
230 e.fillFromMidiData(size, data);
231 assert(e.type == kEngineEventTypeControl);
232 assert(e.channel == 1);
233 assert(e.ctrl.type == kEngineControlEventTypeMidiBank);
234 assert(e.ctrl.param == 123);
235 assert(e.ctrl.value == 0.0f);
237 // test midi-program event
238 size = 2;
239 data[0] = MIDI_STATUS_PROGRAM_CHANGE;
240 data[1] = 77;
241 e.fillFromMidiData(size, data);
242 assert(e.type == kEngineEventTypeControl);
243 assert(e.channel == 0);
244 assert(e.ctrl.type == kEngineControlEventTypeMidiProgram);
245 assert(e.ctrl.param == 77);
246 assert(e.ctrl.value == 0.0f);
249 int main()
251 testControlEventDump();
252 testEventMidiFill();
253 return 0;