summaryrefslogtreecommitdiff
path: root/libs/ardour/midi_model.cc
blob: b1a87551b1eb362c7d37308b0cee8c9d6f3097cc (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
/*
    Copyright (C) 2007 Paul Davis 
	Written by Dave Robillard, 2007

    This program 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 2 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <iostream>
#include <queue>
#include <pbd/enumwriter.h>
#include <ardour/midi_model.h>
#include <ardour/midi_events.h>
#include <ardour/types.h>
#include <ardour/session.h>

using namespace std;
using namespace ARDOUR;

// Note

MidiModel::Note::Note(double t, double d, uint8_t n, uint8_t v)
{
	_on_event.time = t;
	_on_event.buffer = _on_event_buffer;
	_on_event.size = 3;
	_on_event.buffer[0] = MIDI_CMD_NOTE_ON;
	_on_event.buffer[1] = n;
	_on_event.buffer[2] = v;
	
	_off_event.time = t + d;
	_off_event.buffer = _off_event_buffer;
	_off_event.size = 3;
	_off_event.buffer[0] = MIDI_CMD_NOTE_OFF;
	_off_event.buffer[1] = n;
	_off_event.buffer[2] = 0x40;

	assert(time() == t);
	assert(duration() == d);
	assert(note() == n);
	assert(velocity() == v);
}


MidiModel::Note::Note(const Note& copy)
	: _on_event(copy._on_event)
	, _off_event(copy._off_event)
{
	memcpy(_on_event_buffer, copy._on_event_buffer, 3);
	memcpy(_off_event_buffer, copy._off_event_buffer, 3);
	_on_event.buffer = _on_event_buffer;
	_off_event.buffer = _off_event_buffer;
}


// MidiModel

MidiModel::MidiModel(Session& s, size_t size)
	: _session(s)
	, _notes(size)
	, _note_mode(Sustained)
	, _writing(false)
	, _command(NULL)
{
}


/** Read events in frame range \a start .. \a start+cnt into \a dst,
 * adding \a stamp_offset to each event's timestamp.
 * \return number of events written to \a dst
 */
size_t
MidiModel::read (MidiRingBuffer& dst, nframes_t start, nframes_t nframes, nframes_t stamp_offset) const
{
	size_t read_events = 0;

	//cerr << "MM READ " << start << " .. " << nframes << endl;

	/* FIXME: cache last lookup value to avoid the search */

	if (_note_mode == Sustained) {
		LaterNoteEndComparator cmp;
		priority_queue<const Note*,vector<const Note*>,LaterNoteEndComparator> active_notes(cmp);

		/* FIXME: cache last lookup value to avoid the search */
		for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n) {
	
			//cerr << "MM ON " << n->time() << endl;

			if (n->time() >= start + nframes)
				break;

			while ( ! active_notes.empty() ) {
				const Note* const earliest_off = active_notes.top();
				const MidiEvent& ev = earliest_off->off_event();
				if (ev.time < start + nframes && ev.time <= n->time()) {
					dst.write(ev.time + stamp_offset, ev.size, ev.buffer);
					active_notes.pop();
					++read_events;
				} else {
					break;
				}
			}

			// Note on
			if (n->time() >= start) {
				const MidiEvent& ev = n->on_event();
				dst.write(ev.time + stamp_offset, ev.size, ev.buffer);
				active_notes.push(&(*n));
				++read_events;
			}

		}

	// Percussive
	} else {
		for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n) {
			// Note on
			if (n->time() >= start) {
				if (n->time() < start + nframes) {
					const MidiEvent& ev = n->on_event();
					dst.write(ev.time + stamp_offset, ev.size, ev.buffer);
					++read_events;
				} else {
					break;
				}
			}
		}
	}

	return read_events;
}


/** Begin a write of events to the model.
 *
 * If \a mode is Sustained, complete notes with duration are constructed as note
 * on/off events are received.  Otherwise (Percussive), only note on events are
 * stored; note off events are discarded entirely and all contained notes will
 * have duration 0.
 */
void
MidiModel::start_write()
{
	//cerr << "MM START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
	_write_notes.clear();
	_writing = true;
}



/** Finish a write of events to the model.
 *
 * If \a delete_stuck is true and the current mode is Sustained, note on events
 * that were never resolved with a corresonding note off will be deleted.
 * Otherwise they will remain as notes with duration 0.
 */
void
MidiModel::end_write(bool delete_stuck)
{
	assert(_writing);
	
	//cerr << "MM END WRITE\n";

	if (_note_mode == Sustained && delete_stuck) {
		for (Notes::iterator n = _notes.begin(); n != _notes.end() ; ) {
			if (n->duration() == 0) {
				cerr << "WARNING: Stuck note lost: " << n->note() << endl;
				n = _notes.erase(n);
			} else {
				++n;
			}
		}
	}

	_write_notes.clear();
	_writing = false;
}


