2 * Copyright (C) 2010-2011 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2010-2018 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2011-2015 David Robillard <d@drobilla.net>
5 * Copyright (C) 2013-2018 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pbd/stacktrace.h"
25 #include "ardour/midi_track.h"
26 #include "ardour/midi_region.h"
27 #include "ardour/tempo.h"
28 #include "ardour/types.h"
30 #include "gui_thread.h"
31 #include "midi_region_view.h"
32 #include "public_editor.h"
33 #include "step_editor.h"
34 #include "step_entry.h"
36 using namespace ARDOUR;
40 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
45 step_edit_insert_position = 0;
46 _step_edit_triplet_countdown = 0;
47 _step_edit_within_chord = 0;
48 _step_edit_chord_duration = Temporal::Beats();
49 step_edit_region_view = 0;
51 _track->PlaylistChanged.connect (*this, invalidator (*this),
52 boost::bind (&StepEditor::playlist_changed, this),
57 StepEditor::~StepEditor()
59 StepEntry::instance().set_step_editor (0);
63 StepEditor::start_step_editing ()
65 _step_edit_triplet_countdown = 0;
66 _step_edit_within_chord = 0;
67 _step_edit_chord_duration = Temporal::Beats();
68 step_edit_region.reset ();
69 step_edit_region_view = 0;
70 last_added_pitch = -1;
71 last_added_end = Temporal::Beats();
73 resync_step_edit_position ();
74 prepare_step_edit_region ();
75 reset_step_edit_beat_pos ();
77 assert (step_edit_region);
78 assert (step_edit_region_view);
80 StepEntry::instance().set_step_editor (this);
81 delete_connection = StepEntry::instance().signal_delete_event().connect (sigc::mem_fun (*this, &StepEditor::step_entry_hidden));
82 hide_connection = StepEntry::instance(). signal_hide().connect (sigc::mem_fun (*this, &StepEditor::step_entry_done));
84 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
85 step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
87 StepEntry::instance().present ();
91 StepEditor::resync_step_edit_position ()
93 step_edit_insert_position = _editor.get_preferred_edit_position (Editing::EDIT_IGNORE_NONE, false, true);
97 StepEditor::resync_step_edit_to_edit_point ()
99 resync_step_edit_position ();
100 if (step_edit_region) {
101 reset_step_edit_beat_pos ();
106 StepEditor::prepare_step_edit_region ()
108 boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
111 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
114 if (step_edit_region) {
115 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
116 step_edit_region_view = dynamic_cast<MidiRegionView*> (rv);
120 const Meter& m = _mtv.session()->tempo_map().meter_at_sample (step_edit_insert_position);
121 double baf = max (0.0, _mtv.session()->tempo_map().beat_at_sample (step_edit_insert_position));
122 double next_bar_in_beats = baf + m.divisions_per_bar();
123 samplecnt_t next_bar_pos = _mtv.session()->tempo_map().sample_at_beat (next_bar_in_beats);
124 samplecnt_t len = next_bar_pos - step_edit_insert_position;
126 step_edit_region = _mtv.add_region (step_edit_insert_position, len, true);
128 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
129 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
135 StepEditor::reset_step_edit_beat_pos ()
137 assert (step_edit_region);
138 assert (step_edit_region_view);
140 samplecnt_t samples_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
142 if (samples_from_start < 0) {
143 /* this can happen with snap enabled, and the edit point == Playhead. we snap the
144 position of the new region, and it can end up after the edit point.
146 samples_from_start = 0;
149 step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (samples_from_start);
150 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
154 StepEditor::step_entry_hidden (GdkEventAny*)
161 StepEditor::step_entry_done ()
163 hide_connection.disconnect ();
164 delete_connection.disconnect ();
166 /* everything else will follow the change in the model */
167 _track->set_step_editing (false);
171 StepEditor::stop_step_editing ()
173 StepEntry::instance().hide ();
175 if (step_edit_region_view) {
176 step_edit_region_view->hide_step_edit_cursor();
179 step_edit_region.reset ();
183 StepEditor::check_step_edit ()
185 MidiRingBuffer<samplepos_t>& incoming (_track->step_edit_ring_buffer());
187 uint32_t bufsize = 32;
189 buf = new uint8_t[bufsize];
191 while (incoming.read_space()) {
193 Evoral::EventType type;
196 if (!incoming.read_prefix (&time, &type, &size)) {
200 if (size > bufsize) {
203 buf = new uint8_t[bufsize];
206 if (!incoming.read_contents (size, buf)) {
210 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON && size == 3) {
211 step_add_note (buf[0] & 0xf, buf[1], buf[2], Temporal::Beats());
218 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
224 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
230 StepEditor::step_edit_sustain (Temporal::Beats beats)
232 if (step_edit_region_view) {
233 step_edit_region_view->step_sustain (beats);
238 StepEditor::move_step_edit_beat_pos (Temporal::Beats beats)
240 if (!step_edit_region_view) {
244 step_edit_beat_pos = min (step_edit_beat_pos + beats,
245 step_edit_region_view->region_samples_to_region_beats (step_edit_region->length()));
246 } else if (beats < 0.0) {
247 if (-beats < step_edit_beat_pos) {
248 step_edit_beat_pos += beats; // its negative, remember
250 step_edit_beat_pos = Temporal::Beats();
253 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
257 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Temporal::Beats beat_duration)
259 /* do these things in case undo removed the step edit region
261 if (!step_edit_region) {
262 resync_step_edit_position ();
263 prepare_step_edit_region ();
264 reset_step_edit_beat_pos ();
265 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
266 step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
269 assert (step_edit_region);
270 assert (step_edit_region_view);
272 if (beat_duration == 0.0) {
273 beat_duration = StepEntry::instance().note_length();
274 } else if (beat_duration == 0.0) {
276 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
283 MidiStreamView* msv = _mtv.midi_view();
285 /* make sure its visible on the vertical axis */
287 if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
288 msv->update_note_range (pitch);
289 msv->set_note_range (MidiStreamView::ContentsRange);
292 /* make sure its visible on the horizontal axis */
294 samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos + beat_duration);
296 if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
297 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
300 Temporal::Beats at = step_edit_beat_pos;
301 Temporal::Beats len = beat_duration;
303 if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
305 /* avoid any apparent note overlap - move the start of this note
306 up by 1 tick from where the last note ended
309 at += Temporal::Beats::ticks(1);
310 len -= Temporal::Beats::ticks(1);
313 step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
315 last_added_pitch = pitch;
316 last_added_end = at+len;
318 if (_step_edit_triplet_countdown > 0) {
319 _step_edit_triplet_countdown--;
321 if (_step_edit_triplet_countdown == 0) {
322 _step_edit_triplet_countdown = 3;
326 if (!_step_edit_within_chord) {
327 step_edit_beat_pos += beat_duration;
328 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
330 step_edit_beat_pos += Temporal::Beats::ticks(1); // tiny, but no longer overlapping
331 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
334 step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
340 StepEditor::set_step_edit_cursor_width (Temporal::Beats beats)
342 if (step_edit_region_view) {
343 step_edit_region_view->set_step_edit_cursor_width (beats);
348 StepEditor::step_edit_within_triplet() const
350 return _step_edit_triplet_countdown > 0;
354 StepEditor::step_edit_within_chord() const
356 return _step_edit_within_chord;
360 StepEditor::step_edit_toggle_triplet ()
362 if (_step_edit_triplet_countdown == 0) {
363 _step_edit_within_chord = false;
364 _step_edit_triplet_countdown = 3;
366 _step_edit_triplet_countdown = 0;
371 StepEditor::step_edit_toggle_chord ()
373 if (_step_edit_within_chord) {
374 _step_edit_within_chord = false;
375 if (step_edit_region_view) {
376 step_edit_beat_pos += _step_edit_chord_duration;
377 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
380 _step_edit_triplet_countdown = 0;
381 _step_edit_within_chord = true;
386 StepEditor::step_edit_rest (Temporal::Beats beats)
391 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
396 if (success && step_edit_region_view) {
397 step_edit_beat_pos += beats;
398 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
403 StepEditor::step_edit_beat_sync ()
405 step_edit_beat_pos = step_edit_beat_pos.round_up_to_beat();
406 if (step_edit_region_view) {
407 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
412 StepEditor::step_edit_bar_sync ()
414 Session* _session = _mtv.session ();
416 if (!_session || !step_edit_region_view || !step_edit_region) {
420 samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos);
421 fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways).sample;
422 step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat();
423 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
427 StepEditor::playlist_changed ()
429 step_edit_region_connection.disconnect ();
430 _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
431 boost::bind (&StepEditor::region_removed, this, _1),
436 StepEditor::region_removed (boost::weak_ptr<Region> wr)
438 boost::shared_ptr<Region> r (wr.lock());
444 if (step_edit_region == r) {
445 step_edit_region.reset();
446 step_edit_region_view = 0;
447 // force a recompute of the insert position
448 step_edit_beat_pos = Temporal::Beats(-1);
453 StepEditor::name() const
455 return _track->name();