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