Fix displaying of notes in auto-created MIDI region when it's the first region in...
[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         // Find our note range
150         if (source->model())
151                 for (size_t i=0; i < source->model()->n_notes(); ++i)
152                         update_bounds(source->model()->note_at(i)->note());
153         
154         // Display region contents
155         region_view->display_model(source->model());
156 }
157
158 void
159 MidiStreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
160 {
161         StreamView::display_diskstream(ds);
162         draw_note_separators();
163 }
164
165 // FIXME: code duplication with AudioStreamView
166 void
167 MidiStreamView::redisplay_diskstream ()
168 {
169         list<RegionView *>::iterator i, tmp;
170
171         for (i = region_views.begin(); i != region_views.end(); ++i) {
172                 (*i)->enable_display(true); // FIXME: double display, remove
173                 (*i)->set_valid (false);
174                 
175                 /* FIXME: slow.  MidiRegionView needs a find_note_range method
176                  * that finds the range without wasting time drawing the events */
177
178                 // Load model if it isn't already, to get note range
179                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
180                 mrv->midi_region()->midi_source(0)->load_model();
181         }
182         
183         //_lowest_note = 60; // middle C
184         //_highest_note = _lowest_note + 11;
185
186         if (_trackview.is_midi_track()) {
187                 _trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
188         }
189
190         /* Always display at least one octave */
191         if (_highest_note == 127) {
192                 if (_lowest_note > (127 - 11))
193                         _lowest_note = 127 - 11;
194         } else if (_highest_note < _lowest_note + 11) {
195                 _highest_note = _lowest_note + 11;
196         }
197         
198         for (i = region_views.begin(); i != region_views.end(); ) {
199                 tmp = i;
200                 tmp++;
201
202                 if (!(*i)->is_valid()) {
203                         delete *i;
204                         region_views.erase (i);
205                 } else {
206                         (*i)->enable_display(true);
207                         (*i)->set_y_position_and_height(0, height); // apply note range
208                 }
209
210                 i = tmp;
211         }
212         
213         /* now fix layering */
214
215         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
216                 region_layered (*i);
217         }
218         
219         draw_note_separators();
220 }
221
222
223 void
224 MidiStreamView::update_contents_y_position_and_height ()
225 {
226         StreamView::update_contents_y_position_and_height();
227         draw_note_separators();
228 }
229         
230 void
231 MidiStreamView::draw_note_separators()
232 {
233         for (uint8_t i=0; i < 127; ++i) {
234                 if (i >= _lowest_note-1 && i <= _highest_note) {
235                         _note_lines[i]->property_x1() = 0;
236                         _note_lines[i]->property_x2() = canvas_rect->property_x2() - 2;
237                         _note_lines[i]->property_y1() = note_to_y(i);
238                         _note_lines[i]->property_y2() = note_to_y(i);
239                         _note_lines[i]->show();
240                 } else {
241                         _note_lines[i]->hide();
242                 }
243         }
244 }
245         
246
247 void
248 MidiStreamView::set_note_range(VisibleNoteRange r)
249 {
250         _range = r;
251         if (r == FullRange) {
252                 _lowest_note = 0;
253                 _highest_note = 127;
254         } else {
255                 _lowest_note = 60;
256                 _highest_note = 60;
257         }
258         redisplay_diskstream();
259 }
260
261         
262 void 
263 MidiStreamView::update_bounds(uint8_t note_num)
264 {
265         _lowest_note = min(_lowest_note, note_num);
266         _highest_note = max(_highest_note, note_num);
267 }
268
269
270 void
271 MidiStreamView::setup_rec_box ()
272 {
273         // cerr << _trackview.name() << " streamview SRB\n";
274
275         if (_trackview.session().transport_rolling()) {
276
277                 if (!rec_active && 
278                     _trackview.session().record_status() == Session::Recording && 
279                     _trackview.get_diskstream()->record_enabled()) {
280
281                         if (use_rec_regions && rec_regions.size() == rec_rects.size()) {
282
283                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
284
285                                 MidiRegion::SourceList sources;
286                                 
287                                 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
288                                         (*prc).disconnect();
289                                 }
290                                 rec_data_ready_connections.clear();
291
292                                 // FIXME
293                                 boost::shared_ptr<MidiDiskstream> mds = boost::dynamic_pointer_cast<MidiDiskstream>(_trackview.get_diskstream());
294                                 assert(mds);
295
296                                 sources.push_back(mds->write_source());
297                                 
298                                 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())))); 
299
300                                 // handle multi
301                                 
302                                 jack_nframes_t start = 0;
303                                 if (rec_regions.size() > 0) {
304                                         start = rec_regions.back().first->position() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
305                                 }
306                                 
307                                 boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion>
308                                         (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
309                                 assert(region);
310                                 region->set_position (_trackview.session().transport_frame(), this);
311                                 rec_regions.push_back (make_pair(region, (RegionView*)0));
312                                 
313                                 // rec regions are destroyed in setup_rec_box
314
315                                 /* we add the region later */
316                         }
317                         
318                         /* start a new rec box */
319
320                         boost::shared_ptr<MidiTrack> mt = _trackview.midi_track(); /* we know what it is already */
321                         boost::shared_ptr<MidiDiskstream> ds = mt->midi_diskstream();
322                         jack_nframes_t frame_pos = ds->current_capture_start ();
323                         gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
324                         gdouble xend;
325                         uint32_t fill_color;
326
327                         assert(_trackview.midi_track()->mode() == Normal);
328                         
329                         xend = xstart;
330                         fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
331                         
332                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
333                         rec_rect->property_x1() = xstart;
334                         rec_rect->property_y1() = 1.0;
335                         rec_rect->property_x2() = xend;
336                         rec_rect->property_y2() = (double) _trackview.height - 1;
337                         rec_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
338                         rec_rect->property_fill_color_rgba() = fill_color;
339                         rec_rect->lower_to_bottom();
340                         
341                         RecBoxInfo recbox;
342                         recbox.rectangle = rec_rect;
343                         recbox.start = _trackview.session().transport_frame();
344                         recbox.length = 0;
345                         
346                         rec_rects.push_back (recbox);
347                         
348                         screen_update_connection.disconnect();
349                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &MidiStreamView::update_rec_box));    
350                         rec_updating = true;
351                         rec_active = true;
352
353                 } else if (rec_active &&
354                            (_trackview.session().record_status() != Session::Recording ||
355                             !_trackview.get_diskstream()->record_enabled())) {
356
357                         screen_update_connection.disconnect();
358                         rec_active = false;
359                         rec_updating = false;
360
361                 }
362                 
363         } else {
364
365                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
366
367                 if (!rec_rects.empty() || !rec_regions.empty()) {
368
369                         /* disconnect rapid update */
370                         screen_update_connection.disconnect();
371
372                         for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
373                                 (*prc).disconnect();
374                         }
375                         rec_data_ready_connections.clear();
376
377                         rec_updating = false;
378                         rec_active = false;
379                         
380                         /* remove temp regions */
381                         
382                         for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end();) {
383                                 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
384                                 
385                                 tmp = iter;
386                                 ++tmp;
387
388                                 (*iter).first->drop_references ();
389
390                                 iter = tmp;
391                         }
392                         
393                         rec_regions.clear();
394
395                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
396
397                         /* transport stopped, clear boxes */
398                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
399                                 RecBoxInfo &rect = (*iter);
400                                 delete rect.rectangle;
401                         }
402                         
403                         rec_rects.clear();
404                         
405                 }
406         }
407 }
408
409 void
410 MidiStreamView::update_rec_regions (boost::shared_ptr<MidiModel> data, nframes_t start, nframes_t dur)
411 {
412         ENSURE_GUI_THREAD (bind (mem_fun (*this, &MidiStreamView::update_rec_regions), data, start, dur));
413
414         if (use_rec_regions) {
415
416                 uint32_t n = 0;
417                 bool     update_range = false;
418
419                 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
420
421                         list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
422
423                         tmp = iter;
424                         ++tmp;
425
426                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
427                                 /* rect already hidden, this region is done */
428                                 iter = tmp;
429                                 continue;
430                         }
431                         
432                         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(iter->first);
433                         if (!region) {
434                                 continue;
435                         }
436
437                         nframes_t origlen = region->length();
438                         
439                         if (region == rec_regions.back().first && rec_active) {
440
441                                 if (start >= region->midi_source(0)->timeline_position()) {
442                                 
443                                         nframes_t nlen = start + dur - region->position();
444
445                                         if (nlen != region->length()) {
446                                         
447                                                 region->freeze ();
448                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
449                                                 region->set_length (start + dur - region->position(), this);
450                                                 region->thaw ("updated");
451                                                 
452                                                 if (origlen == 1) {
453                                                         /* our special initial length */
454                                                         iter->second = add_region_view_internal (region, false);
455                                                         ((MidiRegionView*)iter->second)->begin_write();
456                                                 }
457
458                                                 /* also update rect */
459                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
460                                                 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
461                                                 rect->property_x2() = xend;
462
463                                                 /* draw events */
464                                                 MidiRegionView* mrv = (MidiRegionView*)iter->second;
465                                                 for (size_t i=0; i < data->n_notes(); ++i) {
466
467                                                         // FIXME: slooooooooow!
468
469                                                         const boost::shared_ptr<Note> note = data->note_at(i);
470                                                         
471                                                         if (note->duration() > 0 && note->end_time() + region->position() > start)
472                                                                 mrv->resolve_note(note->note(), note->end_time());
473
474                                                         if (note->time() + region->position() < start)
475                                                                 continue;
476
477                                                         if (note->time() + region->position() > start + dur)
478                                                                 break;
479
480                                                         mrv->add_note(note);
481
482                                                         if (note->note() < _lowest_note) {
483                                                                 _lowest_note = note->note();
484                                                                 update_range = true;
485                                                         } else if (note->note() > _highest_note) {
486                                                                 _highest_note = note->note();
487                                                                 update_range = true;
488                                                         }
489                                                 }
490                                                 
491                                                 mrv->extend_active_notes();
492                                         }
493                                 }
494
495                         } else {
496                                 
497                                 nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
498
499                                 if (nlen != region->length()) {
500
501                                         if (region->source(0)->length() >= region->position() + nlen) {
502
503                                                 region->freeze ();
504                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
505                                                 region->set_length (nlen, this);
506                                                 region->thaw ("updated");
507                                                 
508                                                 if (origlen == 1) {
509                                                         /* our special initial length */
510                                                         iter->second = add_region_view_internal (region, false);
511                                                 }
512                                                 
513                                                 /* also hide rect */
514                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
515                                                 rect->hide();
516
517                                         }
518                                 }
519                         }
520
521                         iter = tmp;
522                 }
523
524                 if (update_range)
525                         update_contents_y_position_and_height();
526         }
527 }
528
529 void
530 MidiStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t dur, boost::weak_ptr<Source> weak_src)
531 {
532         // this is called from the butler thread for now
533         
534         ENSURE_GUI_THREAD(bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), start, dur, weak_src));
535         
536         boost::shared_ptr<SMFSource> src (boost::dynamic_pointer_cast<SMFSource>(weak_src.lock()));
537         
538         this->update_rec_regions (src->model(), start, dur);
539 }
540
541 void
542 MidiStreamView::color_handler ()
543 {
544
545         //case cMidiTrackBase:
546         if (_trackview.is_midi_track()) {
547                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase.get();
548         } 
549
550         //case cMidiBusBase:
551         if (!_trackview.is_midi_track()) {
552                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase.get();;
553         }
554 }
555