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