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"
40 #include "public_editor.h"
41 #include "ghostregion.h"
42 #include "midi_time_axis.h"
44 #include "rgb_macros.h"
45 #include "gui_thread.h"
50 using namespace ARDOUR;
52 using namespace Editing;
53 using namespace ArdourCanvas;
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)
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)
70 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
72 // FIXME: Some redundancy here with RegionView::init. Need to figure out
73 // where order is important and where it isn't...
76 RegionView::init(basic_color, /*wfd*/false);
78 compute_colors (basic_color);
80 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
82 set_y_position_and_height (0, trackview.height);
85 region_resized (BoundsChanged);
88 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
93 midi_region()->midi_source(0)->load_model();
100 MidiRegionView::clear_events()
102 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
110 MidiRegionView::display_events()
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));
123 MidiRegionView::~MidiRegionView ()
125 in_destructor = true;
128 RegionViewGoingAway (this); /* EMIT_SIGNAL */
131 boost::shared_ptr<ARDOUR::MidiRegion>
132 MidiRegionView::midi_region() const
134 // "Guaranteed" to succeed...
135 return boost::dynamic_pointer_cast<MidiRegion>(_region);
139 MidiRegionView::region_resized (Change what_changed)
141 RegionView::region_resized(what_changed);
143 if (what_changed & ARDOUR::PositionChanged) {
147 } else if (what_changed & Change (StartChanged)) {
149 //cerr << "MIDI RV START CHANGED" << endl;
151 } else if (what_changed & Change (LengthChanged)) {
153 //cerr << "MIDI RV LENGTH CHANGED" << endl;
159 MidiRegionView::reset_width_dependent_items (double pixel_width)
161 RegionView::reset_width_dependent_items(pixel_width);
162 assert(_pixel_width == pixel_width);
168 MidiRegionView::set_y_position_and_height (double y, double h)
170 RegionView::set_y_position_and_height(y, h - 1);
175 name_text->raise_to_top();
180 MidiRegionView::show_region_editor ()
182 cerr << "No MIDI region editor." << endl;
186 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
188 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
191 double unit_position = _region->position () / samples_per_unit;
192 GhostRegion* ghost = new GhostRegion (atv, unit_position);
194 ghost->set_height ();
195 ghost->set_duration (_region->length() / samples_per_unit);
196 ghosts.push_back (ghost);
198 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
204 /** Begin tracking note state for successive calls to add_event
207 MidiRegionView::begin_write()
209 _active_notes = new ArdourCanvas::SimpleRect*[128];
210 for (unsigned i=0; i < 128; ++i)
211 _active_notes[i] = NULL;
215 /** Destroy note state for add_event
218 MidiRegionView::end_write()
220 delete[] _active_notes;
221 _active_notes = NULL;
226 MidiRegionView::add_event (const MidiEvent& ev)
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]);
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();
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;
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;
248 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
249 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
251 ev_rect->property_y1() = y1;
252 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
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;
260 _events.push_back(ev_rect);
262 _active_notes[note] = ev_rect;
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;
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;
279 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
280 ev_diamond->move(x, y);
282 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
283 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
284 _events.push_back(ev_diamond);
289 /** Extend active notes to rightmost edge of region (if length is changed)
292 MidiRegionView::extend_active_notes()
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());