fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / quantize.cc
1 /*
2     Copyright (C) 2004 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 #include <cmath>
20
21 #include "pbd/basename.h"
22
23 #include "ardour/quantize.h"
24 #include "ardour/midi_model.h"
25
26 #include "pbd/i18n.h"
27
28 using namespace std;
29 using namespace PBD;
30 using namespace ARDOUR;
31
32 /** Quantize notes
33  *
34  * grid parameters are the quantize value in beats, ie 1.0 = quantize to beats,
35  * 0.25 = quantize to beats/4, etc.
36  */
37
38 Quantize::Quantize (bool snap_start, bool snap_end,
39                     double start_grid, double end_grid,
40                     float strength, float swing, float threshold)
41         : _snap_start (snap_start)
42         , _snap_end (snap_end)
43         , _start_grid(start_grid)
44         , _end_grid(end_grid)
45         , _strength (strength/100.0)
46         , _swing (swing/100.0)
47         , _threshold (threshold)
48 {
49 }
50
51 Quantize::~Quantize ()
52 {
53 }
54
55 static double
56 swing_position (double pos, double grid, double swing, double offset)
57 {
58         /* beats start out numbered at zero.
59          *
60          * every other position on the start-quantize-grid is
61          * optionally swung, meaning that its position is moved
62          * somewhere between its natural position and 2/3 of
63          * the way to the next start-quantize-grid position.
64          *
65          * so, if the _start grid is 0.5, the beat at 0 isn't
66          * swung, but something at 0.5 is, the beat at 1 isn't
67          * swung, but something at 1.5 is.
68          *
69          * if the start grid is 1.0, the beat at 0 isn't swung,
70          * but the beat at 1.0 is. the beat at 2.0 isn't swung,
71          * but the beat at 3.0 is. and so on.
72          *
73          * so the criterion for a position being swung is
74          * whether or not ((possible_grid_position / grid) % 2) != 0
75          */
76
77         const bool swing_quantize_grid_position = pos > 0.0 && fmod ((pos/grid), 2.0) != 0;
78         const bool swing_previous_grid_position = pos > grid && fmod ((pos-grid)/grid, 2.0) != 0;
79
80         /* one of these will not be subject to swing */
81
82         double swung_pos = pos;
83         double swung_previous_grid_position;
84
85         if (pos > grid) {
86                 swung_previous_grid_position = pos - grid;
87         } else {
88                 swung_previous_grid_position = 0.0;
89         }
90
91         if (swing_previous_grid_position) {
92                 swung_previous_grid_position = swung_previous_grid_position + (2.0/3.0 * swing * grid);
93         }
94
95         if (swing_quantize_grid_position) {
96                 swung_pos = swung_pos + (2.0/3.0 * swing * grid);
97         }
98
99         /* now correct for start-of-model offset */
100
101         pos += offset;
102
103         if (fabs (pos - swung_pos) > fabs (pos - swung_previous_grid_position)) {
104                 pos = swung_previous_grid_position;
105         } else {
106                 pos = swung_pos;
107         }
108
109         return pos;
110 }
111
112 Command*
113 Quantize::operator () (boost::shared_ptr<MidiModel> model,
114                        Evoral::Beats position,
115                        std::vector<Evoral::Sequence<Evoral::Beats>::Notes>& seqs)
116 {
117         /* TODO: Rewrite this to be precise with fixed point? */
118
119         /* Calculate offset from start of model to next closest quantize step,
120            to quantize relative to actual session beats (etc.) rather than from the
121            start of the model.
122         */
123         const double round_pos = round(position.to_double() / _start_grid) * _start_grid;
124         const double offset    = round_pos - position.to_double();
125
126         MidiModel::NoteDiffCommand* cmd = new MidiModel::NoteDiffCommand (model, "quantize");
127
128         for (std::vector<Evoral::Sequence<Evoral::Beats>::Notes>::iterator s = seqs.begin(); s != seqs.end(); ++s) {
129
130                 for (Evoral::Sequence<MidiModel::TimeType>::Notes::iterator i = (*s).begin(); i != (*s).end(); ++i) {
131
132                         /* compute new start + end points WITHOUT the offset
133                          * caused by the start of the model (see above).
134                          *
135                          * these versions of new_start and new_end are
136                          * guaranteed to precisely align with the quantize grid(s).
137                          */
138
139                         double new_start = round (((*i)->time().to_double() - offset) / _start_grid) * _start_grid;
140                         double new_end = round (((*i)->end_time().to_double() - offset) / _end_grid) * _end_grid;
141
142                         if (_swing) {
143
144                                 new_start = swing_position (new_start, _start_grid, _swing, offset);
145                                 new_end = swing_position (new_end, _end_grid, _swing, offset);
146
147                         } else {
148
149                                 /* now correct for start-of-model offset */
150
151                                 new_start += offset;
152                                 new_end += offset;
153                         }
154
155                         double delta = new_start - (*i)->time().to_double();
156
157
158                         if (fabs (delta) >= _threshold) {
159                                 if (_snap_start) {
160                                         delta *= _strength;
161                                         cmd->change ((*i), MidiModel::NoteDiffCommand::StartTime,
162                                                      (*i)->time() + delta);
163                                 }
164                         }
165
166                         if (_snap_end) {
167                                 delta = new_end - (*i)->end_time().to_double();
168
169                                 if (fabs (delta) >= _threshold) {
170                                         Evoral::Beats new_dur(new_end - new_start);
171
172                                         if (!new_dur) {
173                                                 new_dur = Evoral::Beats(_end_grid);
174                                         }
175
176                                         cmd->change ((*i), MidiModel::NoteDiffCommand::Length, new_dur);
177                                 }
178                         }
179                 }
180         }
181
182         return cmd;
183 }