set powermate env keys in SConstruct, so that POWERMATE is set even if SURFACES=false
[ardour.git] / gtk2_ardour / midi_region_view.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 #include <algorithm>
22
23 #include <gtkmm.h>
24
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include <ardour/playlist.h>
28 #include <ardour/midi_region.h>
29 #include <ardour/midi_source.h>
30 #include <ardour/midi_diskstream.h>
31 #include <ardour/midi_events.h>
32
33 #include "streamview.h"
34 #include "midi_region_view.h"
35 #include "midi_time_axis.h"
36 #include "simplerect.h"
37 #include "simpleline.h"
38 #include "public_editor.h"
39 #include "ghostregion.h"
40 #include "midi_time_axis.h"
41 #include "utils.h"
42 #include "rgb_macros.h"
43 #include "gui_thread.h"
44
45 #include "i18n.h"
46
47 using namespace sigc;
48 using namespace ARDOUR;
49 using namespace PBD;
50 using namespace Editing;
51 using namespace ArdourCanvas;
52
53 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
54                                   Gdk::Color& basic_color)
55         : RegionView (parent, tv, r, spu, basic_color)
56         , _active_notes(0)
57 {
58 }
59
60 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, 
61                                   Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
62         : RegionView (parent, tv, r, spu, basic_color, visibility)
63         , _active_notes(0)
64 {
65 }
66
67 void
68 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
69 {
70         // FIXME: Some redundancy here with RegionView::init.  Need to figure out
71         // where order is important and where it isn't...
72         
73         // FIXME
74         RegionView::init(basic_color, /*wfd*/false);
75
76         compute_colors (basic_color);
77
78         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
79
80         set_y_position_and_height (0, trackview.height);
81
82         region_muted ();
83         region_resized (BoundsChanged);
84         region_locked ();
85
86         _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
87
88         set_colors ();
89
90         if (wfd) {
91                 midi_region()->midi_source(0)->load_model();
92                 display_events();
93         }
94 }
95
96
97 void
98 MidiRegionView::display_events()
99 {
100         for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
101                 delete *i;
102         
103         _events.clear();
104         begin_write();
105
106         for (size_t i=0; i < midi_region()->midi_source(0)->model()->n_events(); ++i)
107                 add_event(midi_region()->midi_source(0)->model()->event_at(i));
108
109         end_write();
110 }
111
112
113 MidiRegionView::~MidiRegionView ()
114 {
115         in_destructor = true;
116         end_write();
117
118         RegionViewGoingAway (this); /* EMIT_SIGNAL */
119 }
120
121 boost::shared_ptr<ARDOUR::MidiRegion>
122 MidiRegionView::midi_region() const
123 {
124         // "Guaranteed" to succeed...
125         return boost::dynamic_pointer_cast<MidiRegion>(_region);
126 }
127
128 void
129 MidiRegionView::region_resized (Change what_changed)
130 {
131         RegionView::region_resized(what_changed);
132
133         if (what_changed & ARDOUR::PositionChanged) {
134         
135                 display_events();
136         
137         } else if (what_changed & Change (StartChanged)) {
138
139                 //cerr << "MIDI RV START CHANGED" << endl;
140
141         } else if (what_changed & Change (LengthChanged)) {
142                 
143                 //cerr << "MIDI RV LENGTH CHANGED" << endl;
144         
145         }
146 }
147
148 void
149 MidiRegionView::reset_width_dependent_items (double pixel_width)
150 {
151         RegionView::reset_width_dependent_items(pixel_width);
152         assert(_pixel_width == pixel_width);
153                 
154         display_events();
155 }
156
157 void
158 MidiRegionView::set_y_position_and_height (double y, double h)
159 {
160         RegionView::set_y_position_and_height(y, h - 1);
161                 
162         display_events();
163
164         if (name_text) {
165                 name_text->raise_to_top();
166         }
167 }
168
169 void
170 MidiRegionView::show_region_editor ()
171 {
172         cerr << "No MIDI region editor." << endl;
173 }
174
175 GhostRegion*
176 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
177 {
178         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
179         assert(rtv);
180
181         double unit_position = _region->position () / samples_per_unit;
182         GhostRegion* ghost = new GhostRegion (atv, unit_position);
183
184         cerr << "FIXME: add notes to MIDI region ghost." << endl;
185
186         ghost->set_height ();
187         ghost->set_duration (_region->length() / samples_per_unit);
188         ghosts.push_back (ghost);
189
190         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
191
192         return ghost;
193 }
194
195
196 /** Begin tracking note state for successive calls to add_event
197  */
198 void
199 MidiRegionView::begin_write()
200 {
201         _active_notes = new ArdourCanvas::SimpleRect*[128];
202         for (unsigned i=0; i < 128; ++i)
203                 _active_notes[i] = NULL;
204 }
205
206
207 /** Destroy note state for add_event
208  */
209 void
210 MidiRegionView::end_write()
211 {
212         delete[] _active_notes;
213         _active_notes = NULL;
214 }
215
216
217 void
218 MidiRegionView::add_event (const MidiEvent& ev)
219 {
220         /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
221         for (size_t i=0; i < ev.size; ++i) {
222                 printf("%X ", ev.buffer[i]);
223         }
224         printf("\n\n");*/
225
226         double y1 = trackview.height / 2.0;
227         if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
228                 const Byte& note = ev.buffer[1];
229                 y1 = trackview.height - ((trackview.height / 127.0) * note);
230
231                 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(
232                                 *(ArdourCanvas::Group*)get_canvas_group());
233                 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
234                                 (nframes_t)ev.time);
235                 ev_rect->property_y1() = y1;
236                 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
237                                 _region->length());
238                 ev_rect->property_y2() = y1 + ceil(trackview.height / 127.0);
239                 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
240                 /* outline all but right edge */
241                 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
242                 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
243
244                 _events.push_back(ev_rect);
245                 if (_active_notes)
246                         _active_notes[note] = ev_rect;
247
248         } else if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
249                 const Byte& note = ev.buffer[1];
250                 if (_active_notes && _active_notes[note]) {
251                         _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time);
252                         _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
253                         _active_notes[note] = NULL;
254                 }
255         }
256
257 }
258
259
260 /** Extend active notes to rightmost edge of region (if length is changed)
261  */
262 void
263 MidiRegionView::extend_active_notes()
264 {
265         if (!_active_notes)
266                 return;
267
268         for (unsigned i=0; i < 128; ++i)
269                 if (_active_notes[i])
270                         _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
271 }
272
273
274