2 Copyright (C) 2001-2006 Paul Davis
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.
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.
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.
25 #include <gtkmm2ext/gtk_ui.h>
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>
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"
43 #include "rgb_macros.h"
44 #include "gui_thread.h"
49 using namespace ARDOUR;
51 using namespace Editing;
52 using namespace ArdourCanvas;
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)
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)
69 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
71 // FIXME: Some redundancy here with RegionView::init. Need to figure out
72 // where order is important and where it isn't...
75 RegionView::init(basic_color, /*wfd*/false);
77 compute_colors (basic_color);
79 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
81 set_y_position_and_height (0, trackview.height);
84 region_resized (BoundsChanged);
87 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
92 midi_region()->midi_source(0)->load_model();
99 MidiRegionView::display_events()
101 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
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));
114 MidiRegionView::~MidiRegionView ()
116 in_destructor = true;
119 RegionViewGoingAway (this); /* EMIT_SIGNAL */
122 boost::shared_ptr<ARDOUR::MidiRegion>
123 MidiRegionView::midi_region() const
125 // "Guaranteed" to succeed...
126 return boost::dynamic_pointer_cast<MidiRegion>(_region);
130 MidiRegionView::region_resized (Change what_changed)
132 RegionView::region_resized(what_changed);
134 if (what_changed & ARDOUR::PositionChanged) {
138 } else if (what_changed & Change (StartChanged)) {
140 //cerr << "MIDI RV START CHANGED" << endl;
142 } else if (what_changed & Change (LengthChanged)) {
144 //cerr << "MIDI RV LENGTH CHANGED" << endl;
150 MidiRegionView::reset_width_dependent_items (double pixel_width)
152 RegionView::reset_width_dependent_items(pixel_width);
153 assert(_pixel_width == pixel_width);
159 MidiRegionView::set_y_position_and_height (double y, double h)
161 RegionView::set_y_position_and_height(y, h - 1);
166 name_text->raise_to_top();
171 MidiRegionView::show_region_editor ()
173 cerr << "No MIDI region editor." << endl;
177 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
179 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
182 double unit_position = _region->position () / samples_per_unit;
183 GhostRegion* ghost = new GhostRegion (atv, unit_position);
185 cerr << "FIXME: add notes to MIDI region ghost." << endl;
187 ghost->set_height ();
188 ghost->set_duration (_region->length() / samples_per_unit);
189 ghosts.push_back (ghost);
191 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
197 /** Begin tracking note state for successive calls to add_event
200 MidiRegionView::begin_write()
202 _active_notes = new ArdourCanvas::SimpleRect*[128];
203 for (unsigned i=0; i < 128; ++i)
204 _active_notes[i] = NULL;
208 /** Destroy note state for add_event
211 MidiRegionView::end_write()
213 delete[] _active_notes;
214 _active_notes = NULL;
219 MidiRegionView::add_event (const MidiEvent& ev)
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]);
227 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
228 MidiStreamView* const view = mtv->midi_view();
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;
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;
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 (
243 ev_rect->property_y1() = y1;
244 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
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;
252 _events.push_back(ev_rect);
254 _active_notes[note] = ev_rect;
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;
268 /** Extend active notes to rightmost edge of region (if length is changed)
271 MidiRegionView::extend_active_notes()
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());