2 Copyright (C) 2001-2007 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <gtkmm2ext/gtk_ui.h>
28 #include <sigc++/signal.h>
30 #include <ardour/playlist.h>
31 #include <ardour/tempo.h>
32 #include <ardour/midi_region.h>
33 #include <ardour/midi_source.h>
34 #include <ardour/midi_diskstream.h>
35 #include <ardour/midi_events.h>
36 #include <ardour/midi_model.h>
38 #include "streamview.h"
39 #include "midi_region_view.h"
40 #include "midi_streamview.h"
41 #include "midi_time_axis.h"
42 #include "simplerect.h"
43 #include "simpleline.h"
45 #include "public_editor.h"
46 #include "ghostregion.h"
47 #include "midi_time_axis.h"
49 #include "rgb_macros.h"
50 #include "gui_thread.h"
55 using namespace ARDOUR;
57 using namespace Editing;
58 using namespace ArdourCanvas;
60 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
61 Gdk::Color& basic_color)
62 : RegionView (parent, tv, r, spu, basic_color)
67 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
68 Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
69 : RegionView (parent, tv, r, spu, basic_color, visibility)
75 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
77 // FIXME: Some redundancy here with RegionView::init. Need to figure out
78 // where order is important and where it isn't...
81 RegionView::init(basic_color, /*wfd*/false);
83 compute_colors (basic_color);
85 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
87 set_y_position_and_height (0, trackview.height);
90 region_resized (BoundsChanged);
93 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
98 midi_region()->midi_source(0)->load_model();
102 midi_region()->midi_source(0)->model()->ContentsChanged.connect(sigc::mem_fun(
103 this, &MidiRegionView::redisplay_model));
105 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event));
109 MidiRegionView::canvas_event(GdkEvent* ev)
111 if (trackview.editor.current_mouse_mode() == MouseNote) {
112 if (ev->type == GDK_BUTTON_PRESS) {
113 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
114 MidiStreamView* const view = mtv->midi_view();
116 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
117 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
118 const double roll_height = trackview.height - footer_height;
120 double x = ev->button.x;
121 double y = ev->button.y;
122 get_canvas_group()->w2i(x, y);
124 double note = floor((roll_height - y) / roll_height * (double)note_range) + view->lowest_note();
126 assert(note <= 127.0);
128 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
130 //assert(stamp <= _region->length());
132 const Meter& m = trackview.session().tempo_map().meter_at(stamp);
133 const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
134 double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
136 MidiModel* model = midi_region()->midi_source(0)->model();
138 // Add a 1 beat long note (for now)
139 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
141 model->begin_command();
142 model->add_note(new_note);
143 model->finish_command();
145 view->update_bounds(new_note.note);
156 MidiRegionView::redisplay_model()
164 MidiRegionView::clear_events()
166 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
174 MidiRegionView::display_events()
180 for (size_t i=0; i < midi_region()->midi_source(0)->model()->n_notes(); ++i)
181 add_note(midi_region()->midi_source(0)->model()->note_at(i));
187 MidiRegionView::~MidiRegionView ()
189 in_destructor = true;
192 RegionViewGoingAway (this); /* EMIT_SIGNAL */
195 boost::shared_ptr<ARDOUR::MidiRegion>
196 MidiRegionView::midi_region() const
198 // "Guaranteed" to succeed...
199 return boost::dynamic_pointer_cast<MidiRegion>(_region);
203 MidiRegionView::region_resized (Change what_changed)
205 RegionView::region_resized(what_changed);
207 if (what_changed & ARDOUR::PositionChanged) {
211 } else if (what_changed & Change (StartChanged)) {
213 //cerr << "MIDI RV START CHANGED" << endl;
215 } else if (what_changed & Change (LengthChanged)) {
217 //cerr << "MIDI RV LENGTH CHANGED" << endl;
223 MidiRegionView::reset_width_dependent_items (double pixel_width)
225 RegionView::reset_width_dependent_items(pixel_width);
226 assert(_pixel_width == pixel_width);
232 MidiRegionView::set_y_position_and_height (double y, double h)
234 RegionView::set_y_position_and_height(y, h - 1);
239 name_text->raise_to_top();
244 MidiRegionView::show_region_editor ()
246 cerr << "No MIDI region editor." << endl;
250 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
252 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
255 double unit_position = _region->position () / samples_per_unit;
256 GhostRegion* ghost = new GhostRegion (atv, unit_position);
258 ghost->set_height ();
259 ghost->set_duration (_region->length() / samples_per_unit);
260 ghosts.push_back (ghost);
262 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
268 /** Begin tracking note state for successive calls to add_event
271 MidiRegionView::begin_write()
273 _active_notes = new ArdourCanvas::SimpleRect*[128];
274 for (unsigned i=0; i < 128; ++i)
275 _active_notes[i] = NULL;
279 /** Destroy note state for add_event
282 MidiRegionView::end_write()
284 delete[] _active_notes;
285 _active_notes = NULL;
289 /** Add a MIDI event.
291 * This is used while recording, and handles displaying still-unresolved notes.
292 * Displaying an existing model is simpler, and done with add_note.
295 MidiRegionView::add_event (const MidiEvent& ev)
297 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
298 for (size_t i=0; i < ev.size; ++i) {
299 printf("%X ", ev.buffer[i]);
303 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
304 MidiStreamView* const view = mtv->midi_view();
305 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
307 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
308 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
309 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
311 if (mtv->note_mode() == Note) {
312 if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
313 const Byte& note = ev.buffer[1];
314 const double y1 = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
315 - footer_height - 3.0;
317 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
318 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
320 ev_rect->property_y1() = y1;
321 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
323 ev_rect->property_y2() = y1 + ceil(pixel_range);
324 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
325 /* outline all but right edge */
326 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
327 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
329 _events.push_back(ev_rect);
331 _active_notes[note] = ev_rect;
333 } else if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
334 const Byte& note = ev.buffer[1];
335 if (_active_notes && _active_notes[note]) {
336 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time);
337 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
338 _active_notes[note] = NULL;
342 } else if (mtv->note_mode() == Percussion) {
343 const Byte& note = ev.buffer[1];
344 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time);
345 const double y = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
346 - footer_height - 3.0;
348 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
349 ev_diamond->move(x, y);
351 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
352 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
353 _events.push_back(ev_diamond);
358 /** Extend active notes to rightmost edge of region (if length is changed)
361 MidiRegionView::extend_active_notes()
366 for (unsigned i=0; i < 128; ++i)
367 if (_active_notes[i])
368 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
372 /** Add a MIDI note (with duration).
374 * This does no 'realtime' note resolution, notes from a MidiModel have a
375 * duration so they can be drawn in full immediately.
378 MidiRegionView::add_note (const MidiModel::Note& note)
380 assert(note.start >= 0);
381 assert(note.start < _region->length());
382 //assert(note.start + note.duration < _region->length());
384 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
385 for (size_t i=0; i < ev.size; ++i) {
386 printf("%X ", ev.buffer[i]);
390 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
391 MidiStreamView* const view = mtv->midi_view();
392 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
394 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
395 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
396 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
398 if (mtv->note_mode() == Note) {
399 const double y1 = trackview.height - (pixel_range * (note.note - view->lowest_note() + 1))
400 - footer_height - 3.0;
402 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
403 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.start);
404 ev_rect->property_y1() = y1;
405 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.start + note.duration));
406 ev_rect->property_y2() = y1 + ceil(pixel_range);
408 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
409 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
410 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
413 _events.push_back(ev_rect);
415 } else if (mtv->note_mode() == Percussion) {
416 const double x = trackview.editor.frame_to_pixel((nframes_t)note.start);
417 const double y = trackview.height - (pixel_range * (note.note - view->lowest_note() + 1))
418 - footer_height - 3.0;
420 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
421 ev_diamond->move(x, y);
423 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
424 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
425 _events.push_back(ev_diamond);