summaryrefslogtreecommitdiff
path: root/test/lv2.cc
blob: 82a957a44343e18453515f476dcbd252d4421e8e (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            lv2.cc
 *
 *  Thu Feb 12 14:55:41 CET 2015
 *  Copyright 2015 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include <cppunit/extensions/HelperMacros.h>

#include <unistd.h>
#include <memory.h>
#include <stdio.h>
#include <arpa/inet.h>

#include "lv2_test_host.h"

#define DG_URI "http://drumgizmo.org/lv2"

enum class Ports {
	FreeWheel = 0,
	MidiPort,
	AudioPortOffset,
};

/**
 * Tests that should be performed:
 * -------------------------------
 * - Run without port connects (shouldn't crash)
 * - Run without output ports connects (shouldn't crash)
 * - Run with buffer size 0
 * - Run with VERY LARGE buffer size (>1MB?)
 * - Run with buffer size a prime number (and thereby not power of 2)
 * - Run with HUGE number of midi events in one buffer (10000)
 */
class test_lv2 : public CppUnit::TestFixture
{
	CPPUNIT_TEST_SUITE(test_lv2);
	CPPUNIT_TEST(open_and_verify);
	CPPUNIT_TEST(run_no_ports_connected);
	CPPUNIT_TEST(run_no_output_ports_connected);
	CPPUNIT_TEST(test1);
	CPPUNIT_TEST_SUITE_END();

public:
	void setUp() {}
	void tearDown() {}

	void open_and_verify()
	{
		int res;

		LV2TestHost h(LV2_PATH);

		res = h.open(DG_URI);
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.verify();
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.close();
		CPPUNIT_ASSERT_EQUAL(0, res);
	}

	void run_no_ports_connected()
	{
		int res;

		LV2TestHost h(LV2_PATH);

		res = h.open(DG_URI);
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.verify();
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.createInstance(44100);
		CPPUNIT_ASSERT_EQUAL(0, res);

		const char config_fmt[] =
			"<config>\n"
			"  <value name=\"drumkitfile\">%s</value>\n"
			"  <value name=\"midimapfile\">%s</value>\n"
			"  <value name=\"enable_velocity_modifier\">%s</value>\n"
			"  <value name=\"velocity_modifier_falloff\">%f</value>\n"
			"  <value name=\"velocity_modifier_weight\">%f</value>\n"
			"  <value name=\"enable_velocity_randomiser\">%s</value>\n"
			"  <value name=\"velocity_randomiser_weight\">%f</value>\n"
			"  <value name=\"enable_resampling\">%s</value>\n"
			"</config>";

		const char drumkitfile[] = "kit/kit1.xml";
		const char midimapfile[] = "kit/midimap.xml";
		bool enable_velocity_modifier = true;
		float velocity_modifier_falloff = 0.5;
		float velocity_modifier_weight = 0.25;
		bool enable_velocity_randomiser = false;
		float velocity_randomiser_weight = 0.1;
		bool enable_resampling = false;

		char config[sizeof(config_fmt) * 2];
		sprintf(config, config_fmt,
		        drumkitfile,
		        midimapfile,
		        enable_velocity_modifier?"true":"false",
		        velocity_modifier_falloff,
		        velocity_modifier_weight,
		        enable_velocity_randomiser?"true":"false",
		        velocity_randomiser_weight,
		        enable_resampling?"true":"false");

		res = h.loadConfig(config, strlen(config));
		CPPUNIT_ASSERT_EQUAL(0, res);

		// run for 1 samples to trigger kit loading
		res = h.run(1);
		CPPUNIT_ASSERT_EQUAL(0, res);
		usleep(1000); // wait for kit to get loaded (async),

		res = h.run(100);
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.destroyInstance();
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.close();
		CPPUNIT_ASSERT_EQUAL(0, res);
	}

	void run_no_output_ports_connected()
	{
		int res;

		LV2TestHost h(LV2_PATH);

		res = h.open(DG_URI);
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.verify();
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.createInstance(44100);
		CPPUNIT_ASSERT_EQUAL(0, res);

		const char config_fmt[] =
			"<config>\n"
			"  <value name=\"drumkitfile\">%s</value>\n"
			"  <value name=\"midimapfile\">%s</value>\n"
			"  <value name=\"enable_velocity_modifier\">%s</value>\n"
			"  <value name=\"velocity_modifier_falloff\">%f</value>\n"
			"  <value name=\"velocity_modifier_weight\">%f</value>\n"
			"  <value name=\"enable_velocity_randomiser\">%s</value>\n"
			"  <value name=\"velocity_randomiser_weight\">%f</value>\n"
			"  <value name=\"enable_resampling\">%s</value>\n"
			"</config>";

		const char drumkitfile[] = "kit/kit1.xml";
		const char midimapfile[] = "kit/midimap.xml";
		bool enable_velocity_modifier = true;
		float velocity_modifier_falloff = 0.5;
		float velocity_modifier_weight = 0.25;
		bool enable_velocity_randomiser = false;
		float velocity_randomiser_weight = 0.1;
		bool enable_resampling = false;

		char config[sizeof(config_fmt) * 2];
		sprintf(config, config_fmt,
		        drumkitfile,
		        midimapfile,
		        enable_velocity_modifier?"true":"false",
		        velocity_modifier_falloff,
		        velocity_modifier_weight,
		        enable_velocity_randomiser?"true":"false",
		        velocity_randomiser_weight,
		        enable_resampling?"true":"false");

		res = h.loadConfig(config, strlen(config));
		CPPUNIT_ASSERT_EQUAL(0, res);

		// Port buffers:
		char sequence_buffer[4096];
		bool freeWheel = false;

		// Free wheel port
		res = h.connectPort((int)Ports::FreeWheel, (void*)&freeWheel);

		LV2TestHost::Sequence seq(sequence_buffer, sizeof(sequence_buffer));
		res = h.connectPort((int)Ports::MidiPort, seq.data());
		CPPUNIT_ASSERT_EQUAL(0, res);

		// run for 1 samples to trigger kit loading
		res = h.run(1);
		CPPUNIT_ASSERT_EQUAL(0, res);
		usleep(1000); // wait for kit to get loaded (async),

		seq.addMidiNote(5, 1, 127);
		res = h.run(100);
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.destroyInstance();
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.close();
		CPPUNIT_ASSERT_EQUAL(0, res);
	}

	void test1()
	{
		int res;

		LV2TestHost h(LV2_PATH);

		res = h.open(DG_URI);
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.verify();
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.createInstance(44100);
		CPPUNIT_ASSERT_EQUAL(0, res);

		const char config_fmt[] =
			"<config>\n"
			"  <value name=\"drumkitfile\">%s</value>\n"
			"  <value name=\"midimapfile\">%s</value>\n"
			"  <value name=\"enable_velocity_modifier\">%s</value>\n"
			"  <value name=\"velocity_modifier_falloff\">%f</value>\n"
			"  <value name=\"velocity_modifier_weight\">%f</value>\n"
			"  <value name=\"enable_velocity_randomiser\">%s</value>\n"
			"  <value name=\"velocity_randomiser_weight\">%f</value>\n"
			"  <value name=\"enable_resampling\">%s</value>\n"
			"</config>";

		const char drumkitfile[] = "kit/kit1.xml";
		const char midimapfile[] = "kit/midimap.xml";
		bool enable_velocity_modifier = true;
		float velocity_modifier_falloff = 0.5;
		float velocity_modifier_weight = 0.25;
		bool enable_velocity_randomiser = false;
		float velocity_randomiser_weight = 0.1;
		bool enable_resampling = false;

		char config[sizeof(config_fmt) * 2];
		sprintf(config, config_fmt,
		        drumkitfile,
		        midimapfile,
		        enable_velocity_modifier?"true":"false",
		        velocity_modifier_falloff,
		        velocity_modifier_weight,
		        enable_velocity_randomiser?"true":"false",
		        velocity_randomiser_weight,
		        enable_resampling?"true":"false");

		res = h.loadConfig(config, strlen(config));
		CPPUNIT_ASSERT_EQUAL(0, res);

		// Port buffers:
		char sequence_buffer[4096];
		float pcm_buffer[16][10];
		bool freeWheel = true;

		// Free wheel port
		res = h.connectPort((int)Ports::FreeWheel, (void*)&freeWheel);

		LV2TestHost::Sequence seq(sequence_buffer, sizeof(sequence_buffer));
		res = h.connectPort((int)Ports::MidiPort, seq.data());
		CPPUNIT_ASSERT_EQUAL(0, res);

		for(int i = 0; i < 16; ++i)
		{
			for(int j = 0; j < 10; ++j)
			{
				pcm_buffer[i][j] = 0.42;
			}
			res += h.connectPort((int)Ports::AudioPortOffset + i, pcm_buffer[i]);
		}
		CPPUNIT_ASSERT_EQUAL(0, res);

		// run for 1 samples to trigger kit loading
		res = h.run(1);
		CPPUNIT_ASSERT_EQUAL(0, res);
		sleep(1); // wait for kit to get loaded (async),

		seq.addMidiNote(5, 1, 127);
		for(int i = 0; i < 10; i++)
		{
			res = h.run(10);
			usleep(1000);
			CPPUNIT_ASSERT_EQUAL(0, res);

			//printf("Iteration:\n");
			//for(int k = 0; k < 16; k++) {
			//	printf("#%d ", k);
			//	for(int j = 0; j < 10; j++) printf("[%f]", pcm_buffer[k][j]);
			//	printf("\n");
			//}
			//printf("\n");

			seq.clear();
		}


		seq.addMidiNote(5, 1, 127);
		res = h.run(10);
		usleep(1000);
		CPPUNIT_ASSERT_EQUAL(0, res);

		/*
		printf("Iteration:\n");
		for(int k = 0; k < 4; k++) {
			printf("#%d ", k);
			for(int j = 0; j < 10; j++) printf("[%f]", pcm_buffer[k][j]);
			printf("\n");
		}
		printf("\n");
		*/

		union {
			float f;
			unsigned int u;
		} comp_val;

		comp_val.u = 1040744448; // floating point value 0.133301....

		for(int k = 0; k < 4; k++)
		{
			for(int j = 0; j < 10; j++)
			{
				CPPUNIT_ASSERT_EQUAL(((j==5)?comp_val.f:0), pcm_buffer[k][j]);
			}
		}
		seq.clear();

		res = h.destroyInstance();
		CPPUNIT_ASSERT_EQUAL(0, res);

		res = h.close();
		CPPUNIT_ASSERT_EQUAL(0, res);
	}
};

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(test_lv2);