1 #include "ardour/midi_track.h"
2 #include "ardour/midi_region.h"
3 #include "ardour/tempo.h"
4 #include "ardour/types.h"
6 #include "gui_thread.h"
7 #include "midi_region_view.h"
8 #include "public_editor.h"
9 #include "step_editor.h"
10 #include "step_entry.h"
12 using namespace ARDOUR;
16 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
22 step_edit_insert_position = 0;
23 _step_edit_triplet_countdown = 0;
24 _step_edit_within_chord = 0;
25 _step_edit_chord_duration = 0.0;
26 step_edit_region_view = 0;
28 _track->PlaylistChanged.connect (*this, invalidator (*this),
29 boost::bind (&StepEditor::playlist_changed, this),
34 StepEditor::~StepEditor()
40 StepEditor::start_step_editing ()
42 _step_edit_triplet_countdown = 0;
43 _step_edit_within_chord = 0;
44 _step_edit_chord_duration = 0.0;
45 step_edit_region.reset ();
46 step_edit_region_view = 0;
47 last_added_pitch = -1;
50 resync_step_edit_position ();
51 prepare_step_edit_region ();
52 reset_step_edit_beat_pos ();
54 assert (step_edit_region);
55 assert (step_edit_region_view);
57 if (step_editor == 0) {
58 step_editor = new StepEntry (*this);
59 step_editor->signal_delete_event().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hidden));
60 step_editor->signal_hide().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hide));
63 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
64 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
66 step_editor->set_position (WIN_POS_MOUSE);
67 step_editor->present ();
71 StepEditor::resync_step_edit_position ()
73 step_edit_insert_position = _editor.get_preferred_edit_position ();
77 StepEditor::resync_step_edit_to_edit_point ()
79 resync_step_edit_position ();
80 if (step_edit_region) {
81 reset_step_edit_beat_pos ();
86 StepEditor::prepare_step_edit_region ()
88 boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
91 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
94 if (step_edit_region) {
95 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
96 step_edit_region_view = dynamic_cast<MidiRegionView*> (rv);
99 step_edit_region = _mtv.add_region (step_edit_insert_position);
100 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
101 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
107 StepEditor::reset_step_edit_beat_pos ()
109 assert (step_edit_region);
110 assert (step_edit_region_view);
112 framecnt_t frames_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
114 if (frames_from_start < 0) {
115 /* this can happen with snap enabled, and the edit point == Playhead. we snap the
116 position of the new region, and it can end up after the edit point.
118 frames_from_start = 0;
121 step_edit_beat_pos = step_edit_region_view->frames_to_beats (frames_from_start);
122 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
126 StepEditor::step_editor_hidden (GdkEventAny*)
133 StepEditor::step_editor_hide ()
135 /* everything else will follow the change in the model */
136 _track->set_step_editing (false);
140 StepEditor::stop_step_editing ()
143 step_editor->hide ();
146 if (step_edit_region_view) {
147 step_edit_region_view->hide_step_edit_cursor();
150 step_edit_region.reset ();
154 StepEditor::check_step_edit ()
156 MidiRingBuffer<nframes_t>& incoming (_track->step_edit_ring_buffer());
158 uint32_t bufsize = 32;
160 buf = new uint8_t[bufsize];
162 while (incoming.read_space()) {
164 Evoral::EventType type;
167 incoming.read_prefix (&time, &type, &size);
169 if (size > bufsize) {
172 buf = new uint8_t[bufsize];
175 incoming.read_contents (size, buf);
177 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
178 step_add_note (buf[0] & 0xf, buf[1], buf[2], 0.0);
184 StepEditor::step_add_bank_change (uint8_t channel, uint8_t bank)
190 StepEditor::step_add_program_change (uint8_t channel, uint8_t program)
196 StepEditor::step_edit_sustain (Evoral::MusicalTime beats)
198 if (step_edit_region_view) {
199 step_edit_region_view->step_sustain (beats);
204 StepEditor::move_step_edit_beat_pos (Evoral::MusicalTime beats)
207 step_edit_beat_pos = min (step_edit_beat_pos + beats,
208 step_edit_region_view->frames_to_beats (step_edit_region->length()));
209 } else if (beats < 0.0) {
210 if (beats < step_edit_beat_pos) {
211 step_edit_beat_pos += beats; // its negative, remember
213 step_edit_beat_pos = 0;
216 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
220 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evoral::MusicalTime beat_duration)
222 /* do these things in case undo removed the step edit region
224 if (!step_edit_region) {
225 resync_step_edit_position ();
226 prepare_step_edit_region ();
227 reset_step_edit_beat_pos ();
228 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
229 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
232 assert (step_edit_region);
233 assert (step_edit_region_view);
235 if (beat_duration == 0.0) {
237 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
244 MidiStreamView* msv = _mtv.midi_view();
246 /* make sure its visible on the vertical axis */
248 if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
249 msv->update_note_range (pitch);
250 msv->set_note_range (MidiStreamView::ContentsRange);
253 /* make sure its visible on the horizontal axis */
255 framepos_t fpos = step_edit_region->position() +
256 step_edit_region_view->beats_to_frames (step_edit_beat_pos + beat_duration);
258 if (fpos >= (_editor.leftmost_position() + _editor.current_page_frames())) {
259 _editor.reset_x_origin (fpos - (_editor.current_page_frames()/4));
262 Evoral::MusicalTime at = step_edit_beat_pos;
263 Evoral::MusicalTime len = beat_duration;
265 if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
267 /* avoid any apparent note overlap - move the start of this note
268 up by 1 tick from where the last note ended
271 at += 1.0/Meter::ticks_per_beat;
272 len -= 1.0/Meter::ticks_per_beat;
275 step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
277 last_added_pitch = pitch;
278 last_added_end = at+len;
280 if (_step_edit_triplet_countdown > 0) {
281 _step_edit_triplet_countdown--;
283 if (_step_edit_triplet_countdown == 0) {
284 _step_edit_triplet_countdown = 3;
288 if (!_step_edit_within_chord) {
289 step_edit_beat_pos += beat_duration;
290 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
292 step_edit_beat_pos += 1.0/Meter::ticks_per_beat; // tiny, but no longer overlapping
293 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
300 StepEditor::set_step_edit_cursor_width (Evoral::MusicalTime beats)
302 if (step_edit_region_view) {
303 step_edit_region_view->set_step_edit_cursor_width (beats);
308 StepEditor::step_edit_within_triplet() const
310 return _step_edit_triplet_countdown > 0;
314 StepEditor::step_edit_within_chord() const
316 return _step_edit_within_chord;
320 StepEditor::step_edit_toggle_triplet ()
322 if (_step_edit_triplet_countdown == 0) {
323 _step_edit_within_chord = false;
324 _step_edit_triplet_countdown = 3;
326 _step_edit_triplet_countdown = 0;
331 StepEditor::step_edit_toggle_chord ()
333 if (_step_edit_within_chord) {
334 _step_edit_within_chord = false;
335 step_edit_beat_pos += _step_edit_chord_duration;
336 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
338 _step_edit_triplet_countdown = 0;
339 _step_edit_within_chord = true;
344 StepEditor::step_edit_rest (Evoral::MusicalTime beats)
349 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
355 step_edit_beat_pos += beats;
356 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
361 StepEditor::step_edit_beat_sync ()
363 step_edit_beat_pos = ceil (step_edit_beat_pos);
364 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
368 StepEditor::step_edit_bar_sync ()
370 Session* _session = _mtv.session ();
372 if (!_session || !step_edit_region_view || !step_edit_region) {
376 framepos_t fpos = step_edit_region->position() +
377 step_edit_region_view->beats_to_frames (step_edit_beat_pos);
378 fpos = _session->tempo_map().round_to_bar (fpos, 1);
379 step_edit_beat_pos = ceil (step_edit_region_view->frames_to_beats (fpos - step_edit_region->position()));
380 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
384 StepEditor::playlist_changed ()
386 step_edit_region_connection.disconnect ();
387 _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
388 ui_bind (&StepEditor::region_removed, this, _1),
393 StepEditor::region_removed (boost::weak_ptr<Region> wr)
395 boost::shared_ptr<Region> r (wr.lock());
401 if (step_edit_region == r) {
402 step_edit_region.reset();
403 step_edit_region_view = 0;
404 // force a recompute of the insert position
405 step_edit_beat_pos = -1.0;
410 StepEditor::name() const
412 return _track->name();