Update comments & icon of rubberband example Lua script
[ardour.git] / gtk2_ardour / step_editor.cc
1 /*
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>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "pbd/stacktrace.h"
24
25 #include "ardour/midi_track.h"
26 #include "ardour/midi_region.h"
27 #include "ardour/tempo.h"
28 #include "ardour/types.h"
29
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"
35
36 using namespace ARDOUR;
37 using namespace Gtk;
38 using namespace std;
39
40 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
41         : _editor (e)
42         , _track (t)
43         , _mtv (mtv)
44 {
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;
50
51         _track->PlaylistChanged.connect (*this, invalidator (*this),
52                                          boost::bind (&StepEditor::playlist_changed, this),
53                                          gui_context());
54         playlist_changed ();
55 }
56
57 StepEditor::~StepEditor()
58 {
59         StepEntry::instance().set_step_editor (0);
60 }
61
62 void
63 StepEditor::start_step_editing ()
64 {
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();
72
73         resync_step_edit_position ();
74         prepare_step_edit_region ();
75         reset_step_edit_beat_pos ();
76
77         assert (step_edit_region);
78         assert (step_edit_region_view);
79
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));
83
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());
86
87         StepEntry::instance().present ();
88 }
89
90 void
91 StepEditor::resync_step_edit_position ()
92 {
93         step_edit_insert_position = _editor.get_preferred_edit_position (Editing::EDIT_IGNORE_NONE, false, true);
94 }
95
96 void
97 StepEditor::resync_step_edit_to_edit_point ()
98 {
99         resync_step_edit_position ();
100         if (step_edit_region) {
101                 reset_step_edit_beat_pos ();
102         }
103 }
104
105 void
106 StepEditor::prepare_step_edit_region ()
107 {
108         boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
109
110         if (r) {
111                 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
112         }
113
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);
117
118         } else {
119
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;
125
126                 step_edit_region = _mtv.add_region (step_edit_insert_position, len, true);
127
128                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
129                 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
130         }
131 }
132
133
134 void
135 StepEditor::reset_step_edit_beat_pos ()
136 {
137         assert (step_edit_region);
138         assert (step_edit_region_view);
139
140         samplecnt_t samples_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
141
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.
145                 */
146                 samples_from_start = 0;
147         }
148
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);
151 }
152
153 bool
154 StepEditor::step_entry_hidden (GdkEventAny*)
155 {
156         step_entry_done ();
157         return true;
158 }
159
160 void
161 StepEditor::step_entry_done ()
162 {
163         hide_connection.disconnect ();
164         delete_connection.disconnect ();
165
166         /* everything else will follow the change in the model */
167         _track->set_step_editing (false);
168 }
169
170 void
171 StepEditor::stop_step_editing ()
172 {
173         StepEntry::instance().hide ();
174
175         if (step_edit_region_view) {
176                 step_edit_region_view->hide_step_edit_cursor();
177         }
178
179         step_edit_region.reset ();
180 }
181
182 void
183 StepEditor::check_step_edit ()
184 {
185         MidiRingBuffer<samplepos_t>& incoming (_track->step_edit_ring_buffer());
186         uint8_t* buf;
187         uint32_t bufsize = 32;
188
189         buf = new uint8_t[bufsize];
190
191         while (incoming.read_space()) {
192                 samplepos_t time;
193                 Evoral::EventType type;
194                 uint32_t size;
195
196                 if (!incoming.read_prefix (&time, &type, &size)) {
197                         break;
198                 }
199
200                 if (size > bufsize) {
201                         delete [] buf;
202                         bufsize = size;
203                         buf = new uint8_t[bufsize];
204                 }
205
206                 if (!incoming.read_contents (size, buf)) {
207                         break;
208                 }
209
210                 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON && size == 3) {
211                         step_add_note (buf[0] & 0xf, buf[1], buf[2], Temporal::Beats());
212                 }
213         }
214         delete [] buf;
215 }
216
217 int
218 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
219 {
220         return 0;
221 }
222
223 int
224 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
225 {
226         return 0;
227 }
228
229 void
230 StepEditor::step_edit_sustain (Temporal::Beats beats)
231 {
232         if (step_edit_region_view) {
233                 step_edit_region_view->step_sustain (beats);
234         }
235 }
236
237 void
238 StepEditor::move_step_edit_beat_pos (Temporal::Beats beats)
239 {
240         if (!step_edit_region_view) {
241                 return;
242         }
243         if (beats > 0.0) {
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
249                 } else {
250                         step_edit_beat_pos = Temporal::Beats();
251                 }
252         }
253         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
254 }
255
256 int
257 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Temporal::Beats beat_duration)
258 {
259         /* do these things in case undo removed the step edit region
260          */
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());
267         }
268
269         assert (step_edit_region);
270         assert (step_edit_region_view);
271
272         if (beat_duration == 0.0) {
273                 beat_duration = StepEntry::instance().note_length();
274         } else if (beat_duration == 0.0) {
275                 bool success;
276                 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
277
278                 if (!success) {
279                         return -1;
280                 }
281         }
282
283         MidiStreamView* msv = _mtv.midi_view();
284
285         /* make sure its visible on the vertical axis */
286
287         if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
288                 msv->update_note_range (pitch);
289                 msv->set_note_range (MidiStreamView::ContentsRange);
290         }
291
292         /* make sure its visible on the horizontal axis */
293
294         samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos + beat_duration);
295
296         if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
297                 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
298         }
299
300         Temporal::Beats at = step_edit_beat_pos;
301         Temporal::Beats len = beat_duration;
302
303         if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
304
305                 /* avoid any apparent note overlap - move the start of this note
306                    up by 1 tick from where the last note ended
307                 */
308
309                 at  += Temporal::Beats::ticks(1);
310                 len -= Temporal::Beats::ticks(1);
311         }
312
313         step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
314
315         last_added_pitch = pitch;
316         last_added_end = at+len;
317
318         if (_step_edit_triplet_countdown > 0) {
319                 _step_edit_triplet_countdown--;
320
321                 if (_step_edit_triplet_countdown == 0) {
322                         _step_edit_triplet_countdown = 3;
323                 }
324         }
325
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);
329         } else {
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);
332         }
333
334         step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
335
336         return 0;
337 }
338
339 void
340 StepEditor::set_step_edit_cursor_width (Temporal::Beats beats)
341 {
342         if (step_edit_region_view) {
343                 step_edit_region_view->set_step_edit_cursor_width (beats);
344         }
345 }
346
347 bool
348 StepEditor::step_edit_within_triplet() const
349 {
350         return _step_edit_triplet_countdown > 0;
351 }
352
353 bool
354 StepEditor::step_edit_within_chord() const
355 {
356         return _step_edit_within_chord;
357 }
358
359 void
360 StepEditor::step_edit_toggle_triplet ()
361 {
362         if (_step_edit_triplet_countdown == 0) {
363                 _step_edit_within_chord = false;
364                 _step_edit_triplet_countdown = 3;
365         } else {
366                 _step_edit_triplet_countdown = 0;
367         }
368 }
369
370 void
371 StepEditor::step_edit_toggle_chord ()
372 {
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);
378                 }
379         } else {
380                 _step_edit_triplet_countdown = 0;
381                 _step_edit_within_chord = true;
382         }
383 }
384
385 void
386 StepEditor::step_edit_rest (Temporal::Beats beats)
387 {
388         bool success;
389
390         if (beats == 0.0) {
391                 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
392         } else {
393                 success = true;
394         }
395
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);
399         }
400 }
401
402 void
403 StepEditor::step_edit_beat_sync ()
404 {
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);
408         }
409 }
410
411 void
412 StepEditor::step_edit_bar_sync ()
413 {
414         Session* _session = _mtv.session ();
415
416         if (!_session || !step_edit_region_view || !step_edit_region) {
417                 return;
418         }
419
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);
424 }
425
426 void
427 StepEditor::playlist_changed ()
428 {
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),
432                                                    gui_context());
433 }
434
435 void
436 StepEditor::region_removed (boost::weak_ptr<Region> wr)
437 {
438         boost::shared_ptr<Region> r (wr.lock());
439
440         if (!r) {
441                 return;
442         }
443
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);
449         }
450 }
451
452 string
453 StepEditor::name() const
454 {
455         return _track->name();
456 }