split out the logic behind step editing from MidiTimeAxisView as much as possible
[ardour.git] / gtk2_ardour / step_editor.cc
1 #include "ardour/midi_track.h"
2 #include "ardour/midi_region.h"
3 #include "ardour/tempo.h"
4 #include "ardour/types.h"
5
6 #include "gui_thread.h"
7 #include "midi_region_view.h"
8 #include "public_editor.h"
9 #include "step_editor.h"
10 #include "step_entry.h"
11
12 using namespace ARDOUR;
13 using namespace Gtk;
14 using namespace std;
15
16 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
17         : _editor (e) 
18         , _track (t)
19         , step_editor (0)
20         , _mtv (mtv)
21 {
22         step_edit_insert_position = 0;
23         _step_edit_triplet_countdown = 0;
24         _step_edit_within_chord = 0;
25         _step_edit_chord_duration = 0.0;
26         step_edit_region_view = 0;
27  
28         _track->PlaylistChanged.connect (*this, invalidator (*this),
29                                          boost::bind (&StepEditor::playlist_changed, this),
30                                          gui_context());
31         playlist_changed ();
32 }
33
34 StepEditor::~StepEditor()
35 {
36         delete step_editor;
37 }
38
39 void
40 StepEditor::start_step_editing ()
41 {
42         _step_edit_triplet_countdown = 0;
43         _step_edit_within_chord = 0;
44         _step_edit_chord_duration = 0.0;
45         step_edit_region.reset ();
46         step_edit_region_view = 0;
47
48         resync_step_edit_position ();
49         prepare_step_edit_region ();
50         reset_step_edit_beat_pos ();
51
52         assert (step_edit_region);
53         assert (step_edit_region_view);
54
55         if (step_editor == 0) {
56                 step_editor = new StepEntry (*this);
57                 step_editor->signal_delete_event().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hidden));
58                 step_editor->signal_hide().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hide));
59         }
60
61         step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
62         step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
63
64         step_editor->set_position (WIN_POS_MOUSE);
65         step_editor->present ();
66 }
67
68 void
69 StepEditor::resync_step_edit_position ()
70 {
71         step_edit_insert_position = _editor.get_preferred_edit_position ();
72 }
73
74 void
75 StepEditor::resync_step_edit_to_edit_point ()
76 {
77         resync_step_edit_position ();
78         if (step_edit_region) {
79                 reset_step_edit_beat_pos ();
80         }
81 }
82
83 void
84 StepEditor::prepare_step_edit_region ()
85 {
86         boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
87
88         if (r) {
89                 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
90         }
91
92         if (step_edit_region) {
93                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
94                 step_edit_region_view = dynamic_cast<MidiRegionView*> (rv);
95
96         } else {
97                 step_edit_region = _mtv.add_region (step_edit_insert_position);
98                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
99                 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
100         }
101 }
102
103
104 void
105 StepEditor::reset_step_edit_beat_pos ()
106 {
107         assert (step_edit_region);
108         assert (step_edit_region_view);
109
110         framecnt_t frames_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
111         
112         if (frames_from_start < 0) {
113                 /* this can happen with snap enabled, and the edit point == Playhead. we snap the
114                    position of the new region, and it can end up after the edit point.
115                 */
116                 frames_from_start = 0;
117         }
118         
119         step_edit_beat_pos = step_edit_region_view->frames_to_beats (frames_from_start);
120         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
121 }
122
123 bool
124 StepEditor::step_editor_hidden (GdkEventAny*)
125 {
126         step_editor_hide ();
127         return true;
128 }
129
130 void
131 StepEditor::step_editor_hide ()
132 {
133         /* everything else will follow the change in the model */
134         _track->set_step_editing (false);
135 }
136
137 void
138 StepEditor::stop_step_editing ()
139 {
140         if (step_editor) {
141                 step_editor->hide ();
142         }
143
144         if (step_edit_region_view) {
145                 step_edit_region_view->hide_step_edit_cursor();
146         }
147
148         step_edit_region.reset ();
149 }
150
151 void
152 StepEditor::check_step_edit ()
153 {
154         MidiRingBuffer<nframes_t>& incoming (_track->step_edit_ring_buffer());
155         uint8_t* buf;
156         uint32_t bufsize = 32;
157
158         buf = new uint8_t[bufsize];
159
160         while (incoming.read_space()) {
161                 nframes_t time;
162                 Evoral::EventType type;
163                 uint32_t size;
164
165                 incoming.read_prefix (&time, &type, &size);
166
167                 if (size > bufsize) {
168                         delete [] buf;
169                         bufsize = size;
170                         buf = new uint8_t[bufsize];
171                 }
172
173                 incoming.read_contents (size, buf);
174
175                 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
176                         step_add_note (buf[0] & 0xf, buf[1], buf[2], 0.0);
177                 }
178         }
179 }
180
181 int
182 StepEditor::step_add_bank_change (uint8_t channel, uint8_t bank)
183 {
184         return 0;
185 }
186
187 int
188 StepEditor::step_add_program_change (uint8_t channel, uint8_t program)
189 {
190         return 0;
191 }
192
193 void
194 StepEditor::step_edit_sustain (Evoral::MusicalTime beats)
195 {
196         if (step_edit_region_view) {
197                 step_edit_region_view->step_sustain (beats);
198         }
199 }
200
201 void
202 StepEditor::move_step_edit_beat_pos (Evoral::MusicalTime beats)
203 {
204         if (beats > 0.0) {
205                 step_edit_beat_pos = min (step_edit_beat_pos + beats, 
206                                           step_edit_region_view->frames_to_beats (step_edit_region->length()));
207         } else if (beats < 0.0) {
208                 if (beats < step_edit_beat_pos) {
209                         step_edit_beat_pos += beats; // its negative, remember
210                 } else {
211                         step_edit_beat_pos = 0;
212                 }
213         }
214         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
215 }
216
217 int
218 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evoral::MusicalTime beat_duration)
219 {
220         /* do these things in case undo removed the step edit region
221         */
222         if (!step_edit_region) {
223                 resync_step_edit_position ();
224                 prepare_step_edit_region ();
225                 reset_step_edit_beat_pos ();
226                 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
227                 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
228         }
229
230         assert (step_edit_region);
231         assert (step_edit_region_view);
232                 
233         if (beat_duration == 0.0) {
234                 bool success;
235                 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
236                 
237                 if (!success) {
238                         return -1;
239                 }
240         }
241         
242         MidiStreamView* msv = _mtv.midi_view();
243         
244         /* make sure its visible on the vertical axis */
245         
246         if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
247                 msv->update_note_range (pitch);
248                 msv->set_note_range (MidiStreamView::ContentsRange);
249         }
250         
251         /* make sure its visible on the horizontal axis */
252         
253         nframes64_t fpos = step_edit_region->position() + 
254                 step_edit_region_view->beats_to_frames (step_edit_beat_pos + beat_duration);
255         
256         if (fpos >= (_editor.leftmost_position() + _editor.current_page_frames())) {
257                 _editor.reset_x_origin (fpos - (_editor.current_page_frames()/4));
258         }
259         
260         step_edit_region_view->step_add_note (channel, pitch, velocity, step_edit_beat_pos, beat_duration);
261         
262         if (_step_edit_triplet_countdown > 0) {
263                 _step_edit_triplet_countdown--;
264                 
265                 if (_step_edit_triplet_countdown == 0) {
266                         _step_edit_triplet_countdown = 3;
267                 }
268         }
269         
270         if (!_step_edit_within_chord) {
271                 step_edit_beat_pos += beat_duration;
272                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
273         } else {
274                 step_edit_beat_pos += 1.0/Meter::ticks_per_beat; // tiny, but no longer overlapping
275                 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
276         }
277
278         return 0;
279 }
280
281 void
282 StepEditor::set_step_edit_cursor_width (Evoral::MusicalTime beats)
283 {
284         if (step_edit_region_view) {
285                 step_edit_region_view->set_step_edit_cursor_width (beats);
286         }
287 }
288
289 bool
290 StepEditor::step_edit_within_triplet() const
291 {
292         return _step_edit_triplet_countdown > 0;
293 }
294
295 bool
296 StepEditor::step_edit_within_chord() const
297 {
298         return _step_edit_within_chord;
299 }
300
301 void
302 StepEditor::step_edit_toggle_triplet ()
303 {
304         if (_step_edit_triplet_countdown == 0) {
305                 _step_edit_within_chord = false;
306                 _step_edit_triplet_countdown = 3;
307         } else {
308                 _step_edit_triplet_countdown = 0;
309         }
310 }
311
312 void
313 StepEditor::step_edit_toggle_chord ()
314 {
315         if (_step_edit_within_chord) {
316                 _step_edit_within_chord = false;
317                 step_edit_beat_pos += _step_edit_chord_duration;
318                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
319         } else {
320                 _step_edit_triplet_countdown = 0;
321                 _step_edit_within_chord = true;
322         }
323 }
324
325 void
326 StepEditor::step_edit_rest (Evoral::MusicalTime beats)
327 {
328         bool success;
329
330         if (beats == 0.0) {
331                 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
332         } else {
333                 success = true;
334         }
335
336         if (success) {
337                 step_edit_beat_pos += beats;
338                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
339         }
340 }
341
342 void 
343 StepEditor::step_edit_beat_sync ()
344 {
345         step_edit_beat_pos = ceil (step_edit_beat_pos);
346         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
347 }
348
349 void 
350 StepEditor::step_edit_bar_sync ()
351 {
352         Session* _session = _mtv.session ();
353
354         if (!_session || !step_edit_region_view || !step_edit_region) {
355                 return;
356         }
357
358         framepos_t fpos = step_edit_region->position() + 
359                 step_edit_region_view->beats_to_frames (step_edit_beat_pos);
360         fpos = _session->tempo_map().round_to_bar (fpos, 1);
361         step_edit_beat_pos = ceil (step_edit_region_view->frames_to_beats (fpos - step_edit_region->position()));
362         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
363 }
364
365 void
366 StepEditor::playlist_changed ()
367 {
368         step_edit_region_connection.disconnect ();
369         _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
370                                                    ui_bind (&StepEditor::region_removed, this, _1),
371                                                    gui_context());
372 }
373
374 void
375 StepEditor::region_removed (boost::weak_ptr<Region> wr)
376 {
377         boost::shared_ptr<Region> r (wr.lock());
378  
379         if (!r) {
380                 return;
381         }
382
383         if (step_edit_region == r) {
384                 step_edit_region.reset();
385                 step_edit_region_view = 0;
386                 // force a recompute of the insert position
387                 step_edit_beat_pos = -1.0;
388         }
389 }
390
391 string
392 StepEditor::name() const 
393 {
394         return _track->name();
395 }