Make import GUI report if you are importing a file of a name that
[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         ,  _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, node),
54           _rec_enable_control (*this)
55 {
56         _freeze_record.state = NoFreeze;
57         _declickable = true;
58         _saved_meter_point = _meter_point;
59 }
60
61 Track::~Track ()
62 {
63         if (_diskstream) {
64                 _diskstream->drop_references ();
65         }
66 }
67
68 void
69 Track::set_meter_point (MeterPoint p, void *src)
70 {
71         Route::set_meter_point (p, src);
72 }
73
74 XMLNode&
75 Track::get_state ()
76 {
77         return state (true);
78 }
79
80 XMLNode&
81 Track::get_template ()
82 {
83         return state (false);
84 }
85
86 void
87 Track::toggle_monitor_input ()
88 {
89         for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
90                 (*i)->ensure_monitor_input(!(*i)->monitoring_input());
91         }
92 }
93
94 nframes_t
95 Track::update_total_latency ()
96 {
97         _own_latency = 0;
98
99         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
100                 if ((*i)->active ()) {
101                         _own_latency += (*i)->latency ();
102                 }
103         }
104
105         set_port_latency (_own_latency);
106
107         return _own_latency;
108 }
109
110
111 Track::FreezeRecord::~FreezeRecord ()
112 {
113         for (vector<FreezeRecordInsertInfo*>::iterator i = insert_info.begin(); i != insert_info.end(); ++i) {
114                 delete *i;
115         }
116 }
117
118 Track::FreezeState
119 Track::freeze_state() const
120 {
121         return _freeze_record.state;
122 }
123
124 Track::RecEnableControllable::RecEnableControllable (Track& s)
125         : Controllable (X_("recenable")), track (s)
126 {
127 }
128
129 void
130 Track::RecEnableControllable::set_value (float val)
131 {
132         bool bval = ((val >= 0.5f) ? true: false);
133         track.set_record_enable (bval, this);
134 }
135
136 float
137 Track::RecEnableControllable::get_value (void) const
138 {
139         if (track.record_enabled()) { return 1.0f; }
140         return 0.0f;
141 }
142
143 bool
144 Track::record_enabled () const
145 {
146         return _diskstream->record_enabled ();
147 }
148
149 bool
150 Track::can_record()
151 {
152         bool will_record = true;
153         for (size_t i = 0; i < _inputs.size() && will_record; i++) {
154                 if (!_inputs[i]->connected())
155                         will_record = false;
156         }
157
158         return will_record;
159 }
160         
161 void
162 Track::set_record_enable (bool yn, void *src)
163 {
164         if (_freeze_record.state == Frozen) {
165                 return;
166         }
167
168         if (_mix_group && src != _mix_group && _mix_group->is_active()) {
169                 _mix_group->apply (&Track::set_record_enable, yn, _mix_group);
170                 return;
171         }
172
173         /* keep track of the meter point as it was before we rec-enabled */
174         if (!_diskstream->record_enabled()) {
175                 _saved_meter_point = _meter_point;
176         }
177         
178         _diskstream->set_record_enabled (yn);
179
180         if (_diskstream->record_enabled()) {
181                 set_meter_point (MeterInput, this);
182         } else {
183                 set_meter_point (_saved_meter_point, this);
184         }
185
186         _rec_enable_control.Changed ();
187 }
188
189 int
190 Track::set_name (string str, void *src)
191 {
192         int ret;
193
194         if (record_enabled() && _session.actively_recording()) {
195                 /* this messes things up if done while recording */
196                 return -1;
197         }
198
199         if (_diskstream->set_name (str)) {
200                 return -1;
201         }
202
203         /* save state so that the statefile fully reflects any filename changes */
204
205         if ((ret = IO::set_name (str, src)) == 0) {
206                 _session.save_state ("");
207         }
208         return ret;
209 }
210
211 void
212 Track::set_latency_delay (nframes_t longest_session_latency)
213 {
214         Route::set_latency_delay (longest_session_latency);
215         _diskstream->set_roll_delay (_roll_delay);
216 }
217