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