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"
48 #include "automation_time_axis.h"
49 #include "automation_region_view.h"
51 #include "midi_util.h"
52 #include "gui_thread.h"
58 using namespace ARDOUR;
60 using namespace Editing;
61 using namespace ArdourCanvas;
63 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
64 : RegionView (parent, tv, r, spu, basic_color)
65 , _default_note_length(0.0)
67 , _note_group(new ArdourCanvas::Group(*parent))
68 , _delta_command(NULL)
72 _note_group->raise_to_top();
75 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
76 : RegionView (parent, tv, r, spu, basic_color, visibility)
77 , _default_note_length(0.0)
79 , _note_group(new ArdourCanvas::Group(*parent))
80 , _delta_command(NULL)
84 _note_group->raise_to_top();
88 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
91 midi_region()->midi_source(0)->load_model();
93 const Meter& m = trackview.session().tempo_map().meter_at(_region->start());
94 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->start());
95 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
98 _model = midi_region()->midi_source(0)->model();
99 _enable_display = false;
101 RegionView::init(basic_color, false);
103 compute_colors (basic_color);
105 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
107 set_y_position_and_height (0, trackview.height);
110 region_resized (BoundsChanged);
113 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
117 _enable_display = true;
123 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
126 midi_region()->midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegionView::switch_source));
128 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
133 MidiRegionView::canvas_event(GdkEvent* ev)
135 static bool delete_mod = false;
136 static Editing::MidiEditMode original_mode;
138 static double drag_start_x, drag_start_y;
139 static double last_x, last_y;
140 double event_x, event_y;
141 nframes_t event_frame = 0;
143 static ArdourCanvas::SimpleRect* drag_rect = NULL;
145 if (trackview.editor.current_mouse_mode() != MouseNote)
152 if (ev->key.keyval == GDK_Delete && !delete_mod) {
154 original_mode = trackview.editor.current_midi_edit_mode();
155 trackview.editor.set_midi_edit_mode(MidiEditErase);
156 start_remove_command();
157 _mouse_state = EraseTouchDragging;
158 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
159 _mouse_state = SelectTouchDragging;
163 case GDK_KEY_RELEASE:
164 if (ev->key.keyval == GDK_Delete) {
165 if (_mouse_state == EraseTouchDragging) {
170 trackview.editor.set_midi_edit_mode(original_mode);
173 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
178 case GDK_BUTTON_PRESS:
179 if (_mouse_state != SelectTouchDragging && _mouse_state != EraseTouchDragging)
180 _mouse_state = Pressed;
181 _pressed_button = ev->button.button;
184 case GDK_ENTER_NOTIFY:
185 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
186 Keyboard::magic_widget_grab_focus();
190 case GDK_MOTION_NOTIFY:
191 event_x = ev->motion.x;
192 event_y = ev->motion.y;
193 group->w2i(event_x, event_y);
195 event_frame = trackview.editor.pixel_to_frame(event_x);
196 trackview.editor.snap_to(event_frame);
198 switch (_mouse_state) {
199 case Pressed: // Drag start
202 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
203 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
204 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
207 drag_start_x = event_x;
208 drag_start_y = event_y;
210 drag_rect = new ArdourCanvas::SimpleRect(*group);
211 drag_rect->property_x1() = event_x;
212 drag_rect->property_y1() = event_y;
213 drag_rect->property_x2() = event_x;
214 drag_rect->property_y2() = event_y;
215 drag_rect->property_outline_what() = 0xFF;
216 drag_rect->property_outline_color_rgba()
217 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
218 drag_rect->property_fill_color_rgba()
219 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
221 _mouse_state = SelectRectDragging;
224 // Add note drag start
225 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
226 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
227 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
230 drag_start_x = event_x;
231 drag_start_y = event_y;
233 drag_rect = new ArdourCanvas::SimpleRect(*group);
234 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
236 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
237 drag_rect->property_x2() = event_x;
238 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
239 drag_rect->property_outline_what() = 0xFF;
240 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
242 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
244 _mouse_state = AddDragging;
250 case SelectRectDragging: // Select drag motion
251 case AddDragging: // Add note drag motion
252 if (ev->motion.is_hint) {
255 GdkModifierType state;
256 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
261 if (_mouse_state == AddDragging)
262 event_x = trackview.editor.frame_to_pixel(event_frame);
265 if (event_x > drag_start_x)
266 drag_rect->property_x2() = event_x;
268 drag_rect->property_x1() = event_x;
270 if (drag_rect && _mouse_state == SelectRectDragging) {
271 if (event_y > drag_start_y)
272 drag_rect->property_y2() = event_y;
274 drag_rect->property_y1() = event_y;
276 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
282 case EraseTouchDragging:
283 case SelectTouchDragging:
291 case GDK_BUTTON_RELEASE:
292 event_x = ev->motion.x;
293 event_y = ev->motion.y;
294 group->w2i(event_x, event_y);
295 group->ungrab(ev->button.time);
296 event_frame = trackview.editor.pixel_to_frame(event_x);
298 switch (_mouse_state) {
299 case Pressed: // Clicked
300 switch (trackview.editor.current_midi_edit_mode()) {
305 trackview.editor.snap_to(event_frame);
306 event_x = trackview.editor.frame_to_pixel(event_frame);
307 create_note_at(event_x, event_y, _default_note_length);
313 case SelectRectDragging: // Select drag done
318 case AddDragging: // Add drag done
320 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
321 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
322 trackview.editor.pixel_to_frame(
323 drag_rect->property_x2() - drag_rect->property_x1()));
341 /** Add a note to the model, and the view, at a canvas (click) coordinate */
343 MidiRegionView::create_note_at(double x, double y, double dur)
345 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
346 MidiStreamView* const view = mtv->midi_view();
348 double note = midi_stream_view()->y_to_note(y);
351 assert(note <= 127.0);
353 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
355 //assert(stamp <= _region->length());
357 //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
358 //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
359 //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
361 // Add a 1 beat long note (for now)
362 const Note new_note(stamp, dur, (uint8_t)note, 0x40);
364 view->update_bounds(new_note.note());
366 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
368 _model->apply_command(cmd);
370 //add_note(new_note);
375 MidiRegionView::clear_events()
377 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
385 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
395 MidiRegionView::redisplay_model()
397 // Don't redisplay the model if we're currently recording and displaying that
408 for (size_t i=0; i < _model->n_notes(); ++i)
409 add_note(_model->note_at(i));
413 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
414 i != _model->controls().end(); ++i) {
418 boost::shared_ptr<AutomationTimeAxisView> at
419 = midi_view()->automation_child(i->second->parameter());
423 Gdk::Color col = midi_stream_view()->get_region_color();
425 boost::shared_ptr<AutomationRegionView> arv;
428 Glib::Mutex::Lock list_lock (i->second->list()->lock());
430 arv = boost::shared_ptr<AutomationRegionView>(
431 new AutomationRegionView(at->canvas_display,
432 *at.get(), _region, i->second->list(),
433 midi_stream_view()->get_samples_per_unit(), col));
436 arv->set_duration(_region->length(), this);
437 arv->init(col, true);
439 _automation_children.insert(std::make_pair(i->second->parameter(), arv));
442 _model->read_unlock();
445 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
450 MidiRegionView::~MidiRegionView ()
452 in_destructor = true;
458 RegionViewGoingAway (this); /* EMIT_SIGNAL */
463 MidiRegionView::region_resized (Change what_changed)
465 RegionView::region_resized(what_changed);
467 if (what_changed & ARDOUR::PositionChanged) {
472 } else if (what_changed & Change (StartChanged)) {
474 //cerr << "MIDI RV START CHANGED" << endl;
476 } else if (what_changed & Change (LengthChanged)) {
478 //cerr << "MIDI RV LENGTH CHANGED" << endl;
484 MidiRegionView::reset_width_dependent_items (double pixel_width)
486 RegionView::reset_width_dependent_items(pixel_width);
487 assert(_pixel_width == pixel_width);
494 MidiRegionView::set_y_position_and_height (double y, double h)
496 RegionView::set_y_position_and_height(y, h - 1);
502 name_text->raise_to_top();
507 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
509 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
512 double unit_position = _region->position () / samples_per_unit;
513 GhostRegion* ghost = new GhostRegion (atv, unit_position);
515 ghost->set_height ();
516 ghost->set_duration (_region->length() / samples_per_unit);
517 ghosts.push_back (ghost);
519 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
525 /** Begin tracking note state for successive calls to add_event
528 MidiRegionView::begin_write()
530 assert(!_active_notes);
531 _active_notes = new CanvasNote*[128];
532 for (unsigned i=0; i < 128; ++i)
533 _active_notes[i] = NULL;
537 /** Destroy note state for add_event
540 MidiRegionView::end_write()
542 delete[] _active_notes;
543 _active_notes = NULL;
547 /** Add a MIDI event.
549 * This is used while recording, and handles displaying still-unresolved notes.
550 * Displaying an existing model is simpler, and done with add_note.
553 MidiRegionView::add_event (const MidiEvent& ev)
555 /*printf("MRV add Event, time = %f, size = %u, data = ", ev.time(), ev.size());
556 for (size_t i=0; i < ev.size(); ++i) {
557 printf("%X ", ev.buffer()[i]);
561 //ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
562 ArdourCanvas::Group* const group = _note_group;
564 if (midi_view()->note_mode() == Sustained) {
565 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
566 const Byte& note = ev.buffer()[1];
567 const double y1 = midi_stream_view()->note_to_y(note);
569 CanvasNote* ev_rect = new CanvasNote(*this, *group);
570 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
571 (nframes_t)ev.time());
572 ev_rect->property_y1() = y1;
573 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
575 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
576 ev_rect->property_fill_color_rgba() = note_fill_color(ev.velocity());
577 ev_rect->property_outline_color_rgba() = note_outline_color(ev.velocity());
578 /* outline all but right edge */
579 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
581 ev_rect->raise_to_top();
583 _events.push_back(ev_rect);
585 _active_notes[note] = ev_rect;
587 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
588 const Byte& note = ev.buffer()[1];
589 if (_active_notes && _active_notes[note]) {
590 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
591 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
592 _active_notes[note] = NULL;
596 } else if (midi_view()->note_mode() == Percussive) {
597 const Byte& note = ev.buffer()[1];
598 const double diamond_size = midi_stream_view()->note_height() / 2.0;
599 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
600 const double y = midi_stream_view()->note_to_y(note) + ((diamond_size-2) / 4.0);
602 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
603 ev_diamond->move(x, y);
605 ev_diamond->property_fill_color_rgba() = note_fill_color(ev.velocity());
606 ev_diamond->property_outline_color_rgba() = note_outline_color(ev.velocity());
608 _events.push_back(ev_diamond);
613 /** Extend active notes to rightmost edge of region (if length is changed)
616 MidiRegionView::extend_active_notes()
621 for (unsigned i=0; i < 128; ++i)
622 if (_active_notes[i])
623 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
627 /** Add a MIDI note to the view (with duration).
629 * This does no 'realtime' note resolution, notes from a MidiModel have a
630 * duration so they can be drawn in full immediately.
633 MidiRegionView::add_note (const Note& note)
635 assert(note.time() >= 0);
636 //assert(note.time() < _region->length());
638 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
640 if (midi_view()->note_mode() == Sustained) {
641 const double y1 = midi_stream_view()->note_to_y(note.note());
643 CanvasNote* ev_rect = new CanvasNote(*this, *group, ¬e);
644 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
645 ev_rect->property_y1() = y1;
646 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
647 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
649 ev_rect->property_fill_color_rgba() = note_fill_color(note.velocity());
650 ev_rect->property_outline_color_rgba() = note_outline_color(note.velocity());
651 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
654 _events.push_back(ev_rect);
656 } else if (midi_view()->note_mode() == Percussive) {
657 const double diamond_size = midi_stream_view()->note_height() / 2.0;
658 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
659 const double y = midi_stream_view()->note_to_y(note.note()) + ((diamond_size-2) / 4.0);
661 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
662 ev_diamond->move(x, y);
664 ev_diamond->property_fill_color_rgba() = note_fill_color(note.velocity());
665 ev_diamond->property_outline_color_rgba() = note_outline_color(note.velocity());
666 _events.push_back(ev_diamond);
671 MidiRegionView::delete_selection()
673 assert(_delta_command);
675 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
676 if ((*i)->selected())
677 _delta_command->remove(*(*i)->note());
683 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
685 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
686 if ((*i)->selected() && (*i) != ev)
687 (*i)->selected(false);
693 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
695 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
697 (*i)->selected(false);
700 _selection.insert(ev);
702 if ( ! ev->selected())
707 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
710 clear_selection_except(ev);
712 _selection.insert(ev);
714 if ( ! ev->selected())
720 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
723 clear_selection_except(ev);
725 _selection.erase(ev);
733 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
735 const double last_y = std::min(y1, y2);
736 const double y = std::max(y1, y2);
738 // FIXME: so, so, so much slower than this should be
741 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
742 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
743 (*i)->selected(true);
744 _selection.insert(*i);
746 (*i)->selected(false);
747 _selection.erase(*i);
751 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
752 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
753 (*i)->selected(true);
754 _selection.insert(*i);
756 (*i)->selected(false);
757 _selection.erase(*i);
765 MidiRegionView::move_selection(double dx, double dy)
767 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
768 (*i)->item()->move(dx, dy);
773 MidiRegionView::note_dropped(CanvasMidiEvent* ev, double dt, uint8_t dnote)
775 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
776 if (_selection.find(ev) != _selection.end()) {
777 start_delta_command();
779 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
780 command_remove_note(*i);
781 Note copy(*(*i)->note());
783 copy.set_time((*i)->note()->time() + dt);
784 copy.set_note((*i)->note()->note() + dnote);
786 command_add_note(copy);
794 MidiRegionView::note_entered(ArdourCanvas::CanvasMidiEvent* ev)
796 if (ev->note() && _mouse_state == EraseTouchDragging) {
797 start_delta_command();
799 _delta_command->remove(*ev->note());
800 } else if (_mouse_state == SelectTouchDragging) {
801 note_selected(ev, true);
807 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
809 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
811 display_model(msrc->model());