Merged with trunk R999.
[ardour.git] / gtk2_ardour / midi_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/midi_playlist.h>
27 #include <ardour/midi_region.h>
28 #include <ardour/midi_source.h>
29 #include <ardour/midi_diskstream.h>
30 #include <ardour/midi_track.h>
31 #include <ardour/smf_source.h>
32 #include <ardour/region_factory.h>
33
34 #include "midi_streamview.h"
35 #include "region_view.h"
36 #include "midi_region_view.h"
37 #include "midi_time_axis.h"
38 #include "canvas-simplerect.h"
39 #include "region_selection.h"
40 #include "selection.h"
41 #include "public_editor.h"
42 #include "ardour_ui.h"
43 #include "rgb_macros.h"
44 #include "gui_thread.h"
45 #include "utils.h"
46 #include "color.h"
47
48 using namespace ARDOUR;
49 using namespace PBD;
50 using namespace Editing;
51
52 MidiStreamView::MidiStreamView (MidiTimeAxisView& tv)
53         : StreamView (tv)
54 {
55         if (tv.is_track())
56                 stream_base_color = color_map[cMidiTrackBase];
57         else
58                 stream_base_color = color_map[cMidiBusBase];
59         
60         canvas_rect->property_fill_color_rgba() = stream_base_color;
61         canvas_rect->property_outline_color_rgba() = color_map[cAudioTrackOutline];
62
63         //use_rec_regions = tv.editor.show_waveforms_recording ();
64         use_rec_regions = true;
65 }
66
67 MidiStreamView::~MidiStreamView ()
68 {
69 }
70
71
72 void
73 MidiStreamView::add_region_view_internal (boost::shared_ptr<Region> r, bool wait_for_waves)
74 {
75         ENSURE_GUI_THREAD (bind (mem_fun (*this, &MidiStreamView::add_region_view), r));
76
77         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion> (r);
78
79         if (region == 0) {
80                 return;
81         }
82
83         MidiRegionView *region_view;
84         list<RegionView *>::iterator i;
85
86         for (i = region_views.begin(); i != region_views.end(); ++i) {
87                 if ((*i)->region() == r) {
88                         
89                         /* great. we already have a MidiRegionView for this Region. use it again. */
90
91                         (*i)->set_valid (true);
92                         return;
93                 }
94         }
95         
96         // can't we all just get along?
97         assert(_trackview.midi_track()->mode() != Destructive);
98
99         region_view = new MidiRegionView (canvas_group, _trackview, region, 
100                         _samples_per_unit, region_color);
101
102         region_view->init (region_color, wait_for_waves);
103         region_views.push_front (region_view);
104         
105         /* follow global waveform setting */
106
107         // FIXME
108         //region_view->set_waveform_visible(_trackview.editor.show_waveforms());
109
110         /* catch regionview going away */
111
112         region->GoingAway.connect (bind (mem_fun (*this, &MidiStreamView::remove_region_view), region));
113         
114         RegionViewAdded (region_view);
115 }
116
117 // FIXME: code duplication with AudioStreamVIew
118 void
119 MidiStreamView::redisplay_diskstream ()
120 {
121         list<RegionView *>::iterator i, tmp;
122
123         for (i = region_views.begin(); i != region_views.end(); ++i) {
124                 (*i)->set_valid (false);
125         }
126
127         if (_trackview.is_midi_track()) {
128                 _trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
129         }
130
131         for (i = region_views.begin(); i != region_views.end(); ) {
132                 tmp = i;
133                 tmp++;
134
135                 if (!(*i)->is_valid()) {
136                         delete *i;
137                         region_views.erase (i);
138                 } 
139
140                 i = tmp;
141         }
142
143         /* now fix layering */
144
145         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
146                 region_layered (*i);
147         }
148 }
149
150
151 void
152 MidiStreamView::setup_rec_box ()
153 {
154         // cerr << _trackview.name() << " streamview SRB\n";
155
156         if (_trackview.session().transport_rolling()) {
157
158                 if (!rec_active && 
159                     _trackview.session().record_status() == Session::Recording && 
160                     _trackview.get_diskstream()->record_enabled()) {
161
162                         if (use_rec_regions && rec_regions.size() == rec_rects.size()) {
163
164                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
165
166                                 MidiRegion::SourceList sources;
167
168                                 // FIXME
169                                 boost::shared_ptr<MidiDiskstream> mds = boost::dynamic_pointer_cast<MidiDiskstream>(_trackview.get_diskstream());
170                                 assert(mds);
171
172                                 sources.push_back(mds->write_source());
173                                 
174                                 rec_data_ready_connections.push_back (mds->write_source()->ViewDataRangeReady.connect (bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), mds->write_source()))); 
175
176                                 // handle multi
177                                 
178                                 jack_nframes_t start = 0;
179                                 if (rec_regions.size() > 0) {
180                                         start = rec_regions.back()->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
181                                 }
182                                 
183                                 boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion>
184                                         (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
185                                 assert(region);
186                                 region->set_position (_trackview.session().transport_frame(), this);
187                                 rec_regions.push_back (region);
188                                 
189                                 // rec regions are destroyed in setup_rec_box
190
191                                 /* we add the region later */
192                         }
193                         
194                         /* start a new rec box */
195
196                         MidiTrack* mt = _trackview.midi_track(); /* we know what it is already */
197                         boost::shared_ptr<MidiDiskstream> ds = mt->midi_diskstream();
198                         jack_nframes_t frame_pos = ds->current_capture_start ();
199                         gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
200                         gdouble xend;
201                         uint32_t fill_color;
202
203                         assert(_trackview.midi_track()->mode() == Normal);
204                         
205                         xend = xstart;
206                         fill_color = color_map[cRecordingRectFill];
207                         
208                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
209                         rec_rect->property_x1() = xstart;
210                         rec_rect->property_y1() = 1.0;
211                         rec_rect->property_x2() = xend;
212                         rec_rect->property_y2() = (double) _trackview.height - 1;
213                         rec_rect->property_outline_color_rgba() = color_map[cRecordingRectOutline];
214                         rec_rect->property_fill_color_rgba() = fill_color;
215                         
216                         RecBoxInfo recbox;
217                         recbox.rectangle = rec_rect;
218                         recbox.start = _trackview.session().transport_frame();
219                         recbox.length = 0;
220                         
221                         rec_rects.push_back (recbox);
222                         
223                         screen_update_connection.disconnect();
224                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &MidiStreamView::update_rec_box));    
225                         rec_updating = true;
226                         rec_active = true;
227
228                 } else if (rec_active &&
229                            (_trackview.session().record_status() != Session::Recording ||
230                             !_trackview.get_diskstream()->record_enabled())) {
231
232                         screen_update_connection.disconnect();
233                         rec_active = false;
234                         rec_updating = false;
235
236                 }
237                 
238         } else {
239
240                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
241
242                 if (!rec_rects.empty() || !rec_regions.empty()) {
243
244                         /* disconnect rapid update */
245                         screen_update_connection.disconnect();
246
247                         for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
248                                 (*prc).disconnect();
249                         }
250                         rec_data_ready_connections.clear();
251
252                         rec_updating = false;
253                         rec_active = false;
254                         last_rec_data_frame = 0;
255                         
256                         /* remove temp regions */
257                         
258                         for (list<boost::shared_ptr<Region> >::iterator iter = rec_regions.begin(); iter != rec_regions.end();) {
259                                 list<boost::shared_ptr<Region> >::iterator tmp;
260                                 tmp = iter;
261                                 ++tmp;
262                                 (*iter)->drop_references ();
263                                 iter = tmp;
264                         }
265                         
266                         rec_regions.clear();
267
268                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
269
270                         /* transport stopped, clear boxes */
271                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
272                                 RecBoxInfo &rect = (*iter);
273                                 delete rect.rectangle;
274                         }
275                         
276                         rec_rects.clear();
277                         
278                 }
279         }
280 }
281
282 void
283 MidiStreamView::update_rec_regions ()
284 {
285         if (use_rec_regions) {
286
287
288                 uint32_t n = 0;
289
290                 for (list<boost::shared_ptr<Region> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
291
292                         list<boost::shared_ptr<Region> >::iterator tmp;
293
294                         tmp = iter;
295                         ++tmp;
296
297                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
298                                 /* rect already hidden, this region is done */
299                                 iter = tmp;
300                                 continue;
301                         }
302                         
303                         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(*iter);
304                         assert(region);
305
306                         jack_nframes_t origlen = region->length();
307
308                         if (region == rec_regions.back() && rec_active) {
309
310                                 if (last_rec_data_frame > region->start()) {
311
312                                         jack_nframes_t nlen = last_rec_data_frame - region->start();
313
314                                         if (nlen != region->length()) {
315
316                                                 region->freeze ();
317                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
318                                                 region->set_length (nlen, this);
319                                                 region->thaw ("updated");
320
321                                                 if (origlen == 1) {
322                                                         /* our special initial length */
323                                                         add_region_view_internal (region, false);
324                                                 }
325
326                                                 /* also update rect */
327                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
328                                                 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
329                                                 rect->property_x2() = xend;
330                                         }
331                                 }
332
333                         } else {
334
335                                 jack_nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
336
337                                 if (nlen != region->length()) {
338
339                                         if (region->source(0)->length() >= region->start() + nlen) {
340
341                                                 region->freeze ();
342                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
343                                                 region->set_length (nlen, this);
344                                                 region->thaw ("updated");
345                                                 
346                                                 if (origlen == 1) {
347                                                         /* our special initial length */
348                                                         add_region_view_internal (region, false);
349                                                 }
350                                                 
351                                                 /* also hide rect */
352                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
353                                                 rect->hide();
354
355                                         }
356                                 }
357                         }
358
359                         iter = tmp;
360                 }
361         }
362 }
363
364 void
365 MidiStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t cnt, boost::shared_ptr<Source> src)
366 {
367         // this is called from the butler thread for now
368         // yeah we need a "peak" building thread or something, though there's not really any
369         // work for it to do...  whatever. :)
370         
371         ENSURE_GUI_THREAD(bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), start, cnt, src));
372         
373         //cerr << "REC DATA: " << start << " --- " << cnt << endl;
374
375         if (rec_data_ready_map.size() == 0 || start+cnt > last_rec_data_frame) {
376                 last_rec_data_frame = start + cnt;
377         }
378
379         rec_data_ready_map[src] = true;
380
381         if (rec_data_ready_map.size() == _trackview.get_diskstream()->n_channels().get(DataType::MIDI)) {
382                 this->update_rec_regions ();
383                 rec_data_ready_map.clear();
384         }
385 }
386
387 void
388 MidiStreamView::color_handler (ColorID id, uint32_t val)
389 {
390         switch (id) {
391         case cMidiTrackBase:
392                 if (_trackview.is_midi_track()) {
393                         canvas_rect->property_fill_color_rgba() = val;
394                 } 
395                 break;
396         case cMidiBusBase:
397                 if (!_trackview.is_midi_track()) {
398                         canvas_rect->property_fill_color_rgba() = val;
399                 }
400                 break;
401         case cMidiTrackOutline:
402                 canvas_rect->property_outline_color_rgba() = val;
403                 break;
404
405         default:
406                 break;
407         }
408 }