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 cerr << "REGION KEY PRESS\n";
151 if (ev->key.keyval == GDK_Delete && !delete_mod) {
153 original_mode = trackview.editor.current_midi_edit_mode();
154 trackview.editor.set_midi_edit_mode(MidiEditErase);
155 start_remove_command();
156 _mouse_state = EraseDragging;
160 case GDK_KEY_RELEASE:
161 cerr << "REGION KEY RELEASE\n";
162 if (ev->key.keyval == GDK_Delete) {
163 if (_mouse_state == EraseDragging) {
168 trackview.editor.set_midi_edit_mode(original_mode);
174 case GDK_BUTTON_PRESS:
175 //group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, ev->button.time);
176 _mouse_state = Pressed;
177 _pressed_button = ev->button.button;
178 //cerr << "PRESSED: " << press_button << endl;
181 case GDK_ENTER_NOTIFY:
182 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
183 Keyboard::magic_widget_grab_focus();
187 case GDK_MOTION_NOTIFY:
188 event_x = ev->motion.x;
189 event_y = ev->motion.y;
190 group->w2i(event_x, event_y);
192 event_frame = trackview.editor.pixel_to_frame(event_x);
193 trackview.editor.snap_to(event_frame);
195 switch (_mouse_state) {
196 case Pressed: // Drag start
199 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
200 cerr << "SELECT START\n";
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 = SelectDragging;
222 // Add note drag start
223 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
224 cerr << "PENCIL START\n";
225 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
226 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
229 drag_start_x = event_x;
230 drag_start_y = event_y;
232 drag_rect = new ArdourCanvas::SimpleRect(*group);
233 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
235 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
236 drag_rect->property_x2() = event_x;
237 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
238 drag_rect->property_outline_what() = 0xFF;
239 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
241 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
243 _mouse_state = AddDragging;
247 } else if (trackview.editor.current_midi_edit_mode() == MidiEditErase) {
248 cerr << "ERASE DRAGGING\n";
249 _mouse_state = EraseDragging;
255 case SelectDragging: // Select drag motion
256 case AddDragging: // Add note drag motion
257 if (ev->motion.is_hint) {
260 GdkModifierType state;
261 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
266 if (_mouse_state == AddDragging)
267 event_x = trackview.editor.frame_to_pixel(event_frame);
270 if (event_x > drag_start_x)
271 drag_rect->property_x2() = event_x;
273 drag_rect->property_x1() = event_x;
275 if (drag_rect && _mouse_state == SelectDragging) {
276 if (event_y > drag_start_y)
277 drag_rect->property_y2() = event_y;
279 drag_rect->property_y1() = event_y;
281 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
296 case GDK_BUTTON_RELEASE:
297 event_x = ev->motion.x;
298 event_y = ev->motion.y;
299 group->w2i(event_x, event_y);
300 group->ungrab(ev->button.time);
301 event_frame = trackview.editor.pixel_to_frame(event_x);
303 switch (_mouse_state) {
304 case Pressed: // Clicked
305 switch (trackview.editor.current_midi_edit_mode()) {
310 trackview.editor.snap_to(event_frame);
311 event_x = trackview.editor.frame_to_pixel(event_frame);
312 create_note_at(event_x, event_y, _default_note_length);
318 case SelectDragging: // Select drag done
323 case AddDragging: // Add drag done
325 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
326 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
327 trackview.editor.pixel_to_frame(
328 drag_rect->property_x2() - drag_rect->property_x1()));
346 /** Add a note to the model, and the view, at a canvas (click) coordinate */
348 MidiRegionView::create_note_at(double x, double y, double dur)
350 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
351 MidiStreamView* const view = mtv->midi_view();
353 double note = midi_stream_view()->y_to_note(y);
356 assert(note <= 127.0);
358 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
360 //assert(stamp <= _region->length());
362 //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
363 //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
364 //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
366 // Add a 1 beat long note (for now)
367 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
369 view->update_bounds(new_note.note());
371 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
373 _model->apply_command(cmd);
375 //add_note(new_note);
380 MidiRegionView::clear_events()
382 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
390 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
400 MidiRegionView::redisplay_model()
407 for (size_t i=0; i < _model->n_notes(); ++i)
408 add_note(_model->note_at(i));
412 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
417 MidiRegionView::~MidiRegionView ()
419 in_destructor = true;
422 RegionViewGoingAway (this); /* EMIT_SIGNAL */
427 MidiRegionView::region_resized (Change what_changed)
429 RegionView::region_resized(what_changed);
431 if (what_changed & ARDOUR::PositionChanged) {
436 } else if (what_changed & Change (StartChanged)) {
438 //cerr << "MIDI RV START CHANGED" << endl;
440 } else if (what_changed & Change (LengthChanged)) {
442 //cerr << "MIDI RV LENGTH CHANGED" << endl;
448 MidiRegionView::reset_width_dependent_items (double pixel_width)
450 RegionView::reset_width_dependent_items(pixel_width);
451 assert(_pixel_width == pixel_width);
458 MidiRegionView::set_y_position_and_height (double y, double h)
460 RegionView::set_y_position_and_height(y, h - 1);
466 name_text->raise_to_top();
471 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
473 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
476 double unit_position = _region->position () / samples_per_unit;
477 GhostRegion* ghost = new GhostRegion (atv, unit_position);
479 ghost->set_height ();
480 ghost->set_duration (_region->length() / samples_per_unit);
481 ghosts.push_back (ghost);
483 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
489 /** Begin tracking note state for successive calls to add_event
492 MidiRegionView::begin_write()
494 _active_notes = new CanvasNote*[128];
495 for (unsigned i=0; i < 128; ++i)
496 _active_notes[i] = NULL;
500 /** Destroy note state for add_event
503 MidiRegionView::end_write()
505 delete[] _active_notes;
506 _active_notes = NULL;
510 /** Add a MIDI event.
512 * This is used while recording, and handles displaying still-unresolved notes.
513 * Displaying an existing model is simpler, and done with add_note.
516 MidiRegionView::add_event (const MidiEvent& ev)
518 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
519 for (size_t i=0; i < ev.size; ++i) {
520 printf("%X ", ev.buffer[i]);
524 //ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
525 ArdourCanvas::Group* const group = _note_group;
527 if (midi_view()->note_mode() == Sustained) {
528 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
529 const Byte& note = ev.buffer()[1];
530 const double y1 = midi_stream_view()->note_to_y(note);
532 CanvasNote* ev_rect = new CanvasNote(*this, *group);
533 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
534 (nframes_t)ev.time());
535 ev_rect->property_y1() = y1;
536 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
538 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
539 ev_rect->property_fill_color_rgba() = note_fill_color(ev.velocity());
540 ev_rect->property_outline_color_rgba() = note_outline_color(ev.velocity());
541 /* outline all but right edge */
542 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
544 ev_rect->raise_to_top();
546 _events.push_back(ev_rect);
548 _active_notes[note] = ev_rect;
550 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
551 const Byte& note = ev.buffer()[1];
552 if (_active_notes && _active_notes[note]) {
553 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
554 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
555 _active_notes[note] = NULL;
559 } else if (midi_view()->note_mode() == Percussive) {
560 const Byte& note = ev.buffer()[1];
561 const double diamond_size = midi_stream_view()->note_height() / 2.0;
562 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
563 const double y = midi_stream_view()->note_to_y(note) + ((diamond_size-2) / 4.0);
565 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
566 ev_diamond->move(x, y);
568 ev_diamond->property_fill_color_rgba() = note_fill_color(ev.velocity());
569 ev_diamond->property_outline_color_rgba() = note_outline_color(ev.velocity());
571 _events.push_back(ev_diamond);
576 /** Extend active notes to rightmost edge of region (if length is changed)
579 MidiRegionView::extend_active_notes()
584 for (unsigned i=0; i < 128; ++i)
585 if (_active_notes[i])
586 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
590 /** Add a MIDI note to the view (with duration).
592 * This does no 'realtime' note resolution, notes from a MidiModel have a
593 * duration so they can be drawn in full immediately.
596 MidiRegionView::add_note (const MidiModel::Note& note)
598 assert(note.time() >= 0);
599 //assert(note.time() < _region->length());
601 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
603 if (midi_view()->note_mode() == Sustained) {
604 const double y1 = midi_stream_view()->note_to_y(note.note());
606 CanvasNote* ev_rect = new CanvasNote(*this, *group, ¬e);
607 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
608 ev_rect->property_y1() = y1;
609 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
610 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
612 ev_rect->property_fill_color_rgba() = note_fill_color(note.velocity());
613 ev_rect->property_outline_color_rgba() = note_outline_color(note.velocity());
614 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
617 _events.push_back(ev_rect);
619 } else if (midi_view()->note_mode() == Percussive) {
620 const double diamond_size = midi_stream_view()->note_height() / 2.0;
621 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
622 const double y = midi_stream_view()->note_to_y(note.note()) + ((diamond_size-2) / 4.0);
624 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
625 ev_diamond->move(x, y);
627 ev_diamond->property_fill_color_rgba() = note_fill_color(note.velocity());
628 ev_diamond->property_outline_color_rgba() = note_outline_color(note.velocity());
629 _events.push_back(ev_diamond);
634 MidiRegionView::delete_selection()
636 assert(_delta_command);
638 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
639 if ((*i)->selected())
640 _delta_command->remove(*(*i)->note());
646 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
648 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
649 if ((*i)->selected() && (*i) != ev)
650 (*i)->selected(false);
656 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
658 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
660 (*i)->selected(false);
663 _selection.insert(ev);
668 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
671 clear_selection_except(ev);
673 _selection.insert(ev);
679 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
682 clear_selection_except(ev);
684 _selection.erase(ev);
690 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
692 const double last_y = std::min(y1, y2);
693 const double y = std::max(y1, y2);
695 // FIXME: so, so, so much slower than this should be
698 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
699 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
700 (*i)->selected(true);
701 _selection.insert(*i);
703 (*i)->selected(false);
704 _selection.erase(*i);
708 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
709 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
710 (*i)->selected(true);
711 _selection.insert(*i);
713 (*i)->selected(false);
714 _selection.erase(*i);
722 MidiRegionView::move_selection(double dx, double dy)
724 cerr << "MOVE SELECTION: " << dx << ", " << dy << endl;
725 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
726 (*i)->item()->move(dx, dy);
730 MidiRegionView::note_dropped(CanvasMidiEvent* ev, double dt, uint8_t dnote)
732 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
733 if (_selection.find(ev) != _selection.end()) {
734 start_delta_command();
736 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
737 command_remove_note(*i);
738 MidiModel::Note copy(*(*i)->note());
740 copy.set_time((*i)->note()->time() + dt);
741 copy.set_note((*i)->note()->note() + dnote);
743 command_add_note(copy);