fixes for destructive track offsets of various kinds; move from jack_nframes_t -...
[ardour.git] / libs / ardour / auditioner.cc
1 /*
2     Copyright (C) 2001 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
21 #include <glibmm/thread.h>
22
23 #include <pbd/error.h>
24
25 #include <ardour/audio_diskstream.h>
26 #include <ardour/audioregion.h>
27 #include <ardour/route.h>
28 #include <ardour/session.h>
29 #include <ardour/auditioner.h>
30 #include <ardour/audioplaylist.h>
31 #include <ardour/panner.h>
32 #include <ardour/data_type.h>
33 #include <ardour/region_factory.h>
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 #include "i18n.h"
40
41 Auditioner::Auditioner (Session& s)
42         : AudioTrack (s, "auditioner", Route::Hidden)
43 {
44         string left = Config->get_auditioner_output_left();
45         string right = Config->get_auditioner_output_right();
46         
47         if ((left.length() == 0) && (right.length() == 0)) {
48                 return;
49         }
50
51         defer_pan_reset ();
52
53         if (left.length()) {
54                 add_output_port (left, this, DataType::AUDIO);
55         }
56
57         if (right.length()) {
58                 audio_diskstream()->add_channel();
59                 add_output_port (right, this, DataType::AUDIO);
60         }
61
62         allow_pan_reset ();
63         
64         IO::output_changed.connect (mem_fun (*this, &Auditioner::output_changed));
65
66         the_region.reset ((AudioRegion*) 0);
67         g_atomic_int_set (&_active, 0);
68 }
69
70 Auditioner::~Auditioner ()
71 {
72 }
73
74 AudioPlaylist&
75 Auditioner::prepare_playlist ()
76 {
77         // FIXME auditioner is still audio-only
78         AudioPlaylist* const apl = dynamic_cast<AudioPlaylist*>(_diskstream->playlist());
79         assert(apl);
80
81         apl->clear (false);
82         return *apl;
83 }
84
85 void
86 Auditioner::audition_current_playlist ()
87 {
88         if (g_atomic_int_get (&_active)) {
89                 /* don't go via session for this, because we are going
90                    to remain active.
91                 */
92                 cancel_audition ();
93         }
94
95         Glib::Mutex::Lock lm (lock);
96         _diskstream->seek (0);
97         length = _diskstream->playlist()->get_maximum_extent();
98         current_frame = 0;
99
100         /* force a panner reset now that we have all channels */
101
102         _panner->reset (n_outputs(), _diskstream->n_channels());
103
104         g_atomic_int_set (&_active, 1);
105 }
106
107 void
108 Auditioner::audition_region (boost::shared_ptr<Region> region)
109 {
110         if (g_atomic_int_get (&_active)) {
111                 /* don't go via session for this, because we are going
112                    to remain active.
113                 */
114                 cancel_audition ();
115         }
116
117         if (boost::dynamic_pointer_cast<AudioRegion>(region) == 0) {
118                 error << _("Auditioning of non-audio regions not yet supported") << endmsg;
119                 return;
120         }
121
122         Glib::Mutex::Lock lm (lock);
123
124         /* copy it */
125
126         boost::shared_ptr<AudioRegion> the_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
127         the_region->set_position (0, this);
128
129         _diskstream->playlist()->clear (false);
130         _diskstream->playlist()->add_region (the_region, 0, 1, false);
131
132         while (_diskstream->n_channels() < the_region->n_channels()) {
133                 audio_diskstream()->add_channel ();
134         }
135
136         while (_diskstream->n_channels() > the_region->n_channels()) {
137                 audio_diskstream()->remove_channel ();
138         }
139
140         /* force a panner reset now that we have all channels */
141
142         _panner->reset (n_outputs(), _diskstream->n_channels());
143
144         length = the_region->length();
145         _diskstream->seek (0);
146         current_frame = 0;
147         g_atomic_int_set (&_active, 1);
148 }
149
150 int
151 Auditioner::play_audition (nframes_t nframes)
152 {
153         bool need_butler;
154         nframes_t this_nframes;
155         int ret;
156
157         if (g_atomic_int_get (&_active) == 0) {
158                 silence (nframes, 0);
159                 return 0;
160         }
161
162         this_nframes = min (nframes, length - current_frame);
163
164         _diskstream->prepare ();
165
166         if ((ret = roll (this_nframes, current_frame, current_frame + nframes, 0, false, false, false)) != 0) {
167                 silence (nframes, 0);
168                 return ret;
169         }
170
171         need_butler = _diskstream->commit (this_nframes);
172         current_frame += this_nframes;
173
174         if (current_frame >= length) {
175                 _session.cancel_audition ();
176                 return 0;
177         } else {
178                 return need_butler ? 1 : 0;
179         }
180 }
181
182 void
183 Auditioner::output_changed (IOChange change, void* src)
184 {
185         if (change & ConnectionsChanged) {
186                 const char ** connections;
187                 connections =  output (0)->get_connections ();
188                 if (connections) {
189                         Config->set_auditioner_output_left (connections[0]);
190                         free (connections);
191                 }
192                 
193                 connections = output (1)->get_connections ();
194                 if (connections) {
195                         Config->set_auditioner_output_right (connections[0]);
196                         free (connections);
197                 }
198         }
199 }