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"
44 #include "canvas-hit.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 _model = midi_region()->midi_source(0)->model();
78 _enable_display = false;
80 RegionView::init(basic_color, false);
82 compute_colors (basic_color);
84 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
86 set_y_position_and_height (0, trackview.height);
89 region_resized (BoundsChanged);
92 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
96 _enable_display = true;
102 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
105 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
110 MidiRegionView::canvas_event(GdkEvent* ev)
112 enum State { None, Pressed, Dragging };
113 static int press_button = 0;
116 static double last_x, last_y;
117 double event_x, event_y;
119 static ArdourCanvas::SimpleRect* select_rect = NULL;
121 if (trackview.editor.current_mouse_mode() != MouseNote)
125 case GDK_BUTTON_PRESS:
126 //group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, ev->button.time);
128 press_button = ev->button.button;
129 cerr << "PRESSED: " << press_button << endl;
132 case GDK_MOTION_NOTIFY:
133 event_x = ev->motion.x;
134 event_y = ev->motion.y;
135 group->w2i(event_x, event_y);
138 case Pressed: // Drag start
139 cerr << "PRESSED MOTION: " << press_button << endl;
140 if (press_button == 1) { // Select rect start
141 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
142 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
147 select_rect = new ArdourCanvas::SimpleRect(*group);
148 select_rect->property_x1() = event_x;
149 select_rect->property_y1() = event_y;
150 select_rect->property_x2() = event_x;
151 select_rect->property_y2() = event_y;
152 select_rect->property_outline_color_rgba() = 0xFF000099;
153 select_rect->property_fill_color_rgba() = 0xFFDDDD33;
160 case Dragging: // Select rect motion
161 if (ev->motion.is_hint) {
164 GdkModifierType state;
165 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
171 select_rect->property_x2() = event_x;
172 select_rect->property_y2() = event_y;
185 case GDK_BUTTON_RELEASE:
186 group->ungrab(ev->button.time);
188 case Pressed: // Clicked
189 if (ev->button.button == 1)
190 create_note_at(ev->button.x, ev->button.y);
193 case Dragging: // Select rect done
210 /** Add a note to the model, and the view, at a canvas (click) coordinate */
212 MidiRegionView::create_note_at(double x, double y)
214 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
215 MidiStreamView* const view = mtv->midi_view();
217 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
218 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
219 const double roll_height = trackview.height - footer_height;
221 get_canvas_group()->w2i(x, y);
223 double note = floor((roll_height - y) / roll_height * (double)note_range) + view->lowest_note();
225 assert(note <= 127.0);
227 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
229 //assert(stamp <= _region->length());
231 const Meter& m = trackview.session().tempo_map().meter_at(stamp);
232 const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
233 double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
235 // Add a 1 beat long note (for now)
236 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
238 view->update_bounds(new_note.note());
240 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
242 _model->apply_command(cmd);
244 //add_note(new_note);
249 MidiRegionView::clear_events()
251 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
259 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
269 MidiRegionView::redisplay_model()
276 for (size_t i=0; i < _model->n_notes(); ++i)
277 add_note(_model->note_at(i));
281 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
286 MidiRegionView::~MidiRegionView ()
288 in_destructor = true;
291 RegionViewGoingAway (this); /* EMIT_SIGNAL */
294 boost::shared_ptr<ARDOUR::MidiRegion>
295 MidiRegionView::midi_region() const
297 // "Guaranteed" to succeed...
298 return boost::dynamic_pointer_cast<MidiRegion>(_region);
302 MidiRegionView::region_resized (Change what_changed)
304 RegionView::region_resized(what_changed);
306 if (what_changed & ARDOUR::PositionChanged) {
311 } else if (what_changed & Change (StartChanged)) {
313 //cerr << "MIDI RV START CHANGED" << endl;
315 } else if (what_changed & Change (LengthChanged)) {
317 //cerr << "MIDI RV LENGTH CHANGED" << endl;
323 MidiRegionView::reset_width_dependent_items (double pixel_width)
325 RegionView::reset_width_dependent_items(pixel_width);
326 assert(_pixel_width == pixel_width);
333 MidiRegionView::set_y_position_and_height (double y, double h)
335 RegionView::set_y_position_and_height(y, h - 1);
341 name_text->raise_to_top();
346 MidiRegionView::show_region_editor ()
348 cerr << "No MIDI region editor." << endl;
352 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
354 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
357 double unit_position = _region->position () / samples_per_unit;
358 GhostRegion* ghost = new GhostRegion (atv, unit_position);
360 ghost->set_height ();
361 ghost->set_duration (_region->length() / samples_per_unit);
362 ghosts.push_back (ghost);
364 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
370 /** Begin tracking note state for successive calls to add_event
373 MidiRegionView::begin_write()
375 _active_notes = new CanvasNote*[128];
376 for (unsigned i=0; i < 128; ++i)
377 _active_notes[i] = NULL;
381 /** Destroy note state for add_event
384 MidiRegionView::end_write()
386 delete[] _active_notes;
387 _active_notes = NULL;
391 /** Add a MIDI event.
393 * This is used while recording, and handles displaying still-unresolved notes.
394 * Displaying an existing model is simpler, and done with add_note.
397 MidiRegionView::add_event (const MidiEvent& ev)
399 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
400 for (size_t i=0; i < ev.size; ++i) {
401 printf("%X ", ev.buffer[i]);
405 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
406 MidiStreamView* const view = mtv->midi_view();
407 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
409 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
410 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
411 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
413 if (mtv->note_mode() == Sustained) {
414 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
415 const Byte& note = ev.buffer()[1];
416 const double y1 = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
417 - footer_height - 3.0;
419 CanvasNote* ev_rect = new CanvasNote(*this, *group);
420 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
421 (nframes_t)ev.time());
422 ev_rect->property_y1() = y1;
423 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
425 ev_rect->property_y2() = y1 + ceil(pixel_range);
426 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
427 /* outline all but right edge */
428 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
429 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
431 ev_rect->raise_to_top();
433 _events.push_back(ev_rect);
435 _active_notes[note] = ev_rect;
437 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
438 const Byte& note = ev.buffer()[1];
439 if (_active_notes && _active_notes[note]) {
440 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
441 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
442 _active_notes[note] = NULL;
446 } else if (mtv->note_mode() == Percussive) {
447 const Byte& note = ev.buffer()[1];
448 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
449 const double y = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
450 - footer_height - 3.0;
452 CanvasHit* ev_diamond = new CanvasHit(*this, *group, std::min(pixel_range, 5.0));
453 ev_diamond->move(x, y);
455 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
456 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
458 _events.push_back(ev_diamond);
463 /** Extend active notes to rightmost edge of region (if length is changed)
466 MidiRegionView::extend_active_notes()
471 for (unsigned i=0; i < 128; ++i)
472 if (_active_notes[i])
473 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
477 /** Add a MIDI note to the view (with duration).
479 * This does no 'realtime' note resolution, notes from a MidiModel have a
480 * duration so they can be drawn in full immediately.
483 MidiRegionView::add_note (const MidiModel::Note& note)
485 assert(note.time() >= 0);
486 assert(note.time() < _region->length());
487 //assert(note.time() + note.duration < _region->length());
489 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
490 MidiStreamView* const view = mtv->midi_view();
491 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
493 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
494 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
495 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
496 const uint8_t fill_alpha = 0x20 + (uint8_t)(note.velocity() * 1.5);
497 const uint32_t fill = RGBA_TO_UINT(0xE0 + note.velocity()/127.0 * 0x10, 0xE0, 0xE0, fill_alpha);
498 const uint8_t outline_alpha = 0x80 + (uint8_t)(note.velocity());
499 const uint32_t outline = RGBA_TO_UINT(0xE0 + note.velocity()/127.0 * 0x10, 0xE0, 0xE0, outline_alpha);
501 //printf("Range: %d\n", note_range);
502 //printf("Event, time = %f, note = %d\n", note.time(), note.note());
505 if (mtv->note_mode() == Sustained) {
506 const double y1 = trackview.height - (pixel_range * (note.note() - view->lowest_note() + 1))
507 - footer_height - 3.0;
509 ArdourCanvas::SimpleRect * ev_rect = new CanvasNote(*this, *group);
510 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
511 ev_rect->property_y1() = y1;
512 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
513 ev_rect->property_y2() = y1 + ceil(pixel_range);
515 ev_rect->property_fill_color_rgba() = fill;
516 ev_rect->property_outline_color_rgba() = outline;
517 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
520 _events.push_back(ev_rect);
522 } else if (mtv->note_mode() == Percussive) {
523 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
524 const double y = trackview.height - (pixel_range * (note.note() - view->lowest_note() + 1))
525 - footer_height - 3.0;
527 CanvasHit* ev_diamond = new CanvasHit(*this, *group, std::min(pixel_range, 5.0));
528 ev_diamond->move(x, y);
530 ev_diamond->property_fill_color_rgba() = fill;
531 ev_diamond->property_outline_color_rgba() = outline;
532 _events.push_back(ev_diamond);