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 "rgb_macros.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)
64 , _delta_command(NULL)
69 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
70 Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
71 : RegionView (parent, tv, r, spu, basic_color, visibility)
73 , _delta_command(NULL)
79 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
82 midi_region()->midi_source(0)->load_model();
84 _model = midi_region()->midi_source(0)->model();
85 _enable_display = false;
87 RegionView::init(basic_color, false);
89 compute_colors (basic_color);
91 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
93 set_y_position_and_height (0, trackview.height);
96 region_resized (BoundsChanged);
99 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
103 _enable_display = true;
109 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
112 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
117 MidiRegionView::canvas_event(GdkEvent* ev)
119 enum State { None, Pressed, Dragging };
120 static int press_button = 0;
123 static double last_x, last_y;
124 double event_x, event_y;
126 static ArdourCanvas::SimpleRect* select_rect = NULL;
128 if (trackview.editor.current_mouse_mode() != MouseNote)
133 if (ev->key.keyval == GDK_Delete)
134 start_remove_command();
137 case GDK_KEY_RELEASE:
138 if (_command_mode == Remove && ev->key.keyval == GDK_Delete)
142 case GDK_BUTTON_PRESS:
143 //group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, ev->button.time);
145 press_button = ev->button.button;
146 cerr << "PRESSED: " << press_button << endl;
149 case GDK_MOTION_NOTIFY:
150 event_x = ev->motion.x;
151 event_y = ev->motion.y;
152 group->w2i(event_x, event_y);
155 case Pressed: // Drag start
156 cerr << "PRESSED MOTION: " << press_button << endl;
157 if (press_button == 1) { // Select rect start
158 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
159 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
164 select_rect = new ArdourCanvas::SimpleRect(*group);
165 select_rect->property_x1() = event_x;
166 select_rect->property_y1() = event_y;
167 select_rect->property_x2() = event_x;
168 select_rect->property_y2() = event_y;
169 select_rect->property_outline_color_rgba() = 0xFF000099;
170 select_rect->property_fill_color_rgba() = 0xFFDDDD33;
177 case Dragging: // Select rect motion
178 if (ev->motion.is_hint) {
181 GdkModifierType state;
182 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
188 select_rect->property_x2() = event_x;
189 select_rect->property_y2() = event_y;
202 case GDK_BUTTON_RELEASE:
203 group->ungrab(ev->button.time);
205 case Pressed: // Clicked
206 if (ev->button.button == 1)
207 create_note_at(ev->button.x, ev->button.y);
210 case Dragging: // Select rect done
227 /** Add a note to the model, and the view, at a canvas (click) coordinate */
229 MidiRegionView::create_note_at(double x, double y)
231 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
232 MidiStreamView* const view = mtv->midi_view();
234 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
235 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
236 const double roll_height = trackview.height - footer_height;
238 get_canvas_group()->w2i(x, y);
240 double note = floor((roll_height - y) / roll_height * (double)note_range) + view->lowest_note();
242 assert(note <= 127.0);
244 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
246 //assert(stamp <= _region->length());
248 const Meter& m = trackview.session().tempo_map().meter_at(stamp);
249 const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
250 double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
252 // Add a 1 beat long note (for now)
253 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
255 view->update_bounds(new_note.note());
257 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
259 _model->apply_command(cmd);
261 //add_note(new_note);
266 MidiRegionView::clear_events()
268 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
276 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
286 MidiRegionView::redisplay_model()
293 for (size_t i=0; i < _model->n_notes(); ++i)
294 add_note(_model->note_at(i));
298 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
303 MidiRegionView::~MidiRegionView ()
305 in_destructor = true;
308 RegionViewGoingAway (this); /* EMIT_SIGNAL */
311 boost::shared_ptr<ARDOUR::MidiRegion>
312 MidiRegionView::midi_region() const
314 // "Guaranteed" to succeed...
315 return boost::dynamic_pointer_cast<MidiRegion>(_region);
319 MidiRegionView::region_resized (Change what_changed)
321 RegionView::region_resized(what_changed);
323 if (what_changed & ARDOUR::PositionChanged) {
328 } else if (what_changed & Change (StartChanged)) {
330 //cerr << "MIDI RV START CHANGED" << endl;
332 } else if (what_changed & Change (LengthChanged)) {
334 //cerr << "MIDI RV LENGTH CHANGED" << endl;
340 MidiRegionView::reset_width_dependent_items (double pixel_width)
342 RegionView::reset_width_dependent_items(pixel_width);
343 assert(_pixel_width == pixel_width);
350 MidiRegionView::set_y_position_and_height (double y, double h)
352 RegionView::set_y_position_and_height(y, h - 1);
358 name_text->raise_to_top();
363 MidiRegionView::show_region_editor ()
365 cerr << "No MIDI region editor." << endl;
369 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
371 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
374 double unit_position = _region->position () / samples_per_unit;
375 GhostRegion* ghost = new GhostRegion (atv, unit_position);
377 ghost->set_height ();
378 ghost->set_duration (_region->length() / samples_per_unit);
379 ghosts.push_back (ghost);
381 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
387 /** Begin tracking note state for successive calls to add_event
390 MidiRegionView::begin_write()
392 _active_notes = new CanvasNote*[128];
393 for (unsigned i=0; i < 128; ++i)
394 _active_notes[i] = NULL;
398 /** Destroy note state for add_event
401 MidiRegionView::end_write()
403 delete[] _active_notes;
404 _active_notes = NULL;
408 /** Add a MIDI event.
410 * This is used while recording, and handles displaying still-unresolved notes.
411 * Displaying an existing model is simpler, and done with add_note.
414 MidiRegionView::add_event (const MidiEvent& ev)
416 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
417 for (size_t i=0; i < ev.size; ++i) {
418 printf("%X ", ev.buffer[i]);
422 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
423 MidiStreamView* const view = mtv->midi_view();
424 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
426 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
427 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
428 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
430 if (mtv->note_mode() == Sustained) {
431 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
432 const Byte& note = ev.buffer()[1];
433 const double y1 = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
434 - footer_height - 3.0;
436 CanvasNote* ev_rect = new CanvasNote(*this, *group);
437 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
438 (nframes_t)ev.time());
439 ev_rect->property_y1() = y1;
440 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
442 ev_rect->property_y2() = y1 + ceil(pixel_range);
443 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
444 /* outline all but right edge */
445 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
446 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
448 ev_rect->raise_to_top();
450 _events.push_back(ev_rect);
452 _active_notes[note] = ev_rect;
454 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
455 const Byte& note = ev.buffer()[1];
456 if (_active_notes && _active_notes[note]) {
457 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
458 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
459 _active_notes[note] = NULL;
463 } else if (mtv->note_mode() == Percussive) {
464 const Byte& note = ev.buffer()[1];
465 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
466 const double y = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
467 - footer_height - 3.0;
469 CanvasHit* ev_diamond = new CanvasHit(*this, *group, std::min(pixel_range, 5.0));
470 ev_diamond->move(x, y);
472 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
473 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
475 _events.push_back(ev_diamond);
480 /** Extend active notes to rightmost edge of region (if length is changed)
483 MidiRegionView::extend_active_notes()
488 for (unsigned i=0; i < 128; ++i)
489 if (_active_notes[i])
490 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
494 /** Add a MIDI note to the view (with duration).
496 * This does no 'realtime' note resolution, notes from a MidiModel have a
497 * duration so they can be drawn in full immediately.
500 MidiRegionView::add_note (const MidiModel::Note& note)
502 assert(note.time() >= 0);
503 assert(note.time() < _region->length());
504 //assert(note.time() + note.duration < _region->length());
506 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
507 MidiStreamView* const view = mtv->midi_view();
508 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
510 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
511 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
512 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
513 const uint8_t fill_alpha = 0x20 + (uint8_t)(note.velocity() * 1.5);
514 const uint32_t fill = RGBA_TO_UINT(0xE0 + note.velocity()/127.0 * 0x10, 0xE0, 0xE0, fill_alpha);
515 const uint8_t outline_alpha = 0x80 + (uint8_t)(note.velocity());
516 const uint32_t outline = RGBA_TO_UINT(0xE0 + note.velocity()/127.0 * 0x10, 0xE0, 0xE0, outline_alpha);
518 //printf("Range: %d\n", note_range);
519 //printf("Event, time = %f, note = %d\n", note.time(), note.note());
522 if (mtv->note_mode() == Sustained) {
523 const double y1 = trackview.height - (pixel_range * (note.note() - view->lowest_note() + 1))
524 - footer_height - 3.0;
526 ArdourCanvas::SimpleRect * ev_rect = new CanvasNote(*this, *group, ¬e);
527 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
528 ev_rect->property_y1() = y1;
529 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
530 ev_rect->property_y2() = y1 + ceil(pixel_range);
532 ev_rect->property_fill_color_rgba() = fill;
533 ev_rect->property_outline_color_rgba() = outline;
534 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
537 _events.push_back(ev_rect);
539 } else if (mtv->note_mode() == Percussive) {
540 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
541 const double y = trackview.height - (pixel_range * (note.note() - view->lowest_note() + 1))
542 - footer_height - 3.0;
544 CanvasHit* ev_diamond = new CanvasHit(*this, *group, std::min(pixel_range, 5.0), ¬e);
545 ev_diamond->move(x, y);
547 ev_diamond->property_fill_color_rgba() = fill;
548 ev_diamond->property_outline_color_rgba() = outline;
549 _events.push_back(ev_diamond);