summaryrefslogtreecommitdiff
path: root/gtk2_ardour/midi_streamview.cc
blob: a2b4229650eb6f835b085f4036e7783c7c03c9b2 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
    Copyright (C) 2001-2007 Paul Davis 

    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 <cmath>
#include <cassert>
#include <utility>

#include <gtkmm.h>

#include <gtkmm2ext/gtk_ui.h>

#include <ardour/midi_playlist.h>
#include <ardour/midi_region.h>
#include <ardour/midi_source.h>
#include <ardour/midi_diskstream.h>
#include <ardour/midi_track.h>
#include <ardour/midi_events.h>
#include <ardour/smf_source.h>
#include <ardour/region_factory.h>

#include "midi_streamview.h"
#include "region_view.h"
#include "midi_region_view.h"
#include "midi_time_axis.h"
#include "canvas-simplerect.h"
#include "region_selection.h"
#include "selection.h"
#include "public_editor.h"
#include "ardour_ui.h"
#include "rgb_macros.h"
#include "gui_thread.h"
#include "utils.h"
#include "simplerect.h"
#include "simpleline.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;
using namespace Editing;

MidiStreamView::MidiStreamView (MidiTimeAxisView& tv)
	: StreamView (tv)
	, _range(ContentsRange)
	, _lowest_note(60)
	, _highest_note(60)
{
	if (tv.is_track())
		stream_base_color = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
	else
		stream_base_color = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();
	
	canvas_rect->property_fill_color_rgba() = stream_base_color;
	canvas_rect->property_outline_color_rgba() = RGBA_BLACK;

	use_rec_regions = tv.editor.show_waveforms_recording ();
	
	_note_line_group = new ArdourCanvas::Group (*canvas_group);
	
	for (uint8_t i=0; i < 127; ++i) {
		_note_lines[i] = new ArdourCanvas::SimpleLine(*_note_line_group,
				0, note_to_y(i), 10, note_to_y(i));
		_note_lines[i]->property_color_rgba() = 0xEEEEEE55;
	}
}

MidiStreamView::~MidiStreamView ()
{
}


RegionView*
MidiStreamView::add_region_view_internal (boost::shared_ptr<Region> r, bool wfd)
{
	boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion> (r);

	if (region == 0) {
		return NULL;
	}

	MidiRegionView *region_view;
	list<RegionView *>::iterator i;

	for (i = region_views.begin(); i != region_views.end(); ++i) {
		if ((*i)->region() == r) {
			
			/* great. we already have a MidiRegionView for this Region. use it again. */

			(*i)->set_valid (true);
			(*i)->enable_display(wfd);
			display_region(dynamic_cast<MidiRegionView*>(*i), wfd);

			return NULL;
		}
	}
	
	region_view = new MidiRegionView (canvas_group, _trackview, region, 
			_samples_per_unit, region_color);
		
	region_view->init (region_color, false);
	region_views.push_front (region_view);
	
	/* follow global waveform setting */

	if (wfd) {
		region_view->enable_display(true);
		region_view->midi_region()->midi_source(0)->load_model();
	}

	/* display events and find note range */
	display_region(region_view, wfd);

	/* always display at least 1 octave range */
	_highest_note = max(_highest_note, static_cast<uint8_t>(_lowest_note + 11));

	/* catch regionview going away */
	region->GoingAway.connect (bind (mem_fun (*this, &MidiStreamView::remove_region_view), region));
	
	RegionViewAdded (region_view);

	return region_view;
}

void
MidiStreamView::display_region(MidiRegionView* region_view, bool load_model)
{
	if ( ! region_view)
		return;

	boost::shared_ptr<MidiSource> source(region_view->midi_region()->midi_source(0));

	if (load_model)
		source->load_model();

	if (source->model()) {
		// Find our note range
		for (size_t i=0; i < source->model()->n_notes(); ++i) {
			const Note& note = source->model()->note_at(i);
			update_bounds(note.note());
		}
	}
	
	// Display region contents
	region_view->display_model(source->model());
}

void
MidiStreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
{
	StreamView::display_diskstream(ds);
	draw_note_separators();
}

