convert from Glib:: to Glib::Threads for all thread-related API
[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/midi_model.h"
24 #include "ardour/midi_region.h"
25 #include "ardour/midi_source.h"
26 #include "ardour/midi_stretch.h"
27 #include "ardour/types.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 MidiStretch::MidiStretch (Session& s, const TimeFXRequest& req)
36         : Filter (s)
37         , _request (req)
38 {
39 }
40
41 MidiStretch::~MidiStretch ()
42 {
43 }
44
45 int
46 MidiStretch::run (boost::shared_ptr<Region> r, Progress*)
47 {
48         SourceList nsrcs;
49         char suffix[32];
50
51         boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(r);
52         if (!region)
53                 return -1;
54
55         /* the name doesn't need to be super-precise, but allow for 2 fractional
56            digits just to disambiguate close but not identical stretches.
57         */
58
59         snprintf (suffix, sizeof (suffix), "@%d", (int) floor (_request.time_fraction * 100.0f));
60
61         string new_name = region->name();
62         string::size_type at = new_name.find ('@');
63
64         // remove any existing stretch indicator
65
66         if (at != string::npos && at > 2) {
67                 new_name = new_name.substr (0, at - 1);
68         }
69
70         new_name += suffix;
71
72         /* create new sources */
73
74         if (make_new_sources (region, nsrcs, suffix))
75                 return -1;
76
77         // FIXME: how to make a whole file region if it isn't?
78         //assert(region->whole_file());
79
80         boost::shared_ptr<MidiSource> src = region->midi_source(0);
81         src->load_model();
82
83         boost::shared_ptr<MidiModel> old_model = src->model();
84
85         boost::shared_ptr<MidiSource> new_src = boost::dynamic_pointer_cast<MidiSource>(nsrcs[0]);
86         assert(new_src);
87
88         Glib::Threads::Mutex::Lock sl (new_src->mutex ());
89
90         new_src->load_model(false, true);
91         boost::shared_ptr<MidiModel> new_model = new_src->model();
92         new_model->start_write();
93
94         /* Note: pass true into force_discrete for the begin() iterator so that the model doesn't
95          * do interpolation of controller data when we stretch.
96          */
97         for (Evoral::Sequence<MidiModel::TimeType>::const_iterator i = old_model->begin (0, true);
98                         i != old_model->end(); ++i) {
99                 const double new_time = i->time() * _request.time_fraction;
100
101                 // FIXME: double copy
102                 Evoral::Event<MidiModel::TimeType> ev(*i, true);
103                 ev.set_time(new_time);
104                 new_model->append(ev, Evoral::next_event_id());
105         }
106
107         new_model->end_write (Evoral::Sequence<Evoral::MusicalTime>::DeleteStuckNotes);
108         new_model->set_edited (true);
109
110         new_src->copy_interpolation_from (src);
111
112         const int ret = finish (region, nsrcs, new_name);
113
114         results[0]->set_length((framecnt_t) floor (r->length() * _request.time_fraction));
115
116         return ret;
117 }
118