Added missing files.
[ardour.git] / libs / ardour / midi_stretch.cc
1 /*
2     Copyright (C) 2008 Paul Davis 
3     Author:  Dave Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <pbd/error.h>
22
23 #include <ardour/types.h>
24 #include <ardour/midi_stretch.h>
25 #include <ardour/session.h>
26 #include <ardour/midi_region.h>
27
28 #include "i18n.h"
29
30 using namespace std;
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 MidiStretch::MidiStretch (Session& s, TimeFXRequest& req)
35         : Filter (s)
36         , _request (req)
37 {
38 }
39
40 MidiStretch::~MidiStretch ()
41 {
42 }
43
44 int
45 MidiStretch::run (boost::shared_ptr<Region> r)
46 {
47         SourceList nsrcs;
48         char suffix[32];
49
50         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(r);
51         if (!region)
52                 return -1;
53
54         /* the name doesn't need to be super-precise, but allow for 2 fractional
55            digits just to disambiguate close but not identical stretches.
56         */
57         
58         snprintf (suffix, sizeof (suffix), "@%d", (int) floor (_request.time_fraction * 100.0f));
59         
60         string new_name = region->name();
61         string::size_type at = new_name.find ('@');
62
63         // remove any existing stretch indicator
64
65         if (at != string::npos && at > 2) {
66                 new_name = new_name.substr (0, at - 1);
67         }
68
69         new_name += suffix;
70         
71         /* create new sources */
72         
73         if (make_new_sources (region, nsrcs, suffix))
74                 return -1;
75
76         // FIXME: how to make a whole file region if it isn't?
77         //assert(region->whole_file());
78         
79         boost::shared_ptr<MidiSource> src = region->midi_source(0);
80         src->load_model();
81
82         boost::shared_ptr<MidiModel> old_model = src->model();
83
84         boost::shared_ptr<MidiSource> new_src = boost::dynamic_pointer_cast<MidiSource>(nsrcs[0]);
85         assert(new_src);
86
87         boost::shared_ptr<MidiModel> new_model = new_src->model();
88         new_model->start_write();
89         
90         for (MidiModel::const_iterator i = old_model->begin(); i != old_model->end(); ++i) {
91                 const double new_time = i->time() * _request.time_fraction;
92                 
93                 // FIXME: double copy
94                 MidiEvent ev = MidiEvent(*i, true);
95                 ev.time() = new_time;
96                 new_model->append(ev);
97         }
98
99         new_model->end_write();
100         new_model->set_edited(true);
101         
102         const int ret = finish (region, nsrcs, new_name);
103         
104         results[0]->set_length(r->length() * _request.time_fraction, NULL);
105
106         return ret;
107 }
108