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