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 , _delta_command(NULL)
71 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
72 Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
73 : RegionView (parent, tv, r, spu, basic_color, visibility)
74 , _default_note_length(0.0)
76 , _delta_command(NULL)
82 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
85 midi_region()->midi_source(0)->load_model();
87 const Meter& m = trackview.session().tempo_map().meter_at(_region->start());
88 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->start());
89 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
92 _model = midi_region()->midi_source(0)->model();
93 _enable_display = false;
95 RegionView::init(basic_color, false);
97 compute_colors (basic_color);
99 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
101 set_y_position_and_height (0, trackview.height);
104 region_resized (BoundsChanged);
107 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
111 _enable_display = true;
117 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
120 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
125 MidiRegionView::canvas_event(GdkEvent* ev)
127 enum State { None, Pressed, SelectDragging, AddDragging };
128 static int press_button = 0;
131 static double drag_start_x, drag_start_y;
132 static double last_x, last_y;
133 double event_x, event_y;
135 static ArdourCanvas::SimpleRect* drag_rect = NULL;
137 if (trackview.editor.current_mouse_mode() != MouseNote)
142 if (ev->key.keyval == GDK_Delete)
143 start_remove_command();
146 case GDK_KEY_RELEASE:
147 if (_command_mode == Remove && ev->key.keyval == GDK_Delete) {
153 case GDK_BUTTON_PRESS:
154 //group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, ev->button.time);
156 press_button = ev->button.button;
157 //cerr << "PRESSED: " << press_button << endl;
160 case GDK_ENTER_NOTIFY:
161 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
162 Keyboard::magic_widget_grab_focus();
166 case GDK_MOTION_NOTIFY:
167 event_x = ev->motion.x;
168 event_y = ev->motion.y;
169 group->w2i(event_x, event_y);
172 case Pressed: // Drag start
174 if (press_button == 1 && _command_mode != Remove) { // Select rect start
175 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
176 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
179 drag_start_x = event_x;
180 drag_start_y = event_y;
182 drag_rect = new ArdourCanvas::SimpleRect(*group);
183 drag_rect->property_x1() = event_x;
184 drag_rect->property_y1() = event_y;
185 drag_rect->property_x2() = event_x;
186 drag_rect->property_y2() = event_y;
187 drag_rect->property_outline_what() = 0xFF;
188 drag_rect->property_outline_color_rgba()
189 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
190 drag_rect->property_fill_color_rgba()
191 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
193 _state = SelectDragging;
196 } else if (press_button == 3) { // Add note drag start
197 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
198 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
201 drag_start_x = event_x;
202 drag_start_y = event_y;
204 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
205 midi_view()->editor.snap_to(event_frame);
207 drag_rect = new ArdourCanvas::SimpleRect(*group);
208 drag_rect->property_x1() = midi_view()->editor.frame_to_pixel(event_frame);
210 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
211 drag_rect->property_x2() = event_x;
212 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
213 drag_rect->property_outline_what() = 0xFF;
214 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
215 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
217 _state = AddDragging;
223 case SelectDragging: // Select rect motion
224 case AddDragging: // Add note rect motion
225 if (ev->motion.is_hint) {
228 GdkModifierType state;
229 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
234 if (_state == AddDragging) {
235 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
236 midi_view()->editor.snap_to(event_frame);
237 event_x = midi_view()->editor.frame_to_pixel(event_frame);
241 drag_rect->property_x2() = event_x;
243 if (drag_rect && _state == SelectDragging)
244 drag_rect->property_y2() = event_y;
256 case GDK_BUTTON_RELEASE:
257 event_x = ev->motion.x;
258 event_y = ev->motion.y;
259 group->w2i(event_x, event_y);
260 group->ungrab(ev->button.time);
262 case Pressed: // Clicked
263 if (ev->button.button == 3) {
264 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
265 midi_view()->editor.snap_to(event_frame);
266 event_x = midi_view()->editor.frame_to_pixel(event_frame);
267 create_note_at(event_x, event_y, _default_note_length);
271 case SelectDragging: // Select drag done
276 case AddDragging: // Add drag done
278 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
279 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
280 trackview.editor.pixel_to_frame(
281 drag_rect->property_x2() - drag_rect->property_x1()));
299 /** Add a note to the model, and the view, at a canvas (click) coordinate */
301 MidiRegionView::create_note_at(double x, double y, double dur)
303 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
304 MidiStreamView* const view = mtv->midi_view();
306 double note = midi_stream_view()->y_to_note(y);
309 assert(note <= 127.0);
311 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
313 //assert(stamp <= _region->length());
315 //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
316 //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
317 //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
319 // Add a 1 beat long note (for now)
320 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
322 view->update_bounds(new_note.note());
324 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
326 _model->apply_command(cmd);
328 //add_note(new_note);
333 MidiRegionView::clear_events()
335 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
343 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
353 MidiRegionView::redisplay_model()
360 for (size_t i=0; i < _model->n_notes(); ++i)
361 add_note(_model->note_at(i));
365 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
370 MidiRegionView::~MidiRegionView ()
372 in_destructor = true;
375 RegionViewGoingAway (this); /* EMIT_SIGNAL */
380 MidiRegionView::region_resized (Change what_changed)
382 RegionView::region_resized(what_changed);
384 if (what_changed & ARDOUR::PositionChanged) {
389 } else if (what_changed & Change (StartChanged)) {
391 //cerr << "MIDI RV START CHANGED" << endl;
393 } else if (what_changed & Change (LengthChanged)) {
395 //cerr << "MIDI RV LENGTH CHANGED" << endl;
401 MidiRegionView::reset_width_dependent_items (double pixel_width)
403 RegionView::reset_width_dependent_items(pixel_width);
404 assert(_pixel_width == pixel_width);
411 MidiRegionView::set_y_position_and_height (double y, double h)
413 RegionView::set_y_position_and_height(y, h - 1);
419 name_text->raise_to_top();
424 MidiRegionView::show_region_editor ()
426 cerr << "No MIDI region editor." << endl;
430 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
432 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
435 double unit_position = _region->position () / samples_per_unit;
436 GhostRegion* ghost = new GhostRegion (atv, unit_position);
438 ghost->set_height ();
439 ghost->set_duration (_region->length() / samples_per_unit);
440 ghosts.push_back (ghost);
442 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
448 /** Begin tracking note state for successive calls to add_event
451 MidiRegionView::begin_write()
453 _active_notes = new CanvasNote*[128];
454 for (unsigned i=0; i < 128; ++i)
455 _active_notes[i] = NULL;
459 /** Destroy note state for add_event
462 MidiRegionView::end_write()
464 delete[] _active_notes;
465 _active_notes = NULL;
469 /** Add a MIDI event.
471 * This is used while recording, and handles displaying still-unresolved notes.
472 * Displaying an existing model is simpler, and done with add_note.
475 MidiRegionView::add_event (const MidiEvent& ev)
477 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
478 for (size_t i=0; i < ev.size; ++i) {
479 printf("%X ", ev.buffer[i]);
483 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
485 if (midi_view()->note_mode() == Sustained) {
486 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
487 const Byte& note = ev.buffer()[1];
488 const double y1 = midi_stream_view()->note_to_y(note);
490 CanvasNote* ev_rect = new CanvasNote(*this, *group);
491 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
492 (nframes_t)ev.time());
493 ev_rect->property_y1() = y1;
494 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
496 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
497 ev_rect->property_fill_color_rgba() = note_fill_color(ev.velocity());
498 ev_rect->property_outline_color_rgba() = note_outline_color(ev.velocity());
499 /* outline all but right edge */
500 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
502 ev_rect->raise_to_top();
504 _events.push_back(ev_rect);
506 _active_notes[note] = ev_rect;
508 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
509 const Byte& note = ev.buffer()[1];
510 if (_active_notes && _active_notes[note]) {
511 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
512 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
513 _active_notes[note] = NULL;
517 } else if (midi_view()->note_mode() == Percussive) {
518 const Byte& note = ev.buffer()[1];
519 const double diamond_size = midi_stream_view()->note_height() / 2.0;
520 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
521 const double y = midi_stream_view()->note_to_y(note) + ((diamond_size-2) / 4.0);
523 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
524 ev_diamond->move(x, y);
526 ev_diamond->property_fill_color_rgba() = note_fill_color(ev.velocity());
527 ev_diamond->property_outline_color_rgba() = note_outline_color(ev.velocity());
529 _events.push_back(ev_diamond);
534 /** Extend active notes to rightmost edge of region (if length is changed)
537 MidiRegionView::extend_active_notes()
542 for (unsigned i=0; i < 128; ++i)
543 if (_active_notes[i])
544 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
548 /** Add a MIDI note to the view (with duration).
550 * This does no 'realtime' note resolution, notes from a MidiModel have a
551 * duration so they can be drawn in full immediately.
554 MidiRegionView::add_note (const MidiModel::Note& note)
556 assert(note.time() >= 0);
557 assert(note.time() < _region->length());
559 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
561 if (midi_view()->note_mode() == Sustained) {
562 const double y1 = midi_stream_view()->note_to_y(note.note());
564 ArdourCanvas::SimpleRect * ev_rect = new CanvasNote(*this, *group, ¬e);
565 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
566 ev_rect->property_y1() = y1;
567 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
568 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
570 ev_rect->property_fill_color_rgba() = note_fill_color(note.velocity());
571 ev_rect->property_outline_color_rgba() = note_outline_color(note.velocity());
572 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
575 _events.push_back(ev_rect);
577 } else if (midi_view()->note_mode() == Percussive) {
578 const double diamond_size = midi_stream_view()->note_height() / 2.0;
579 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
580 const double y = midi_stream_view()->note_to_y(note.note()) + ((diamond_size-2) / 4.0);
582 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
583 ev_diamond->move(x, y);
585 ev_diamond->property_fill_color_rgba() = note_fill_color(note.velocity());
586 ev_diamond->property_outline_color_rgba() = note_outline_color(note.velocity());
587 _events.push_back(ev_diamond);
592 MidiRegionView::delete_selection()
594 assert(_delta_command);
596 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
597 if ((*i)->selected())
598 _delta_command->remove(*(*i)->note());
604 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
606 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
607 if ((*i)->selected() && (*i) != ev)
608 (*i)->selected(false);
614 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
616 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
618 (*i)->selected(false);
621 _selection.insert(ev);
626 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
629 clear_selection_except(ev);
631 _selection.insert(ev);
637 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
640 clear_selection_except(ev);
642 _selection.erase(ev);