2 Copyright (C) 2004 Paul Davis
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.
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.
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.
21 #include "pbd/basename.h"
23 #include "ardour/quantize.h"
24 #include "ardour/midi_model.h"
30 using namespace ARDOUR;
34 * grid parameters are the quantize value in beats, ie 1.0 = quantize to beats,
35 * 0.25 = quantize to beats/4, etc.
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)
45 , _strength (strength/100.0)
46 , _swing (swing/100.0)
47 , _threshold (threshold)
51 Quantize::~Quantize ()
56 swing_position (double pos, double grid, double swing, double offset)
58 /* beats start out numbered at zero.
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.
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.
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.
73 * so the criterion for a position being swung is
74 * whether or not ((possible_grid_position / grid) % 2) != 0
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;
80 /* one of these will not be subject to swing */
82 double swung_pos = pos;
83 double swung_previous_grid_position;
86 swung_previous_grid_position = pos - grid;
88 swung_previous_grid_position = 0.0;
91 if (swing_previous_grid_position) {
92 swung_previous_grid_position = swung_previous_grid_position + (2.0/3.0 * swing * grid);
95 if (swing_quantize_grid_position) {
96 swung_pos = swung_pos + (2.0/3.0 * swing * grid);
99 /* now correct for start-of-model offset */
103 if (fabs (pos - swung_pos) > fabs (pos - swung_previous_grid_position)) {
104 pos = swung_previous_grid_position;
113 Quantize::operator () (boost::shared_ptr<MidiModel> model,
114 Evoral::Beats position,
115 std::vector<Evoral::Sequence<Evoral::Beats>::Notes>& seqs)
117 /* TODO: Rewrite this to be precise with fixed point? */
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
123 const double round_pos = round(position.to_double() / _start_grid) * _start_grid;
124 const double offset = round_pos - position.to_double();
126 MidiModel::NoteDiffCommand* cmd = new MidiModel::NoteDiffCommand (model, "quantize");
128 for (std::vector<Evoral::Sequence<Evoral::Beats>::Notes>::iterator s = seqs.begin(); s != seqs.end(); ++s) {
130 for (Evoral::Sequence<MidiModel::TimeType>::Notes::iterator i = (*s).begin(); i != (*s).end(); ++i) {
132 /* compute new start + end points WITHOUT the offset
133 * caused by the start of the model (see above).
135 * these versions of new_start and new_end are
136 * guaranteed to precisely align with the quantize grid(s).
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;
144 new_start = swing_position (new_start, _start_grid, _swing, offset);
145 new_end = swing_position (new_end, _end_grid, _swing, offset);
149 /* now correct for start-of-model offset */
155 double delta = new_start - (*i)->time().to_double();
158 if (fabs (delta) >= _threshold) {
161 cmd->change ((*i), MidiModel::NoteDiffCommand::StartTime,
162 (*i)->time() + delta);
167 delta = new_end - (*i)->end_time().to_double();
169 if (fabs (delta) >= _threshold) {
170 Evoral::Beats new_dur(new_end - new_start);
173 new_dur = Evoral::Beats(_end_grid);
176 cmd->change ((*i), MidiModel::NoteDiffCommand::Length, new_dur);