Recording to SMF. Playback not quite working yet, just some buglets left to iron...
[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/playlist_templates.h>
32 #include <ardour/midi_source.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 (Region *r, bool wait_for_waves)
74 {
75         ENSURE_GUI_THREAD (bind (mem_fun (*this, &MidiStreamView::add_region_view), r));
76
77         MidiRegion* region = dynamic_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 (mem_fun (*this, &MidiStreamView::remove_region_view));
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                                 MidiDiskstream* mds = dynamic_cast<MidiDiskstream*>(_trackview.get_diskstream());
167                                 assert(mds);
168
169                                 sources.push_back((Source*)mds->write_source());
170                                 
171                                 rec_data_ready_connections.push_back (mds->write_source()->ViewDataRangeReady.connect (bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), mds->write_source()))); 
172
173                                 // handle multi
174                                 
175                                 jack_nframes_t start = 0;
176                                 if (rec_regions.size() > 0) {
177                                         start = rec_regions.back()->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
178                                 }
179                                 
180                                 MidiRegion * region = new MidiRegion(sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false);
181                                 region->set_position (_trackview.session().transport_frame(), this);
182                                 rec_regions.push_back (region);
183                                 /* catch it if it goes away */
184                                 region->GoingAway.connect (mem_fun (*this, &MidiStreamView::remove_rec_region));
185
186                                 /* we add the region later */
187                         }
188                         
189                         /* start a new rec box */
190
191                         MidiTrack* mt = _trackview.midi_track(); /* we know what it is already */
192                         MidiDiskstream& ds = mt->midi_diskstream();
193                         jack_nframes_t frame_pos = ds.current_capture_start ();
194                         gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
195                         gdouble xend;
196                         uint32_t fill_color;
197
198                         assert(_trackview.midi_track()->mode() == Normal);
199                         
200                         xend = xstart;
201                         fill_color = color_map[cRecordingRectFill];
202                         
203                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
204                         rec_rect->property_x1() = xstart;
205                         rec_rect->property_y1() = 1.0;
206                         rec_rect->property_x2() = xend;
207                         rec_rect->property_y2() = (double) _trackview.height - 1;
208                         rec_rect->property_outline_color_rgba() = color_map[cRecordingRectOutline];
209                         rec_rect->property_fill_color_rgba() = fill_color;
210                         
211                         RecBoxInfo recbox;
212                         recbox.rectangle = rec_rect;
213                         recbox.start = _trackview.session().transport_frame();
214                         recbox.length = 0;
215                         
216                         rec_rects.push_back (recbox);
217                         
218                         screen_update_connection.disconnect();
219                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &MidiStreamView::update_rec_box));    
220                         rec_updating = true;
221                         rec_active = true;
222
223                 } else if (rec_active &&
224                            (_trackview.session().record_status() != Session::Recording ||
225                             !_trackview.get_diskstream()->record_enabled())) {
226
227                         screen_update_connection.disconnect();
228                         rec_active = false;
229                         rec_updating = false;
230
231                 }
232                 
233         } else {
234
235                 cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
236
237                 if (!rec_rects.empty() || !rec_regions.empty()) {
238
239                         /* disconnect rapid update */
240                         screen_update_connection.disconnect();
241
242                         for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
243                                 (*prc).disconnect();
244                         }
245                         rec_data_ready_connections.clear();
246
247                         rec_updating = false;
248                         rec_active = false;
249                         last_rec_data_frame = 0;
250                         
251                         /* remove temp regions */
252                         for (list<Region*>::iterator iter=rec_regions.begin(); iter != rec_regions.end(); )
253                         {
254                                 list<Region*>::iterator tmp;
255
256                                 tmp = iter;
257                                 ++tmp;
258
259                                 /* this will trigger the remove_region_view */
260                                 delete *iter;
261
262                                 iter = tmp;
263                         }
264                         
265                         rec_regions.clear();
266
267                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
268                 
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<Region*>::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
291
292                         list<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                         // FIXME?
304                         MidiRegion * region = dynamic_cast<MidiRegion*>(*iter);
305                         assert(region);
306
307                         jack_nframes_t origlen = region->length();
308
309                         if (region == rec_regions.back() && rec_active) {
310
311                                 if (last_rec_data_frame > region->start()) {
312
313                                         jack_nframes_t nlen = last_rec_data_frame - region->start();
314
315                                         if (nlen != region->length()) {
316
317                                                 region->freeze ();
318                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
319                                                 region->set_length (nlen, this);
320                                                 region->thaw ("updated");
321
322                                                 if (origlen == 1) {
323                                                         /* our special initial length */
324                                                         add_region_view_internal (region, false);
325                                                 }
326
327                                                 /* also update rect */
328                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
329                                                 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
330                                                 rect->property_x2() = xend;
331                                         }
332                                 }
333
334                         } else {
335
336                                 jack_nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
337
338                                 if (nlen != region->length()) {
339
340                                         if (region->source(0).length() >= region->start() + nlen) {
341
342                                                 region->freeze ();
343                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
344                                                 region->set_length (nlen, this);
345                                                 region->thaw ("updated");
346                                                 
347                                                 if (origlen == 1) {
348                                                         /* our special initial length */
349                                                         add_region_view_internal (region, false);
350                                                 }
351                                                 
352                                                 /* also hide rect */
353                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
354                                                 rect->hide();
355
356                                         }
357                                 }
358                         }
359
360                         iter = tmp;
361                 }
362         }
363 }
364
365 void
366 MidiStreamView::rec_data_range_ready (jack_nframes_t start, jack_nframes_t cnt, Source * src)
367 {
368         // this is called from the butler thread for now
369         // yeah we need a "peak" building thread or something, though there's not really any
370         // work for it to do...  whatever. :)
371         
372         ENSURE_GUI_THREAD(bind (mem_fun (*this, &MidiStreamView::rec_data_range_ready), start, cnt, src));
373         
374         //cerr << "REC DATA: " << start << " --- " << cnt << endl;
375
376         if (rec_data_ready_map.size() == 0 || start+cnt > last_rec_data_frame) {
377                 last_rec_data_frame = start + cnt;
378         }
379
380         rec_data_ready_map[src] = true;
381
382         if (rec_data_ready_map.size() == _trackview.get_diskstream()->n_channels().get(DataType::MIDI)) {
383                 this->update_rec_regions ();
384                 rec_data_ready_map.clear();
385         }
386 }
387
388 void
389 MidiStreamView::color_handler (ColorID id, uint32_t val)
390 {
391         switch (id) {
392         case cMidiTrackBase:
393                 if (_trackview.is_midi_track()) {
394                         canvas_rect->property_fill_color_rgba() = val;
395                 } 
396                 break;
397         case cMidiBusBase:
398                 if (!_trackview.is_midi_track()) {
399                         canvas_rect->property_fill_color_rgba() = val;
400                 }
401                 break;
402         case cMidiTrackOutline:
403                 canvas_rect->property_outline_color_rgba() = val;
404                 break;
405
406         default:
407                 break;
408         }
409 }