Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[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/io_processor.h>
27 #include <ardour/audioregion.h>
28 #include <ardour/audiosource.h>
29 #include <ardour/route_group_specialized.h>
30 #include <ardour/processor.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         , _rec_enable_control (new RecEnableControllable(*this))
44 {
45         _declickable = true;
46         _freeze_record.state = NoFreeze;
47         _saved_meter_point = _meter_point;
48         _mode = mode;
49 }
50
51 Track::Track (Session& sess, const XMLNode& node, DataType default_type)
52         : Route (sess, node)
53         , _rec_enable_control (new RecEnableControllable(*this))
54 {
55         _freeze_record.state = NoFreeze;
56         _declickable = true;
57         _saved_meter_point = _meter_point;
58 }
59
60 Track::~Track ()
61 {
62 }
63
64 void
65 Track::set_meter_point (MeterPoint p, void *src)
66 {
67         Route::set_meter_point (p, src);
68 }
69
70 XMLNode&
71 Track::get_state ()
72 {
73         return state (true);
74 }
75
76 XMLNode&
77 Track::get_template ()
78 {
79         return state (false);
80 }
81
82 void
83 Track::toggle_monitor_input ()
84 {
85         for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
86                 i->ensure_monitor_input(!i->monitoring_input());
87         }
88 }
89
90 ARDOUR::nframes_t
91 Track::update_total_latency ()
92 {
93         nframes_t old = _own_latency;
94
95         if (_user_latency) {
96                 _own_latency = _user_latency;
97         } else {
98                 _own_latency = 0;
99
100                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
101                         if ((*i)->active ()) {
102                                 _own_latency += (*i)->signal_latency ();
103                         }
104                 }
105         }
106
107 #undef DEBUG_LATENCY
108 #ifdef DEBUG_LATENCY
109         cerr << _name << ": internal redirect (final) latency = " << _own_latency << endl;
110 #endif
111
112         set_port_latency (_own_latency);
113
114         if (old != _own_latency) {
115                 signal_latency_changed (); /* EMIT SIGNAL */
116         }
117
118         return _own_latency;
119 }
120
121 Track::FreezeRecord::~FreezeRecord ()
122 {
123         for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
124                 delete *i;
125         }
126 }
127
128 Track::FreezeState
129 Track::freeze_state() const
130 {
131         return _freeze_record.state;
132 }
133
134 Track::RecEnableControllable::RecEnableControllable (Track& s)
135         : Controllable (X_("recenable")), track (s)
136 {
137 }
138
139 void
140 Track::RecEnableControllable::set_value (float val)
141 {
142         bool bval = ((val >= 0.5f) ? true: false);
143         track.set_record_enable (bval, this);
144 }
145
146 float
147 Track::RecEnableControllable::get_value (void) const
148 {
149         if (track.record_enabled()) { return 1.0f; }
150         return 0.0f;
151 }
152
153 bool
154 Track::record_enabled () const
155 {
156         return _diskstream->record_enabled ();
157 }
158
159 bool
160 Track::can_record()
161 {
162         bool will_record = true;
163         for (PortSet::iterator i = _inputs.begin(); i != _inputs.end() && will_record; ++i) {
164                 if (!i->connected())
165                         will_record = false;
166         }
167
168         return will_record;
169 }
170         
171 void
172 Track::set_record_enable (bool yn, void *src)
173 {
174         if (_freeze_record.state == Frozen) {
175                 return;
176         }
177
178         if (_mix_group && src != _mix_group && _mix_group->is_active()) {
179                 _mix_group->apply (&Track::set_record_enable, yn, _mix_group);
180                 return;
181         }
182
183         /* keep track of the meter point as it was before we rec-enabled */
184         if (!_diskstream->record_enabled()) {
185                 _saved_meter_point = _meter_point;
186         }
187         
188         _diskstream->set_record_enabled (yn);
189
190         if (_diskstream->record_enabled()) {
191                 set_meter_point (MeterInput, this);
192         } else {
193                 set_meter_point (_saved_meter_point, this);
194         }
195
196         _rec_enable_control->Changed ();
197 }
198
199
200 bool
201 Track::set_name (const string& str)
202 {
203         bool ret;
204
205         if (record_enabled() && _session.actively_recording()) {
206                 /* this messes things up if done while recording */
207                 return false;
208         }
209
210         if (_diskstream->set_name (str)) {
211                 return false;
212         }
213
214         /* save state so that the statefile fully reflects any filename changes */
215
216         if ((ret = IO::set_name (str)) == 0) {
217                 _session.save_state ("");
218         }
219
220         return ret;
221 }
222
223 void
224 Track::set_latency_delay (nframes_t longest_session_latency)
225 {
226         Route::set_latency_delay (longest_session_latency);
227         _diskstream->set_roll_delay (_roll_delay);
228 }
229