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"
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)
63 , _default_note_length(0.0)
65 , _delta_command(NULL)
70 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
71 Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
72 : RegionView (parent, tv, r, spu, basic_color, visibility)
73 , _default_note_length(0.0)
75 , _delta_command(NULL)
81 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
84 midi_region()->midi_source(0)->load_model();
86 const Meter& m = trackview.session().tempo_map().meter_at(_region->start());
87 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->start());
88 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
91 _model = midi_region()->midi_source(0)->model();
92 _enable_display = false;
94 RegionView::init(basic_color, false);
96 compute_colors (basic_color);
98 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
100 set_y_position_and_height (0, trackview.height);
103 region_resized (BoundsChanged);
106 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
110 _enable_display = true;
116 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
119 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
124 MidiRegionView::canvas_event(GdkEvent* ev)
126 enum State { None, Pressed, SelectDragging, AddDragging };
127 static int press_button = 0;
130 static double drag_start_x, drag_start_y;
131 static double last_x, last_y;
132 double event_x, event_y;
134 static ArdourCanvas::SimpleRect* drag_rect = NULL;
136 if (trackview.editor.current_mouse_mode() != MouseNote)
141 if (ev->key.keyval == GDK_Delete)
142 start_remove_command();
145 case GDK_KEY_RELEASE:
146 if (_command_mode == Remove && ev->key.keyval == GDK_Delete)
150 case GDK_BUTTON_PRESS:
151 //group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, ev->button.time);
153 press_button = ev->button.button;
154 //cerr << "PRESSED: " << press_button << endl;
157 case GDK_MOTION_NOTIFY:
158 event_x = ev->motion.x;
159 event_y = ev->motion.y;
160 group->w2i(event_x, event_y);
163 case Pressed: // Drag start
165 if (press_button == 1 && _command_mode != Remove) { // Select rect start
166 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
167 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
170 drag_start_x = event_x;
171 drag_start_y = event_y;
173 drag_rect = new ArdourCanvas::SimpleRect(*group);
174 drag_rect->property_x1() = event_x;
175 drag_rect->property_y1() = event_y;
176 drag_rect->property_x2() = event_x;
177 drag_rect->property_y2() = event_y;
178 drag_rect->property_outline_what() = 0xFF;
179 drag_rect->property_outline_color_rgba()
180 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
181 drag_rect->property_fill_color_rgba()
182 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
184 _state = SelectDragging;
187 } else if (press_button == 3) { // Add note drag start
188 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
189 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
192 drag_start_x = event_x;
193 drag_start_y = event_y;
195 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
196 midi_view()->editor.snap_to(event_frame);
198 drag_rect = new ArdourCanvas::SimpleRect(*group);
199 drag_rect->property_x1() = midi_view()->editor.frame_to_pixel(event_frame);
201 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
202 drag_rect->property_x2() = event_x;
203 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
204 drag_rect->property_outline_what() = 0xFF;
205 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
206 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
208 _state = AddDragging;
214 case SelectDragging: // Select rect motion
215 case AddDragging: // Add note rect motion
216 if (ev->motion.is_hint) {
219 GdkModifierType state;
220 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
225 if (_state == AddDragging) {
226 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
227 midi_view()->editor.snap_to(event_frame);
228 event_x = midi_view()->editor.frame_to_pixel(event_frame);
232 drag_rect->property_x2() = event_x;
234 if (drag_rect && _state == SelectDragging)
235 drag_rect->property_y2() = event_y;
247 case GDK_BUTTON_RELEASE:
248 event_x = ev->motion.x;
249 event_y = ev->motion.y;
250 group->w2i(event_x, event_y);
251 group->ungrab(ev->button.time);
253 case Pressed: // Clicked
254 if (ev->button.button == 3) {
255 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
256 midi_view()->editor.snap_to(event_frame);
257 event_x = midi_view()->editor.frame_to_pixel(event_frame);
258 create_note_at(event_x, event_y, _default_note_length);
262 case SelectDragging: // Select drag done
267 case AddDragging: // Add drag done
269 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
270 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
271 trackview.editor.pixel_to_frame(
272 drag_rect->property_x2() - drag_rect->property_x1()));
290 /** Add a note to the model, and the view, at a canvas (click) coordinate */
292 MidiRegionView::create_note_at(double x, double y, double dur)
294 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
295 MidiStreamView* const view = mtv->midi_view();
297 double note = midi_stream_view()->y_to_note(y);
300 assert(note <= 127.0);
302 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
304 //assert(stamp <= _region->length());
306 //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
307 //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
308 //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
310 // Add a 1 beat long note (for now)
311 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
313 view->update_bounds(new_note.note());
315 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
317 _model->apply_command(cmd);
319 //add_note(new_note);
324 MidiRegionView::clear_events()
326 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
334 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
344 MidiRegionView::redisplay_model()
351 for (size_t i=0; i < _model->n_notes(); ++i)
352 add_note(_model->note_at(i));
356 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
361 MidiRegionView::~MidiRegionView ()
363 in_destructor = true;
366 RegionViewGoingAway (this); /* EMIT_SIGNAL */
371 MidiRegionView::region_resized (Change what_changed)
373 RegionView::region_resized(what_changed);
375 if (what_changed & ARDOUR::PositionChanged) {
380 } else if (what_changed & Change (StartChanged)) {
382 //cerr << "MIDI RV START CHANGED" << endl;
384 } else if (what_changed & Change (LengthChanged)) {
386 //cerr << "MIDI RV LENGTH CHANGED" << endl;
392 MidiRegionView::reset_width_dependent_items (double pixel_width)
394 RegionView::reset_width_dependent_items(pixel_width);
395 assert(_pixel_width == pixel_width);
402 MidiRegionView::set_y_position_and_height (double y, double h)
404 RegionView::set_y_position_and_height(y, h - 1);
410 name_text->raise_to_top();
415 MidiRegionView::show_region_editor ()
417 cerr << "No MIDI region editor." << endl;
421 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
423 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
426 double unit_position = _region->position () / samples_per_unit;
427 GhostRegion* ghost = new GhostRegion (atv, unit_position);
429 ghost->set_height ();
430 ghost->set_duration (_region->length() / samples_per_unit);
431 ghosts.push_back (ghost);
433 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
439 /** Begin tracking note state for successive calls to add_event
442 MidiRegionView::begin_write()
444 _active_notes = new CanvasNote*[128];
445 for (unsigned i=0; i < 128; ++i)
446 _active_notes[i] = NULL;
450 /** Destroy note state for add_event
453 MidiRegionView::end_write()
455 delete[] _active_notes;
456 _active_notes = NULL;
460 /** Add a MIDI event.
462 * This is used while recording, and handles displaying still-unresolved notes.
463 * Displaying an existing model is simpler, and done with add_note.
466 MidiRegionView::add_event (const MidiEvent& ev)
468 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
469 for (size_t i=0; i < ev.size; ++i) {
470 printf("%X ", ev.buffer[i]);
474 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
476 if (midi_view()->note_mode() == Sustained) {
477 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
478 const Byte& note = ev.buffer()[1];
479 const double y1 = midi_stream_view()->note_to_y(note);
481 CanvasNote* ev_rect = new CanvasNote(*this, *group);
482 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
483 (nframes_t)ev.time());
484 ev_rect->property_y1() = y1;
485 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
487 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
488 ev_rect->property_fill_color_rgba() = note_fill_color(ev.velocity());
489 ev_rect->property_outline_color_rgba() = note_outline_color(ev.velocity());
490 /* outline all but right edge */
491 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
493 ev_rect->raise_to_top();
495 _events.push_back(ev_rect);
497 _active_notes[note] = ev_rect;
499 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
500 const Byte& note = ev.buffer()[1];
501 if (_active_notes && _active_notes[note]) {
502 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
503 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
504 _active_notes[note] = NULL;
508 } else if (midi_view()->note_mode() == Percussive) {
509 const Byte& note = ev.buffer()[1];
510 const double diamond_size = midi_stream_view()->note_height() / 2.0;
511 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
512 const double y = midi_stream_view()->note_to_y(note) + ((diamond_size-2) / 4.0);
514 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
515 ev_diamond->move(x, y);
517 ev_diamond->property_fill_color_rgba() = note_fill_color(ev.velocity());
518 ev_diamond->property_outline_color_rgba() = note_outline_color(ev.velocity());
520 _events.push_back(ev_diamond);
525 /** Extend active notes to rightmost edge of region (if length is changed)
528 MidiRegionView::extend_active_notes()
533 for (unsigned i=0; i < 128; ++i)
534 if (_active_notes[i])
535 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
539 /** Add a MIDI note to the view (with duration).
541 * This does no 'realtime' note resolution, notes from a MidiModel have a
542 * duration so they can be drawn in full immediately.
545 MidiRegionView::add_note (const MidiModel::Note& note)
547 assert(note.time() >= 0);
548 assert(note.time() < _region->length());
550 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
552 if (midi_view()->note_mode() == Sustained) {
553 const double y1 = midi_stream_view()->note_to_y(note.note());
555 ArdourCanvas::SimpleRect * ev_rect = new CanvasNote(*this, *group, ¬e);
556 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
557 ev_rect->property_y1() = y1;
558 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
559 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
561 ev_rect->property_fill_color_rgba() = note_fill_color(note.velocity());
562 ev_rect->property_outline_color_rgba() = note_outline_color(note.velocity());
563 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
566 _events.push_back(ev_rect);
568 } else if (midi_view()->note_mode() == Percussive) {
569 const double diamond_size = midi_stream_view()->note_height() / 2.0;
570 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
571 const double y = midi_stream_view()->note_to_y(note.note()) + ((diamond_size-2) / 4.0);
573 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
574 ev_diamond->move(x, y);
576 ev_diamond->property_fill_color_rgba() = note_fill_color(note.velocity());
577 ev_diamond->property_outline_color_rgba() = note_outline_color(note.velocity());
578 _events.push_back(ev_diamond);