21eae23f908020e9a028bf97e5df13652563efa8
[ardour.git] / gtk2_ardour / midi_streamview.cc
1 /*
2     Copyright (C) 2001-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <cassert>
21 #include <utility>
22
23 #include <gtkmm.h>
24
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include <ardour/midi_playlist.h>
28 #include <ardour/midi_region.h>
29 #include <ardour/midi_source.h>
30 #include <ardour/midi_diskstream.h>
31 #include <ardour/midi_track.h>
32 #include <ardour/midi_events.h>
33 #include <ardour/smf_source.h>
34 #include <ardour/region_factory.h>
35
36 #include "midi_streamview.h"
37 #include "region_view.h"
38 #include "midi_region_view.h"
39 #include "midi_time_axis.h"
40 #include "canvas-simplerect.h"
41 #include "region_selection.h"
42 #include "selection.h"
43 #include "public_editor.h"
44 #include "ardour_ui.h"
45 #include "rgb_macros.h"
46 #include "gui_thread.h"
47 #include "utils.h"
48 #include "simplerect.h"
49 #include "simpleline.h"
50
51 using namespace std;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Editing;
55
56 MidiStreamView::MidiStreamView (MidiTimeAxisView& tv)
57         : StreamView (tv)
58         , _range(ContentsRange)
59         , _lowest_note(60)
60         , _highest_note(60)
61 {
62         if (tv.is_track())
63                 stream_base_color = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
64         else
65                 stream_base_color = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();
66         
67         canvas_rect->property_fill_color_rgba() = stream_base_color;
68         canvas_rect->property_outline_color_rgba() = RGBA_BLACK;
69
70         use_rec_regions = tv.editor.show_waveforms_recording ();
71         
72         _note_line_group = new ArdourCanvas::Group (*canvas_group);
73         
74         for (uint8_t i=0; i < 127; ++i) {
75                 _note_lines[i] = new ArdourCanvas::SimpleLine(*_note_line_group,
76                                 0, note_to_y(i), 10, note_to_y(i));
77                 _note_lines[i]->property_color_rgba() = 0xEEEEEE55;
78         }
79 }
80
81 MidiStreamView::~MidiStreamView ()
82 {
83 }
84
85
86 RegionView*
87 MidiStreamView::add_region_view_internal (boost::shared_ptr<Region> r, bool wfd)
88 {
89         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion> (r);
90
91         if (region == 0) {
92                 return NULL;
93         }
94
95         MidiRegionView *region_view;
96         list<RegionView *>::iterator i;
97
98         for (i = region_views.begin(); i != region_views.end(); ++i) {
99                 if ((*i)->region() == r) {
100                         
101                         /* great. we already have a MidiRegionView for this Region. use it again. */
102
103                         (*i)->set_valid (true);
104                         (*i)->enable_display(wfd);
105                         display_region(dynamic_cast<MidiRegionView*>(*i), wfd);
106
107                         return NULL;
108                 }
109         }
110         
111         region_view = new MidiRegionView (canvas_group, _trackview, region, 
112                         _samples_per_unit, region_color);
113                 
114         region_view->init (region_color, false);
115         region_views.push_front (region_view);
116         
117         /* follow global waveform setting */
118
119         if (wfd) {
120                 region_view->enable_display(true);
121                 region_view->midi_region()->midi_source(0)->load_model();
122         }
123
124         /* display events and find note range */
125         display_region(region_view, wfd);
126
127         /* always display at least 1 octave range */
128         _highest_note = max(_highest_note, static_cast<uint8_t>(_lowest_note + 11));
129
130         /* catch regionview going away */
131         region->GoingAway.connect (bind (mem_fun (*this, &MidiStreamView::remove_region_view), region));
132         
133         RegionViewAdded (region_view);
134
135         return region_view;
136 }
137
138 void
139 MidiStreamView::display_region(MidiRegionView* region_view, bool load_model)
140 {
141         if ( ! region_view)
142                 return;
143
144         boost::shared_ptr<MidiSource> source(region_view->midi_region()->midi_source(0));
145
146         if (load_model)
147                 source->load_model();
148
149         if (source->model()) {
150                 // Find our note range
151                 for (size_t i=0; i < source->model()->n_notes(); ++i) {
152                         const MidiModel::Note& note = source->model()->note_at(i);
153                         update_bounds(note.note());
154                 }
155         }
156         
157         // Display region contents
158         region_view->display_model(source->model());
159 }
160
161 void
162 MidiStreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
163 {
164         StreamView::display_diskstream(ds);
165         draw_note_separators();
166 }
167
168 // FIXME: code duplication with AudioStreamView
169 void
170 MidiStreamView::redisplay_diskstream ()
171 {
172         list<RegionView *>::iterator i, tmp;
173
174         for (i = region_views.begin(); i != region_views.end(); ++i) {
175                 (*i)->enable_display(true); // FIXME: double display
176                 (*i)->set_valid (false);
177                 
178                 /* FIXME: slow.  MidiRegionView needs a find_note_range method
179                  * that finds the range without wasting time drawing the events */
180
181                 // Load model if it isn't already, to get note range
182                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
183                 mrv->midi_region()->midi_source(0)->load_model();
184         }
185         
186         //_lowest_note = 60; // middle C
187         //_highest_note = _lowest_note + 11;
188
189         if (_trackview.is_midi_track()) {
190                 _trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
191         }
192
193         /* Always display at least one octave */
194         if (_highest_note == 127) {
195                 if (_lowest_note > (127 - 11))
196                         _lowest_note = 127 - 11;
197         } else if (_highest_note < _lowest_note + 11) {
198                 _highest_note = _lowest_note + 11;
199         }
200         
201         for (i = region_views.begin(); i != region_views.end(); ) {
202                 tmp = i;
203                 tmp++;
204
205                 if (!(*i)->is_valid()) {
206                         delete *i;
207                         region_views.erase (i);
208                 } else {
209                         (*i)->enable_display(true);
210                         (*i)->set_y_position_and_height(0, height); // apply note range
211                 }
212
213                 i = tmp;
214         }
215         
216         /* now fix layering */
217
218         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
219                 region_layered (*i);
220         }
221         
222         draw_note_separators();
223 }
224
225
226 void
227 MidiStreamView::update_contents_y_position_and_height ()
228 {
229         StreamView::update_contents_y_position_and_height();
230         draw_note_separators();
231 }
232         
233 void
234 MidiStreamView::draw_note_separators()
235 {
236         for (uint8_t i=0; i < 127; ++i) {
237                 if (i >= _lowest_note-1 && i <= _highest_note) {
238                         _note_lines[i]->property_x1() = 0;
239                         _note_lines[i]->property_x2() = canvas_rect->property_x2() - 2;
240                         _note_lines[i]->property_y1() = note_to_y(i);
241                         _note_lines[i]->property_y2() = note_to_y(i);
242                         _note_lines[i]->show();
243                 } else {
244                         _note_lines[i]->hide();
245                 }
246         }
247 }
248         
249
250 void
251 MidiStreamView::set_note_range(VisibleNoteRange r)
252 {
253         _range = r;
254         if (r == FullRange) {
255                 _lowest_note = 0;
256                 _highest_note = 127;
257         } else {
258                 _lowest_note = 60;
259                 _highest_note = 60;
260         }
261         redisplay_diskstream();
262 }
263
264         
265 void 
266 MidiStreamView::update_bounds(uint8_t note_num)
267 {
268         _lowest_note = min(_lowest_note, note_num);
269         _highest_note = max(_highest_note, note_num);
270 }
271
272
273 void
274 MidiStreamView::setup_rec_box ()
275 {
276         // cerr << _trackview.name() << " streamview SRB\n";
277
278         if (_trackview.session().transport_rolling()) {
279
280                 if (!rec_active && 
281                     _trackview.session().record_status() == Session::Recording && 
282                     _trackview.get_diskstream()->record_enabled()) {
283
284                         if (use_rec_regions && rec_regions.size() == rec_rects.size()) {
285
286                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
287
288                                 MidiRegion::SourceList sources;
289                                 
290                                 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
291                                         (*prc).disconnect();
292                                 }
293                                 rec_data_ready_connections.clear();
294
295                                 // FIXME
296                                 boost::shared_ptr<MidiDiskstream> mds = boost::dynamic_pointer_cast<MidiDiskstream>(_trackview.get_diskstream());
297                                 assert(mds);
298
299                                 sources.push_back(mds->write_source());
300                                 
301                                 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())))); 
302
303                                 // handle multi
304                                 
305                                 jack_nframes_t start = 0;
306                                 if (rec_regions.size() > 0) {
307                                         start = rec_regions.back().first->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
308                                 }
309                                 
310                                 boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion>
311                                         (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
312                                 assert(region);
313                                 region->set_position (_trackview.session().transport_frame(), this);
314                                 rec_regions.push_back (make_pair(region, (RegionView*)0));
315                                 
316                                 // rec regions are destroyed in setup_rec_box
317
318                                 /* we add the region later */
319                         }
320                         
321                         /* start a new rec box */
322
323                         boost::shared_ptr<MidiTrack> mt = _trackview.midi_track(); /* we know what it is already */
324                         boost::shared_ptr<MidiDiskstream> ds = mt->midi_diskstream();
325                         jack_nframes_t frame_pos = ds->current_capture_start ();
326                         gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
327                         gdouble xend;
328                         uint32_t fill_color;
329
330                         assert(_trackview.midi_track()->mode() == Normal);
331                         
332                         xend = xstart;
333                         fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
334                         
335                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
336                         rec_rect->property_x1() = xstart;
337                         rec_rect->property_y1() = 1.0;
338                         rec_rect->property_x2() = xend;
339                         rec_rect->property_y2() = (double) _trackview.height - 1;
340                         rec_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
341                         rec_rect->property_fill_color_rgba() = fill_color;
342                         rec_rect->lower_to_bottom();
343                         
344                         RecBoxInfo recbox;
345                         recbox.rectangle = rec_rect;
346                         recbox.start = _trackview.session().transport_frame();
347                         recbox.length = 0;
348                         
349                         rec_rects.push_back (recbox);
350                         
351                         screen_update_connection.disconnect();
352                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &MidiStreamView::update_rec_box));    
353                         rec_updating = true;
354                         rec_active = true;
355
356                 } else if (rec_active &&
357                            (_trackview.session().record_status() != Session::Recording ||
358                             !_trackview.get_diskstream()->record_enabled())) {
359
360                         screen_update_connection.disconnect();
361                         rec_active = false;
362                         rec_updating = false;
363
364                 }
365                 
366         } else {
367
368                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
369
370                 if (!rec_rects.empty() || !rec_regions.empty()) {
371
372                         /* disconnect rapid update */
373                         screen_update_connection.disconnect();
374
375                         for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
376                                 (*prc).disconnect();
377                         }
378                         rec_data_ready_connections.clear();
379
380                         rec_updating = false;
381                         rec_active = false;
382                         
383                         /* remove temp regions */
384                         
385                         for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end();) {
386                                 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
387                                 
388                                 tmp = iter;
389                                 ++tmp;
390
391                                 (*iter).first->drop_references ();
392
393                                 iter = tmp;
394                         }
395                         
396                         rec_regions.clear();
397
398                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
399
400                         /* transport stopped, clear boxes */
401                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
402                                 RecBoxInfo &rect = (*iter);
403                                 delete rect.rectangle;
404                         }
405                         
406                         rec_rects.clear();
407                         
408                 }
409         }
410 }
411
412 void
413 MidiStreamView::update_rec_regions (boost::shared_ptr<MidiModel> data, nframes_t start, nframes_t dur)
414 {
415         ENSURE_GUI_THREAD (bind (mem_fun (*this, &MidiStreamView::update_rec_regions), data, start, dur));
416
417         if (use_rec_regions) {
418
419                 uint32_t n = 0;
420
421                 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
422
423                         list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
424
425                         tmp = iter;
426                         ++tmp;
427
428                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
429                                 /* rect already hidden, this region is done */
430                                 iter = tmp;
431                                 continue;
432                         }
433                         
434                         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(iter->first);
435                         if (!region) {
436                                 continue;
437                         }
438
439                         nframes_t origlen = region->length();
440                         
441                         //cerr << "MIDI URR: " << start << " * " << dur
442                         //      << " (origlen " << origlen << ")" << endl;
443
444                         if (region == rec_regions.back().first && rec_active) {
445
446                                 if (start >= region->start()) {
447
448                                         nframes_t nlen = start + dur - region->start();
449
450                                         if (nlen != region->length()) {
451
452                                                 region->freeze ();
453                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
454                                                 region->set_length (nlen, this);
455                                                 region->thaw ("updated");
456
457                                                 if (origlen == 1) {
458                                                         /* our special initial length */
459                                                         iter->second = add_region_view_internal (region, false);
460                                                         ((MidiRegionView*)iter->second)->begin_write();
461                                                 }
462
463                                                 /* also update rect */
464                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
465                                                 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
466                                                 rect->property_x2() = xend;
467
468                                                 /* draw events */
469                                                 MidiRegionView* mrv = (MidiRegionView*)iter->second;
470                                                 // FIXME: slow
471                                                 for (size_t i=0; i < data->n_notes(); ++i) {
472                                                         const MidiModel::Note& note = data->note_at(i);
473                                                         if (note.time() > start + dur)
474                                                                 break;
475
476                                                         if (note.time() >= start)
477                                                                 if (data->note_mode() == Percussive || note.duration() > 0)
478                                                                         mrv->add_note(note);
479                                                                 else
480                                                                         mrv->add_event(note.on_event());
481
482                                                         if (note.duration() > 0 && note.end_time() >= start)
483                                                                 mrv->add_event(note.off_event());
484
485                                                 }
486                                                 
487                                                 mrv->extend_active_notes();
488
489                                         }
490                                 }
491
492                         } else {
493
494                                 nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
495
496                                 if (nlen != region->length()) {
497
498                                         if (region->source(0)->length() >= region->start() + nlen) {
499
500                                                 region->freeze ();
501                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
502                                                 region->set_length (nlen, this);
503                                                 region->thaw ("updated");
504                                                 
505                                                 if (origlen == 1) {
506                                                         /* our special initial length */
507                                                         iter->second = add_region_view_internal (region, false);
508                                                 }
509                                                 
510                                                 /* also hide rect */
511                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
512                                                 rect->hide();
513
514                                         }
515                                 }
516                         }
517
518                         iter = tmp;
519                 }
520         }
521 }
522
523 void
524 MidiStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t dur, boost::weak_ptr<Source> weak_src)
525 {
526         // this is called from the butler thread for now
527         
528         ENSURE_GUI_THREAD(bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), start, dur, weak_src));
529         
530         boost::shared_ptr<SMFSource> src (boost::dynamic_pointer_cast<SMFSource>(weak_src.lock()));
531         
532         //cerr << src.get() << " MIDI READY: " << start << " * " << dur
533         //      << " -- " << data->size() << " events!" << endl;
534         
535         this->update_rec_regions (src->model(), start, dur);
536 }
537
538 void
539 MidiStreamView::color_handler ()
540 {
541
542         //case cMidiTrackBase:
543         if (_trackview.is_midi_track()) {
544                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
545         } 
546
547         //case cMidiBusBase:
548         if (!_trackview.is_midi_track()) {
549                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
550         }
551 }
552