2 * Copyright (C) 2005-2018 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
4 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
5 * Copyright (C) 2007 Doug McLain <doug@nostar.net>
6 * Copyright (C) 2008-2014 David Robillard <d@drobilla.net>
7 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
8 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
9 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
10 * Copyright (C) 2016-2017 Nick Mainsbridge <mainsbridge@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "evoral/Note.h"
29 #include "ardour/parameter_descriptor.h"
31 #include "canvas/container.h"
32 #include "canvas/polygon.h"
33 #include "canvas/rectangle.h"
34 #include "canvas/debug.h"
36 #include "waveview/wave_view.h"
38 #include "automation_time_axis.h"
39 #include "ghostregion.h"
40 #include "midi_streamview.h"
41 #include "midi_time_axis.h"
42 #include "region_view.h"
43 #include "midi_region_view.h"
44 #include "rgb_macros.h"
47 #include "ui_config.h"
50 using namespace Editing;
51 using namespace ARDOUR;
52 using ArdourCanvas::Duple;
54 GhostRegion::GhostRegion (RegionView& rv,
55 ArdourCanvas::Container* parent,
57 TimeAxisView& source_tv,
61 , source_trackview(source_tv)
63 group = new ArdourCanvas::Container (parent);
64 CANVAS_DEBUG_NAME (group, "ghost region");
65 group->set_position (ArdourCanvas::Duple (initial_pos, 0));
67 base_rect = new ArdourCanvas::Rectangle (group);
68 CANVAS_DEBUG_NAME (base_rect, "ghost region rect");
69 base_rect->set_x0 (0);
70 base_rect->set_y0 (1.0);
71 base_rect->set_y1 (trackview.current_height());
72 base_rect->set_outline (false);
74 if (!is_automation_ghost()) {
78 GhostRegion::set_colors();
80 /* the parent group of a ghostregion is a dedicated group for ghosts,
81 so the new ghost would want to get to the top of that group*/
82 group->raise_to_top ();
85 GhostRegion::~GhostRegion ()
87 parent_rv.remove_ghost(this);
88 trackview.erase_ghost(this);
94 GhostRegion::set_duration (double units)
96 base_rect->set_x1 (units);
100 GhostRegion::set_height ()
102 base_rect->set_y1 (trackview.current_height());
106 GhostRegion::set_colors ()
108 if (is_automation_ghost()) {
109 base_rect->set_fill_color (UIConfiguration::instance().color_mod ("ghost track base", "ghost track base"));
114 GhostRegion::source_track_color(unsigned char alpha)
116 Gdk::Color color = source_trackview.color();
117 return RGBA_TO_UINT (color.get_red() / 256, color.get_green() / 256, color.get_blue() / 256, alpha);
121 GhostRegion::is_automation_ghost()
123 return (dynamic_cast<AutomationTimeAxisView*>(&trackview)) != 0;
126 AudioGhostRegion::AudioGhostRegion (RegionView& rv,
128 TimeAxisView& source_tv,
129 double initial_unit_pos)
130 : GhostRegion(rv, tv.ghost_group(), tv, source_tv, initial_unit_pos)
136 AudioGhostRegion::set_samples_per_pixel (double fpp)
138 for (vector<ArdourWaveView::WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
139 (*i)->set_samples_per_pixel (fpp);
144 AudioGhostRegion::set_height ()
146 vector<ArdourWaveView::WaveView*>::iterator i;
149 GhostRegion::set_height();
151 double const ht = ((trackview.current_height()) / (double) waves.size());
153 for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
154 (*i)->set_height (ht);
155 (*i)->set_y_position (n * ht);
160 AudioGhostRegion::set_colors ()
162 GhostRegion::set_colors();
165 if (is_automation_ghost()) {
166 fill_color = UIConfiguration::instance().color ("ghost track wave fill");
169 fill_color = source_track_color(200);
172 for (uint32_t n=0; n < waves.size(); ++n) {
173 waves[n]->set_outline_color (UIConfiguration::instance().color ("ghost track wave"));
174 waves[n]->set_fill_color (fill_color);
175 waves[n]->set_clip_color (UIConfiguration::instance().color ("ghost track wave clip"));
176 waves[n]->set_zero_color (UIConfiguration::instance().color ("ghost track zero line"));
180 /** The general constructor; called when the destination timeaxisview doesn't have
183 * @param rv The parent RegionView that is being ghosted.
184 * @param tv TimeAxisView that this ghost region is on.
185 * @param source_tv TimeAxisView that we are the ghost for.
187 MidiGhostRegion::MidiGhostRegion(MidiRegionView& rv,
189 TimeAxisView& source_tv,
190 double initial_unit_pos)
191 : GhostRegion(rv, tv.ghost_group(), tv, source_tv, initial_unit_pos)
192 , _note_group (new ArdourCanvas::Container (group))
194 , _optimization_iterator(events.end())
196 _outline = UIConfiguration::instance().color ("ghost track midi outline");
198 base_rect->lower_to_bottom();
202 * @param rv The parent RegionView being ghosted.
203 * @param msv MidiStreamView that this ghost region is on.
204 * @param source_tv TimeAxisView that we are the ghost for.
206 MidiGhostRegion::MidiGhostRegion(MidiRegionView& rv,
208 TimeAxisView& source_tv,
209 double initial_unit_pos)
211 msv.midi_underlay_group,
215 , _note_group (new ArdourCanvas::Container (group))
217 , _optimization_iterator(events.end())
219 _outline = UIConfiguration::instance().color ("ghost track midi outline");
221 base_rect->lower_to_bottom();
224 MidiGhostRegion::~MidiGhostRegion()
230 MidiGhostRegion::GhostEvent::GhostEvent (NoteBase* e, ArdourCanvas::Container* g)
234 if (dynamic_cast<Note*>(e)) {
235 item = new ArdourCanvas::Rectangle(
236 g, ArdourCanvas::Rect(e->x0(), e->y0(), e->x1(), e->y1()));
239 Hit* hit = dynamic_cast<Hit*>(e);
243 ArdourCanvas::Polygon* poly = new ArdourCanvas::Polygon(g);
244 poly->set(Hit::points(e->y1() - e->y0()));
245 poly->set_position(hit->position());
250 CANVAS_DEBUG_NAME (item, "ghost note item");
253 MidiGhostRegion::GhostEvent::~GhostEvent ()
255 /* event is not ours to delete */
260 MidiGhostRegion::set_samples_per_pixel (double /*spu*/)
264 /** @return MidiStreamView that we are providing a ghost for */
266 MidiGhostRegion::midi_view ()
268 StreamView* sv = source_trackview.view ();
270 MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
277 MidiGhostRegion::set_height ()
279 GhostRegion::set_height();
280 update_contents_height ();
284 MidiGhostRegion::set_colors()
286 GhostRegion::set_colors();
287 _outline = UIConfiguration::instance().color ("ghost track midi outline");
289 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
290 it->second->item->set_fill_color (UIConfiguration::instance().color_mod((*it).second->event->base_color(), "ghost track midi fill"));
291 it->second->item->set_outline_color (_outline);
296 note_height(TimeAxisView& trackview, MidiStreamView* mv)
298 const double tv_height = trackview.current_height();
299 const double note_range = mv->contents_note_range();
301 return std::max(1.0, floor(tv_height / note_range - 1.0));
305 note_y(TimeAxisView& trackview, MidiStreamView* mv, uint8_t note_num)
307 const double tv_height = trackview.current_height();
308 const double note_range = mv->contents_note_range();
309 const double s = tv_height / note_range;
311 return tv_height - (note_num + 1 - mv->lowest_note()) * s;
315 MidiGhostRegion::update_contents_height ()
317 MidiStreamView* mv = midi_view();
323 double const h = note_height(trackview, mv);
325 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
326 uint8_t const note_num = it->second->event->note()->note();
328 double const y = note_y(trackview, mv, note_num);
330 if (!it->second->is_hit) {
331 _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(it->second->item);
332 _tmp_rect->set (ArdourCanvas::Rect (_tmp_rect->x0(), y, _tmp_rect->x1(), y + h));
334 _tmp_poly = static_cast<ArdourCanvas::Polygon*>(it->second->item);
335 ArdourCanvas::Duple position = _tmp_poly->position();
337 _tmp_poly->set_position(position);
338 _tmp_poly->set(Hit::points(h));
344 MidiGhostRegion::add_note (NoteBase* n)
346 GhostEvent* event = new GhostEvent (n, _note_group);
347 events.insert (make_pair (n->note(), event));
349 event->item->set_fill_color (UIConfiguration::instance().color_mod(n->base_color(), "ghost track midi fill"));
350 event->item->set_outline_color (_outline);
352 MidiStreamView* mv = midi_view();
356 if (!n->item()->visible()) {
359 uint8_t const note_num = n->note()->note();
360 double const h = note_height(trackview, mv);
361 double const y = note_y(trackview, mv, note_num);
362 if (!event->is_hit) {
363 _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(event->item);
364 _tmp_rect->set (ArdourCanvas::Rect (_tmp_rect->x0(), y, _tmp_rect->x1(), y + h));
366 _tmp_poly = static_cast<ArdourCanvas::Polygon*>(event->item);
367 Duple position = _tmp_poly->position();
369 _tmp_poly->set_position(position);
370 _tmp_poly->set(Hit::points(h));
377 MidiGhostRegion::clear_events()
379 _note_group->clear (true);
381 _optimization_iterator = events.end();
384 /** Update the positions of our representation of a note.
385 * @param ev The GhostEvent from the parent MidiRegionView.
388 MidiGhostRegion::update_note (GhostEvent* ev)
390 MidiStreamView* mv = midi_view();
396 _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(ev->item);
398 uint8_t const note_num = ev->event->note()->note();
399 double const y = note_y(trackview, mv, note_num);
400 double const h = note_height(trackview, mv);
402 _tmp_rect->set (ArdourCanvas::Rect (ev->event->x0(), y, ev->event->x1(), y + h));
405 /** Update the positions of our representation of a parent's hit.
406 * @param ev The GhostEvent from the parent MidiRegionView.
409 MidiGhostRegion::update_hit (GhostEvent* ev)
411 MidiStreamView* mv = midi_view();
417 _tmp_poly = static_cast<ArdourCanvas::Polygon*>(ev->item);
419 uint8_t const note_num = ev->event->note()->note();
420 double const h = note_height(trackview, mv);
421 double const y = note_y(trackview, mv, note_num);
423 ArdourCanvas::Duple ppos = ev->item->position();
424 ArdourCanvas::Duple gpos = _tmp_poly->position();
428 _tmp_poly->set_position(gpos);
429 _tmp_poly->set(Hit::points(h));
433 MidiGhostRegion::remove_note (NoteBase* note)
435 EventList::iterator f = events.find (note->note());
436 if (f == events.end()) {
443 _optimization_iterator = events.end ();
446 MidiGhostRegion::redisplay_model ()
448 /* we rely on the parent MRV having removed notes not in the model */
449 for (EventList::iterator i = events.begin(); i != events.end(); ) {
451 boost::shared_ptr<NoteType> note = i->first;
452 GhostEvent* cne = i->second;
453 const bool visible = (note->note() >= parent_mrv._current_range_min) &&
454 (note->note() <= parent_mrv._current_range_max);
471 /** Given a note in our parent region (ie the actual MidiRegionView), find our
472 * representation of it.
473 * @return Our Event, or 0 if not found.
475 MidiGhostRegion::GhostEvent *
476 MidiGhostRegion::find_event (boost::shared_ptr<NoteType> parent)
478 /* we are using _optimization_iterator to speed up the common case where a caller
479 is going through our notes in order.
482 if (_optimization_iterator != events.end()) {
483 ++_optimization_iterator;
484 if (_optimization_iterator != events.end() && _optimization_iterator->first == parent) {
485 return _optimization_iterator->second;
489 _optimization_iterator = events.find (parent);
490 if (_optimization_iterator != events.end()) {
491 return _optimization_iterator->second;