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