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