84f6f13f1d115f5f8bfb3a3638fb72b8e7eef4f2
[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 (0);   
50         }
51
52         if (right == "default") {
53                 right = _session.engine().get_nth_physical_output (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         IO::output_changed.connect (mem_fun (*this, &Auditioner::output_changed));
75
76         the_region.reset ((AudioRegion*) 0);
77         g_atomic_int_set (&_active, 0);
78 }
79
80 Auditioner::~Auditioner ()
81 {
82 }
83
84 AudioPlaylist&
85 Auditioner::prepare_playlist ()
86 {
87         // FIXME auditioner is still audio-only
88         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
89         assert(apl);
90
91         apl->clear ();
92         return *apl;
93 }
94
95 void
96 Auditioner::audition_current_playlist ()
97 {
98         if (g_atomic_int_get (&_active)) {
99                 /* don't go via session for this, because we are going
100                    to remain active.
101                 */
102                 cancel_audition ();
103         }
104
105         Glib::Mutex::Lock lm (lock);
106         _diskstream->seek (0);
107         length = _diskstream->playlist()->get_maximum_extent();
108         current_frame = 0;
109
110         /* force a panner reset now that we have all channels */
111
112         _panner->reset (n_outputs(), _diskstream->n_channels());
113
114         g_atomic_int_set (&_active, 1);
115 }
116
117 void
118 Auditioner::audition_region (boost::shared_ptr<Region> region)
119 {
120         if (g_atomic_int_get (&_active)) {
121                 /* don't go via session for this, because we are going
122                    to remain active.
123                 */
124                 cancel_audition ();
125         }
126
127         if (boost::dynamic_pointer_cast<AudioRegion>(region) == 0) {
128                 error << _("Auditioning of non-audio regions not yet supported") << endmsg;
129                 return;
130         }
131
132         Glib::Mutex::Lock lm (lock);
133
134         /* copy it */
135
136         boost::shared_ptr<AudioRegion> the_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
137         the_region->set_position (0, this);
138
139         _diskstream->playlist()->clear ();
140         _diskstream->playlist()->add_region (the_region, 0, 1);
141
142         while (_diskstream->n_channels() < the_region->n_channels()) {
143                 audio_diskstream()->add_channel ();
144         }
145
146         while (_diskstream->n_channels() > the_region->n_channels()) {
147                 audio_diskstream()->remove_channel ();
148         }
149
150         /* force a panner reset now that we have all channels */
151
152         _panner->reset (n_outputs(), _diskstream->n_channels());
153
154         length = the_region->length();
155         _diskstream->seek (0);
156         current_frame = 0;
157         g_atomic_int_set (&_active, 1);
158 }
159
160 int
161 Auditioner::play_audition (nframes_t nframes)
162 {
163         bool need_butler;
164         nframes_t this_nframes;
165         int ret;
166
167         if (g_atomic_int_get (&_active) == 0) {
168                 silence (nframes, 0);
169                 return 0;
170         }
171
172         this_nframes = min (nframes, length - current_frame);
173
174         _diskstream->prepare ();
175
176         if ((ret = roll (this_nframes, current_frame, current_frame + nframes, 0, false, false, false)) != 0) {
177                 silence (nframes, 0);
178                 return ret;
179         }
180
181         need_butler = _diskstream->commit (this_nframes);
182         current_frame += this_nframes;
183
184         if (current_frame >= length) {
185                 _session.cancel_audition ();
186                 return 0;
187         } else {
188                 return need_butler ? 1 : 0;
189         }
190 }
191
192 void
193 Auditioner::output_changed (IOChange change, void* src)
194 {
195         string phys;
196
197         if (change & ConnectionsChanged) {
198                 const char ** connections;
199                 connections =  output (0)->get_connections ();
200                 if (connections) {
201                         phys = _session.engine().get_nth_physical_output (0);
202                         if (phys != connections[0]) {
203                                 Config->set_auditioner_output_left (connections[0]);
204                         } else {
205                                 Config->set_auditioner_output_left ("default");
206                         }
207                         free (connections);
208                 } else {
209                         Config->set_auditioner_output_left ("");
210                 }
211                 
212                 connections = output (1)->get_connections ();
213                 if (connections) {
214                         phys = _session.engine().get_nth_physical_output (1);
215                         if (phys != connections[0]) {
216                                 Config->set_auditioner_output_right (connections[0]);
217                         } else {
218                                 Config->set_auditioner_output_right ("default");
219                         }
220                         free (connections);
221                 } else {
222                         Config->set_auditioner_output_right ("");
223                 }
224         }
225 }