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