summaryrefslogtreecommitdiff
path: root/tools/bb/bb.cc
blob: fb6a89c81fb9b93849841ec53b412dcfacc3d5b0 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

#include <unistd.h>
#include <stdint.h>

#include <jack/jack.h>
#include <jack/midiport.h>

#include "evoral/midi_events.h"

#include "bb.h"
#include "gui.h"

using std::cerr;
using std::endl;


static bool
second_simultaneous_midi_byte_is_first (uint8_t a, uint8_t b)
{
	bool b_first = false;

	/* two events at identical times. we need to determine
	   the order in which they should occur.

	   the rule is:

	   Controller messages
	   Program Change
	   Note Off
	   Note On
	   Note Pressure
	   Channel Pressure
	   Pitch Bend
	*/

	if ((a) >= 0xf0 || (b) >= 0xf0 || ((a & 0xf) != (b & 0xf))) {

		/* if either message is not a channel message, or if the channels are
		 * different, we don't care about the type.
		 */

		b_first = true;

	} else {

		switch (b & 0xf0) {
		case MIDI_CMD_CONTROL:
			b_first = true;
			break;

		case MIDI_CMD_PGM_CHANGE:
			switch (a & 0xf0) {
			case MIDI_CMD_CONTROL:
				break;
			case MIDI_CMD_PGM_CHANGE:
			case MIDI_CMD_NOTE_OFF:
			case MIDI_CMD_NOTE_ON:
			case MIDI_CMD_NOTE_PRESSURE:
			case MIDI_CMD_CHANNEL_PRESSURE:
			case MIDI_CMD_BENDER:
				b_first = true;
			}
			break;

		case MIDI_CMD_NOTE_OFF:
			switch (a & 0xf0) {
			case MIDI_CMD_CONTROL:
			case MIDI_CMD_PGM_CHANGE:
				break;
			case MIDI_CMD_NOTE_OFF:
			case MIDI_CMD_NOTE_ON:
			case MIDI_CMD_NOTE_PRESSURE:
			case MIDI_CMD_CHANNEL_PRESSURE:
			case MIDI_CMD_BENDER:
				b_first = true;
			}
			break;

		case MIDI_CMD_NOTE_ON:
			switch (a & 0xf0) {
			case MIDI_CMD_CONTROL:
			case MIDI_CMD_PGM_CHANGE:
			case MIDI_CMD_NOTE_OFF:
				break;
			case MIDI_CMD_NOTE_ON:
			case MIDI_CMD_NOTE_PRESSURE:
			case MIDI_CMD_CHANNEL_PRESSURE:
			case MIDI_CMD_BENDER:
				b_first = true;
			}
			break;
		case MIDI_CMD_NOTE_PRESSURE:
			switch (a & 0xf0) {
			case MIDI_CMD_CONTROL:
			case MIDI_CMD_PGM_CHANGE:
			case MIDI_CMD_NOTE_OFF:
			case MIDI_CMD_NOTE_ON:
				break;
			case MIDI_CMD_NOTE_PRESSURE:
			case MIDI_CMD_CHANNEL_PRESSURE:
			case MIDI_CMD_BENDER:
				b_first = true;
			}
			break;

		case MIDI_CMD_CHANNEL_PRESSURE:
			switch (a & 0xf0) {
			case MIDI_CMD_CONTROL:
			case MIDI_CMD_PGM_CHANGE:
			case MIDI_CMD_NOTE_OFF:
			case MIDI_CMD_NOTE_ON:
			case MIDI_CMD_NOTE_PRESSURE:
				break;
			case MIDI_CMD_CHANNEL_PRESSURE:
			case MIDI_CMD_BENDER:
				b_first = true;
			}
			break;
		case MIDI_CMD_BENDER:
			switch (a & 0xf0) {
			case MIDI_CMD_CONTROL:
			case MIDI_CMD_PGM_CHANGE:
			case MIDI_CMD_NOTE_OFF:
			case MIDI_CMD_NOTE_ON:
			case MIDI_CMD_NOTE_PRESSURE:
			case MIDI_CMD_CHANNEL_PRESSURE:
				break;
			case MIDI_CMD_BENDER:
				b_first = true;
			}
			break;
		}
	}

	return b_first;
}

BeatBox::BeatBox (int sr)
	: _start_requested (false)
	, _running (false)
	, _measures (2)
	, _tempo (120)
	, _meter_beats (4)
	, _meter_beat_type (4)
	, _input (0)
	, _output (0)
	, superclock_cnt (0)
	, last_start (0)
	, _sample_rate (sr)
	, whole_note_superclocks (0)
	, beat_superclocks (0)
	, measure_superclocks (0)
	, _quantize_divisor (4)
	, clear_pending (false)
{
	for (uint32_t n = 0; n < 1024; ++n) {
		event_pool.push_back (new Event());
	}
}

BeatBox::~BeatBox ()
{
}

