adjust h-size of color theme manager "Reset to Defaults" button
[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 "pbd/stacktrace.h"
21
22 #include "ardour/midi_track.h"
23 #include "ardour/midi_region.h"
24 #include "ardour/tempo.h"
25 #include "ardour/types.h"
26
27 #include "gui_thread.h"
28 #include "midi_region_view.h"
29 #include "public_editor.h"
30 #include "step_editor.h"
31 #include "step_entry.h"
32
33 using namespace ARDOUR;
34 using namespace Gtk;
35 using namespace std;
36
37 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
38         : _editor (e)
39         , _track (t)
40         , _mtv (mtv)
41 {
42         step_edit_insert_position = 0;
43         _step_edit_triplet_countdown = 0;
44         _step_edit_within_chord = 0;
45         _step_edit_chord_duration = Temporal::Beats();
46         step_edit_region_view = 0;
47
48         _track->PlaylistChanged.connect (*this, invalidator (*this),
49                                          boost::bind (&StepEditor::playlist_changed, this),
50                                          gui_context());
51         playlist_changed ();
52 }
53
54 StepEditor::~StepEditor()
55 {
56         StepEntry::instance().set_step_editor (0);
57 }
58
59 void
60 StepEditor::start_step_editing ()
61 {
62         _step_edit_triplet_countdown = 0;
63         _step_edit_within_chord = 0;
64         _step_edit_chord_duration = Temporal::Beats();
65         step_edit_region.reset ();
66         step_edit_region_view = 0;
67         last_added_pitch = -1;
68         last_added_end = Temporal::Beats();
69
70         resync_step_edit_position ();
71         prepare_step_edit_region ();
72         reset_step_edit_beat_pos ();
73
74         assert (step_edit_region);
75         assert (step_edit_region_view);
76
77         StepEntry::instance().set_step_editor (this);
78         delete_connection = StepEntry::instance().signal_delete_event().connect (sigc::mem_fun (*this, &StepEditor::step_entry_hidden));
79         hide_connection = StepEntry::instance(). signal_hide().connect (sigc::mem_fun (*this, &StepEditor::step_entry_done));
80
81         step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
82         step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
83
84         StepEntry::instance().present ();
85 }
86
87 void
88 StepEditor::resync_step_edit_position ()
89 {
90         step_edit_insert_position = _editor.get_preferred_edit_position (Editing::EDIT_IGNORE_NONE, false, true);
91 }
92
93 void
94 StepEditor::resync_step_edit_to_edit_point ()
95 {
96         resync_step_edit_position ();
97         if (step_edit_region) {
98                 reset_step_edit_beat_pos ();
99         }
100 }
101
102 void
103 StepEditor::prepare_step_edit_region ()
104 {
105         boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
106
107         if (r) {
108                 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
109         }
110
111         if (step_edit_region) {
112                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
113                 step_edit_region_view = dynamic_cast<MidiRegionView*> (rv);
114
115         } else {
116
117                 const Meter& m = _mtv.session()->tempo_map().meter_at_sample (step_edit_insert_position);
118                 double baf = max (0.0, _mtv.session()->tempo_map().beat_at_sample (step_edit_insert_position));
119                 double next_bar_in_beats =  baf + m.divisions_per_bar();
120                 samplecnt_t next_bar_pos = _mtv.session()->tempo_map().sample_at_beat (next_bar_in_beats);
121                 samplecnt_t len = next_bar_pos - step_edit_insert_position;
122
123                 step_edit_region = _mtv.add_region (step_edit_insert_position, len, true);
124
125                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
126                 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
127         }
128 }
129
130
131 void
132 StepEditor::reset_step_edit_beat_pos ()
133 {
134         assert (step_edit_region);
135         assert (step_edit_region_view);
136
137         samplecnt_t samples_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
138
139         if (samples_from_start < 0) {
140                 /* this can happen with snap enabled, and the edit point == Playhead. we snap the
141                    position of the new region, and it can end up after the edit point.
142                 */
143                 samples_from_start = 0;
144         }
145
146         step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (samples_from_start);
147         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
148 }
149
150 bool
151 StepEditor::step_entry_hidden (GdkEventAny*)
152 {
153         step_entry_done ();
154         return true;
155 }
156
157 void
158 StepEditor::step_entry_done ()
159 {
160         hide_connection.disconnect ();
161         delete_connection.disconnect ();
162
163         /* everything else will follow the change in the model */
164         _track->set_step_editing (false);
165 }
166
167 void
168 StepEditor::stop_step_editing ()
169 {
170         StepEntry::instance().hide ();
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], Temporal::Beats());
205                 }
206         }
207         delete [] buf;
208 }
209
210 int
211 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
212 {
213         return 0;
214 }
215
216 int
217 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
218 {
219         return 0;
220 }
221
222 void
223 StepEditor::step_edit_sustain (Temporal::Beats beats)
224 {
225         if (step_edit_region_view) {
226                 step_edit_region_view->step_sustain (beats);
227         }
228 }
229
230 void
231 StepEditor::move_step_edit_beat_pos (Temporal::Beats beats)
232 {
233         if (!step_edit_region_view) {
234                 return;
235         }
236         if (beats > 0.0) {
237                 step_edit_beat_pos = min (step_edit_beat_pos + beats,
238                                           step_edit_region_view->region_samples_to_region_beats (step_edit_region->length()));
239         } else if (beats < 0.0) {
240                 if (-beats < step_edit_beat_pos) {
241                         step_edit_beat_pos += beats; // its negative, remember
242                 } else {
243                         step_edit_beat_pos = Temporal::Beats();
244                 }
245         }
246         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
247 }
248
249 int
250 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Temporal::Beats beat_duration)
251 {
252         /* do these things in case undo removed the step edit region
253          */
254         if (!step_edit_region) {
255                 resync_step_edit_position ();
256                 prepare_step_edit_region ();
257                 reset_step_edit_beat_pos ();
258                 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
259                 step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
260         }
261
262         assert (step_edit_region);
263         assert (step_edit_region_view);
264
265         if (beat_duration == 0.0) {
266                 beat_duration = StepEntry::instance().note_length();
267         } else if (beat_duration == 0.0) {
268                 bool success;
269                 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
270
271                 if (!success) {
272                         return -1;
273                 }
274         }
275
276         MidiStreamView* msv = _mtv.midi_view();
277
278         /* make sure its visible on the vertical axis */
279
280         if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
281                 msv->update_note_range (pitch);
282                 msv->set_note_range (MidiStreamView::ContentsRange);
283         }
284
285         /* make sure its visible on the horizontal axis */
286
287         samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos + beat_duration);
288
289         if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
290                 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
291         }
292
293         Temporal::Beats at = step_edit_beat_pos;
294         Temporal::Beats len = beat_duration;
295
296         if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
297
298                 /* avoid any apparent note overlap - move the start of this note
299                    up by 1 tick from where the last note ended
300                 */
301
302                 at  += Temporal::Beats::ticks(1);
303                 len -= Temporal::Beats::ticks(1);
304         }
305
306         step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
307
308         last_added_pitch = pitch;
309         last_added_end = at+len;
310
311         if (_step_edit_triplet_countdown > 0) {
312                 _step_edit_triplet_countdown--;
313
314                 if (_step_edit_triplet_countdown == 0) {
315                         _step_edit_triplet_countdown = 3;
316                 }
317         }
318
319         if (!_step_edit_within_chord) {
320                 step_edit_beat_pos += beat_duration;
321                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
322         } else {
323                 step_edit_beat_pos += Temporal::Beats::ticks(1); // tiny, but no longer overlapping
324                 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
325         }
326
327         step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
328
329         return 0;
330 }
331
332 void
333 StepEditor::set_step_edit_cursor_width (Temporal::Beats beats)
334 {
335         if (step_edit_region_view) {
336                 step_edit_region_view->set_step_edit_cursor_width (beats);
337         }
338 }
339
340 bool
341 StepEditor::step_edit_within_triplet() const
342 {
343         return _step_edit_triplet_countdown > 0;
344 }
345
346 bool
347 StepEditor::step_edit_within_chord() const
348 {
349         return _step_edit_within_chord;
350 }
351
352 void
353 StepEditor::step_edit_toggle_triplet ()
354 {
355         if (_step_edit_triplet_countdown == 0) {
356                 _step_edit_within_chord = false;
357                 _step_edit_triplet_countdown = 3;
358         } else {
359                 _step_edit_triplet_countdown = 0;
360         }
361 }
362
363 void
364 StepEditor::step_edit_toggle_chord ()
365 {
366         if (_step_edit_within_chord) {
367                 _step_edit_within_chord = false;
368                 if (step_edit_region_view) {
369                         step_edit_beat_pos += _step_edit_chord_duration;
370                         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
371                 }
372         } else {
373                 _step_edit_triplet_countdown = 0;
374                 _step_edit_within_chord = true;
375         }
376 }
377
378 void
379 StepEditor::step_edit_rest (Temporal::Beats beats)
380 {
381         bool success;
382
383         if (beats == 0.0) {
384                 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
385         } else {
386                 success = true;
387         }
388
389         if (success && step_edit_region_view) {
390                 step_edit_beat_pos += beats;
391                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
392         }
393 }
394
395 void
396 StepEditor::step_edit_beat_sync ()
397 {
398         step_edit_beat_pos = step_edit_beat_pos.round_up_to_beat();
399         if (step_edit_region_view) {
400                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
401         }
402 }
403
404 void
405 StepEditor::step_edit_bar_sync ()
406 {
407         Session* _session = _mtv.session ();
408
409         if (!_session || !step_edit_region_view || !step_edit_region) {
410                 return;
411         }
412
413         samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos);
414         fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways).sample;
415         step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat();
416         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
417 }
418
419 void
420 StepEditor::playlist_changed ()
421 {
422         step_edit_region_connection.disconnect ();
423         _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
424                                                    boost::bind (&StepEditor::region_removed, this, _1),
425                                                    gui_context());
426 }
427
428 void
429 StepEditor::region_removed (boost::weak_ptr<Region> wr)
430 {
431         boost::shared_ptr<Region> r (wr.lock());
432
433         if (!r) {
434                 return;
435         }
436
437         if (step_edit_region == r) {
438                 step_edit_region.reset();
439                 step_edit_region_view = 0;
440                 // force a recompute of the insert position
441                 step_edit_beat_pos = Temporal::Beats(-1);
442         }
443 }
444
445 string
446 StepEditor::name() const
447 {
448         return _track->name();
449 }