Make SMFSource suck significantly less.
[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
20 #include <pbd/basename.h>
21
22 #include <ardour/types.h>
23 #include <ardour/quantize.h>
24 #include <ardour/session.h>
25 #include <ardour/smf_source.h>
26 #include <ardour/midi_region.h>
27 #include <ardour/tempo.h>
28
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace ARDOUR;
33
34
35 /** Quantize notes, valid for MIDI regions only.
36  *
37  * Q is the quantize value in beats, ie 1.0 = quantize to beats,
38  * 0.25 = quantize to beats/4, etc.
39  */
40 Quantize::Quantize (Session& s, double q)
41         : Filter (s)
42         , _q(q)
43 {
44 }
45
46 Quantize::~Quantize ()
47 {
48 }
49
50 int
51 Quantize::run (boost::shared_ptr<Region> r)
52 {
53         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(r);
54         if (!region)
55                 return -1;
56
57         // FIXME: how to make a whole file region if it isn't?
58         //assert(region->whole_file());
59
60         boost::shared_ptr<MidiSource> src = region->midi_source(0);
61         src->load_model();
62
63         boost::shared_ptr<MidiModel> model = src->model();
64         
65         // FIXME: Model really needs to be switched to beat time (double) ASAP
66         
67         const Tempo& t = session.tempo_map().tempo_at(r->start());
68         const Meter& m = session.tempo_map().meter_at(r->start());
69
70         double q_frames = _q * (m.frames_per_bar(t, session.frame_rate()) / (double)m.beats_per_bar());
71
72         for (MidiModel::Notes::iterator i = model->notes().begin(); i != model->notes().end(); ++i) {
73                 const double new_time = lrint(i->time() / q_frames) * q_frames;
74                 const double new_dur = ((i->time() != 0 && new_dur < (q_frames * 1.5))
75                         ? q_frames
76                         : lrint(i->duration() / q_frames) * q_frames);
77                 
78                 i->set_time(new_time);
79                 i->set_duration(new_dur);
80         }
81
82         model->set_edited(true);
83
84         return 0;
85 #if 0
86         SourceList nsrcs;
87         SourceList::iterator si;
88         nframes_t blocksize = 256 * 1024;
89         Sample* buf = 0;
90         nframes_t fpos;
91         nframes_t fstart;
92         nframes_t to_read;
93         int ret = -1;
94
95         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(r);
96         if (!region)
97                 return ret;
98
99         /* create new sources */
100
101         if (make_new_sources (region, nsrcs)) {
102                 goto out;
103         }
104
105         fstart = region->start();
106
107         if (blocksize > region->length()) {
108                 blocksize = region->length();
109         }
110
111         fpos = max (fstart, (fstart + region->length() - blocksize));
112         buf = new Sample[blocksize];
113         to_read = blocksize;
114
115         /* now read it backwards */
116
117         while (to_read) {
118
119                 uint32_t n;
120
121                 for (n = 0, si = nsrcs.begin(); n < region->n_channels(); ++n, ++si) {
122
123                         /* read it in */
124                         
125                         if (region->audio_source (n)->read (buf, fpos, to_read) != to_read) {
126                                 goto out;
127                         }
128
129                         /* swap memory order */
130                         
131                         for (nframes_t i = 0; i < to_read/2; ++i) {
132                                 swap (buf[i],buf[to_read-1-i]);
133                         }
134                         
135                         /* write it out */
136
137                         boost::shared_ptr<AudioSource> asrc(boost::dynamic_pointer_cast<AudioSource>(*si));
138
139                         if (asrc && asrc->write (buf, to_read) != to_read) {
140                                 goto out;
141                         }
142                 }
143
144                 if (fpos > fstart + blocksize) {
145                         fpos -= to_read;
146                         to_read = blocksize;
147                 } else {
148                         to_read = fpos - fstart;
149                         fpos = fstart;
150                 }
151         };
152
153         ret = finish (region, nsrcs);
154
155   out:
156
157         if (buf) {
158                 delete [] buf;
159         }
160
161         if (ret) {
162                 for (si = nsrcs.begin(); si != nsrcs.end(); ++si) {
163                         boost::shared_ptr<AudioSource> asrc(boost::dynamic_pointer_cast<AudioSource>(*si));
164                         asrc->mark_for_remove ();
165                 }
166         }
167         
168         return ret;
169 #endif
170 }