Expose virtual-keyboard port as async-port
[ardour.git] / libs / ardour / quantize.cc
1 /*
2  * Copyright (C) 2007-2015 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2015 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #include <cmath>
22
23 #include "pbd/basename.h"
24
25 #include "ardour/quantize.h"
26 #include "ardour/midi_model.h"
27
28 #include "pbd/i18n.h"
29
30 using namespace std;
31 using namespace PBD;
32 using namespace ARDOUR;
33
34 /** Quantize notes
35  *
36  * grid parameters are the quantize value in beats, ie 1.0 = quantize to beats,
37  * 0.25 = quantize to beats/4, etc.
38  */
39
40 Quantize::Quantize (bool snap_start, bool snap_end,
41                     double start_grid, double end_grid,
42                     float strength, float swing, float threshold)
43         : _snap_start (snap_start)
44         , _snap_end (snap_end)
45         , _start_grid(start_grid)
46         , _end_grid(end_grid)
47         , _strength (strength/100.0)
48         , _swing (swing/100.0)
49         , _threshold (threshold)
50 {
51 }
52
53 Quantize::~Quantize ()
54 {
55 }
56
57 static double
58 swing_position (double pos, double grid, double swing, double offset)
59 {
60         /* beats start out numbered at zero.
61          *
62          * every other position on the start-quantize-grid is
63          * optionally swung, meaning that its position is moved
64          * somewhere between its natural position and 2/3 of
65          * the way to the next start-quantize-grid position.
66          *
67          * so, if the _start grid is 0.5, the beat at 0 isn't
68          * swung, but something at 0.5 is, the beat at 1 isn't
69          * swung, but something at 1.5 is.
70          *
71          * if the start grid is 1.0, the beat at 0 isn't swung,
72          * but the beat at 1.0 is. the beat at 2.0 isn't swung,
73          * but the beat at 3.0 is. and so on.
74          *
75          * so the criterion for a position being swung is
76          * whether or not ((possible_grid_position / grid) % 2) != 0
77          */
78
79         const bool swing_quantize_grid_position = pos > 0.0 && fmod ((pos/grid), 2.0) != 0;
80         const bool swing_previous_grid_position = pos > grid && fmod ((pos-grid)/grid, 2.0) != 0;
81
82         /* one of these will not be subject to swing */
83
84         double swung_pos = pos;
85         double swung_previous_grid_position;
86
87         if (pos > grid) {
88                 swung_previous_grid_position = pos - grid;
89         } else {
90                 swung_previous_grid_position = 0.0;
91         }
92
93         if (swing_previous_grid_position) {
94                 swung_previous_grid_position = swung_previous_grid_position + (2.0/3.0 * swing * grid);
95         }
96
97         if (swing_quantize_grid_position) {
98                 swung_pos = swung_pos + (2.0/3.0 * swing * grid);
99         }
100
101         /* now correct for start-of-model offset */
102
103         pos += offset;
104
105         if (fabs (pos - swung_pos) > fabs (pos - swung_previous_grid_position)) {
106                 pos = swung_previous_grid_position;
107         } else {
108                 pos = swung_pos;
109         }
110
111         return pos;
112 }
113
114 Command*
115 Quantize::operator () (boost::shared_ptr<MidiModel> model,
116                        Temporal::Beats position,
117                        std::vector<Evoral::Sequence<Temporal::Beats>::Notes>& seqs)
118 {
119         /* TODO: Rewrite this to be precise with fixed point? */
120
121         /* Calculate offset from start of model to next closest quantize step,
122            to quantize relative to actual session beats (etc.) rather than from the
123            start of the model.
124         */
125         const double round_pos = round(position.to_double() / _start_grid) * _start_grid;
126         const double offset    = round_pos - position.to_double();
127
128         MidiModel::NoteDiffCommand* cmd = new MidiModel::NoteDiffCommand (model, "quantize");
129
130         for (std::vector<Evoral::Sequence<Temporal::Beats>::Notes>::iterator s = seqs.begin(); s != seqs.end(); ++s) {
131
132                 for (Evoral::Sequence<MidiModel::TimeType>::Notes::iterator i = (*s).begin(); i != (*s).end(); ++i) {
133
134                         /* compute new start + end points WITHOUT the offset
135                          * caused by the start of the model (see above).
136                          *
137                          * these versions of new_start and new_end are
138                          * guaranteed to precisely align with the quantize grid(s).
139                          */
140
141                         double new_start = round (((*i)->time().to_double() - offset) / _start_grid) * _start_grid;
142                         double new_end = round (((*i)->end_time().to_double() - offset) / _end_grid) * _end_grid;
143
144                         if (_swing) {
145
146                                 new_start = swing_position (new_start, _start_grid, _swing, offset);
147                                 new_end = swing_position (new_end, _end_grid, _swing, offset);
148
149                         } else {
150
151                                 /* now correct for start-of-model offset */
152
153                                 new_start += offset;
154                                 new_end += offset;
155                         }
156
157                         double delta = new_start - (*i)->time().to_double();
158
159
160                         if (fabs (delta) >= _threshold) {
161                                 if (_snap_start) {
162                                         delta *= _strength;
163                                         cmd->change ((*i), MidiModel::NoteDiffCommand::StartTime,
164                                                      (*i)->time() + delta);
165                                 }
166                         }
167
168                         if (_snap_end) {
169                                 delta = new_end - (*i)->end_time().to_double();
170
171                                 if (fabs (delta) >= _threshold) {
172                                         Temporal::Beats new_dur(new_end - new_start);
173
174                                         if (!new_dur) {
175                                                 new_dur = Temporal::Beats(_end_grid);
176                                         }
177
178                                         cmd->change ((*i), MidiModel::NoteDiffCommand::Length, new_dur);
179                                 }
180                         }
181                 }
182         }
183
184         return cmd;
185 }