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 = Evoral::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 = Evoral::Beats();
64 step_edit_region.reset ();
65 step_edit_region_view = 0;
66 last_added_pitch = -1;
67 last_added_end = Evoral::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_frame (step_edit_insert_position);
119 const Tempo& t = _mtv.session()->tempo_map().tempo_at_frame (step_edit_insert_position);
121 double baf = _mtv.session()->tempo_map().beat_at_frame (step_edit_insert_position);
122 double next_bar_in_beats = baf + m.divisions_per_bar();
123 framecnt_t next_bar_pos = _mtv.session()->tempo_map().frame_at_beat (next_bar_in_beats);
124 framecnt_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 framecnt_t frames_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
142 if (frames_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 frames_from_start = 0;
149 step_edit_beat_pos = step_edit_region_view->region_frames_to_region_beats (frames_from_start);
150 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
154 StepEditor::step_editor_hidden (GdkEventAny*)
157 return true; // XXX remember position ?!
161 StepEditor::step_editor_hide ()
163 /* everything else will follow the change in the model */
164 _track->set_step_editing (false);
168 StepEditor::stop_step_editing ()
171 step_editor->hide ();
174 if (step_edit_region_view) {
175 step_edit_region_view->hide_step_edit_cursor();
178 step_edit_region.reset ();
182 StepEditor::check_step_edit ()
184 MidiRingBuffer<framepos_t>& incoming (_track->step_edit_ring_buffer());
186 uint32_t bufsize = 32;
188 buf = new uint8_t[bufsize];
190 while (incoming.read_space()) {
192 Evoral::EventType type;
195 incoming.read_prefix (&time, &type, &size);
197 if (size > bufsize) {
200 buf = new uint8_t[bufsize];
203 incoming.read_contents (size, buf);
205 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
206 step_add_note (buf[0] & 0xf, buf[1], buf[2], Evoral::Beats());
212 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
218 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
224 StepEditor::step_edit_sustain (Evoral::Beats beats)
226 if (step_edit_region_view) {
227 step_edit_region_view->step_sustain (beats);
232 StepEditor::move_step_edit_beat_pos (Evoral::Beats beats)
234 if (!step_edit_region_view) {
238 step_edit_beat_pos = min (step_edit_beat_pos + beats,
239 step_edit_region_view->region_frames_to_region_beats (step_edit_region->length()));
240 } else if (beats < 0.0) {
241 if (-beats < step_edit_beat_pos) {
242 step_edit_beat_pos += beats; // its negative, remember
244 step_edit_beat_pos = Evoral::Beats();
247 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
251 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evoral::Beats beat_duration)
253 /* do these things in case undo removed the step edit region
255 if (!step_edit_region) {
256 resync_step_edit_position ();
257 prepare_step_edit_region ();
258 reset_step_edit_beat_pos ();
259 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
260 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
263 assert (step_edit_region);
264 assert (step_edit_region_view);
266 if (beat_duration == 0.0 && step_editor) {
267 beat_duration = step_editor->note_length();
268 } else if (beat_duration == 0.0) {
270 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
277 MidiStreamView* msv = _mtv.midi_view();
279 /* make sure its visible on the vertical axis */
281 if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
282 msv->update_note_range (pitch);
283 msv->set_note_range (MidiStreamView::ContentsRange);
286 /* make sure its visible on the horizontal axis */
288 framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos + beat_duration);
290 if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
291 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
294 Evoral::Beats at = step_edit_beat_pos;
295 Evoral::Beats len = beat_duration;
297 if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
299 /* avoid any apparent note overlap - move the start of this note
300 up by 1 tick from where the last note ended
303 at += Evoral::Beats::ticks(1);
304 len -= Evoral::Beats::ticks(1);
307 step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
309 last_added_pitch = pitch;
310 last_added_end = at+len;
312 if (_step_edit_triplet_countdown > 0) {
313 _step_edit_triplet_countdown--;
315 if (_step_edit_triplet_countdown == 0) {
316 _step_edit_triplet_countdown = 3;
320 if (!_step_edit_within_chord) {
321 step_edit_beat_pos += beat_duration;
322 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
324 step_edit_beat_pos += Evoral::Beats::ticks(1); // tiny, but no longer overlapping
325 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
332 StepEditor::set_step_edit_cursor_width (Evoral::Beats beats)
334 if (step_edit_region_view) {
335 step_edit_region_view->set_step_edit_cursor_width (beats);
340 StepEditor::step_edit_within_triplet() const
342 return _step_edit_triplet_countdown > 0;
346 StepEditor::step_edit_within_chord() const
348 return _step_edit_within_chord;
352 StepEditor::step_edit_toggle_triplet ()
354 if (_step_edit_triplet_countdown == 0) {
355 _step_edit_within_chord = false;
356 _step_edit_triplet_countdown = 3;
358 _step_edit_triplet_countdown = 0;
363 StepEditor::step_edit_toggle_chord ()
365 if (_step_edit_within_chord) {
366 _step_edit_within_chord = false;
367 if (step_edit_region_view) {
368 step_edit_beat_pos += _step_edit_chord_duration;
369 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
372 _step_edit_triplet_countdown = 0;
373 _step_edit_within_chord = true;
378 StepEditor::step_edit_rest (Evoral::Beats beats)
383 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
388 if (success && step_edit_region_view) {
389 step_edit_beat_pos += beats;
390 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
395 StepEditor::step_edit_beat_sync ()
397 step_edit_beat_pos = step_edit_beat_pos.round_up_to_beat();
398 if (step_edit_region_view) {
399 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
404 StepEditor::step_edit_bar_sync ()
406 Session* _session = _mtv.session ();
408 if (!_session || !step_edit_region_view || !step_edit_region) {
412 framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos);
413 fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways);
414 step_edit_beat_pos = step_edit_region_view->region_frames_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat();
415 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
419 StepEditor::playlist_changed ()
421 step_edit_region_connection.disconnect ();
422 _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
423 boost::bind (&StepEditor::region_removed, this, _1),
428 StepEditor::region_removed (boost::weak_ptr<Region> wr)
430 boost::shared_ptr<Region> r (wr.lock());
436 if (step_edit_region == r) {
437 step_edit_region.reset();
438 step_edit_region_view = 0;
439 // force a recompute of the insert position
440 step_edit_beat_pos = Evoral::Beats(-1);
445 StepEditor::name() const
447 return _track->name();