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