// FIXME: code duplication with AudioStreamView
void
MidiStreamView::redisplay_diskstream ()
{
	list<RegionView *>::iterator i, tmp;

	for (i = region_views.begin(); i != region_views.end(); ++i) {
		(*i)->enable_display(true); // FIXME: double display, remove
		(*i)->set_valid (false);
		
		/* FIXME: slow.  MidiRegionView needs a find_note_range method
		 * that finds the range without wasting time drawing the events */

		// Load model if it isn't already, to get note range
		MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
		mrv->midi_region()->midi_source(0)->load_model();
	}
	
	//_lowest_note = 60; // middle C
	//_highest_note = _lowest_note + 11;

	if (_trackview.is_midi_track()) {
		_trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
	}

	/* Always display at least one octave */
	if (_highest_note == 127) {
		if (_lowest_note > (127 - 11))
			_lowest_note = 127 - 11;
	} else if (_highest_note < _lowest_note + 11) {
		_highest_note = _lowest_note + 11;
	}
	
	for (i = region_views.begin(); i != region_views.end(); ) {
		tmp = i;
		tmp++;

		if (!(*i)->is_valid()) {
			delete *i;
			region_views.erase (i);
		} else {
			(*i)->enable_display(true);
			(*i)->set_y_position_and_height(0, height); // apply note range
		}

		i = tmp;
	}
	
	/* now fix layering */

	for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
		region_layered (*i);
	}
	
	draw_note_separators();
}


void
MidiStreamView::update_contents_y_position_and_height ()
{
	StreamView::update_contents_y_position_and_height();
	draw_note_separators();
}
	
void
MidiStreamView::draw_note_separators()
{
	for (uint8_t i=0; i < 127; ++i) {
		if (i >= _lowest_note-1 && i <= _highest_note) {
			_note_lines[i]->property_x1() = 0;
			_note_lines[i]->property_x2() = canvas_rect->property_x2() - 2;
			_note_lines[i]->property_y1() = note_to_y(i);
			_note_lines[i]->property_y2() = note_to_y(i);
			_note_lines[i]->show();
		} else {
			_note_lines[i]->hide();
		}
	}
}
	

void
MidiStreamView::set_note_range(VisibleNoteRange r)
{
	_range = r;
	if (r == FullRange) {
		_lowest_note = 0;
		_highest_note = 127;
	} else {
		_lowest_note = 60;
		_highest_note = 60;
	}
	redisplay_diskstream();
}

	
void 
MidiStreamView::update_bounds(uint8_t note_num)
{
	_lowest_note = min(_lowest_note, note_num);
	_highest_note = max(_highest_note, note_num);
}


void
MidiStreamView::setup_rec_box ()
{
	// cerr << _trackview.name() << " streamview SRB\n";

	if (_trackview.session().transport_rolling()) {

		if (!rec_active && 
		    _trackview.session().record_status() == Session::Recording && 
		    _trackview.get_diskstream()->record_enabled()) {

			if (use_rec_regions && rec_regions.size() == rec_rects.size()) {

				/* add a new region, but don't bother if they set use_rec_regions mid-record */

				MidiRegion::SourceList sources;
				
				for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
					(*prc).disconnect();
				}
				rec_data_ready_connections.clear();

				// FIXME
				boost::shared_ptr<MidiDiskstream> mds = boost::dynamic_pointer_cast<MidiDiskstream>(_trackview.get_diskstream());
				assert(mds);

				sources.push_back(mds->write_source());
				
				rec_data_ready_connections.push_back (mds->write_source()->ViewDataRangeReady.connect (bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), boost::weak_ptr<Source>(mds->write_source())))); 

				// handle multi
				
				jack_nframes_t start = 0;
				if (rec_regions.size() > 0) {
					start = rec_regions.back().first->position() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
				}
				
				boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion>
					(RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
				assert(region);
				region->set_position (_trackview.session().transport_frame(), this);
				rec_regions.push_back (make_pair(region, (RegionView*)0));
				
				// rec regions are destroyed in setup_rec_box

				/* we add the region later */
			}
			
			/* start a new rec box */

			boost::shared_ptr<MidiTrack> mt = _trackview.midi_track(); /* we know what it is already */
			boost::shared_ptr<MidiDiskstream> ds = mt->midi_diskstream();
			jack_nframes_t frame_pos = ds->current_capture_start ();
			gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
			gdouble xend;
			uint32_t fill_color;

			assert(_trackview.midi_track()->mode() == Normal);
			
			xend = xstart;
			fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
			
			ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
			rec_rect->property_x1() = xstart;
			rec_rect->property_y1() = 1.0;
			rec_rect->property_x2() = xend;
			rec_rect->property_y2() = (double) _trackview.height - 1;
			rec_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
			rec_rect->property_fill_color_rgba() = fill_color;
			rec_rect->lower_to_bottom();
			
			RecBoxInfo recbox;
			recbox.rectangle = rec_rect;
			recbox.start = _trackview.session().transport_frame();
			recbox.length = 0;
			
			rec_rects.push_back (recbox);
			
			screen_update_connection.disconnect();
			screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &MidiStreamView::update_rec_box));	
			rec_updating = true;
			rec_active = true;

		} else if (rec_active &&
			   (_trackview.session().record_status() != Session::Recording ||
			    !_trackview.get_diskstream()->record_enabled())) {

			screen_update_connection.disconnect();
			rec_active = false;
			rec_updating = false;

		}
		
	} else {

		// cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;

		if (!rec_rects.empty() || !rec_regions.empty()) {

			/* disconnect rapid update */
			screen_update_connection.disconnect();

			for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
				(*prc).disconnect();
			}
			rec_data_ready_connections.clear();

			rec_updating = false;
			rec_active = false;
			
			/* remove temp regions */
			
			for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end();) {
				list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
				
				tmp = iter;
				++tmp;

				(*iter).first->drop_references ();

				iter = tmp;
			}
			
			rec_regions.clear();

			// cerr << "\tclear " << rec_rects.size() << " rec rects\n";

			/* transport stopped, clear boxes */
			for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
				RecBoxInfo &rect = (*iter);
				delete rect.rectangle;
			}
			
			rec_rects.clear();
			
		}
	}
}

