fix some 64bit warnings
[ardour.git] / libs / ardour / audio_track.cc
1 /*
2     Copyright (C) 2002 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     $Id$
19 */
20 #include <pbd/error.h>
21 #include <sigc++/retype.h>
22 #include <sigc++/retype_return.h>
23 #include <sigc++/bind.h>
24
25 #include <ardour/audio_track.h>
26 #include <ardour/diskstream.h>
27 #include <ardour/session.h>
28 #include <ardour/redirect.h>
29 #include <ardour/audioregion.h>
30 #include <ardour/route_group_specialized.h>
31 #include <ardour/insert.h>
32 #include <ardour/audioplaylist.h>
33 #include <ardour/panner.h>
34 #include <ardour/utils.h>
35
36
37
38 #include "i18n.h"
39
40 using namespace std;
41 //using namespace sigc;
42 using namespace ARDOUR;
43
44 AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag)
45         : Route (sess, name, 1, -1, -1, -1, flag),
46           diskstream (0),
47           _midi_rec_enable_control (*this, _session.midi_port())
48 {
49         DiskStream::Flag dflags = DiskStream::Flag (0);
50
51         if (_flags & Hidden) {
52                 dflags = DiskStream::Flag (dflags | DiskStream::Hidden);
53         } else {
54                 dflags = DiskStream::Flag (dflags | DiskStream::Recordable);
55         }
56
57         DiskStream* ds = new DiskStream (_session, name, dflags);
58         
59         set_diskstream (*ds, this);
60         
61         _declickable = true;
62         _freeze_record.state = NoFreeze;
63         _saved_meter_point = _meter_point;
64
65         // we do this even though Route already did it in it's init
66         reset_midi_control (_session.midi_port(), _session.get_midi_control());
67         
68 }
69
70 AudioTrack::AudioTrack (Session& sess, const XMLNode& node)
71         : Route (sess, "to be renamed", 0, 0, -1, -1),
72           diskstream (0),
73           _midi_rec_enable_control (*this, _session.midi_port())
74 {
75         _freeze_record.state = NoFreeze;
76         set_state (node);
77         _declickable = true;
78         _saved_meter_point = _meter_point;
79
80         // we do this even though Route already did it in it's init
81         reset_midi_control (_session.midi_port(), _session.get_midi_control());
82 }
83
84 AudioTrack::~AudioTrack ()
85 {
86         if (diskstream) {
87                 diskstream->unref();
88         }
89 }
90
91 int
92 AudioTrack::deprecated_use_diskstream_connections ()
93 {
94         if (diskstream->deprecated_io_node == 0) {
95                 return 0;
96         }
97
98         const XMLProperty* prop;
99         XMLNode& node (*diskstream->deprecated_io_node);
100
101         /* don't do this more than once. */
102
103         diskstream->deprecated_io_node = 0;
104
105         set_input_minimum (-1);
106         set_input_maximum (-1);
107         set_output_minimum (-1);
108         set_output_maximum (-1);
109         
110         if ((prop = node.property ("gain")) != 0) {
111                 set_gain (atof (prop->value().c_str()), this);
112                 _gain = _desired_gain;
113         }
114
115         if ((prop = node.property ("input-connection")) != 0) {
116                 Connection* c = _session.connection_by_name (prop->value());
117                 
118                 if (c == 0) {
119                         error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
120                         
121                         if ((c = _session.connection_by_name (_("in 1"))) == 0) {
122                                 error << _("No input connections available as a replacement")
123                                 << endmsg;
124                                 return -1;
125                         } else {
126                                 info << string_compose (_("Connection %1 was not available - \"in 1\" used instead"), prop->value())
127                                << endmsg;
128                         }
129                 }
130
131                 use_input_connection (*c, this);
132
133         } else if ((prop = node.property ("inputs")) != 0) {
134                 if (set_inputs (prop->value())) {
135                         error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
136                         return -1;
137                 }
138         }
139         
140         return 0;
141 }
142
143 int
144 AudioTrack::set_diskstream (DiskStream& ds, void *src)
145 {
146         if (diskstream) {
147                 diskstream->unref();
148         }
149
150         diskstream = &ds.ref();
151         diskstream->set_io (*this);
152
153         if (diskstream->deprecated_io_node) {
154
155                 if (!connecting_legal) {
156                         ConnectingLegal.connect (mem_fun (*this, &AudioTrack::deprecated_use_diskstream_connections));
157                 } else {
158                         deprecated_use_diskstream_connections ();
159                 }
160         }
161
162         diskstream->set_record_enabled (false, this);
163         diskstream->monitor_input (false);
164
165         ic_connection.disconnect();
166         ic_connection = input_changed.connect (mem_fun (*diskstream, &DiskStream::handle_input_change));
167
168         diskstream_changed (src); /* EMIT SIGNAL */
169
170         return 0;
171 }       
172
173 int 
174 AudioTrack::use_diskstream (string name)
175 {
176         DiskStream *dstream;
177
178         if ((dstream = _session.diskstream_by_name (name)) == 0) {
179           error << string_compose(_("AudioTrack: diskstream \"%1\" not known by session"), name) << endmsg;
180                 return -1;
181         }
182         
183         return set_diskstream (*dstream, this);
184 }
185
186 int 
187 AudioTrack::use_diskstream (id_t id)
188 {
189         DiskStream *dstream;
190
191         if ((dstream = _session.diskstream_by_id (id)) == 0) {
192                 error << string_compose(_("AudioTrack: diskstream \"%1\" not known by session"), id) << endmsg;
193                 return -1;
194         }
195         
196         return set_diskstream (*dstream, this);
197 }
198
199 bool
200 AudioTrack::record_enabled () const
201 {
202         return diskstream->record_enabled ();
203 }
204
205 void
206 AudioTrack::set_record_enable (bool yn, void *src)
207 {
208         if (_freeze_record.state == Frozen) {
209                 return;
210         }
211
212         if (_mix_group && src != _mix_group && _mix_group->is_active()) {
213                 _mix_group->apply (&AudioTrack::set_record_enable, yn, _mix_group);
214                 return;
215         }
216
217         /* keep track of the meter point as it was before we rec-enabled */
218
219         if (!diskstream->record_enabled()) {
220                 _saved_meter_point = _meter_point;
221         }
222         
223         diskstream->set_record_enabled (yn, src);
224
225         if (diskstream->record_enabled()) {
226                 set_meter_point (MeterInput, this);
227         } else {
228                 set_meter_point (_saved_meter_point, this);
229         }
230
231         if (_session.get_midi_feedback()) {
232                 _midi_rec_enable_control.send_feedback (record_enabled());
233         }
234
235 }
236
237 void
238 AudioTrack::set_meter_point (MeterPoint p, void *src)
239 {
240         Route::set_meter_point (p, src);
241 }
242
243 XMLNode&
244 AudioTrack::state(bool full_state)
245 {
246         XMLNode& track_state (Route::state (full_state));
247         char buf[64];
248
249         /* we don't return diskstream state because we don't
250            own the diskstream exclusively. control of the diskstream
251            state is ceded to the Session, even if we create the
252            diskstream.
253         */
254
255         snprintf (buf, sizeof (buf), "%" PRIu64, diskstream->id());
256         track_state.add_property ("diskstream-id", buf);
257
258         return track_state;
259 }
260
261 int
262 AudioTrack::set_state (const XMLNode& node)
263 {
264         const XMLProperty *prop;
265         XMLNodeConstIterator iter;
266         XMLNodeList midi_kids;
267
268         if (Route::set_state (node)) {
269                 return -1;
270         }
271
272         midi_kids = node.children ("MIDI");
273         
274         for (iter = midi_kids.begin(); iter != midi_kids.end(); ++iter) {
275         
276                 XMLNodeList kids;
277                 XMLNodeConstIterator miter;
278                 XMLNode*    child;
279
280                 kids = (*iter)->children ();
281
282                 for (miter = kids.begin(); miter != kids.end(); ++miter) {
283
284                         child =* miter;
285
286                         if (child->name() == "rec_enable") {
287                         
288                                 MIDI::eventType ev = MIDI::on; /* initialize to keep gcc happy */
289                                 MIDI::byte additional = 0;  /* ditto */
290                                 MIDI::channel_t chn = 0;    /* ditto */
291
292                                 if (get_midi_node_info (child, ev, chn, additional)) {
293                                         _midi_rec_enable_control.set_control_type (chn, ev, additional);
294                                 } else {
295                                   error << string_compose(_("MIDI rec_enable control specification for %1 is incomplete, so it has been ignored"), _name) << endmsg;
296                                 }
297                         }
298                 }
299         }
300
301         
302         if ((prop = node.property ("diskstream-id")) == 0) {
303                 
304                 /* some old sessions use the diskstream name rather than the ID */
305
306                 if ((prop = node.property ("diskstream")) == 0) {
307                         fatal << _("programming error: AudioTrack given state without diskstream!") << endmsg;
308                         /*NOTREACHED*/
309                         return -1;
310                 }
311
312                 if (use_diskstream (prop->value())) {
313                         return -1;
314                 }
315
316         } else {
317                 
318                 id_t id = strtoull (prop->value().c_str(), 0, 10);
319                 
320                 if (use_diskstream (id)) {
321                         return -1;
322                 }
323         }
324
325         pending_state = const_cast<XMLNode*> (&node);
326
327         _session.StateReady.connect (mem_fun (*this, &AudioTrack::set_state_part_two));
328
329         return 0;
330 }
331
332 XMLNode& 
333 AudioTrack::get_state()
334 {
335         XMLNode& root (Route::get_state());
336         XMLNode* freeze_node;
337         char buf[32];
338
339         if (_freeze_record.playlist) {
340                 XMLNode* inode;
341
342                 freeze_node = new XMLNode (X_("freeze-info"));
343                 freeze_node->add_property ("playlist", _freeze_record.playlist->name());
344                 snprintf (buf, sizeof (buf), "%d", (int) _freeze_record.state);
345                 freeze_node->add_property ("state", buf);
346
347                 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
348                         inode = new XMLNode (X_("insert"));
349                         snprintf (buf, sizeof (buf), "%" PRIu64, (*i)->id);
350                         inode->add_property (X_("id"), buf);
351                         inode->add_child_copy ((*i)->state);
352                 
353                         freeze_node->add_child_nocopy (*inode);
354                 }
355
356                 root.add_child_nocopy (*freeze_node);
357         }
358
359         /* Alignment: act as a proxy for the diskstream */
360         
361         XMLNode* align_node = new XMLNode (X_("alignment"));
362         switch (diskstream->alignment_style()) {
363         case ExistingMaterial:
364                 snprintf (buf, sizeof (buf), X_("existing"));
365                 break;
366         case CaptureTime:
367                 snprintf (buf, sizeof (buf), X_("capture"));
368                 break;
369         }
370         align_node->add_property (X_("style"), buf);
371         root.add_child_nocopy (*align_node);
372
373         /* MIDI control */
374
375         MIDI::channel_t chn;
376         MIDI::eventType ev;
377         MIDI::byte      additional;
378         XMLNode*        midi_node = 0;
379         XMLNode*        child;
380         XMLNodeList     midikids;
381
382         midikids = root.children ("MIDI");
383         if (!midikids.empty()) {
384                 midi_node = midikids.front();
385         }
386         else {
387                 midi_node = root.add_child ("MIDI");
388         }
389                 
390         if (_midi_rec_enable_control.get_control_info (chn, ev, additional) && midi_node) {
391
392                 child = midi_node->add_child ("rec_enable");
393                 set_midi_node_info (child, ev, chn, additional);
394         }
395
396         
397         return root;
398 }
399
400 void
401 AudioTrack::set_state_part_two ()
402 {
403         XMLNode* fnode;
404         XMLProperty* prop;
405         LocaleGuard lg (X_("POSIX"));
406
407         /* This is called after all session state has been restored but before
408            have been made ports and connections are established.
409         */
410
411         if (pending_state == 0) {
412                 return;
413         }
414
415         if ((fnode = find_named_node (*pending_state, X_("freeze-info"))) != 0) {
416
417                 
418                 _freeze_record.have_mementos = false;
419                 _freeze_record.state = Frozen;
420                 
421                 for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
422                         delete *i;
423                 }
424                 _freeze_record.insert_info.clear ();
425                 
426                 if ((prop = fnode->property (X_("playlist"))) != 0) {
427                         Playlist* pl = _session.playlist_by_name (prop->value());
428                         if (pl) {
429                                 _freeze_record.playlist = dynamic_cast<AudioPlaylist*> (pl);
430                         } else {
431                                 _freeze_record.playlist = 0;
432                                 _freeze_record.state = NoFreeze;
433                         return;
434                         }
435                 }
436                 
437                 if ((prop = fnode->property (X_("state"))) != 0) {
438                         _freeze_record.state = (FreezeState) atoi (prop->value().c_str());
439                 }
440                 
441                 XMLNodeConstIterator citer;
442                 XMLNodeList clist = fnode->children();
443                 
444                 for (citer = clist.begin(); citer != clist.end(); ++citer) {
445                         if ((*citer)->name() != X_("insert")) {
446                                 continue;
447                         }
448                         
449                         if ((prop = (*citer)->property (X_("id"))) == 0) {
450                                 continue;
451                         }
452                         
453                         FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()));
454                         frii->insert = 0;
455                         sscanf (prop->value().c_str(), "%" PRIu64, &frii->id);
456                         _freeze_record.insert_info.push_back (frii);
457                 }
458         }
459
460         /* Alignment: act as a proxy for the diskstream */
461
462         if ((fnode = find_named_node (*pending_state, X_("alignment"))) != 0) {
463
464                 if ((prop = fnode->property (X_("style"))) != 0) {
465                         if (prop->value() == "existing") {
466                                 diskstream->set_persistent_align_style (ExistingMaterial);
467                         } else if (prop->value() == "capture") {
468                                 diskstream->set_persistent_align_style (CaptureTime);
469                         }
470                 }
471         }
472         return;
473 }       
474
475 uint32_t
476 AudioTrack::n_process_buffers ()
477 {
478         return max ((uint32_t) diskstream->n_channels(), redirect_max_outs);
479 }
480
481 void
482 AudioTrack::passthru_silence (jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t nframes, jack_nframes_t offset, int declick, bool meter)
483 {
484         uint32_t nbufs = n_process_buffers ();
485         process_output_buffers (_session.get_silent_buffers (nbufs), nbufs, start_frame, end_frame, nframes, offset, true, declick, meter);
486 }
487
488 int 
489 AudioTrack::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, 
490                      bool session_state_changing, bool can_record, bool rec_monitors_input)
491 {
492         if (n_outputs() == 0) {
493                 return 0;
494         }
495
496         if (!_active) {
497                 silence (nframes, offset);
498                 return 0;
499         }
500
501         if (session_state_changing) {
502
503                 /* XXX is this safe to do against transport state changes? */
504
505                 passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
506                 return 0;
507         }
508
509         diskstream->check_record_status (start_frame, nframes, can_record);
510
511         bool send_silence;
512         
513         if (_have_internal_generator) {
514                 /* since the instrument has no input streams,
515                    there is no reason to send any signal
516                    into the route.
517                 */
518                 send_silence = true;
519         } else {
520
521                 if (_session.get_auto_input()) {
522                         if (Config->get_no_sw_monitoring()) {
523                                 send_silence = true;
524                         } else {
525                                 send_silence = false;
526                         }
527                 } else {
528                         if (diskstream->record_enabled()) {
529                                 if (Config->get_no_sw_monitoring()) {
530                                         send_silence = true;
531                                 } else {
532                                         send_silence = false;
533                                 }
534                         } else {
535                                 send_silence = true;
536                         }
537                 }
538         }
539
540         apply_gain_automation = false;
541
542         if (send_silence) {
543                 
544                 /* if we're sending silence, but we want the meters to show levels for the signal,
545                    meter right here.
546                 */
547                 
548                 if (_have_internal_generator) {
549                         passthru_silence (start_frame, end_frame, nframes, offset, 0, true);
550                 } else {
551                         if (_meter_point == MeterInput) {
552                                 just_meter_input (start_frame, end_frame, nframes, offset);
553                         }
554                         passthru_silence (start_frame, end_frame, nframes, offset, 0, false);
555                 }
556
557         } else {
558         
559                 /* we're sending signal, but we may still want to meter the input. 
560                  */
561
562                 passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput));
563         }
564
565         return 0;
566 }
567
568 int
569 AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
570                   bool can_record, bool rec_monitors_input)
571 {
572         int dret;
573         Sample* b;
574         Sample* tmpb;
575         jack_nframes_t transport_frame;
576
577         automation_snapshot (start_frame);
578
579         if (n_outputs() == 0 && _redirects.empty()) {
580                 return 0;
581         }
582
583         if (!_active) {
584                 silence (nframes, offset);
585                 return 0;
586         }
587
588         transport_frame = _session.transport_frame();
589
590         if ((nframes = check_initial_delay (nframes, offset, transport_frame)) == 0) {
591                 /* need to do this so that the diskstream sets its
592                    playback distance to zero, thus causing diskstream::commit
593                    to do nothing.
594                 */
595                 return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
596         } 
597
598         _silent = false;
599         apply_gain_automation = false;
600
601         if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
602                 
603                 silence (nframes, offset);
604
605                 return dret;
606         }
607
608         /* special condition applies */
609         
610         if (_meter_point == MeterInput) {
611                 just_meter_input (start_frame, end_frame, nframes, offset);
612         }
613
614         if (diskstream->record_enabled() && !can_record && !_session.get_auto_input()) {
615
616                 /* not actually recording, but we want to hear the input material anyway,
617                    at least potentially (depending on monitoring options)
618                  */
619
620                 passthru (start_frame, end_frame, nframes, offset, 0, true);
621
622         } else if ((b = diskstream->playback_buffer(0)) != 0) {
623
624                 /*
625                   XXX is it true that the earlier test on n_outputs()
626                   means that we can avoid checking it again here? i think
627                   so, because changing the i/o configuration of an IO
628                   requires holding the AudioEngine lock, which we hold
629                   while in the process() tree.
630                 */
631
632                 
633                 /* copy the diskstream data to all output buffers */
634                 
635                 vector<Sample*>& bufs = _session.get_passthru_buffers ();
636                 uint32_t limit = n_process_buffers ();
637                 
638                 uint32_t n;
639                 uint32_t i;
640
641
642                 for (i = 0, n = 1; i < limit; ++i, ++n) {
643                         memcpy (bufs[i], b, sizeof (Sample) * nframes); 
644                         if (n < diskstream->n_channels()) {
645                                 tmpb = diskstream->playback_buffer(n);
646                                 if (tmpb!=0) {
647                                         b = tmpb;
648                                 }
649                         }
650                 }
651
652                 /* don't waste time with automation if we're recording */
653
654                 if (!diskstream->record_enabled()) {
655                         
656                         if (gain_automation_playback()) {
657                                 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
658                         }
659                 }
660
661                 process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, (!_session.get_record_enabled() || _session.get_recording_plugins()), declick, (_meter_point != MeterInput));
662                 
663         } else {
664                 /* problem with the diskstream; just be quiet for a bit */
665                 silence (nframes, offset);
666         }
667
668         return 0;
669 }
670
671 int
672 AudioTrack::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, 
673                          bool can_record, bool rec_monitors_input)
674 {
675         if (n_outputs() == 0 && _redirects.empty()) {
676                 return 0;
677         }
678
679         if (!_active) {
680                 silence (nframes, offset);
681                 return 0;
682         }
683
684         _silent = true;
685         apply_gain_automation = false;
686         
687         silence (nframes, offset);
688
689         return diskstream->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
690 }
691
692 void
693 AudioTrack::toggle_monitor_input ()
694 {
695         for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
696                 (*i)->request_monitor_input(!(*i)->monitoring_input());
697         }
698 }
699
700 int
701 AudioTrack::set_name (string str, void *src)
702 {
703         if (record_enabled() && _session.actively_recording()) {
704                 /* this messes things up if done while recording */
705                 return -1;
706         }
707
708         diskstream->set_name (str, src);
709         return IO::set_name (str, src);
710 }
711
712 int
713 AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, jack_nframes_t start, jack_nframes_t nframes)
714 {
715         gain_t  gain_automation[nframes];
716         gain_t  gain_buffer[nframes];
717         float   mix_buffer[nframes];
718         RedirectList::iterator i;
719         bool post_fader_work = false;
720         gain_t this_gain = _gain;
721         vector<Sample*>::iterator bi;
722         Sample * b;
723         
724         LockMonitor rlock (redirect_lock, __LINE__, __FILE__);
725                 
726         if (diskstream->playlist()->read (buffers[0], mix_buffer, gain_buffer, start, nframes) != nframes) {
727                 return -1;
728         }
729
730         uint32_t n=1;
731         bi = buffers.begin();
732         b = buffers[0];
733         ++bi;
734         for (; bi != buffers.end(); ++bi, ++n) {
735                 if (n < diskstream->n_channels()) {
736                         if (diskstream->playlist()->read ((*bi), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
737                                 return -1;
738                         }
739                         b = (*bi);
740                 }
741                 else {
742                         /* duplicate last across remaining buffers */
743                         memcpy ((*bi), b, sizeof (Sample) * nframes); 
744                 }
745         }
746
747
748         /* note: only run inserts during export. other layers in the machinery
749            will already have checked that there are no external port inserts.
750         */
751         
752         for (i = _redirects.begin(); i != _redirects.end(); ++i) {
753                 Insert *insert;
754                 
755                 if ((insert = dynamic_cast<Insert*>(*i)) != 0) {
756                         switch (insert->placement()) {
757                         case PreFader:
758                                 insert->run (buffers, nbufs, nframes, 0);
759                                 break;
760                         case PostFader:
761                                 post_fader_work = true;
762                                 break;
763                         }
764                 }
765         }
766         
767         if (_gain_automation_curve.automation_state() == Play) {
768                 
769                 _gain_automation_curve.get_vector (start, start + nframes, gain_automation, nframes);
770
771                 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
772                         Sample *b = *bi;
773                         for (jack_nframes_t n = 0; n < nframes; ++n) {
774                                 b[n] *= gain_automation[n];
775                         }
776                 }
777
778         } else {
779
780                 for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
781                         Sample *b = *bi;
782                         for (jack_nframes_t n = 0; n < nframes; ++n) {
783                                 b[n] *= this_gain;
784                         }
785                 }
786         }
787
788         if (post_fader_work) {
789
790                 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
791                         PluginInsert *insert;
792                         
793                         if ((insert = dynamic_cast<PluginInsert*>(*i)) != 0) {
794                                 switch ((*i)->placement()) {
795                                 case PreFader:
796                                         break;
797                                 case PostFader:
798                                         insert->run (buffers, nbufs, nframes, 0);
799                                         break;
800                                 }
801                         }
802                 }
803         } 
804
805         return 0;
806 }
807
808 void
809 AudioTrack::set_latency_delay (jack_nframes_t longest_session_latency)
810 {
811         Route::set_latency_delay (longest_session_latency);
812         diskstream->set_roll_delay (_roll_delay);
813 }
814
815 jack_nframes_t
816 AudioTrack::update_total_latency ()
817 {
818         _own_latency = 0;
819
820         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
821                 if ((*i)->active ()) {
822                         _own_latency += (*i)->latency ();
823                 }
824         }
825
826         set_port_latency (_own_latency);
827
828         return _own_latency;
829 }
830
831 void
832 AudioTrack::bounce (InterThreadInfo& itt)
833 {
834         vector<Source*> srcs;
835         _session.write_one_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
836 }
837
838
839 void
840 AudioTrack::bounce_range (jack_nframes_t start, jack_nframes_t end, InterThreadInfo& itt)
841 {
842         vector<Source*> srcs;
843         _session.write_one_track (*this, start, end, false, srcs, itt);
844 }
845
846 void
847 AudioTrack::freeze (InterThreadInfo& itt)
848 {
849         Insert* insert;
850         vector<Source*> srcs;
851         string new_playlist_name;
852         Playlist* new_playlist;
853         string dir;
854         AudioRegion* region;
855         string region_name;
856         
857         if ((_freeze_record.playlist = diskstream->playlist()) == 0) {
858                 return;
859         }
860
861         uint32_t n = 1;
862
863         while (n < (UINT_MAX-1)) {
864          
865                 string candidate;
866                 
867                 candidate = string_compose ("<F%2>%1", _freeze_record.playlist->name(), n);
868
869                 if (_session.playlist_by_name (candidate) == 0) {
870                         new_playlist_name = candidate;
871                         break;
872                 }
873
874                 ++n;
875
876         } 
877
878         if (n == (UINT_MAX-1)) {
879           error << string_compose (X_("There Are too many frozen versions of playlist \"%1\""
880                             " to create another one"), _freeze_record.playlist->name())
881                << endmsg;
882                 return;
883         }
884
885         if (_session.write_one_track (*this, 0, _session.current_end_frame(), true, srcs, itt)) {
886                 return;
887         }
888
889         _freeze_record.insert_info.clear ();
890         _freeze_record.have_mementos = true;
891
892         {
893                 LockMonitor lm (redirect_lock, __LINE__, __FILE__);
894                 
895                 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); ++r) {
896                         
897                         if ((insert = dynamic_cast<Insert*>(*r)) != 0) {
898                                 
899                                 FreezeRecordInsertInfo* frii  = new FreezeRecordInsertInfo ((*r)->get_state());
900                                 
901                                 frii->insert = insert;
902                                 frii->id = insert->id();
903                                 frii->memento = (*r)->get_memento();
904                                 
905                                 _freeze_record.insert_info.push_back (frii);
906                                 
907                                 /* now deactivate the insert */
908                                 
909                                 insert->set_active (false, this);
910                         }
911                 }
912         }
913
914         new_playlist = new AudioPlaylist (_session, new_playlist_name, false);
915         region_name = new_playlist_name;
916
917         /* create a new region from all filesources, keep it private */
918
919         region = new AudioRegion (srcs, 0, srcs[0]->length(), 
920                                   region_name, 0, 
921                                   (AudioRegion::Flag) (AudioRegion::WholeFile|AudioRegion::DefaultFlags),
922                                   false);
923
924         new_playlist->set_orig_diskstream_id (diskstream->id());
925         new_playlist->add_region (*region, 0);
926         new_playlist->set_frozen (true);
927         region->set_locked (true);
928
929         diskstream->use_playlist (dynamic_cast<AudioPlaylist*>(new_playlist));
930         diskstream->set_record_enabled (false, this);
931
932         _freeze_record.state = Frozen;
933         FreezeChange(); /* EMIT SIGNAL */
934 }
935
936 void
937 AudioTrack::unfreeze ()
938 {
939         if (_freeze_record.playlist) {
940                 diskstream->use_playlist (_freeze_record.playlist);
941
942                 if (_freeze_record.have_mementos) {
943
944                         for (vector<FreezeRecordInsertInfo*>::iterator i = _freeze_record.insert_info.begin(); i != _freeze_record.insert_info.end(); ++i) {
945                                 (*i)->memento ();
946                         }
947
948                 } else {
949
950                         LockMonitor lm (redirect_lock, __LINE__, __FILE__);
951                         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
952                                 for (vector<FreezeRecordInsertInfo*>::iterator ii = _freeze_record.insert_info.begin(); ii != _freeze_record.insert_info.end(); ++ii) {
953                                         if ((*ii)->id == (*i)->id()) {
954                                                 (*i)->set_state (((*ii)->state));
955                                                 break;
956                                         }
957                                 }
958                         }
959                 }
960                 
961                 _freeze_record.playlist = 0;
962         }
963
964         _freeze_record.state = UnFrozen;
965         FreezeChange (); /* EMIT SIGNAL */
966 }
967
968 AudioTrack::FreezeRecord::~FreezeRecord ()
969 {
970         for (vector<FreezeRecordInsertInfo*>::iterator i = insert_info.begin(); i != insert_info.end(); ++i) {
971                 delete *i;
972         }
973 }
974
975 AudioTrack::FreezeState
976 AudioTrack::freeze_state() const
977 {
978         return _freeze_record.state;
979 }
980
981
982 void
983 AudioTrack::reset_midi_control (MIDI::Port* port, bool on)
984 {
985         MIDI::channel_t chn;
986         MIDI::eventType ev;
987         MIDI::byte extra;
988
989         Route::reset_midi_control (port, on);
990         
991         _midi_rec_enable_control.get_control_info (chn, ev, extra);
992         if (!on) {
993                 chn = -1;
994         }
995         _midi_rec_enable_control.midi_rebind (port, chn);
996 }
997
998 void
999 AudioTrack::send_all_midi_feedback ()
1000 {
1001         if (_session.get_midi_feedback()) {
1002
1003                 Route::send_all_midi_feedback();
1004
1005                 _midi_rec_enable_control.send_feedback (record_enabled());
1006         }
1007 }
1008
1009
1010 AudioTrack::MIDIRecEnableControl::MIDIRecEnableControl (AudioTrack& s,  MIDI::Port* port)
1011         : MIDI::Controllable (port, 0), track (s), setting(false)
1012 {
1013         last_written = false; /* XXX need a good out of bound value */
1014 }
1015
1016 void
1017 AudioTrack::MIDIRecEnableControl::set_value (float val)
1018 {
1019         bool bval = ((val >= 0.5f) ? true: false);
1020         
1021         setting = true;
1022         track.set_record_enable (bval, this);
1023         setting = false;
1024 }
1025
1026 void
1027 AudioTrack::MIDIRecEnableControl::send_feedback (bool value)
1028 {
1029
1030         if (!setting && get_midi_feedback()) {
1031                 MIDI::byte val = (MIDI::byte) (value ? 127: 0);
1032                 MIDI::channel_t ch = 0;
1033                 MIDI::eventType ev = MIDI::none;
1034                 MIDI::byte additional = 0;
1035                 MIDI::EventTwoBytes data;
1036             
1037                 if (get_control_info (ch, ev, additional)) {
1038                         data.controller_number = additional;
1039                         data.value = val;
1040
1041                         track._session.send_midi_message (get_port(), ev, ch, data);
1042                 }
1043         }
1044         
1045 }
1046
1047 MIDI::byte*
1048 AudioTrack::MIDIRecEnableControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool val, bool force)
1049 {
1050         if (get_midi_feedback()) {
1051
1052                 MIDI::channel_t ch = 0;
1053                 MIDI::eventType ev = MIDI::none;
1054                 MIDI::byte additional = 0;
1055
1056                 if (get_control_info (ch, ev, additional)) {
1057                         if (val != last_written || force) {
1058                                 *buf++ = ev & ch;
1059                                 *buf++ = additional; /* controller number */
1060                                 *buf++ = (MIDI::byte) (val ? 127: 0);
1061                                 last_written = val;
1062                                 bufsize -= 3;
1063                         }
1064                 }
1065         }
1066
1067         return buf;
1068 }