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>
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.
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.
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.
23 #include "pbd/basename.h"
25 #include "ardour/quantize.h"
26 #include "ardour/midi_model.h"
32 using namespace ARDOUR;
36 * grid parameters are the quantize value in beats, ie 1.0 = quantize to beats,
37 * 0.25 = quantize to beats/4, etc.
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)
47 , _strength (strength/100.0)
48 , _swing (swing/100.0)
49 , _threshold (threshold)
53 Quantize::~Quantize ()
58 swing_position (double pos, double grid, double swing, double offset)
60 /* beats start out numbered at zero.
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.
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.
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.
75 * so the criterion for a position being swung is
76 * whether or not ((possible_grid_position / grid) % 2) != 0
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;
82 /* one of these will not be subject to swing */
84 double swung_pos = pos;
85 double swung_previous_grid_position;
88 swung_previous_grid_position = pos - grid;
90 swung_previous_grid_position = 0.0;
93 if (swing_previous_grid_position) {
94 swung_previous_grid_position = swung_previous_grid_position + (2.0/3.0 * swing * grid);
97 if (swing_quantize_grid_position) {
98 swung_pos = swung_pos + (2.0/3.0 * swing * grid);
101 /* now correct for start-of-model offset */
105 if (fabs (pos - swung_pos) > fabs (pos - swung_previous_grid_position)) {
106 pos = swung_previous_grid_position;
115 Quantize::operator () (boost::shared_ptr<MidiModel> model,
116 Temporal::Beats position,
117 std::vector<Evoral::Sequence<Temporal::Beats>::Notes>& seqs)
119 /* TODO: Rewrite this to be precise with fixed point? */
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
125 const double round_pos = round(position.to_double() / _start_grid) * _start_grid;
126 const double offset = round_pos - position.to_double();
128 MidiModel::NoteDiffCommand* cmd = new MidiModel::NoteDiffCommand (model, "quantize");
130 for (std::vector<Evoral::Sequence<Temporal::Beats>::Notes>::iterator s = seqs.begin(); s != seqs.end(); ++s) {
132 for (Evoral::Sequence<MidiModel::TimeType>::Notes::iterator i = (*s).begin(); i != (*s).end(); ++i) {
134 /* compute new start + end points WITHOUT the offset
135 * caused by the start of the model (see above).
137 * these versions of new_start and new_end are
138 * guaranteed to precisely align with the quantize grid(s).
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;
146 new_start = swing_position (new_start, _start_grid, _swing, offset);
147 new_end = swing_position (new_end, _end_grid, _swing, offset);
151 /* now correct for start-of-model offset */
157 double delta = new_start - (*i)->time().to_double();
160 if (fabs (delta) >= _threshold) {
163 cmd->change ((*i), MidiModel::NoteDiffCommand::StartTime,
164 (*i)->time() + delta);
169 delta = new_end - (*i)->end_time().to_double();
171 if (fabs (delta) >= _threshold) {
172 Temporal::Beats new_dur(new_end - new_start);
175 new_dur = Temporal::Beats(_end_grid);
178 cmd->change ((*i), MidiModel::NoteDiffCommand::Length, new_dur);