8f4d8fc27cf3e47dd8bfe459c9af8d247ed125aa
[ardour.git] / libs / ardour / ardour / export_channel.h
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __ardour_export_channel_h__
22 #define __ardour_export_channel_h__
23
24 #include <set>
25 #include <list>
26
27 #include <boost/scoped_array.hpp>
28 #include <boost/shared_ptr.hpp>
29
30 #include "pbd/signals.h"
31 #include "pbd/ringbuffer.h"
32
33 #include "ardour/buffer_set.h"
34 #include "ardour/export_pointers.h"
35
36 namespace ARDOUR {
37
38 class Session;
39 class AudioTrack;
40 class AudioPort;
41 class AudioRegion;
42 class CapturingProcessor;
43
44 /// Export channel base class interface for different source types
45 class LIBARDOUR_API ExportChannel : public boost::less_than_comparable<ExportChannel>
46 {
47   public:
48
49         virtual ~ExportChannel () {}
50
51         virtual void set_max_buffer_size(samplecnt_t) { }
52
53         virtual void read (Sample const *& data, samplecnt_t samples) const = 0;
54         virtual bool empty () const = 0;
55
56         /// Adds state to node passed
57         virtual void get_state (XMLNode * node) const = 0;
58
59         /// Sets state from node passed
60         virtual void set_state (XMLNode * node, Session & session) = 0;
61
62         // Operator< must be defined for usage in e.g. std::map or std::set to disallow duplicates when necessary
63         virtual bool operator< (ExportChannel const & other) const = 0;
64 };
65
66 /// Basic export channel that reads from AudioPorts
67 class LIBARDOUR_API PortExportChannel : public ExportChannel
68 {
69   public:
70         typedef std::set<boost::weak_ptr<AudioPort> > PortSet;
71
72         PortExportChannel ();
73         ~PortExportChannel ();
74
75         void set_max_buffer_size(samplecnt_t samples);
76
77         void read (Sample const *& data, samplecnt_t samples) const;
78         bool empty () const { return ports.empty(); }
79
80         void get_state (XMLNode * node) const;
81         void set_state (XMLNode * node, Session & session);
82
83         bool operator< (ExportChannel const & other) const;
84
85         void add_port (boost::weak_ptr<AudioPort> port) { ports.insert (port); }
86         PortSet const & get_ports () { return ports; }
87
88   private:
89         PortSet ports;
90         samplecnt_t                 _buffer_size;
91         boost::scoped_array<Sample> _buffer;
92         std::list <boost::shared_ptr<PBD::RingBuffer<Sample> > >  _delaylines;
93 };
94
95
96 /// Handles RegionExportChannels and does actual reading from region
97 class LIBARDOUR_API RegionExportChannelFactory
98 {
99   public:
100         enum Type {
101                 None,
102                 Raw,
103                 Fades,
104                 Processed
105         };
106
107         RegionExportChannelFactory (Session * session, AudioRegion const & region, AudioTrack & track, Type type);
108         ~RegionExportChannelFactory ();
109
110         ExportChannelPtr create (uint32_t channel);
111         void read (uint32_t channel, Sample const *& data, samplecnt_t samples_to_read);
112
113   private:
114
115         int new_cycle_started (samplecnt_t) { buffers_up_to_date = false; return 0; }
116         void update_buffers (samplecnt_t samples);
117
118         AudioRegion const & region;
119         AudioTrack & track;
120         Type type;
121
122         samplecnt_t samples_per_cycle;
123         size_t n_channels;
124         BufferSet buffers;
125         bool buffers_up_to_date;
126         samplecnt_t region_start;
127         samplecnt_t position;
128
129         boost::scoped_array<Sample> mixdown_buffer;
130         boost::scoped_array<Sample> gain_buffer;
131
132         PBD::ScopedConnection export_connection;
133 };
134
135 /// Export channel that reads from region channel
136 class LIBARDOUR_API RegionExportChannel : public ExportChannel
137 {
138         friend class RegionExportChannelFactory;
139
140   public:
141         void read (Sample const *& data, samplecnt_t samples_to_read) const { factory.read (channel, data, samples_to_read); }
142         void get_state (XMLNode * /*node*/) const {};
143         void set_state (XMLNode * /*node*/, Session & /*session*/) {};
144         bool empty () const { return false; }
145         // Region export should never have duplicate channels, so there need not be any semantics here
146         bool operator< (ExportChannel const & other) const { return this < &other; }
147
148   private:
149
150         RegionExportChannel (RegionExportChannelFactory & factory, uint32_t channel)
151                 : factory (factory)
152                 , channel (channel)
153         {}
154
155         RegionExportChannelFactory & factory;
156         uint32_t channel;
157 };
158
159 /// Export channel for exporting from different positions in a route
160 class LIBARDOUR_API RouteExportChannel : public ExportChannel
161 {
162         class ProcessorRemover; // fwd declaration
163
164   public:
165         RouteExportChannel(boost::shared_ptr<CapturingProcessor> processor, size_t channel,
166                            boost::shared_ptr<ProcessorRemover> remover);
167         ~RouteExportChannel();
168
169         static void create_from_route(std::list<ExportChannelPtr> & result, boost::shared_ptr<Route> route);
170
171   public: // ExportChannel interface
172         void set_max_buffer_size(samplecnt_t samples);
173
174         void read (Sample const *& data, samplecnt_t samples) const;
175         bool empty () const { return false; }
176
177         void get_state (XMLNode * node) const;
178         void set_state (XMLNode * node, Session & session);
179
180         bool operator< (ExportChannel const & other) const;
181
182   private:
183
184         // Removes the processor from the track when deleted
185         class ProcessorRemover {
186           public:
187                  ProcessorRemover (boost::shared_ptr<Route> route, boost::shared_ptr<CapturingProcessor> processor)
188                         : route (route), processor (processor) {}
189                 ~ProcessorRemover();
190           private:
191                 boost::shared_ptr<Route> route;
192                 boost::shared_ptr<CapturingProcessor> processor;
193         };
194
195         boost::shared_ptr<CapturingProcessor> processor;
196         size_t channel;
197         // Each channel keeps a ref to the remover. Last one alive
198         // will cause the processor to be removed on deletion.
199         boost::shared_ptr<ProcessorRemover> remover;
200 };
201
202 } // namespace ARDOUR
203
204 #endif