void
MidiStreamView::update_rec_regions (boost::shared_ptr<MidiModel> data, nframes_t start, nframes_t dur)
{
	ENSURE_GUI_THREAD (bind (mem_fun (*this, &MidiStreamView::update_rec_regions), data, start, dur));

	if (use_rec_regions) {

		uint32_t n = 0;
		bool     update_range = false;

		for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {

			list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;

			tmp = iter;
			++tmp;

			if (!canvas_item_visible (rec_rects[n].rectangle)) {
				/* rect already hidden, this region is done */
				iter = tmp;
				continue;
			}
			
			boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(iter->first);
			if (!region) {
				continue;
			}

			nframes_t origlen = region->length();
			
			if (region == rec_regions.back().first && rec_active) {

				if (start >= region->midi_source(0)->timeline_position()) {
				
					nframes_t nlen = start + dur - region->position();

					if (nlen != region->length()) {
					
						region->freeze ();
						region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
						region->set_length (start + dur - region->position(), this);
						region->thaw ("updated");
						
						if (origlen == 1) {
							/* our special initial length */
							iter->second = add_region_view_internal (region, false);
							((MidiRegionView*)iter->second)->begin_write();
						}

						/* also update rect */
						ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
						gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
						rect->property_x2() = xend;

						/* draw events */
						MidiRegionView* mrv = (MidiRegionView*)iter->second;
						for (size_t i=0; i < data->n_notes(); ++i) {

							const Note& note = data->note_at(i);

							if (note.time() + region->position() < start)
								continue;

							if (note.time() + region->position() > start + dur)
								break;

							mrv->add_note(note, true);
							
							if (note.duration() > 0 && note.end_time() >= start)
								mrv->resolve_note(note.note(), note.end_time());

							if (note.note() < _lowest_note) {
								_lowest_note = note.note();
								update_range = true;
							} else if (note.note() > _highest_note) {
								_highest_note = note.note();
								update_range = true;
							}
						}
						
						mrv->extend_active_notes();
					}
				}

			} else {
				
				nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);

				if (nlen != region->length()) {

					if (region->source(0)->length() >= region->position() + nlen) {

						region->freeze ();
						region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
						region->set_length (nlen, this);
						region->thaw ("updated");
						
						if (origlen == 1) {
							/* our special initial length */
							iter->second = add_region_view_internal (region, false);
						}
						
						/* also hide rect */
						ArdourCanvas::Item * rect = rec_rects[n].rectangle;
						rect->hide();

					}
				}
			}

			iter = tmp;
		}

		if (update_range)
			update_contents_y_position_and_height();
	}
}

void
MidiStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t dur, boost::weak_ptr<Source> weak_src)
{
	// this is called from the butler thread for now
	
	ENSURE_GUI_THREAD(bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), start, dur, weak_src));
	
	boost::shared_ptr<SMFSource> src (boost::dynamic_pointer_cast<SMFSource>(weak_src.lock()));
	
	this->update_rec_regions (src->model(), start, dur);
}

void
MidiStreamView::color_handler ()
{

	//case cMidiTrackBase:
	if (_trackview.is_midi_track()) {
		canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
	} 

	//case cMidiBusBase:
	if (!_trackview.is_midi_track()) {
		canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
	}
}