Remove unused quantize type options.
[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/types.h"
24 #include "ardour/quantize.h"
25 #include "ardour/session.h"
26 #include "ardour/smf_source.h"
27 #include "ardour/midi_model.h"
28 #include "ardour/midi_region.h"
29 #include "ardour/tempo.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace PBD;
35 using namespace ARDOUR;
36
37 /** Quantize notes
38  *
39  * grid parameters are the quantize value in beats, ie 1.0 = quantize to beats,
40  * 0.25 = quantize to beats/4, etc.
41  */
42
43 Quantize::Quantize (Session& s, bool snap_start, bool snap_end,
44                     double start_grid, double end_grid,
45                     float strength, float swing, float threshold)
46         : session (s)
47         , _snap_start (snap_start)
48         , _snap_end (snap_end)
49         , _start_grid(start_grid)
50         , _end_grid(end_grid)
51         , _strength (strength/100.0)
52         , _swing (swing/100.0)
53         , _threshold (threshold)
54 {
55 }
56
57 Quantize::~Quantize ()
58 {
59 }
60
61 Command*
62 Quantize::operator () (boost::shared_ptr<MidiModel> model,
63                        double position,
64                        std::vector<Evoral::Sequence<Evoral::MusicalTime>::Notes>& seqs)
65 {
66         /* Calculate offset from start of model to next closest quantize step,
67            to quantize relative to actual session beats (etc.) rather than from the
68            start of the model.
69         */
70         const double round_pos = ceil(position / _start_grid) * _start_grid;
71         const double offset    = round_pos - position;
72
73         bool even;
74         MidiModel::NoteDiffCommand* cmd = new MidiModel::NoteDiffCommand (model, "quantize");
75
76         for (std::vector<Evoral::Sequence<Evoral::MusicalTime>::Notes>::iterator s = seqs.begin(); s != seqs.end(); ++s) {
77
78                 even = false;
79
80                 for (Evoral::Sequence<MidiModel::TimeType>::Notes::iterator i = (*s).begin(); i != (*s).end(); ++i) {
81
82                         double new_start = round ((*i)->time() / _start_grid) * _start_grid + offset;
83                         double new_end = round ((*i)->end_time() / _end_grid) * _end_grid + offset;
84
85                         if (_swing > 0.0 && !even) {
86
87                                 double next_grid = new_start + _start_grid;
88
89                                 /* find a spot 2/3 (* swing factor) of the way between the grid point
90                                    we would put this note at, and the nominal position of the next note.
91                                 */
92
93                                 new_start = new_start + (2.0/3.0 * _swing * (next_grid - new_start));
94
95                         } else if (_swing < 0.0 && !even) {
96
97                                 double prev_grid = new_start - _start_grid;
98
99                                 /* find a spot 2/3 (* swing factor) of the way between the grid point
100                                    we would put this note at, and the nominal position of the previous note.
101                                 */
102
103                                 new_start = new_start - (2.0/3.0 * _swing * (new_start - prev_grid));
104
105                         }
106
107                         double delta = new_start - (*i)->time();
108
109                         if (fabs (delta) >= _threshold) {
110                                 if (_snap_start) {
111                                         delta *= _strength;
112                                         cmd->change ((*i), MidiModel::NoteDiffCommand::StartTime,
113                                                      (*i)->time() + delta);
114                                 }
115                         }
116
117                         if (_snap_end) {
118                                 delta = new_end - (*i)->end_time();
119
120                                 if (fabs (delta) >= _threshold) {
121                                         double new_dur = new_end - new_start;
122
123                                         if (new_dur == 0.0) {
124                                                 new_dur = _end_grid;
125                                         }
126
127                                         cmd->change ((*i), MidiModel::NoteDiffCommand::Length, new_dur);
128                                 }
129                         }
130
131                         even = !even;
132                 }
133         }
134
135         return cmd;
136 }