initial pass at session-renaming functionality
[ardour.git] / libs / ardour / midi_stretch.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: David 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, Progress* progress)
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         Glib::Mutex::Lock sl (new_src->mutex ());
88
89         new_src->load_model(false, true);
90         boost::shared_ptr<MidiModel> new_model = new_src->model();
91         new_model->start_write();
92
93         /* Note: pass true into force_discrete for the begin() iterator so that the model doesn't
94          * do interpolation of controller data when we stretch.
95          */
96         for (Evoral::Sequence<MidiModel::TimeType>::const_iterator i = old_model->begin (0, true);
97                         i != old_model->end(); ++i) {
98                 const double new_time = i->time() * _request.time_fraction;
99
100                 // FIXME: double copy
101                 Evoral::Event<MidiModel::TimeType> ev(*i, true);
102                 ev.time() = new_time;
103                 new_model->append(ev, Evoral::next_event_id());
104         }
105
106         new_model->end_write ();
107         new_model->set_edited (true);
108
109         new_src->copy_interpolation_from (src);
110
111         const int ret = finish (region, nsrcs, new_name);
112
113         results[0]->set_length((framecnt_t) floor (r->length() * _request.time_fraction));
114
115         return ret;
116 }
117