~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/ardour/quantize.cc

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
ImportĀ upstreamĀ versionĀ 3.4~dfsg

Show diffs side-by-side

added added

removed removed

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