2 Copyright (C) 2001-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gtkmm2ext/gtk_ui.h>
27 #include <ardour/playlist.h>
28 #include <ardour/tempo.h>
29 #include <ardour/midi_region.h>
30 #include <ardour/midi_source.h>
31 #include <ardour/midi_diskstream.h>
32 #include <ardour/midi_events.h>
33 #include <ardour/midi_model.h>
35 #include "streamview.h"
36 #include "midi_region_view.h"
37 #include "midi_streamview.h"
38 #include "midi_time_axis.h"
39 #include "simplerect.h"
40 #include "simpleline.h"
42 #include "public_editor.h"
43 #include "ghostregion.h"
44 #include "midi_time_axis.h"
46 #include "rgb_macros.h"
47 #include "gui_thread.h"
52 using namespace ARDOUR;
54 using namespace Editing;
55 using namespace ArdourCanvas;
57 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
58 Gdk::Color& basic_color)
59 : RegionView (parent, tv, r, spu, basic_color)
64 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
65 Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
66 : RegionView (parent, tv, r, spu, basic_color, visibility)
72 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
74 // FIXME: Some redundancy here with RegionView::init. Need to figure out
75 // where order is important and where it isn't...
78 RegionView::init(basic_color, /*wfd*/false);
80 compute_colors (basic_color);
82 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
84 set_y_position_and_height (0, trackview.height);
87 region_resized (BoundsChanged);
90 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
95 midi_region()->midi_source(0)->load_model();
99 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event));
103 MidiRegionView::canvas_event(GdkEvent* ev)
105 if (trackview.editor.current_mouse_mode() == MouseNote) {
106 if (ev->type == GDK_BUTTON_PRESS) {
107 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
108 MidiStreamView* const view = mtv->midi_view();
110 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
111 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
112 const double roll_height = trackview.height - footer_height;
114 double x = ev->button.x;
115 double y = ev->button.y;
116 get_canvas_group()->w2i(x, y);
118 double note = floor((roll_height - y) / roll_height * (double)note_range) + view->lowest_note();
120 assert(note <= 127.0);
122 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
124 //assert(stamp <= _region->length());
126 const Meter& m = trackview.session().tempo_map().meter_at(stamp);
127 const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
128 double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
130 // Add a 1 beat long note (for now)
131 const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
133 MidiModel::Notes& notes = midi_region()->midi_source(0)->model()->notes();
134 MidiModel::Notes::iterator i = upper_bound(notes.begin(), notes.end(), new_note,
135 MidiModel::NoteTimeComparator());
136 notes.insert(i, new_note);
137 view->update_bounds(new_note.note);
148 MidiRegionView::clear_events()
150 for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
158 MidiRegionView::display_events()
164 for (size_t i=0; i < midi_region()->midi_source(0)->model()->n_notes(); ++i)
165 add_note(midi_region()->midi_source(0)->model()->note_at(i));
171 MidiRegionView::~MidiRegionView ()
173 in_destructor = true;
176 RegionViewGoingAway (this); /* EMIT_SIGNAL */
179 boost::shared_ptr<ARDOUR::MidiRegion>
180 MidiRegionView::midi_region() const
182 // "Guaranteed" to succeed...
183 return boost::dynamic_pointer_cast<MidiRegion>(_region);
187 MidiRegionView::region_resized (Change what_changed)
189 RegionView::region_resized(what_changed);
191 if (what_changed & ARDOUR::PositionChanged) {
195 } else if (what_changed & Change (StartChanged)) {
197 //cerr << "MIDI RV START CHANGED" << endl;
199 } else if (what_changed & Change (LengthChanged)) {
201 //cerr << "MIDI RV LENGTH CHANGED" << endl;
207 MidiRegionView::reset_width_dependent_items (double pixel_width)
209 RegionView::reset_width_dependent_items(pixel_width);
210 assert(_pixel_width == pixel_width);
216 MidiRegionView::set_y_position_and_height (double y, double h)
218 RegionView::set_y_position_and_height(y, h - 1);
223 name_text->raise_to_top();
228 MidiRegionView::show_region_editor ()
230 cerr << "No MIDI region editor." << endl;
234 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
236 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
239 double unit_position = _region->position () / samples_per_unit;
240 GhostRegion* ghost = new GhostRegion (atv, unit_position);
242 ghost->set_height ();
243 ghost->set_duration (_region->length() / samples_per_unit);
244 ghosts.push_back (ghost);
246 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
252 /** Begin tracking note state for successive calls to add_event
255 MidiRegionView::begin_write()
257 _active_notes = new ArdourCanvas::SimpleRect*[128];
258 for (unsigned i=0; i < 128; ++i)
259 _active_notes[i] = NULL;
263 /** Destroy note state for add_event
266 MidiRegionView::end_write()
268 delete[] _active_notes;
269 _active_notes = NULL;
273 /** Add a MIDI event.
275 * This is used while recording, and handles displaying still-unresolved notes.
276 * Displaying an existing model is simpler, and done with add_note.
279 MidiRegionView::add_event (const MidiEvent& ev)
281 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
282 for (size_t i=0; i < ev.size; ++i) {
283 printf("%X ", ev.buffer[i]);
287 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
288 MidiStreamView* const view = mtv->midi_view();
289 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
291 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
292 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
293 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
295 if (mtv->note_mode() == Note) {
296 if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
297 const Byte& note = ev.buffer[1];
298 const double y1 = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
299 - footer_height - 3.0;
301 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
302 ev_rect->property_x1() = trackview.editor.frame_to_pixel (
304 ev_rect->property_y1() = y1;
305 ev_rect->property_x2() = trackview.editor.frame_to_pixel (
307 ev_rect->property_y2() = y1 + ceil(pixel_range);
308 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
309 /* outline all but right edge */
310 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
311 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
313 _events.push_back(ev_rect);
315 _active_notes[note] = ev_rect;
317 } else if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
318 const Byte& note = ev.buffer[1];
319 if (_active_notes && _active_notes[note]) {
320 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time);
321 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
322 _active_notes[note] = NULL;
326 } else if (mtv->note_mode() == Percussion) {
327 const Byte& note = ev.buffer[1];
328 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time);
329 const double y = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
330 - footer_height - 3.0;
332 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
333 ev_diamond->move(x, y);
335 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
336 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
337 _events.push_back(ev_diamond);
342 /** Extend active notes to rightmost edge of region (if length is changed)
345 MidiRegionView::extend_active_notes()
350 for (unsigned i=0; i < 128; ++i)
351 if (_active_notes[i])
352 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
356 /** Add a MIDI note (with duration).
358 * This does no 'realtime' note resolution, notes from a MidiModel have a
359 * duration so they can be drawn in full immediately.
362 MidiRegionView::add_note (const MidiModel::Note& note)
364 assert(note.start >= 0);
365 assert(note.start < _region->length());
366 //assert(note.start + note.duration < _region->length());
368 /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
369 for (size_t i=0; i < ev.size; ++i) {
370 printf("%X ", ev.buffer[i]);
374 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
375 MidiStreamView* const view = mtv->midi_view();
376 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
378 const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
379 const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
380 const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
382 if (mtv->note_mode() == Note) {
383 const double y1 = trackview.height - (pixel_range * (note.note - view->lowest_note() + 1))
384 - footer_height - 3.0;
386 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
387 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.start);
388 ev_rect->property_y1() = y1;
389 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.start + note.duration));
390 ev_rect->property_y2() = y1 + ceil(pixel_range);
392 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
393 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
394 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
397 _events.push_back(ev_rect);
399 } else if (mtv->note_mode() == Percussion) {
400 const double x = trackview.editor.frame_to_pixel((nframes_t)note.start);
401 const double y = trackview.height - (pixel_range * (note.note - view->lowest_note() + 1))
402 - footer_height - 3.0;
404 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
405 ev_diamond->move(x, y);
407 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
408 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
409 _events.push_back(ev_diamond);