2 Copyright (C) 2012 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.
20 #include "ardour/midi_track.h"
21 #include "ardour/midi_region.h"
22 #include "ardour/tempo.h"
23 #include "ardour/types.h"
25 #include "gui_thread.h"
26 #include "midi_region_view.h"
27 #include "public_editor.h"
28 #include "step_editor.h"
29 #include "step_entry.h"
31 using namespace ARDOUR;
35 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
41 step_edit_insert_position = 0;
42 _step_edit_triplet_countdown = 0;
43 _step_edit_within_chord = 0;
44 _step_edit_chord_duration = Temporal::Beats();
45 step_edit_region_view = 0;
47 _track->PlaylistChanged.connect (*this, invalidator (*this),
48 boost::bind (&StepEditor::playlist_changed, this),
53 StepEditor::~StepEditor()
59 StepEditor::start_step_editing ()
61 _step_edit_triplet_countdown = 0;
62 _step_edit_within_chord = 0;
63 _step_edit_chord_duration = Temporal::Beats();
64 step_edit_region.reset ();
65 step_edit_region_view = 0;
66 last_added_pitch = -1;
67 last_added_end = Temporal::Beats();
69 resync_step_edit_position ();
70 prepare_step_edit_region ();
71 reset_step_edit_beat_pos ();
73 assert (step_edit_region);
74 assert (step_edit_region_view);
76 if (step_editor == 0) {
77 step_editor = new StepEntry (*this);
78 step_editor->signal_delete_event().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hidden));
79 step_editor->signal_hide().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hide));
82 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
83 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
85 step_editor->present ();
89 StepEditor::resync_step_edit_position ()
91 step_edit_insert_position = _editor.get_preferred_edit_position (Editing::EDIT_IGNORE_NONE, false, true);
95 StepEditor::resync_step_edit_to_edit_point ()
97 resync_step_edit_position ();
98 if (step_edit_region) {
99 reset_step_edit_beat_pos ();
104 StepEditor::prepare_step_edit_region ()
106 boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
109 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
112 if (step_edit_region) {
113 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
114 step_edit_region_view = dynamic_cast<MidiRegionView*> (rv);
118 const Meter& m = _mtv.session()->tempo_map().meter_at_sample (step_edit_insert_position);
119 double baf = max (0.0, _mtv.session()->tempo_map().beat_at_sample (step_edit_insert_position));
120 double next_bar_in_beats = baf + m.divisions_per_bar();
121 samplecnt_t next_bar_pos = _mtv.session()->tempo_map().sample_at_beat (next_bar_in_beats);
122 samplecnt_t len = next_bar_pos - step_edit_insert_position;
124 step_edit_region = _mtv.add_region (step_edit_insert_position, len, true);
126 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
127 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
133 StepEditor::reset_step_edit_beat_pos ()
135 assert (step_edit_region);
136 assert (step_edit_region_view);
138 samplecnt_t samples_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
140 if (samples_from_start < 0) {
141 /* this can happen with snap enabled, and the edit point == Playhead. we snap the
142 position of the new region, and it can end up after the edit point.
144 samples_from_start = 0;
147 step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (samples_from_start);
148 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
152 StepEditor::step_editor_hidden (GdkEventAny*)
155 return true; // XXX remember position ?!
159 StepEditor::step_editor_hide ()
161 /* everything else will follow the change in the model */
162 _track->set_step_editing (false);
166 StepEditor::stop_step_editing ()
169 step_editor->hide ();
172 if (step_edit_region_view) {
173 step_edit_region_view->hide_step_edit_cursor();
176 step_edit_region.reset ();
180 StepEditor::check_step_edit ()
182 MidiRingBuffer<samplepos_t>& incoming (_track->step_edit_ring_buffer());
184 uint32_t bufsize = 32;
186 buf = new uint8_t[bufsize];
188 while (incoming.read_space()) {
190 Evoral::EventType type;
193 incoming.read_prefix (&time, &type, &size);
195 if (size > bufsize) {
198 buf = new uint8_t[bufsize];
201 incoming.read_contents (size, buf);
203 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
204 step_add_note (buf[0] & 0xf, buf[1], buf[2], Temporal::Beats());
211 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
217 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
223 StepEditor::step_edit_sustain (Temporal::Beats beats)
225 if (step_edit_region_view) {
226 step_edit_region_view->step_sustain (beats);
231 StepEditor::move_step_edit_beat_pos (Temporal::Beats beats)
233 if (!step_edit_region_view) {
237 step_edit_beat_pos = min (step_edit_beat_pos + beats,
238 step_edit_region_view->region_samples_to_region_beats (step_edit_region->length()));
239 } else if (beats < 0.0) {
240 if (-beats < step_edit_beat_pos) {
241 step_edit_beat_pos += beats; // its negative, remember
243 step_edit_beat_pos = Temporal::Beats();
246 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
250 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Temporal::Beats beat_duration)
252 /* do these things in case undo removed the step edit region
254 if (!step_edit_region) {
255 resync_step_edit_position ();
256 prepare_step_edit_region ();
257 reset_step_edit_beat_pos ();
258 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
259 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
262 assert (step_edit_region);
263 assert (step_edit_region_view);
265 if (beat_duration == 0.0 && step_editor) {
266 beat_duration = step_editor->note_length();
267 } else if (beat_duration == 0.0) {
269 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
276 MidiStreamView* msv = _mtv.midi_view();
278 /* make sure its visible on the vertical axis */
280 if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
281 msv->update_note_range (pitch);
282 msv->set_note_range (MidiStreamView::ContentsRange);
285 /* make sure its visible on the horizontal axis */
287 samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos + beat_duration);
289 if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
290 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
293 Temporal::Beats at = step_edit_beat_pos;
294 Temporal::Beats len = beat_duration;
296 if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
298 /* avoid any apparent note overlap - move the start of this note
299 up by 1 tick from where the last note ended
302 at += Temporal::Beats::ticks(1);
303 len -= Temporal::Beats::ticks(1);
306 step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
308 last_added_pitch = pitch;
309 last_added_end = at+len;
311 if (_step_edit_triplet_countdown > 0) {
312 _step_edit_triplet_countdown--;
314 if (_step_edit_triplet_countdown == 0) {
315 _step_edit_triplet_countdown = 3;
319 if (!_step_edit_within_chord) {
320 step_edit_beat_pos += beat_duration;
321 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
323 step_edit_beat_pos += Temporal::Beats::ticks(1); // tiny, but no longer overlapping
324 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
327 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
333 StepEditor::set_step_edit_cursor_width (Temporal::Beats beats)
335 if (step_edit_region_view) {
336 step_edit_region_view->set_step_edit_cursor_width (beats);
341 StepEditor::step_edit_within_triplet() const
343 return _step_edit_triplet_countdown > 0;
347 StepEditor::step_edit_within_chord() const
349 return _step_edit_within_chord;
353 StepEditor::step_edit_toggle_triplet ()
355 if (_step_edit_triplet_countdown == 0) {
356 _step_edit_within_chord = false;
357 _step_edit_triplet_countdown = 3;
359 _step_edit_triplet_countdown = 0;
364 StepEditor::step_edit_toggle_chord ()
366 if (_step_edit_within_chord) {
367 _step_edit_within_chord = false;
368 if (step_edit_region_view) {
369 step_edit_beat_pos += _step_edit_chord_duration;
370 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
373 _step_edit_triplet_countdown = 0;
374 _step_edit_within_chord = true;
379 StepEditor::step_edit_rest (Temporal::Beats beats)
384 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
389 if (success && step_edit_region_view) {
390 step_edit_beat_pos += beats;
391 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
396 StepEditor::step_edit_beat_sync ()
398 step_edit_beat_pos = step_edit_beat_pos.round_up_to_beat();
399 if (step_edit_region_view) {
400 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
405 StepEditor::step_edit_bar_sync ()
407 Session* _session = _mtv.session ();
409 if (!_session || !step_edit_region_view || !step_edit_region) {
413 samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos);
414 fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways).sample;
415 step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat();
416 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
420 StepEditor::playlist_changed ()
422 step_edit_region_connection.disconnect ();
423 _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
424 boost::bind (&StepEditor::region_removed, this, _1),
429 StepEditor::region_removed (boost::weak_ptr<Region> wr)
431 boost::shared_ptr<Region> r (wr.lock());
437 if (step_edit_region == r) {
438 step_edit_region.reset();
439 step_edit_region_view = 0;
440 // force a recompute of the insert position
441 step_edit_beat_pos = Temporal::Beats(-1);
446 StepEditor::name() const
448 return _track->name();