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