Clean up region.h and trim include tree.
[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
26 #include <boost/signals2.hpp>
27 #include <boost/shared_ptr.hpp>
28 #include <boost/operators.hpp>
29
30 #include "pbd/signals.h"
31
32 #include "ardour/buffer_set.h"
33
34 namespace ARDOUR {
35
36 class Session;
37 class AudioTrack;
38 class AudioPort;
39 class AudioRegion;
40
41 /// Export channel base class interface for different source types
42 class ExportChannel : public boost::less_than_comparable<ExportChannel>
43 {
44   public:
45
46         virtual ~ExportChannel () {}
47
48         virtual void read (Sample * data, framecnt_t frames) const = 0;
49         virtual bool empty () const = 0;
50
51         /// Adds state to node passed
52         virtual void get_state (XMLNode * node) const = 0;
53
54         /// Sets state from node passed
55         virtual void set_state (XMLNode * node, Session & session) = 0;
56
57         // Operator< must be defined for usage in e.g. std::map or std::set to disallow duplicates when necessary
58         virtual bool operator< (ExportChannel const & other) const = 0;
59 };
60
61 /// Safe pointer for storing ExportChannels in ordered STL containers
62 class ExportChannelPtr : public boost::shared_ptr<ExportChannel>
63                        , public boost::less_than_comparable<ExportChannel>
64 {
65   public:
66         ExportChannelPtr () {}
67         template<typename Y> explicit ExportChannelPtr (Y * ptr) : boost::shared_ptr<ExportChannel> (ptr) {}
68
69         bool operator< (ExportChannelPtr const & other) const { return **this < *other; }
70 };
71
72 /// Basic export channel that reads from AudioPorts
73 class PortExportChannel : public ExportChannel
74 {
75   public:
76         typedef std::set<AudioPort *> PortSet;
77
78         PortExportChannel () {}
79
80         void read (Sample * data, framecnt_t frames) const;
81         bool empty () const { return ports.empty(); }
82
83         void get_state (XMLNode * node) const;
84         void set_state (XMLNode * node, Session & session);
85
86         bool operator< (ExportChannel const & other) const;
87
88         void add_port (AudioPort * port) { ports.insert (port); }
89         PortSet const & get_ports () { return ports; }
90
91   private:
92         PortSet ports;
93 };
94
95 /// Handles RegionExportChannels and does actual reading from region
96 class RegionExportChannelFactory 
97 {
98   public:
99         enum Type {
100                 Raw,
101                 Fades,
102                 Processed
103         };
104
105         RegionExportChannelFactory (Session * session, AudioRegion const & region, AudioTrack & track, Type type);
106         ~RegionExportChannelFactory ();
107
108         ExportChannelPtr create (uint32_t channel);
109         void read (uint32_t channel, Sample * data, framecnt_t frames_to_read);
110
111   private:
112
113         int new_cycle_started (framecnt_t) { buffers_up_to_date = false; return 0; }
114         void update_buffers (framecnt_t frames);
115
116         AudioRegion const & region;
117         AudioTrack & track;
118         Type type;
119
120         framecnt_t frames_per_cycle;
121         size_t n_channels;
122         BufferSet buffers;
123         bool buffers_up_to_date;
124         framecnt_t region_start;
125         framecnt_t position;
126
127         Sample * mixdown_buffer;
128         Sample * gain_buffer;
129
130         PBD::ScopedConnection export_connection;
131 };
132
133 /// Export channel that reads from region channel
134 class RegionExportChannel : public ExportChannel
135 {
136         friend class RegionExportChannelFactory;
137
138   public:
139         void read (Sample * data, framecnt_t frames_to_read) const { factory.read (channel, data, frames_to_read); }
140         void get_state (XMLNode * /*node*/) const {};
141         void set_state (XMLNode * /*node*/, Session & /*session*/) {};
142         bool empty () const { return false; }
143         // Region export should never have duplicate channels, so there need not be any semantics here
144         bool operator< (ExportChannel const & other) const { return this < &other; }
145
146   private:
147
148         RegionExportChannel (RegionExportChannelFactory & factory, uint32_t channel)
149                 : factory (factory)
150                 , channel (channel)
151         {}
152
153         RegionExportChannelFactory & factory;
154         uint32_t channel;
155 };
156
157 } // namespace ARDOUR
158
159 #endif