use shared_ptr<> for all region handling
[ardour.git] / gtk2_ardour / audio_streamview.cc
1 /*
2     Copyright (C) 2001, 2006 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
22 #include <gtkmm.h>
23
24 #include <gtkmm2ext/gtk_ui.h>
25
26 #include <ardour/audioplaylist.h>
27 #include <ardour/audioregion.h>
28 #include <ardour/audiosource.h>
29 #include <ardour/audio_diskstream.h>
30 #include <ardour/audio_track.h>
31 #include <ardour/playlist_templates.h>
32 #include <ardour/source.h>
33 #include <ardour/region_factory.h>
34
35 #include "audio_streamview.h"
36 #include "audio_region_view.h"
37 #include "tape_region_view.h"
38 #include "audio_time_axis.h"
39 #include "canvas-waveview.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 "crossfade_view.h"
46 #include "rgb_macros.h"
47 #include "gui_thread.h"
48 #include "utils.h"
49 #include "color.h"
50
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Editing;
54
55 AudioStreamView::AudioStreamView (AudioTimeAxisView& tv)
56         : StreamView (tv)
57 {
58         crossfades_visible = true;
59
60         if (tv.is_audio_track())
61                 stream_base_color = color_map[cAudioTrackBase];
62         else
63                 stream_base_color = color_map[cAudioBusBase];
64         
65         canvas_rect->property_fill_color_rgba() = stream_base_color;
66         canvas_rect->property_outline_color_rgba() = color_map[cAudioTrackOutline];
67
68         _amplitude_above_axis = 1.0;
69
70         use_rec_regions = tv.editor.show_waveforms_recording ();
71         last_rec_peak_frame = 0;
72 }
73
74 AudioStreamView::~AudioStreamView ()
75 {
76 }
77
78 int
79 AudioStreamView::set_height (gdouble h)
80 {
81         /* limit the values to something sane-ish */
82         if (h < 10.0 || h > 1000.0) {
83                 return -1;
84         }
85
86         StreamView::set_height(h);
87
88         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
89                 (*i)->set_height (h);
90         }
91
92         return 0;
93 }
94
95 int 
96 AudioStreamView::set_samples_per_unit (gdouble spp)
97 {
98         StreamView::set_samples_per_unit(spp);
99
100         for (CrossfadeViewList::iterator xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
101                 (*xi)->set_samples_per_unit (spp);
102         }
103
104         return 0;
105 }
106
107 int 
108 AudioStreamView::set_amplitude_above_axis (gdouble app)
109 {
110         RegionViewList::iterator i;
111
112         if (app < 1.0) {
113                 return -1;
114         }
115
116         _amplitude_above_axis = app;
117
118         for (i = region_views.begin(); i != region_views.end(); ++i) {
119                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
120                 if (arv)
121                         arv->set_amplitude_above_axis (app);
122         }
123
124         return 0;
125 }
126
127 void
128 AudioStreamView::add_region_view_internal (boost::shared_ptr<Region> r, bool wait_for_waves)
129 {
130         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_region_view), r));
131
132         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
133
134         if (region == 0) {
135                 return;
136         }
137
138         AudioRegionView *region_view;
139         list<RegionView *>::iterator i;
140
141         for (i = region_views.begin(); i != region_views.end(); ++i) {
142                 if ((*i)->region() == r) {
143                         
144                         /* great. we already have a AudioRegionView for this Region. use it again. */
145
146                         (*i)->set_valid (true);
147                         return;
148                 }
149         }
150         
151         switch (_trackview.audio_track()->mode()) {
152         case Normal:
153                 region_view = new AudioRegionView (canvas_group, _trackview, region, 
154                                                    _samples_per_unit, region_color);
155                 break;
156         case Destructive:
157                 region_view = new TapeAudioRegionView (canvas_group, _trackview, region, 
158                                                        _samples_per_unit, region_color);
159                 break;
160         }
161
162         region_view->init (region_color, wait_for_waves);
163         region_view->set_amplitude_above_axis(_amplitude_above_axis);
164         region_views.push_front (region_view);
165         
166         /* follow global waveform setting */
167
168         region_view->set_waveform_visible(_trackview.editor.show_waveforms());
169
170         /* catch regionview going away */
171
172         region->GoingAway.connect (bind (mem_fun (*this, &AudioStreamView::remove_region_view), region));
173         
174         RegionViewAdded (region_view);
175 }
176
177 void
178 AudioStreamView::remove_region_view (boost::shared_ptr<Region> r)
179 {
180         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_region_view), r));
181
182         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
183                 list<CrossfadeView*>::iterator tmp;
184                 
185                 tmp = i;
186                 ++tmp;
187                 
188                 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(r);
189                 if (ar && (*i)->crossfade.involves (ar)) {
190                         delete *i;
191                         crossfade_views.erase (i);
192                 }
193                 
194                 i = tmp;
195         }
196
197         StreamView::remove_region_view(r);
198 }
199
200 void
201 AudioStreamView::undisplay_diskstream ()
202 {
203         StreamView::undisplay_diskstream();
204
205         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
206                 delete *i;
207         }
208
209         crossfade_views.clear ();
210 }
211
212 void
213 AudioStreamView::playlist_modified ()
214 {
215         ENSURE_GUI_THREAD (mem_fun (*this, &AudioStreamView::playlist_modified));
216
217         StreamView::playlist_modified();
218         
219         /* if the playlist is modified, make sure xfades are on top and all the regionviews are stacked 
220            correctly.
221         */
222
223         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
224                 (*i)->get_canvas_group()->raise_to_top();
225         }
226 }
227
228 void
229 AudioStreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
230 {
231         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::playlist_changed), ds));
232
233         StreamView::playlist_changed(ds);
234
235         AudioPlaylist* apl = dynamic_cast<AudioPlaylist*>(ds->playlist());
236         if (apl)
237                 playlist_connections.push_back (apl->NewCrossfade.connect (mem_fun (*this, &AudioStreamView::add_crossfade)));
238 }
239
240 void
241 AudioStreamView::add_crossfade (Crossfade *crossfade)
242 {
243         AudioRegionView* lview = 0;
244         AudioRegionView* rview = 0;
245
246         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_crossfade), crossfade));
247
248         /* first see if we already have a CrossfadeView for this Crossfade */
249
250         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
251                 if (&(*i)->crossfade == crossfade) {
252                         if (!crossfades_visible) {
253                                 (*i)->hide();
254                         } else {
255                                 (*i)->show ();
256                         }
257                         (*i)->set_valid (true);
258                         return;
259                 }
260         }
261
262         /* create a new one */
263
264         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
265                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
266
267                 if (!lview && arv && (arv->region() == crossfade->out())) {
268                         lview = arv;
269                 }
270                 if (!rview && arv && (arv->region() == crossfade->in())) {
271                         rview = arv;
272                 }
273         }
274
275         CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display,
276                                                _trackview,
277                                                *crossfade,
278                                                _samples_per_unit,
279                                                region_color,
280                                                *lview, *rview);
281
282         crossfade->Invalidated.connect (mem_fun (*this, &AudioStreamView::remove_crossfade));
283         crossfade_views.push_back (cv);
284
285         if (!crossfades_visible) {
286                 cv->hide ();
287         }
288 }
289
290 void
291 AudioStreamView::remove_crossfade (Crossfade *xfade)
292 {
293         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_crossfade), xfade));
294
295         for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
296                 if (&(*i)->crossfade == xfade) {
297                         delete *i;
298                         crossfade_views.erase (i);
299                         break;
300                 }
301         }
302 }
303
304 void
305 AudioStreamView::redisplay_diskstream ()
306 {
307         list<RegionView *>::iterator i, tmp;
308         list<CrossfadeView*>::iterator xi, tmpx;
309
310         
311         for (i = region_views.begin(); i != region_views.end(); ++i) {
312                 (*i)->set_valid (false);
313         }
314
315         for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
316                 (*xi)->set_valid (false);
317                 if ((*xi)->visible()) {
318                         (*xi)->show ();
319                 }
320         }
321
322         if (_trackview.is_audio_track()) {
323                 _trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
324                 AudioPlaylist* apl = dynamic_cast<AudioPlaylist*>(_trackview.get_diskstream()->playlist());
325                 if (apl)
326                         apl->foreach_crossfade (this, &AudioStreamView::add_crossfade);
327         }
328
329         for (i = region_views.begin(); i != region_views.end(); ) {
330                 tmp = i;
331                 tmp++;
332
333                 if (!(*i)->is_valid()) {
334                         delete *i;
335                         region_views.erase (i);
336                 } 
337
338                 i = tmp;
339         }
340
341         for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
342                 tmpx = xi;
343                 tmpx++;
344
345                 if (!(*xi)->valid()) {
346                         delete *xi;
347                         crossfade_views.erase (xi);
348                 }
349
350                 xi = tmpx;
351         }
352
353         /* now fix layering */
354
355         playlist_modified ();
356 }
357
358 void
359 AudioStreamView::set_show_waveforms (bool yn)
360 {
361         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
362                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
363                 if (arv)
364                         arv->set_waveform_visible (yn);
365         }
366 }
367
368 void
369 AudioStreamView::set_waveform_shape (WaveformShape shape)
370 {
371         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
372                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
373                 if (arv)
374                         arv->set_waveform_shape (shape);
375         }
376 }               
377                 
378 void
379 AudioStreamView::setup_rec_box ()
380 {
381         // cerr << _trackview.name() << " streamview SRB\n";
382
383         if (_trackview.session().transport_rolling()) {
384
385                 // cerr << "\trolling\n";
386
387                 if (!rec_active && 
388                     _trackview.session().record_status() == Session::Recording && 
389                     _trackview.get_diskstream()->record_enabled()) {
390
391                         if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
392
393                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
394
395                                 SourceList sources;
396
397                                 for (list<sigc::connection>::iterator prc = peak_ready_connections.begin(); prc != peak_ready_connections.end(); ++prc) {
398                                         (*prc).disconnect();
399                                 }
400                                 peak_ready_connections.clear();
401                                         
402                                 // FIXME
403                                 boost::shared_ptr<AudioDiskstream> ads = boost::dynamic_pointer_cast<AudioDiskstream>(_trackview.get_diskstream());
404                                 assert(ads);
405
406                                 for (uint32_t n=0; n < ads->n_channels(); ++n) {
407                                         AudioSource *src = (AudioSource *) ads->write_source (n);
408                                         if (src) {
409                                                 sources.push_back (src);
410                                                 peak_ready_connections.push_back (src->PeakRangeReady.connect (bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), src))); 
411                                         }
412                                 }
413
414                                 // handle multi
415                                 
416                                 jack_nframes_t start = 0;
417                                 if (rec_regions.size() > 0) {
418                                         start = rec_regions.back()->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
419                                 }
420                                 
421                                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion>
422                                                                        (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
423                                 region->set_position (_trackview.session().transport_frame(), this);
424                                 rec_regions.push_back (region);
425                                 /* catch it if it goes away */
426                                 region->GoingAway.connect (bind (mem_fun (*this, &AudioStreamView::remove_rec_region), region));
427
428                                 /* we add the region later */
429                         }
430                         
431                         /* start a new rec box */
432
433                         AudioTrack* at;
434
435                         at = _trackview.audio_track(); /* we know what it is already */
436                         boost::shared_ptr<AudioDiskstream> ds = at->audio_diskstream();
437                         jack_nframes_t frame_pos = ds->current_capture_start ();
438                         gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
439                         gdouble xend;
440                         uint32_t fill_color;
441
442                         switch (_trackview.audio_track()->mode()) {
443                         case Normal:
444                                 xend = xstart;
445                                 fill_color = color_map[cRecordingRectFill];
446                                 break;
447
448                         case Destructive:
449                                 xend = xstart + 2;
450                                 fill_color = color_map[cRecordingRectFill];
451                                 /* make the recording rect translucent to allow
452                                    the user to see the peak data coming in, etc.
453                                 */
454                                 fill_color = UINT_RGBA_CHANGE_A (fill_color, 120);
455                                 break;
456                         }
457                         
458                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
459                         rec_rect->property_x1() = xstart;
460                         rec_rect->property_y1() = 1.0;
461                         rec_rect->property_x2() = xend;
462                         rec_rect->property_y2() = (double) _trackview.height - 1;
463                         rec_rect->property_outline_color_rgba() = color_map[cRecordingRectOutline];
464                         rec_rect->property_fill_color_rgba() = fill_color;
465                         
466                         RecBoxInfo recbox;
467                         recbox.rectangle = rec_rect;
468                         recbox.start = _trackview.session().transport_frame();
469                         recbox.length = 0;
470                         
471                         rec_rects.push_back (recbox);
472                         
473                         screen_update_connection.disconnect();
474                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &AudioStreamView::update_rec_box));   
475                         rec_updating = true;
476                         rec_active = true;
477
478                 } else if (rec_active &&
479                            (_trackview.session().record_status() != Session::Recording ||
480                             !_trackview.get_diskstream()->record_enabled())) {
481
482                         screen_update_connection.disconnect();
483                         rec_active = false;
484                         rec_updating = false;
485
486                 }
487                 
488         } else {
489
490                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
491
492                 if (!rec_rects.empty() || !rec_regions.empty()) {
493
494                         /* disconnect rapid update */
495                         screen_update_connection.disconnect();
496
497                         for (list<sigc::connection>::iterator prc = peak_ready_connections.begin(); prc != peak_ready_connections.end(); ++prc) {
498                                 (*prc).disconnect();
499                         }
500                         peak_ready_connections.clear();
501
502                         rec_updating = false;
503                         rec_active = false;
504                         last_rec_peak_frame = 0;
505                         
506                         /* remove temp regions */
507                         
508                         rec_regions.clear();
509
510                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
511
512                         /* transport stopped, clear boxes */
513                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
514                                 RecBoxInfo &rect = (*iter);
515                                 delete rect.rectangle;
516                         }
517                         
518                         rec_rects.clear();
519                         
520                 }
521         }
522 }
523
524 void
525 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
526 {
527         for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
528                 ((*i)->*pmf) ();
529         }
530 }
531
532 void
533 AudioStreamView::rec_peak_range_ready (jack_nframes_t start, jack_nframes_t cnt, Source * src)
534 {
535         // this is called from the peak building thread
536
537         ENSURE_GUI_THREAD(bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), start, cnt, src));
538         
539         if (rec_peak_ready_map.size() == 0 || start+cnt > last_rec_peak_frame) {
540                 last_rec_peak_frame = start + cnt;
541         }
542
543         rec_peak_ready_map[src] = true;
544
545         if (rec_peak_ready_map.size() == _trackview.get_diskstream()->n_channels()) {
546                 this->update_rec_regions ();
547                 rec_peak_ready_map.clear();
548         }
549 }
550
551 void
552 AudioStreamView::update_rec_regions ()
553 {
554         if (use_rec_regions) {
555
556                 uint32_t n = 0;
557
558                 for (list<boost::shared_ptr<Region> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
559
560                         list<boost::shared_ptr<Region> >::iterator tmp;
561
562                         tmp = iter;
563                         ++tmp;
564
565                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
566                                 /* rect already hidden, this region is done */
567                                 iter = tmp;
568                                 continue;
569                         }
570                         
571                         // FIXME
572                         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(*iter);
573                         assert(region);
574
575                         jack_nframes_t origlen = region->length();
576
577                         if (region == rec_regions.back() && rec_active) {
578
579                                 if (last_rec_peak_frame > region->start()) {
580
581                                         jack_nframes_t nlen = last_rec_peak_frame - region->start();
582
583                                         if (nlen != region->length()) {
584
585                                                 region->freeze ();
586                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
587                                                 region->set_length (nlen, this);
588                                                 region->thaw ("updated");
589
590                                                 if (origlen == 1) {
591                                                         /* our special initial length */
592                                                         add_region_view_internal (region, false);
593                                                 }
594
595                                                 /* also update rect */
596                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
597                                                 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
598                                                 rect->property_x2() = xend;
599                                         }
600                                 }
601
602                         } else {
603
604                                 jack_nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
605
606                                 if (nlen != region->length()) {
607
608                                         if (region->source(0).length() >= region->start() + nlen) {
609
610                                                 region->freeze ();
611                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
612                                                 region->set_length (nlen, this);
613                                                 region->thaw ("updated");
614                                                 
615                                                 if (origlen == 1) {
616                                                         /* our special initial length */
617                                                         add_region_view_internal (region, false);
618                                                 }
619                                                 
620                                                 /* also hide rect */
621                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
622                                                 rect->hide();
623
624                                         }
625                                 }
626                         }
627
628                         iter = tmp;
629                 }
630         }
631 }
632
633 void
634 AudioStreamView::show_all_xfades ()
635 {
636         foreach_crossfadeview (&CrossfadeView::show);
637         crossfades_visible = true;
638 }
639
640 void
641 AudioStreamView::hide_all_xfades ()
642 {
643         foreach_crossfadeview (&CrossfadeView::hide);
644         crossfades_visible = false;
645 }
646
647 void
648 AudioStreamView::hide_xfades_involving (AudioRegionView& rv)
649 {
650         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
651                 if ((*i)->crossfade.involves (rv.audio_region())) {
652                         (*i)->fake_hide ();
653                 }
654         }
655 }
656
657 void
658 AudioStreamView::reveal_xfades_involving (AudioRegionView& rv)
659 {
660         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
661                 if ((*i)->crossfade.involves (rv.audio_region()) && (*i)->visible()) {
662                         (*i)->show ();
663                 }
664         }
665 }
666
667 void
668 AudioStreamView::color_handler (ColorID id, uint32_t val)
669 {
670         switch (id) {
671         case cAudioTrackBase:
672                 if (_trackview.is_track()) {
673                         canvas_rect->property_fill_color_rgba() = val;
674                 } 
675                 break;
676         case cAudioBusBase:
677                 if (!_trackview.is_track()) {
678                         canvas_rect->property_fill_color_rgba() = val;
679                 }
680                 break;
681         case cAudioTrackOutline:
682                 canvas_rect->property_outline_color_rgba() = val;
683                 break;
684
685         default:
686                 break;
687         }
688 }
689