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