94d2a397cfe266c8888758004f190aecd490ffa1
[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         playlist_modified ();
146 }
147
148
149 void
150 MidiStreamView::setup_rec_box ()
151 {
152         // cerr << _trackview.name() << " streamview SRB\n";
153
154         if (_trackview.session().transport_rolling()) {
155
156                 if (!rec_active && 
157                     _trackview.session().record_status() == Session::Recording && 
158                     _trackview.get_diskstream()->record_enabled()) {
159
160                         if (use_rec_regions && rec_regions.size() == rec_rects.size()) {
161
162                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
163
164                                 MidiRegion::SourceList sources;
165
166                                 // FIXME
167                                 boost::shared_ptr<MidiDiskstream> mds = boost::dynamic_pointer_cast<MidiDiskstream>(_trackview.get_diskstream());
168                                 assert(mds);
169
170                                 sources.push_back(mds->write_source());
171                                 
172                                 rec_data_ready_connections.push_back (mds->write_source()->ViewDataRangeReady.connect (bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), mds->write_source()))); 
173
174                                 // handle multi
175                                 
176                                 jack_nframes_t start = 0;
177                                 if (rec_regions.size() > 0) {
178                                         start = rec_regions.back()->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
179                                 }
180                                 
181                                 boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion>
182                                         (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
183                                 assert(region);
184                                 region->set_position (_trackview.session().transport_frame(), this);
185                                 rec_regions.push_back (region);
186                                 /* catch it if it goes away */
187                                 region->GoingAway.connect (bind (mem_fun (*this, &MidiStreamView::remove_rec_region), region));
188
189                                 /* we add the region later */
190                         }
191                         
192                         /* start a new rec box */
193
194                         MidiTrack* mt = _trackview.midi_track(); /* we know what it is already */
195                         boost::shared_ptr<MidiDiskstream> ds = mt->midi_diskstream();
196                         jack_nframes_t frame_pos = ds->current_capture_start ();
197                         gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
198                         gdouble xend;
199                         uint32_t fill_color;
200
201                         assert(_trackview.midi_track()->mode() == Normal);
202                         
203                         xend = xstart;
204                         fill_color = color_map[cRecordingRectFill];
205                         
206                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
207                         rec_rect->property_x1() = xstart;
208                         rec_rect->property_y1() = 1.0;
209                         rec_rect->property_x2() = xend;
210                         rec_rect->property_y2() = (double) _trackview.height - 1;
211                         rec_rect->property_outline_color_rgba() = color_map[cRecordingRectOutline];
212                         rec_rect->property_fill_color_rgba() = fill_color;
213                         
214                         RecBoxInfo recbox;
215                         recbox.rectangle = rec_rect;
216                         recbox.start = _trackview.session().transport_frame();
217                         recbox.length = 0;
218                         
219                         rec_rects.push_back (recbox);
220                         
221                         screen_update_connection.disconnect();
222                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &MidiStreamView::update_rec_box));    
223                         rec_updating = true;
224                         rec_active = true;
225
226                 } else if (rec_active &&
227                            (_trackview.session().record_status() != Session::Recording ||
228                             !_trackview.get_diskstream()->record_enabled())) {
229
230                         screen_update_connection.disconnect();
231                         rec_active = false;
232                         rec_updating = false;
233
234                 }
235                 
236         } else {
237
238                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
239
240                 if (!rec_rects.empty() || !rec_regions.empty()) {
241
242                         /* disconnect rapid update */
243                         screen_update_connection.disconnect();
244
245                         for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
246                                 (*prc).disconnect();
247                         }
248                         rec_data_ready_connections.clear();
249
250                         rec_updating = false;
251                         rec_active = false;
252                         last_rec_data_frame = 0;
253                         
254                         /* remove temp regions */
255                         rec_regions.clear();
256
257                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
258
259                         /* transport stopped, clear boxes */
260                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
261                                 RecBoxInfo &rect = (*iter);
262                                 delete rect.rectangle;
263                         }
264                         
265                         rec_rects.clear();
266                         
267                 }
268         }
269 }
270
271 void
272 MidiStreamView::update_rec_regions ()
273 {
274         if (use_rec_regions) {
275
276
277                 uint32_t n = 0;
278
279                 for (list<boost::shared_ptr<Region> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
280
281                         list<boost::shared_ptr<Region> >::iterator tmp;
282
283                         tmp = iter;
284                         ++tmp;
285
286                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
287                                 /* rect already hidden, this region is done */
288                                 iter = tmp;
289                                 continue;
290                         }
291                         
292                         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(*iter);
293                         assert(region);
294
295                         jack_nframes_t origlen = region->length();
296
297                         if (region == rec_regions.back() && rec_active) {
298
299                                 if (last_rec_data_frame > region->start()) {
300
301                                         jack_nframes_t nlen = last_rec_data_frame - region->start();
302
303                                         if (nlen != region->length()) {
304
305                                                 region->freeze ();
306                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
307                                                 region->set_length (nlen, this);
308                                                 region->thaw ("updated");
309
310                                                 if (origlen == 1) {
311                                                         /* our special initial length */
312                                                         add_region_view_internal (region, false);
313                                                 }
314
315                                                 /* also update rect */
316                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
317                                                 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
318                                                 rect->property_x2() = xend;
319                                         }
320                                 }
321
322                         } else {
323
324                                 jack_nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
325
326                                 if (nlen != region->length()) {
327
328                                         if (region->source(0)->length() >= region->start() + nlen) {
329
330                                                 region->freeze ();
331                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
332                                                 region->set_length (nlen, this);
333                                                 region->thaw ("updated");
334                                                 
335                                                 if (origlen == 1) {
336                                                         /* our special initial length */
337                                                         add_region_view_internal (region, false);
338                                                 }
339                                                 
340                                                 /* also hide rect */
341                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
342                                                 rect->hide();
343
344                                         }
345                                 }
346                         }
347
348                         iter = tmp;
349                 }
350         }
351 }
352
353 void
354 MidiStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t cnt, boost::shared_ptr<Source> src)
355 {
356         // this is called from the butler thread for now
357         // yeah we need a "peak" building thread or something, though there's not really any
358         // work for it to do...  whatever. :)
359         
360         ENSURE_GUI_THREAD(bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), start, cnt, src));
361         
362         //cerr << "REC DATA: " << start << " --- " << cnt << endl;
363
364         if (rec_data_ready_map.size() == 0 || start+cnt > last_rec_data_frame) {
365                 last_rec_data_frame = start + cnt;
366         }
367
368         rec_data_ready_map[src] = true;
369
370         if (rec_data_ready_map.size() == _trackview.get_diskstream()->n_channels().get(DataType::MIDI)) {
371                 this->update_rec_regions ();
372                 rec_data_ready_map.clear();
373         }
374 }
375
376 void
377 MidiStreamView::color_handler (ColorID id, uint32_t val)
378 {
379         switch (id) {
380         case cMidiTrackBase:
381                 if (_trackview.is_midi_track()) {
382                         canvas_rect->property_fill_color_rgba() = val;
383                 } 
384                 break;
385         case cMidiBusBase:
386                 if (!_trackview.is_midi_track()) {
387                         canvas_rect->property_fill_color_rgba() = val;
388                 }
389                 break;
390         case cMidiTrackOutline:
391                 canvas_rect->property_outline_color_rgba() = val;
392                 break;
393
394         default:
395                 break;
396         }
397 }