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