int
BeatBox::register_ports (jack_client_t* jack)
{
	if ((_input = jack_port_register (jack, "midi-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0)) == 0) {
		cerr << "no input port\n";
		return -1;
	}
	if ((_output = jack_port_register (jack, "midi-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0)) == 0) {
		cerr << "no output port\n";
		jack_port_unregister (jack, _input);
		return -1;
	}

	return 0;
}

void
BeatBox::start ()
{
	/* compute tempo, beat steps etc. */

	/*
	   superclocks_per_minute = superclock_ticks_per_second * 60;
	   beats_per_minute = _tempo;
	   whole_notes_per_minute = beats_per_minute / _meter_beat_type;
	*/

	whole_note_superclocks = (superclock_ticks_per_second * 60) / (_tempo / _meter_beat_type);
	cerr << "there are " << _tempo / _meter_beat_type << " whole notes per minute, which is " << superclock_ticks_per_second * 60 << " sct, so wns = " << whole_note_superclocks << endl;
	beat_superclocks = whole_note_superclocks / _meter_beat_type;
	measure_superclocks = beat_superclocks * _meter_beats;

	/* we can start */

	_start_requested = true;
}

void
BeatBox::stop ()
{
	_start_requested = false;
}

int
BeatBox::process (int nsamples)
{
	if (!_running) {
		if (_start_requested) {
			_running = true;
			last_start = superclock_cnt;
		}

	} else {
		if (!_start_requested) {
			_running = false;
		}
	}

	superclock_t superclocks = samples_to_superclock (nsamples, _sample_rate);

	if (!_running) {
		superclock_cnt += superclocks;
		return 0;
	}

	superclock_t process_start = superclock_cnt - last_start;
	superclock_t process_end = process_start + superclocks;
	const superclock_t loop_length = _measures * measure_superclocks;
	const superclock_t orig_superclocks = superclocks;

	process_start %= loop_length;
	process_end   %= loop_length;

	bool two_pass_required;
	superclock_t offset = 0;

	if (process_end < process_start) {
		two_pass_required = true;
		process_end = loop_length;
		superclocks = process_end - process_start;
	} else {
		two_pass_required = false;
	}

	unsigned char* buffer;
	void* out_buf;
	void* in_buf;
	jack_midi_event_t in_event;
	jack_nframes_t event_index;
	jack_nframes_t event_count;

	/* do this on the first pass only */
	out_buf = jack_port_get_buffer (_output, nsamples);
	jack_midi_clear_buffer (out_buf);

  second_pass:

	/* Output */

	if (clear_pending) {

		for (Events::iterator ee = _current_events.begin(); ee != _current_events.end(); ++ee) {
			event_pool.push_back (*ee);
		}
		_current_events.clear ();
		clear_pending = false;
	}

	for (Events::iterator ee = _current_events.begin(); ee != _current_events.end(); ++ee) {
		Event* e = (*ee);

		if (e->size && (e->time >= process_start && e->time < process_end)) {
			if ((buffer = jack_midi_event_reserve (out_buf, superclock_to_samples (offset + e->time - process_start, _sample_rate), e->size)) != 0) {
				memcpy (buffer, e->buf, e->size);
			} else {
				cerr << "Could not reserve space for output event @ " << e << " of size " << e->size << " @ " << offset + e->time - process_start
				     << " (samples: " << superclock_to_samples (offset + e->time - process_start, _sample_rate) << ") offset is " << offset
				     << ")\n";
			}
		}

		if (e->time >= process_end) {
			break;
		}
	}

	/* input */

	in_buf = jack_port_get_buffer (_input, nsamples);
	event_index = 0;

	while (jack_midi_event_get (&in_event, in_buf, event_index++) == 0) {

		superclock_t event_time = superclock_cnt + samples_to_superclock (in_event.time, _sample_rate);
		superclock_t elapsed_time = event_time - last_start;
		superclock_t in_loop_time = elapsed_time % loop_length;
		superclock_t quantized_time;

		if (_quantize_divisor != 0) {
			const superclock_t time_per_beat = whole_note_superclocks / _quantize_divisor;
			quantized_time = (in_loop_time / time_per_beat) * time_per_beat;
		} else {
			quantized_time = elapsed_time;
		}

		if (in_event.size > 24) {
			cerr << "Ignored large MIDI event\n";
			continue;
		}

		if (event_pool.empty()) {
			cerr << "No more events, grow pool\n";
			continue;
		}

		Event* e = event_pool.back();
		event_pool.pop_back ();

		e->time = quantized_time;
		e->size = in_event.size;
		memcpy (e->buf, in_event.buffer, in_event.size);

		_current_events.insert (e);
	}

	superclock_cnt += superclocks;

	if (two_pass_required) {
		offset = superclocks;
		superclocks = orig_superclocks - superclocks;
		process_start = 0;
		process_end = superclocks;
		two_pass_required = false;
		cerr << "2nd Pass for " << superclocks << " offset = " << offset << endl;
		goto second_pass;
	}

	return 0;
}

void
BeatBox::set_quantize (int divisor)
{
	_quantize_divisor = divisor;
}

void
BeatBox::clear ()
{
	clear_pending = true;
}

bool
BeatBox::EventComparator::operator() (Event const * a, Event const *b) const
{
	if (a->time == b->time) {
		if (a->buf[0] == b->buf[0]) {
			return a < b;
		}
		return !second_simultaneous_midi_byte_is_first (a->buf[0], b->buf[0]);
	}
	return a->time < b->time;
}

static int
process (jack_nframes_t nsamples, void* arg)
{
	BeatBox* bbox = static_cast<BeatBox*> (arg);
	return bbox->process (nsamples);
}

int
main (int argc, char* argv[])
{
	jack_client_t* jack;
	const char *server_name = NULL;
	jack_options_t options = JackNullOption;
	jack_status_t status;

	if ((jack = jack_client_open ("beatbox", options, &status, server_name)) == 0) {
		cerr << "Could not connect to JACK\n";
		return -1;
	}

	BeatBox* bbox = new BeatBox (jack_get_sample_rate (jack));
	BBGUI* gui = new BBGUI (&argc, &argv, jack, bbox);

	bbox->register_ports (jack);

	jack_set_process_callback (jack, process, bbox);
	jack_activate (jack);

	bbox->start ();

	gui->run ();
}