Heavy-duty abstraction work to split type-specific classes into
[ardour.git] / libs / ardour / track.cc
1 /*
2     Copyright (C) 2006 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 #include <pbd/error.h>
19 #include <sigc++/retype.h>
20 #include <sigc++/retype_return.h>
21 #include <sigc++/bind.h>
22
23 #include <ardour/track.h>
24 #include <ardour/diskstream.h>
25 #include <ardour/session.h>
26 #include <ardour/redirect.h>
27 #include <ardour/audioregion.h>
28 #include <ardour/audiosource.h>
29 #include <ardour/route_group_specialized.h>
30 #include <ardour/insert.h>
31 #include <ardour/audioplaylist.h>
32 #include <ardour/panner.h>
33 #include <ardour/utils.h>
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
42         : Route (sess, name, 1, -1, -1, -1, flag, default_type)
43         , _diskstream (0)
44         ,  _rec_enable_control (*this)
45 {
46         _declickable = true;
47         _freeze_record.state = NoFreeze;
48         _saved_meter_point = _meter_point;
49         _mode = mode;
50 }
51
52 Track::Track (Session& sess, const XMLNode& node, DataType default_type)
53         : Route (sess, "to be renamed", 0, 0, -1, -1, Route::Flag(0), default_type)
54         , _diskstream (0)
55         , _rec_enable_control (*this)
56 {
57         _freeze_record.state = NoFreeze;
58         _declickable = true;
59         _saved_meter_point = _meter_point;
60 }
61
62 Track::~Track ()
63 {
64         if (_diskstream) {
65                 _diskstream->unref();
66         }
67 }
68
69 void
70 Track::set_meter_point (MeterPoint p, void *src)
71 {
72         Route::set_meter_point (p, src);
73 }
74
75 XMLNode&
76 Track::get_state ()
77 {
78         return state (true);
79 }
80
81 XMLNode&
82 Track::get_template ()
83 {
84         return state (false);
85 }
86
87 void
88 Track::toggle_monitor_input ()
89 {
90         for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
91                 (*i)->request_monitor_input(!(*i)->monitoring_input());
92         }
93 }
94
95 jack_nframes_t
96 Track::update_total_latency ()
97 {
98         _own_latency = 0;
99
100         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
101                 if ((*i)->active ()) {
102                         _own_latency += (*i)->latency ();
103                 }
104         }
105
106         set_port_latency (_own_latency);
107
108         return _own_latency;
109 }
110
111
112 Track::FreezeRecord::~FreezeRecord ()
113 {
114         for (vector<FreezeRecordInsertInfo*>::iterator i = insert_info.begin(); i != insert_info.end(); ++i) {
115                 delete *i;
116         }
117 }
118
119 Track::FreezeState
120 Track::freeze_state() const
121 {
122         return _freeze_record.state;
123 }
124
125 Track::RecEnableControllable::RecEnableControllable (Track& s)
126         : track (s)
127 {
128 }
129
130 void
131 Track::RecEnableControllable::set_value (float val)
132 {
133         bool bval = ((val >= 0.5f) ? true: false);
134         track.set_record_enable (bval, this);
135 }
136
137 float
138 Track::RecEnableControllable::get_value (void) const
139 {
140         if (track.record_enabled()) { return 1.0f; }
141         return 0.0f;
142 }
143
144 bool
145 Track::record_enabled () const
146 {
147         return _diskstream->record_enabled ();
148 }
149         
150 void
151 Track::set_record_enable (bool yn, void *src)
152 {
153         if (_freeze_record.state == Frozen) {
154                 return;
155         }
156
157         if (_mix_group && src != _mix_group && _mix_group->is_active()) {
158                 _mix_group->apply (&Track::set_record_enable, yn, _mix_group);
159                 return;
160         }
161
162         /* keep track of the meter point as it was before we rec-enabled */
163
164         if (!_diskstream->record_enabled()) {
165                 _saved_meter_point = _meter_point;
166         }
167         
168         _diskstream->set_record_enabled (yn);
169
170         if (_diskstream->record_enabled()) {
171                 set_meter_point (MeterInput, this);
172         } else {
173                 set_meter_point (_saved_meter_point, this);
174         }
175
176         _rec_enable_control.Changed ();
177 }
178
179 void
180 Track::set_mode (TrackMode m)
181 {
182         if (_diskstream) {
183                 if (_mode != m) {
184                         _mode = m;
185                         _diskstream->set_destructive (m == Destructive);
186                         ModeChanged();
187                 }
188         }
189 }
190
191 int
192 Track::set_name (string str, void *src)
193 {
194         int ret;
195
196         if (record_enabled() && _session.actively_recording()) {
197                 /* this messes things up if done while recording */
198                 return -1;
199         }
200
201         if (_diskstream->set_name (str)) {
202                 return -1;
203         }
204
205         /* save state so that the statefile fully reflects any filename changes */
206
207         if ((ret = IO::set_name (str, src)) == 0) {
208                 _session.save_state ("");
209         }
210         return ret;
211 }
212
213 void
214 Track::set_latency_delay (jack_nframes_t longest_session_latency)
215 {
216         Route::set_latency_delay (longest_session_latency);
217         _diskstream->set_roll_delay (_roll_delay);
218 }
219