Update GPL boilerplate and (C)
[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                 incoming.read_prefix (&time, &type, &size);
197
198                 if (size > bufsize) {
199                         delete [] buf;
200                         bufsize = size;
201                         buf = new uint8_t[bufsize];
202                 }
203
204                 incoming.read_contents (size, buf);
205
206                 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
207                         step_add_note (buf[0] & 0xf, buf[1], buf[2], Temporal::Beats());
208                 }
209         }
210         delete [] buf;
211 }
212
213 int
214 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
215 {
216         return 0;
217 }
218
219 int
220 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
221 {
222         return 0;
223 }
224
225 void
226 StepEditor::step_edit_sustain (Temporal::Beats beats)
227 {
228         if (step_edit_region_view) {
229                 step_edit_region_view->step_sustain (beats);
230         }
231 }
232
233 void
234 StepEditor::move_step_edit_beat_pos (Temporal::Beats beats)
235 {
236         if (!step_edit_region_view) {
237                 return;
238         }
239         if (beats > 0.0) {
240                 step_edit_beat_pos = min (step_edit_beat_pos + beats,
241                                           step_edit_region_view->region_samples_to_region_beats (step_edit_region->length()));
242         } else if (beats < 0.0) {
243                 if (-beats < step_edit_beat_pos) {
244                         step_edit_beat_pos += beats; // its negative, remember
245                 } else {
246                         step_edit_beat_pos = Temporal::Beats();
247                 }
248         }
249         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
250 }
251
252 int
253 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Temporal::Beats beat_duration)
254 {
255         /* do these things in case undo removed the step edit region
256          */
257         if (!step_edit_region) {
258                 resync_step_edit_position ();
259                 prepare_step_edit_region ();
260                 reset_step_edit_beat_pos ();
261                 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
262                 step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
263         }
264
265         assert (step_edit_region);
266         assert (step_edit_region_view);
267
268         if (beat_duration == 0.0) {
269                 beat_duration = StepEntry::instance().note_length();
270         } else if (beat_duration == 0.0) {
271                 bool success;
272                 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
273
274                 if (!success) {
275                         return -1;
276                 }
277         }
278
279         MidiStreamView* msv = _mtv.midi_view();
280
281         /* make sure its visible on the vertical axis */
282
283         if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
284                 msv->update_note_range (pitch);
285                 msv->set_note_range (MidiStreamView::ContentsRange);
286         }
287
288         /* make sure its visible on the horizontal axis */
289
290         samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos + beat_duration);
291
292         if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
293                 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
294         }
295
296         Temporal::Beats at = step_edit_beat_pos;
297         Temporal::Beats len = beat_duration;
298
299         if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
300
301                 /* avoid any apparent note overlap - move the start of this note
302                    up by 1 tick from where the last note ended
303                 */
304
305                 at  += Temporal::Beats::ticks(1);
306                 len -= Temporal::Beats::ticks(1);
307         }
308
309         step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
310
311         last_added_pitch = pitch;
312         last_added_end = at+len;
313
314         if (_step_edit_triplet_countdown > 0) {
315                 _step_edit_triplet_countdown--;
316
317                 if (_step_edit_triplet_countdown == 0) {
318                         _step_edit_triplet_countdown = 3;
319                 }
320         }
321
322         if (!_step_edit_within_chord) {
323                 step_edit_beat_pos += beat_duration;
324                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
325         } else {
326                 step_edit_beat_pos += Temporal::Beats::ticks(1); // tiny, but no longer overlapping
327                 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
328         }
329
330         step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
331
332         return 0;
333 }
334
335 void
336 StepEditor::set_step_edit_cursor_width (Temporal::Beats beats)
337 {
338         if (step_edit_region_view) {
339                 step_edit_region_view->set_step_edit_cursor_width (beats);
340         }
341 }
342
343 bool
344 StepEditor::step_edit_within_triplet() const
345 {
346         return _step_edit_triplet_countdown > 0;
347 }
348
349 bool
350 StepEditor::step_edit_within_chord() const
351 {
352         return _step_edit_within_chord;
353 }
354
355 void
356 StepEditor::step_edit_toggle_triplet ()
357 {
358         if (_step_edit_triplet_countdown == 0) {
359                 _step_edit_within_chord = false;
360                 _step_edit_triplet_countdown = 3;
361         } else {
362                 _step_edit_triplet_countdown = 0;
363         }
364 }
365
366 void
367 StepEditor::step_edit_toggle_chord ()
368 {
369         if (_step_edit_within_chord) {
370                 _step_edit_within_chord = false;
371                 if (step_edit_region_view) {
372                         step_edit_beat_pos += _step_edit_chord_duration;
373                         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
374                 }
375         } else {
376                 _step_edit_triplet_countdown = 0;
377                 _step_edit_within_chord = true;
378         }
379 }
380
381 void
382 StepEditor::step_edit_rest (Temporal::Beats beats)
383 {
384         bool success;
385
386         if (beats == 0.0) {
387                 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
388         } else {
389                 success = true;
390         }
391
392         if (success && step_edit_region_view) {
393                 step_edit_beat_pos += beats;
394                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
395         }
396 }
397
398 void
399 StepEditor::step_edit_beat_sync ()
400 {
401         step_edit_beat_pos = step_edit_beat_pos.round_up_to_beat();
402         if (step_edit_region_view) {
403                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
404         }
405 }
406
407 void
408 StepEditor::step_edit_bar_sync ()
409 {
410         Session* _session = _mtv.session ();
411
412         if (!_session || !step_edit_region_view || !step_edit_region) {
413                 return;
414         }
415
416         samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos);
417         fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways).sample;
418         step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat();
419         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
420 }
421
422 void
423 StepEditor::playlist_changed ()
424 {
425         step_edit_region_connection.disconnect ();
426         _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
427                                                    boost::bind (&StepEditor::region_removed, this, _1),
428                                                    gui_context());
429 }
430
431 void
432 StepEditor::region_removed (boost::weak_ptr<Region> wr)
433 {
434         boost::shared_ptr<Region> r (wr.lock());
435
436         if (!r) {
437                 return;
438         }
439
440         if (step_edit_region == r) {
441                 step_edit_region.reset();
442                 step_edit_region_view = 0;
443                 // force a recompute of the insert position
444                 step_edit_beat_pos = Temporal::Beats(-1);
445         }
446 }
447
448 string
449 StepEditor::name() const
450 {
451         return _track->name();
452 }