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