fix up file renaming code a little bit
[ArdourMidi.git] / libs / ardour / midi_stretch.cc
blobdedae6acb1c2e16fa81358c3f04824bcbd4b9199
1 /*
2 Copyright (C) 2008 Paul Davis
3 Author: Dave Robillard
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.
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.
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.
21 #include "pbd/error.h"
23 #include "ardour/types.h"
24 #include "ardour/midi_stretch.h"
25 #include "ardour/session.h"
26 #include "ardour/midi_region.h"
28 #include "i18n.h"
30 using namespace std;
31 using namespace ARDOUR;
32 using namespace PBD;
34 MidiStretch::MidiStretch (Session& s, TimeFXRequest& req)
35 : Filter (s)
36 , _request (req)
40 MidiStretch::~MidiStretch ()
44 int
45 MidiStretch::run (boost::shared_ptr<Region> r)
47 SourceList nsrcs;
48 char suffix[32];
50 boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(r);
51 if (!region)
52 return -1;
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.
58 snprintf (suffix, sizeof (suffix), "@%d", (int) floor (_request.time_fraction * 100.0f));
60 string new_name = region->name();
61 string::size_type at = new_name.find ('@');
63 // remove any existing stretch indicator
65 if (at != string::npos && at > 2) {
66 new_name = new_name.substr (0, at - 1);
69 new_name += suffix;
71 /* create new sources */
73 if (make_new_sources (region, nsrcs, suffix))
74 return -1;
76 // FIXME: how to make a whole file region if it isn't?
77 //assert(region->whole_file());
79 boost::shared_ptr<MidiSource> src = region->midi_source(0);
80 src->load_model();
82 boost::shared_ptr<MidiModel> old_model = src->model();
84 boost::shared_ptr<MidiSource> new_src = boost::dynamic_pointer_cast<MidiSource>(nsrcs[0]);
85 assert(new_src);
87 Glib::Mutex::Lock sl (new_src->mutex ());
89 new_src->load_model(false, true);
90 boost::shared_ptr<MidiModel> new_model = new_src->model();
91 new_model->start_write();
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.
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;
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());
106 new_model->end_write();
107 new_model->set_edited(true);
109 new_src->copy_interpolation_from (src);
111 const int ret = finish (region, nsrcs, new_name);
113 results[0]->set_length((nframes_t) floor (r->length() * _request.time_fraction), NULL);
115 return ret;