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 "midi_util.h"
50 #include "gui_thread.h"
56 using namespace ARDOUR;
58 using namespace Editing;
59 using namespace ArdourCanvas;
61 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
62 Gdk::Color& basic_color)
63 : RegionView (parent, tv, r, spu, basic_color)
64 , _default_note_length(0.0)
66 , _note_group(new ArdourCanvas::Group(*parent))
67 , _delta_command(NULL)
71 _note_group->raise_to_top();
74 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
75 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 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
131 MidiRegionView::canvas_event(GdkEvent* ev)
133 static bool delete_mod = false;
134 static Editing::MidiEditMode original_mode;
136 static double drag_start_x, drag_start_y;
137 static double last_x, last_y;
138 double event_x, event_y;
139 nframes_t event_frame = 0;
141 static ArdourCanvas::SimpleRect* drag_rect = NULL;
143 if (trackview.editor.current_mouse_mode() != MouseNote)
150 if (ev->key.keyval == GDK_Delete && !delete_mod) {
152 original_mode = trackview.editor.current_midi_edit_mode();
153 trackview.editor.set_midi_edit_mode(MidiEditErase);
154 start_remove_command();
155 _mouse_state = EraseTouchDragging;
156 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
157 _mouse_state = SelectTouchDragging;
161 case GDK_KEY_RELEASE:
162 if (ev->key.keyval == GDK_Delete) {
163 if (_mouse_state == EraseTouchDragging) {
168 trackview.editor.set_midi_edit_mode(original_mode);
171 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
176 case GDK_BUTTON_PRESS:
177 if (_mouse_state != SelectTouchDragging && _mouse_state != EraseTouchDragging)
178 _mouse_state = Pressed;
179 _pressed_button = ev->button.button;
182 case GDK_ENTER_NOTIFY:
183 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
184 Keyboard::magic_widget_grab_focus();
188 case GDK_MOTION_NOTIFY:
189 event_x = ev->motion.x;
190 event_y = ev->motion.y;
191 group->w2i(event_x, event_y);
193 event_frame = trackview.editor.pixel_to_frame(event_x);
194 trackview.editor.snap_to(event_frame);
196 switch (_mouse_state) {
197 case Pressed: // Drag start
200 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
201 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
202 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
205 drag_start_x = event_x;
206 drag_start_y = event_y;
208 drag_rect = new ArdourCanvas::SimpleRect(*group);
209 drag_rect->property_x1() = event_x;
210 drag_rect->property_y1() = event_y;
211 drag_rect->property_x2() = event_x;
212 drag_rect->property_y2() = event_y;
213 drag_rect->property_outline_what() = 0xFF;
214 drag_rect->property_outline_color_rgba()
215 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
216 drag_rect->property_fill_color_rgba()
217 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
219 _mouse_state = SelectRectDragging;
222 // Add note drag start
223 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
224 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
225 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
228 drag_start_x = event_x;
229 drag_start_y = event_y;
231 drag_rect = new ArdourCanvas::SimpleRect(*group);
232 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
234 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
235 drag_rect->property_x2() = event_x;
236 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
237 drag_rect->property_outline_what() = 0xFF;
238 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
240 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
242 _mouse_state = AddDragging;
248 case SelectRectDragging: // Select drag motion
249 case AddDragging: // Add note drag motion
250 if (ev->motion.is_hint) {
253 GdkModifierType state;
254 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
259 if (_mouse_state == AddDragging)
260 event_x = trackview.editor.frame_to_pixel(event_frame);
263 if (event_x > drag_start_x)
264 drag_rect->property_x2() = event_x;
266 drag_rect->property_x1() = event_x;
268 if (drag_rect && _mouse_state == SelectRectDragging) {
269 if (event_y > drag_start_y)
270 drag_rect->property_y2() = event_y;
272 drag_rect->property_y1() = event_y;
274 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
280 case EraseTouchDragging:
281 case SelectTouchDragging:
289 case GDK_BUTTON_RELEASE:
290 event_x = ev->motion.x;
291 event_y = ev->motion.y;
292 group->w2i(event_x, event_y);
293 group->ungrab(ev->button.time);
294 event_frame = trackview.editor.pixel_to_frame(event_x);
296 switch (_mouse_state) {
297 case Pressed: // Clicked
298 switch (trackview.editor.current_midi_edit_mode()) {
303 trackview.editor.snap_to(event_frame);
304 event_x = trackview.editor.frame_to_pixel(event_frame);
305 create_note_at(event_x, event_y, _default_note_length);
311 case SelectRectDragging: // Select drag done
316 case AddDragging: // Add drag done
318 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
319 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
320 trackview.editor.pixel_to_frame(
321 drag_rect->property_x2() - drag_rect->property_x1()));
339 /** Add a note to the model, and the view, at a canvas (click) coordinate */
341 MidiRegionView::create_note_at(double x, double y, double dur)
343 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
344 MidiStreamView* const view = mtv->midi_view();
346 double note = midi_stream_view()->y_to_note(y);
349 assert(note <= 127.0);
351 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
353 //assert(stamp <= _region->length());
355 //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
356 //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
357 //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
359 // Add a 1 beat long note (for now)
360 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
362 view->update_bounds(new_note.note());
364 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
366 _model->apply_command(cmd);
368 //add_note(new_note);
373 MidiRegionView::clear_events()
375 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
383 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
393 MidiRegionView::redisplay_model()
400 for (size_t i=0; i < _model->n_notes(); ++i)
401 add_note(_model->note_at(i));
405 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
410 MidiRegionView::~MidiRegionView ()
412 in_destructor = true;
415 RegionViewGoingAway (this); /* EMIT_SIGNAL */
420 MidiRegionView::region_resized (Change what_changed)
422 RegionView::region_resized(what_changed);
424 if (what_changed & ARDOUR::PositionChanged) {
429 } else if (what_changed & Change (StartChanged)) {
431 //cerr << "MIDI RV START CHANGED" << endl;
433 } else if (what_changed & Change (LengthChanged)) {
435 //cerr << "MIDI RV LENGTH CHANGED" << endl;
441 MidiRegionView::reset_width_dependent_items (double pixel_width)
443 RegionView::reset_width_dependent_items(pixel_width);
444 assert(_pixel_width == pixel_width);
451 MidiRegionView::set_y_position_and_height (double y, double h)
453 RegionView::set_y_position_and_height(y, h - 1);
459 name_text->raise_to_top();
464 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
466 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
469 double unit_position = _region->position () / samples_per_unit;
470 GhostRegion* ghost = new GhostRegion (atv, unit_position);
472 ghost->set_height ();
473 ghost->set_duration (_region->length() / samples_per_unit);
474 ghosts.push_back (ghost);
476 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
482 /** Begin tracking note state for successive calls to add_event
485 MidiRegionView::begin_write()
487 _active_notes = new CanvasNote*[128];
488 for (unsigned i=0; i < 128; ++i)
489 _active_notes[i] = NULL;
493 /** Destroy note state for add_event
496 MidiRegionView::end_write()
498 delete[] _active_notes;
499 _active_notes = NULL;
503 /** Add a MIDI event.
505 * This is used while recording, and handles displaying still-unresolved notes.
506 * Displaying an existing model is simpler, and done with add_note.
509 MidiRegionView::add_event (const MidiEvent& ev)
511 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
512 for (size_t i=0; i < ev.size; ++i) {
513 printf("%X ", ev.buffer[i]);
517 //ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
518 ArdourCanvas::Group* const group = _note_group;
520 if (midi_view()->note_mode() == Sustained) {
521 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
522 const Byte& note = ev.buffer()[1];
523 const double y1 = midi_stream_view()->note_to_y(note);
525 CanvasNote* ev_rect = new CanvasNote(*this, *group);
526 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
527 (nframes_t)ev.time());
528 ev_rect->property_y1() = y1;
529 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
531 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
532 ev_rect->property_fill_color_rgba() = note_fill_color(ev.velocity());
533 ev_rect->property_outline_color_rgba() = note_outline_color(ev.velocity());
534 /* outline all but right edge */
535 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
537 ev_rect->raise_to_top();
539 _events.push_back(ev_rect);
541 _active_notes[note] = ev_rect;
543 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
544 const Byte& note = ev.buffer()[1];
545 if (_active_notes && _active_notes[note]) {
546 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
547 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
548 _active_notes[note] = NULL;
552 } else if (midi_view()->note_mode() == Percussive) {
553 const Byte& note = ev.buffer()[1];
554 const double diamond_size = midi_stream_view()->note_height() / 2.0;
555 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
556 const double y = midi_stream_view()->note_to_y(note) + ((diamond_size-2) / 4.0);
558 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
559 ev_diamond->move(x, y);
561 ev_diamond->property_fill_color_rgba() = note_fill_color(ev.velocity());
562 ev_diamond->property_outline_color_rgba() = note_outline_color(ev.velocity());
564 _events.push_back(ev_diamond);
569 /** Extend active notes to rightmost edge of region (if length is changed)
572 MidiRegionView::extend_active_notes()
577 for (unsigned i=0; i < 128; ++i)
578 if (_active_notes[i])
579 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
583 /** Add a MIDI note to the view (with duration).
585 * This does no 'realtime' note resolution, notes from a MidiModel have a
586 * duration so they can be drawn in full immediately.
589 MidiRegionView::add_note (const MidiModel::Note& note)
591 assert(note.time() >= 0);
592 //assert(note.time() < _region->length());
594 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
596 if (midi_view()->note_mode() == Sustained) {
597 const double y1 = midi_stream_view()->note_to_y(note.note());
599 CanvasNote* ev_rect = new CanvasNote(*this, *group, ¬e);
600 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
601 ev_rect->property_y1() = y1;
602 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
603 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
605 ev_rect->property_fill_color_rgba() = note_fill_color(note.velocity());
606 ev_rect->property_outline_color_rgba() = note_outline_color(note.velocity());
607 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
610 _events.push_back(ev_rect);
612 } else if (midi_view()->note_mode() == Percussive) {
613 const double diamond_size = midi_stream_view()->note_height() / 2.0;
614 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
615 const double y = midi_stream_view()->note_to_y(note.note()) + ((diamond_size-2) / 4.0);
617 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
618 ev_diamond->move(x, y);
620 ev_diamond->property_fill_color_rgba() = note_fill_color(note.velocity());
621 ev_diamond->property_outline_color_rgba() = note_outline_color(note.velocity());
622 _events.push_back(ev_diamond);
627 MidiRegionView::delete_selection()
629 assert(_delta_command);
631 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
632 if ((*i)->selected())
633 _delta_command->remove(*(*i)->note());
639 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
641 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
642 if ((*i)->selected() && (*i) != ev)
643 (*i)->selected(false);
649 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
651 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
653 (*i)->selected(false);
656 _selection.insert(ev);
658 if ( ! ev->selected())
663 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
666 clear_selection_except(ev);
668 _selection.insert(ev);
670 if ( ! ev->selected())
676 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
679 clear_selection_except(ev);
681 _selection.erase(ev);
689 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
691 const double last_y = std::min(y1, y2);
692 const double y = std::max(y1, y2);
694 // FIXME: so, so, so much slower than this should be
697 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
698 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
699 (*i)->selected(true);
700 _selection.insert(*i);
702 (*i)->selected(false);
703 _selection.erase(*i);
707 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
708 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
709 (*i)->selected(true);
710 _selection.insert(*i);
712 (*i)->selected(false);
713 _selection.erase(*i);
721 MidiRegionView::move_selection(double dx, double dy)
723 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
724 (*i)->item()->move(dx, dy);
729 MidiRegionView::note_dropped(CanvasMidiEvent* ev, double dt, uint8_t dnote)
731 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
732 if (_selection.find(ev) != _selection.end()) {
733 start_delta_command();
735 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
736 command_remove_note(*i);
737 MidiModel::Note copy(*(*i)->note());
739 copy.set_time((*i)->note()->time() + dt);
740 copy.set_note((*i)->note()->note() + dnote);
742 command_add_note(copy);
750 MidiRegionView::note_entered(ArdourCanvas::CanvasMidiEvent* ev)
752 cerr << "NOTE ENTERED: " << _mouse_state << endl;
753 if (ev->note() && _mouse_state == EraseTouchDragging) {
754 start_delta_command();
756 _delta_command->remove(*ev->note());
757 } else if (_mouse_state == SelectTouchDragging) {
758 note_selected(ev, true);