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 get_canvas_group()->w2i(x, y);
236 double note = floor((contents_height() - y) / contents_height() * (double)contents_note_range())
237 + view->lowest_note();
240 assert(note <= 127.0);
242 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
244 //assert(stamp <= _region->length());
246 const Meter& m = trackview.session().tempo_map().meter_at(stamp);
247 const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
248 double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
250 // Add a 1 beat long note (for now)
251 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
253 view->update_bounds(new_note.note());
255 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
257 _model->apply_command(cmd);
259 //add_note(new_note);
264 MidiRegionView::clear_events()
266 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
274 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
284 MidiRegionView::redisplay_model()
291 for (size_t i=0; i < _model->n_notes(); ++i)
292 add_note(_model->note_at(i));
296 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
301 MidiRegionView::~MidiRegionView ()
303 in_destructor = true;
306 RegionViewGoingAway (this); /* EMIT_SIGNAL */
311 MidiRegionView::region_resized (Change what_changed)
313 RegionView::region_resized(what_changed);
315 if (what_changed & ARDOUR::PositionChanged) {
320 } else if (what_changed & Change (StartChanged)) {
322 //cerr << "MIDI RV START CHANGED" << endl;
324 } else if (what_changed & Change (LengthChanged)) {
326 //cerr << "MIDI RV LENGTH CHANGED" << endl;
332 MidiRegionView::reset_width_dependent_items (double pixel_width)
334 RegionView::reset_width_dependent_items(pixel_width);
335 assert(_pixel_width == pixel_width);
342 MidiRegionView::set_y_position_and_height (double y, double h)
344 RegionView::set_y_position_and_height(y, h - 1);
350 name_text->raise_to_top();
355 MidiRegionView::show_region_editor ()
357 cerr << "No MIDI region editor." << endl;
361 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
363 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
366 double unit_position = _region->position () / samples_per_unit;
367 GhostRegion* ghost = new GhostRegion (atv, unit_position);
369 ghost->set_height ();
370 ghost->set_duration (_region->length() / samples_per_unit);
371 ghosts.push_back (ghost);
373 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
379 /** Begin tracking note state for successive calls to add_event
382 MidiRegionView::begin_write()
384 _active_notes = new CanvasNote*[128];
385 for (unsigned i=0; i < 128; ++i)
386 _active_notes[i] = NULL;
390 /** Destroy note state for add_event
393 MidiRegionView::end_write()
395 delete[] _active_notes;
396 _active_notes = NULL;
400 /** Add a MIDI event.
402 * This is used while recording, and handles displaying still-unresolved notes.
403 * Displaying an existing model is simpler, and done with add_note.
406 MidiRegionView::add_event (const MidiEvent& ev)
408 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
409 for (size_t i=0; i < ev.size; ++i) {
410 printf("%X ", ev.buffer[i]);
414 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
416 if (midi_view()->note_mode() == Sustained) {
417 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
418 const Byte& note = ev.buffer()[1];
419 const double y1 = note_to_y(note);
421 CanvasNote* ev_rect = new CanvasNote(*this, *group);
422 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
423 (nframes_t)ev.time());
424 ev_rect->property_y1() = y1;
425 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
427 ev_rect->property_y2() = y1 + note_height();
428 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
429 /* outline all but right edge */
430 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
431 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
433 ev_rect->raise_to_top();
435 _events.push_back(ev_rect);
437 _active_notes[note] = ev_rect;
439 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
440 const Byte& note = ev.buffer()[1];
441 if (_active_notes && _active_notes[note]) {
442 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
443 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
444 _active_notes[note] = NULL;
448 } else if (midi_view()->note_mode() == Percussive) {
449 const Byte& note = ev.buffer()[1];
450 const double diamond_size = std::min(note_height(), 5.0);
451 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
452 const double y = note_to_y(note) + (diamond_size / 2.0);
454 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
455 ev_diamond->move(x, y);
457 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
458 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
460 _events.push_back(ev_diamond);
465 /** Extend active notes to rightmost edge of region (if length is changed)
468 MidiRegionView::extend_active_notes()
473 for (unsigned i=0; i < 128; ++i)
474 if (_active_notes[i])
475 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
479 /** Add a MIDI note to the view (with duration).
481 * This does no 'realtime' note resolution, notes from a MidiModel have a
482 * duration so they can be drawn in full immediately.
485 MidiRegionView::add_note (const MidiModel::Note& note)
487 assert(note.time() >= 0);
488 assert(note.time() < _region->length());
489 //assert(note.time() + note.duration < _region->length());
491 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
492 MidiStreamView* const view = mtv->midi_view();
493 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
495 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
496 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
497 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
498 const uint8_t fill_alpha = 0x20 + (uint8_t)(note.velocity() * 1.5);
499 const uint32_t fill = RGBA_TO_UINT(0xE0 + note.velocity()/127.0 * 0x10, 0xE0, 0xE0, fill_alpha);
500 const uint8_t outline_alpha = 0x80 + (uint8_t)(note.velocity());
501 const uint32_t outline = RGBA_TO_UINT(0xE0 + note.velocity()/127.0 * 0x10, 0xE0, 0xE0, outline_alpha);
503 //printf("Range: %d\n", note_range);
504 //printf("Event, time = %f, note = %d\n", note.time(), note.note());
507 if (mtv->note_mode() == Sustained) {
508 const double y1 = trackview.height - (pixel_range * (note.note() - view->lowest_note() + 1))
509 - footer_height - 3.0;
511 ArdourCanvas::SimpleRect * ev_rect = new CanvasNote(*this, *group, ¬e);
512 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
513 ev_rect->property_y1() = y1;
514 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
515 ev_rect->property_y2() = y1 + ceil(pixel_range);
517 ev_rect->property_fill_color_rgba() = fill;
518 ev_rect->property_outline_color_rgba() = outline;
519 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
522 _events.push_back(ev_rect);
524 } else if (mtv->note_mode() == Percussive) {
525 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
526 const double y = trackview.height - (pixel_range * (note.note() - view->lowest_note() + 1))
527 - footer_height - 3.0;
529 CanvasHit* ev_diamond = new CanvasHit(*this, *group, std::min(pixel_range, 5.0), ¬e);
530 ev_diamond->move(x, y);
532 ev_diamond->property_fill_color_rgba() = fill;
533 ev_diamond->property_outline_color_rgba() = outline;
534 _events.push_back(ev_diamond);