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