14327cd72d7b416dd37d5515867958e182a024f5
[ardour.git] / gtk2_ardour / step_editor.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include "ardour/midi_track.h"
21 #include "ardour/midi_region.h"
22 #include "ardour/tempo.h"
23 #include "ardour/types.h"
24
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"
30
31 using namespace ARDOUR;
32 using namespace Gtk;
33 using namespace std;
34
35 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
36         : _editor (e)
37         , _track (t)
38         , step_editor (0)
39         , _mtv (mtv)
40 {
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;
46
47         _track->PlaylistChanged.connect (*this, invalidator (*this),
48                                          boost::bind (&StepEditor::playlist_changed, this),
49                                          gui_context());
50         playlist_changed ();
51 }
52
53 StepEditor::~StepEditor()
54 {
55         delete step_editor;
56 }
57
58 void
59 StepEditor::start_step_editing ()
60 {
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();
68
69         resync_step_edit_position ();
70         prepare_step_edit_region ();
71         reset_step_edit_beat_pos ();
72
73         assert (step_edit_region);
74         assert (step_edit_region_view);
75
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));
80         }
81
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());
84
85         step_editor->present ();
86 }
87
88 void
89 StepEditor::resync_step_edit_position ()
90 {
91         step_edit_insert_position = _editor.get_preferred_edit_position (Editing::EDIT_IGNORE_NONE, false, true);
92 }
93
94 void
95 StepEditor::resync_step_edit_to_edit_point ()
96 {
97         resync_step_edit_position ();
98         if (step_edit_region) {
99                 reset_step_edit_beat_pos ();
100         }
101 }
102
103 void
104 StepEditor::prepare_step_edit_region ()
105 {
106         boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
107
108         if (r) {
109                 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
110         }
111
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);
115
116         } else {
117
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;
123
124                 step_edit_region = _mtv.add_region (step_edit_insert_position, len, true);
125
126                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
127                 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
128         }
129 }
130
131
132 void
133 StepEditor::reset_step_edit_beat_pos ()
134 {
135         assert (step_edit_region);
136         assert (step_edit_region_view);
137
138         samplecnt_t samples_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
139
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.
143                 */
144                 samples_from_start = 0;
145         }
146
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);
149 }
150
151 bool
152 StepEditor::step_editor_hidden (GdkEventAny*)
153 {
154         step_editor_hide ();
155         return true; // XXX remember position ?!
156 }
157
158 void
159 StepEditor::step_editor_hide ()
160 {
161         /* everything else will follow the change in the model */
162         _track->set_step_editing (false);
163 }
164
165 void
166 StepEditor::stop_step_editing ()
167 {
168         if (step_editor) {
169                 step_editor->hide ();
170         }
171
172         if (step_edit_region_view) {
173                 step_edit_region_view->hide_step_edit_cursor();
174         }
175
176         step_edit_region.reset ();
177 }
178
179 void
180 StepEditor::check_step_edit ()
181 {
182         MidiRingBuffer<samplepos_t>& incoming (_track->step_edit_ring_buffer());
183         uint8_t* buf;
184         uint32_t bufsize = 32;
185
186         buf = new uint8_t[bufsize];
187
188         while (incoming.read_space()) {
189                 samplepos_t time;
190                 Evoral::EventType type;
191                 uint32_t size;
192
193                 incoming.read_prefix (&time, &type, &size);
194
195                 if (size > bufsize) {
196                         delete [] buf;
197                         bufsize = size;
198                         buf = new uint8_t[bufsize];
199                 }
200
201                 incoming.read_contents (size, buf);
202
203                 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
204                         step_add_note (buf[0] & 0xf, buf[1], buf[2], Evoral::Beats());
205                 }
206         }
207 }
208
209 int
210 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
211 {
212         return 0;
213 }
214
215 int
216 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
217 {
218         return 0;
219 }
220
221 void
222 StepEditor::step_edit_sustain (Evoral::Beats beats)
223 {
224         if (step_edit_region_view) {
225                 step_edit_region_view->step_sustain (beats);
226         }
227 }
228
229 void
230 StepEditor::move_step_edit_beat_pos (Evoral::Beats beats)
231 {
232         if (!step_edit_region_view) {
233                 return;
234         }
235         if (beats > 0.0) {
236                 step_edit_beat_pos = min (step_edit_beat_pos + beats,
237                                           step_edit_region_view->region_samples_to_region_beats (step_edit_region->length()));
238         } else if (beats < 0.0) {
239                 if (-beats < step_edit_beat_pos) {
240                         step_edit_beat_pos += beats; // its negative, remember
241                 } else {
242                         step_edit_beat_pos = Evoral::Beats();
243                 }
244         }
245         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
246 }
247
248 int
249 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evoral::Beats beat_duration)
250 {
251         /* do these things in case undo removed the step edit region
252          */
253         if (!step_edit_region) {
254                 resync_step_edit_position ();
255                 prepare_step_edit_region ();
256                 reset_step_edit_beat_pos ();
257                 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
258                 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
259         }
260
261         assert (step_edit_region);
262         assert (step_edit_region_view);
263
264         if (beat_duration == 0.0 && step_editor) {
265                 beat_duration = step_editor->note_length();
266         } else if (beat_duration == 0.0) {
267                 bool success;
268                 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
269
270                 if (!success) {
271                         return -1;
272                 }
273         }
274
275         MidiStreamView* msv = _mtv.midi_view();
276
277         /* make sure its visible on the vertical axis */
278
279         if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
280                 msv->update_note_range (pitch);
281                 msv->set_note_range (MidiStreamView::ContentsRange);
282         }
283
284         /* make sure its visible on the horizontal axis */
285
286         samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos + beat_duration);
287
288         if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
289                 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
290         }
291
292         Evoral::Beats at = step_edit_beat_pos;
293         Evoral::Beats len = beat_duration;
294
295         if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
296
297                 /* avoid any apparent note overlap - move the start of this note
298                    up by 1 tick from where the last note ended
299                 */
300
301                 at  += Evoral::Beats::ticks(1);
302                 len -= Evoral::Beats::ticks(1);
303         }
304
305         step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
306
307         last_added_pitch = pitch;
308         last_added_end = at+len;
309
310         if (_step_edit_triplet_countdown > 0) {
311                 _step_edit_triplet_countdown--;
312
313                 if (_step_edit_triplet_countdown == 0) {
314                         _step_edit_triplet_countdown = 3;
315                 }
316         }
317
318         if (!_step_edit_within_chord) {
319                 step_edit_beat_pos += beat_duration;
320                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
321         } else {
322                 step_edit_beat_pos += Evoral::Beats::ticks(1); // tiny, but no longer overlapping
323                 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
324         }
325
326         step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
327
328         return 0;
329 }
330
331 void
332 StepEditor::set_step_edit_cursor_width (Evoral::Beats beats)
333 {
334         if (step_edit_region_view) {
335                 step_edit_region_view->set_step_edit_cursor_width (beats);
336         }
337 }
338
339 bool
340 StepEditor::step_edit_within_triplet() const
341 {
342         return _step_edit_triplet_countdown > 0;
343 }
344
345 bool
346 StepEditor::step_edit_within_chord() const
347 {
348         return _step_edit_within_chord;
349 }
350
351 void
352 StepEditor::step_edit_toggle_triplet ()
353 {
354         if (_step_edit_triplet_countdown == 0) {
355                 _step_edit_within_chord = false;
356                 _step_edit_triplet_countdown = 3;
357         } else {
358                 _step_edit_triplet_countdown = 0;
359         }
360 }
361
362 void
363 StepEditor::step_edit_toggle_chord ()
364 {
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);
370                 }
371         } else {
372                 _step_edit_triplet_countdown = 0;
373                 _step_edit_within_chord = true;
374         }
375 }
376
377 void
378 StepEditor::step_edit_rest (Evoral::Beats beats)
379 {
380         bool success;
381
382         if (beats == 0.0) {
383                 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
384         } else {
385                 success = true;
386         }
387
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);
391         }
392 }
393
394 void
395 StepEditor::step_edit_beat_sync ()
396 {
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);
400         }
401 }
402
403 void
404 StepEditor::step_edit_bar_sync ()
405 {
406         Session* _session = _mtv.session ();
407
408         if (!_session || !step_edit_region_view || !step_edit_region) {
409                 return;
410         }
411
412         samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos);
413         fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways).sample;
414         step_edit_beat_pos = step_edit_region_view->region_samples_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);
416 }
417
418 void
419 StepEditor::playlist_changed ()
420 {
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),
424                                                    gui_context());
425 }
426
427 void
428 StepEditor::region_removed (boost::weak_ptr<Region> wr)
429 {
430         boost::shared_ptr<Region> r (wr.lock());
431
432         if (!r) {
433                 return;
434         }
435
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);
441         }
442 }
443
444 string
445 StepEditor::name() const
446 {
447         return _track->name();
448 }