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