leftmost_position => leftmost_sample, current_page_frames => current_page_samples
[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 = 0.0;
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 = 0.0;
64         step_edit_region.reset ();
65         step_edit_region_view = 0;
66         last_added_pitch = -1;
67         last_added_end = 0;
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->set_position (WIN_POS_MOUSE);
86         step_editor->present ();
87 }
88
89 void
90 StepEditor::resync_step_edit_position ()
91 {
92         step_edit_insert_position = _editor.get_preferred_edit_position ();
93 }
94
95 void
96 StepEditor::resync_step_edit_to_edit_point ()
97 {
98         resync_step_edit_position ();
99         if (step_edit_region) {
100                 reset_step_edit_beat_pos ();
101         }
102 }
103
104 void
105 StepEditor::prepare_step_edit_region ()
106 {
107         boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
108
109         if (r) {
110                 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
111         }
112
113         if (step_edit_region) {
114                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
115                 step_edit_region_view = dynamic_cast<MidiRegionView*> (rv);
116
117         } else {
118
119                 const Meter& m = _mtv.session()->tempo_map().meter_at (step_edit_insert_position);
120                 const Tempo& t = _mtv.session()->tempo_map().tempo_at (step_edit_insert_position);
121
122                 step_edit_region = _mtv.add_region (step_edit_insert_position, floor (m.frames_per_bar (t, _mtv.session()->frame_rate())), true);
123
124                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
125                 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
126         }
127 }
128
129
130 void
131 StepEditor::reset_step_edit_beat_pos ()
132 {
133         assert (step_edit_region);
134         assert (step_edit_region_view);
135
136         framecnt_t frames_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
137
138         if (frames_from_start < 0) {
139                 /* this can happen with snap enabled, and the edit point == Playhead. we snap the
140                    position of the new region, and it can end up after the edit point.
141                 */
142                 frames_from_start = 0;
143         }
144
145         step_edit_beat_pos = step_edit_region_view->region_frames_to_region_beats (frames_from_start);
146         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
147 }
148
149 bool
150 StepEditor::step_editor_hidden (GdkEventAny*)
151 {
152         step_editor_hide ();
153         return true;
154 }
155
156 void
157 StepEditor::step_editor_hide ()
158 {
159         /* everything else will follow the change in the model */
160         _track->set_step_editing (false);
161 }
162
163 void
164 StepEditor::stop_step_editing ()
165 {
166         if (step_editor) {
167                 step_editor->hide ();
168         }
169
170         if (step_edit_region_view) {
171                 step_edit_region_view->hide_step_edit_cursor();
172         }
173
174         step_edit_region.reset ();
175 }
176
177 void
178 StepEditor::check_step_edit ()
179 {
180         MidiRingBuffer<framepos_t>& incoming (_track->step_edit_ring_buffer());
181         uint8_t* buf;
182         uint32_t bufsize = 32;
183
184         buf = new uint8_t[bufsize];
185
186         while (incoming.read_space()) {
187                 framepos_t time;
188                 Evoral::EventType type;
189                 uint32_t size;
190
191                 incoming.read_prefix (&time, &type, &size);
192
193                 if (size > bufsize) {
194                         delete [] buf;
195                         bufsize = size;
196                         buf = new uint8_t[bufsize];
197                 }
198
199                 incoming.read_contents (size, buf);
200
201                 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
202                         step_add_note (buf[0] & 0xf, buf[1], buf[2], 0.0);
203                 }
204         }
205 }
206
207 int
208 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
209 {
210         return 0;
211 }
212
213 int
214 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
215 {
216         return 0;
217 }
218
219 void
220 StepEditor::step_edit_sustain (Evoral::MusicalTime beats)
221 {
222         if (step_edit_region_view) {
223                 step_edit_region_view->step_sustain (beats);
224         }
225 }
226
227 void
228 StepEditor::move_step_edit_beat_pos (Evoral::MusicalTime beats)
229 {
230         if (beats > 0.0) {
231                 step_edit_beat_pos = min (step_edit_beat_pos + beats,
232                                           step_edit_region_view->region_frames_to_region_beats (step_edit_region->length()));
233         } else if (beats < 0.0) {
234                 if (-beats < step_edit_beat_pos) {
235                         step_edit_beat_pos += beats; // its negative, remember
236                 } else {
237                         step_edit_beat_pos = 0;
238                 }
239         }
240         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
241 }
242
243 int
244 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evoral::MusicalTime beat_duration)
245 {
246         /* do these things in case undo removed the step edit region
247         */
248         if (!step_edit_region) {
249                 resync_step_edit_position ();
250                 prepare_step_edit_region ();
251                 reset_step_edit_beat_pos ();
252                 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
253                 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
254         }
255
256         assert (step_edit_region);
257         assert (step_edit_region_view);
258
259         if (beat_duration == 0.0) {
260                 bool success;
261                 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
262
263                 if (!success) {
264                         return -1;
265                 }
266         }
267
268         MidiStreamView* msv = _mtv.midi_view();
269
270         /* make sure its visible on the vertical axis */
271
272         if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
273                 msv->update_note_range (pitch);
274                 msv->set_note_range (MidiStreamView::ContentsRange);
275         }
276
277         /* make sure its visible on the horizontal axis */
278
279         framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos + beat_duration);
280
281         if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
282                 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
283         }
284
285         Evoral::MusicalTime at = step_edit_beat_pos;
286         Evoral::MusicalTime len = beat_duration;
287
288         if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
289
290                 /* avoid any apparent note overlap - move the start of this note
291                    up by 1 tick from where the last note ended
292                 */
293
294                 at += 1.0/Timecode::BBT_Time::ticks_per_beat;
295                 len -= 1.0/Timecode::BBT_Time::ticks_per_beat;
296         }
297
298         step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
299
300         last_added_pitch = pitch;
301         last_added_end = at+len;
302
303         if (_step_edit_triplet_countdown > 0) {
304                 _step_edit_triplet_countdown--;
305
306                 if (_step_edit_triplet_countdown == 0) {
307                         _step_edit_triplet_countdown = 3;
308                 }
309         }
310
311         if (!_step_edit_within_chord) {
312                 step_edit_beat_pos += beat_duration;
313                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
314         } else {
315                 step_edit_beat_pos += 1.0/Timecode::BBT_Time::ticks_per_beat; // tiny, but no longer overlapping
316                 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
317         }
318
319         return 0;
320 }
321
322 void
323 StepEditor::set_step_edit_cursor_width (Evoral::MusicalTime beats)
324 {
325         if (step_edit_region_view) {
326                 step_edit_region_view->set_step_edit_cursor_width (beats);
327         }
328 }
329
330 bool
331 StepEditor::step_edit_within_triplet() const
332 {
333         return _step_edit_triplet_countdown > 0;
334 }
335
336 bool
337 StepEditor::step_edit_within_chord() const
338 {
339         return _step_edit_within_chord;
340 }
341
342 void
343 StepEditor::step_edit_toggle_triplet ()
344 {
345         if (_step_edit_triplet_countdown == 0) {
346                 _step_edit_within_chord = false;
347                 _step_edit_triplet_countdown = 3;
348         } else {
349                 _step_edit_triplet_countdown = 0;
350         }
351 }
352
353 void
354 StepEditor::step_edit_toggle_chord ()
355 {
356         if (_step_edit_within_chord) {
357                 _step_edit_within_chord = false;
358                 step_edit_beat_pos += _step_edit_chord_duration;
359                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
360         } else {
361                 _step_edit_triplet_countdown = 0;
362                 _step_edit_within_chord = true;
363         }
364 }
365
366 void
367 StepEditor::step_edit_rest (Evoral::MusicalTime beats)
368 {
369         bool success;
370
371         if (beats == 0.0) {
372                 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
373         } else {
374                 success = true;
375         }
376
377         if (success) {
378                 step_edit_beat_pos += beats;
379                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
380         }
381 }
382
383 void
384 StepEditor::step_edit_beat_sync ()
385 {
386         step_edit_beat_pos = ceil (step_edit_beat_pos);
387         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
388 }
389
390 void
391 StepEditor::step_edit_bar_sync ()
392 {
393         Session* _session = _mtv.session ();
394
395         if (!_session || !step_edit_region_view || !step_edit_region) {
396                 return;
397         }
398
399         framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos);
400         fpos = _session->tempo_map().round_to_bar (fpos, 1);
401         step_edit_beat_pos = ceil (step_edit_region_view->region_frames_to_region_beats (fpos - step_edit_region->position()));
402         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
403 }
404
405 void
406 StepEditor::playlist_changed ()
407 {
408         step_edit_region_connection.disconnect ();
409         _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
410                                                    boost::bind (&StepEditor::region_removed, this, _1),
411                                                    gui_context());
412 }
413
414 void
415 StepEditor::region_removed (boost::weak_ptr<Region> wr)
416 {
417         boost::shared_ptr<Region> r (wr.lock());
418
419         if (!r) {
420                 return;
421         }
422
423         if (step_edit_region == r) {
424                 step_edit_region.reset();
425                 step_edit_region_view = 0;
426                 // force a recompute of the insert position
427                 step_edit_beat_pos = -1.0;
428         }
429 }
430
431 string
432 StepEditor::name() const
433 {
434         return _track->name();
435 }