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_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"
42 #include "rgb_macros.h"
43 #include "gui_thread.h"
48 using namespace ARDOUR;
50 using namespace Editing;
51 using namespace ArdourCanvas;
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)
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)
68 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
70 // FIXME: Some redundancy here with RegionView::init. Need to figure out
71 // where order is important and where it isn't...
74 RegionView::init(basic_color, /*wfd*/false);
76 compute_colors (basic_color);
78 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
80 set_y_position_and_height (0, trackview.height);
83 region_resized (BoundsChanged);
86 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
90 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion> (_region);
91 mr->midi_source(0)->load_model();
94 for (size_t i=0; i < mr->midi_source(0)->model().n_events(); ++i)
95 add_event(mr->midi_source(0)->model().event_at(i));
99 MidiRegionView::~MidiRegionView ()
101 in_destructor = true;
104 RegionViewGoingAway (this); /* EMIT_SIGNAL */
107 boost::shared_ptr<ARDOUR::MidiRegion>
108 MidiRegionView::midi_region() const
110 // "Guaranteed" to succeed...
111 return boost::dynamic_pointer_cast<MidiRegion>(_region);
115 MidiRegionView::show_region_editor ()
117 cerr << "No MIDI region editor." << endl;
121 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
128 /** Begin tracking note state for successive calls to add_event
131 MidiRegionView::begin_write()
133 _active_notes = new ArdourCanvas::SimpleRect*[128];
134 for (unsigned i=0; i < 128; ++i)
135 _active_notes[i] = NULL;
139 /** Destroy note state for add_event
142 MidiRegionView::end_write()
144 delete[] _active_notes;
145 _active_notes = NULL;
150 MidiRegionView::add_event (const MidiEvent& ev)
152 /*printf("Event, time = %u, size = %zu, data = ",
154 for (size_t i=0; i < ev.size; ++i) {
155 printf("%X ", ev.buffer[i]);
158 double y1 = trackview.height / 2.0;
159 if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
160 const Byte& note = ev.buffer[1];
161 y1 = trackview.height - ((trackview.height / 127.0) * note);
163 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(
164 *(ArdourCanvas::Group*)get_canvas_group());
165 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
167 ev_rect->property_y1() = y1;
168 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
170 ev_rect->property_y2() = y1 + ceil(trackview.height / 127.0);
171 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
172 /* outline all but right edge */
173 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
174 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
176 _events.push_back(ev_rect);
178 _active_notes[note] = ev_rect;
180 } else if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
181 const Byte& note = ev.buffer[1];
182 if (_active_notes && _active_notes[note]) {
183 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel(ev.time);
184 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
185 _active_notes[note] = NULL;
192 /** Extend active notes to rightmost edge of region (if length is changed)
195 MidiRegionView::extend_active_notes()
200 for (unsigned i=0; i < 128; ++i)
201 if (_active_notes[i])
202 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());