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