/** Append contents of \a buf to model.  NOT realtime safe.
 *
 * Timestamps of events in \a buf are expected to be relative to
 * the start of this model (t=0) and MUST be monotonically increasing
 * and MUST be >= the latest event currently in the model.
 *
 * Events in buf are deep copied.
 */
void
MidiModel::append(const MidiBuffer& buf)
{ 
	assert(_writing);

	for (MidiBuffer::const_iterator i = buf.begin(); i != buf.end(); ++i) {
		const MidiEvent& ev = *i;
		
		assert(_notes.empty() || ev.time >= _notes.back().time());

		if (ev.type() == MIDI_CMD_NOTE_ON)
			append_note_on(ev.time, ev.note(), ev.velocity());
		else if (ev.type() == MIDI_CMD_NOTE_OFF)
			append_note_off(ev.time, ev.note());
	}
}


/** Append \a in_event to model.  NOT realtime safe.
 *
 * Timestamps of events in \a buf are expected to be relative to
 * the start of this model (t=0) and MUST be monotonically increasing
 * and MUST be >= the latest event currently in the model.
 */
void
MidiModel::append(double time, size_t size, const Byte* buf)
{
	assert(_notes.empty() || time >= _notes.back().time());
	assert(_writing);

	if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_ON)
		append_note_on(time, buf[1], buf[2]);
	else if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_OFF)
		append_note_off(time, buf[1]);
}


void
MidiModel::append_note_on(double time, uint8_t note_num, uint8_t velocity)
{
	assert(_writing);
	_notes.push_back(Note(time, 0, note_num, velocity));
	if (_note_mode == Sustained) {
		//cerr << "MM Appending note on " << (unsigned)(uint8_t)note_num << endl;
		_write_notes.push_back(_notes.size() - 1);
	} else {
		//cerr << "MM NOT appending note on" << endl;
	}
}


void
MidiModel::append_note_off(double time, uint8_t note_num)
{
	assert(_writing);
	if (_note_mode == Percussive) {
		//cerr << "MM Ignoring note off (percussive mode)" << endl;
		return;
	} else {
		//cerr << "MM Attempting to resolve note off " << (unsigned)(uint8_t)note_num << endl;
	}

	/* FIXME: make _write_notes fixed size (127 noted) for speed */
	
	/* FIXME: note off velocity for that one guy out there who actually has
	 * keys that send it */

	for (WriteNotes::iterator n = _write_notes.begin(); n != _write_notes.end(); ++n) {
		Note& note = _notes[*n];
		//cerr << (unsigned)(uint8_t)note.note() << " ? " << (unsigned)note_num << endl;
		if (note.note() == note_num) {
			assert(time > note.time());
			note.set_duration(time - note.time());
			_write_notes.erase(n);
			//cerr << "MidiModel resolved note, duration: " << note.duration() << endl;
			break;
		}
	}
}


void
MidiModel::add_note(const Note& note)
{
	// FIXME: take source lock
	Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note, note_time_comparator);
	_notes.insert(i, note);
	if (_command)
		_command->add_note(note);
}


void
MidiModel::remove_note(const Note& note)
{
	// FIXME: take source lock
	Notes::iterator n = find(_notes.begin(), _notes.end(), note);
	if (n != _notes.end())
		_notes.erase(n);
	
	if (_command)
		_command->remove_note(note);
}



void
MidiModel::begin_command()
{
	assert(!_command);
	_session.begin_reversible_command("midi edit");
	_command = new MidiEditCommand(*this);
}


void
MidiModel::finish_command()
{
	_session.commit_reversible_command(_command);
	_command = NULL;
}


// MidiEditCommand


void
MidiModel::MidiEditCommand::add_note(const Note& note)
{
	//cerr << "MEC: apply" << endl;

	_removed_notes.remove(note);
	_added_notes.push_back(note);
}


void
MidiModel::MidiEditCommand::remove_note(const Note& note)
{
	//cerr << "MEC: remove" << endl;

	_added_notes.remove(note);
	_removed_notes.push_back(note);
}

		
void 
MidiModel::MidiEditCommand::operator()()
{
	//cerr << "MEC: apply" << endl;
	assert(!_model.current_command());
	
	for (std::list<Note>::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
		_model.add_note(*i);
	
	for (std::list<Note>::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
		_model.remove_note(*i);

	_model.ContentsChanged(); /* EMIT SIGNAL */
}


void
MidiModel::MidiEditCommand::undo()
{
	//cerr << "MEC: undo" << endl;
	assert(!_model.current_command());

	for (std::list<Note>::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
		_model.remove_note(*i);
	
	for (std::list<Note>::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
		_model.add_note(*i);
	
	_model.ContentsChanged(); /* EMIT SIGNAL */
}