change Session::convert_to_frames_at() to Session::convert_to_frames() to reflect...
[ardour.git] / libs / ardour / export_channel.cc
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 #include "ardour/audio_buffer.h"
22 #include "ardour/audio_port.h"
23 #include "ardour/audio_track.h"
24 #include "ardour/audioengine.h"
25 #include "ardour/audioregion.h"
26 #include "ardour/export_channel.h"
27 #include "ardour/export_failed.h"
28 #include "ardour/session.h"
29
30 #include "pbd/error.h"
31
32 using namespace ARDOUR;
33
34 bool
35 PortExportChannel::operator< (ExportChannel const & other) const
36 {
37         PortExportChannel const * pec;
38         if (!(pec = dynamic_cast<PortExportChannel const *> (&other))) {
39                 return this < &other;
40         }
41         return ports < pec->ports;
42 }
43
44 void
45 PortExportChannel::read (Sample * data, framecnt_t frames) const
46 {
47         memset (data, 0, frames * sizeof (float));
48
49         for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) {
50                 if (*it != 0) {
51                         Sample* port_buffer = (*it)->get_audio_buffer(frames).data();
52
53                         for (uint32_t i = 0; i < frames; ++i) {
54                                 data[i] += (float) port_buffer[i];
55                         }
56                 }
57         }
58 }
59
60 void
61 PortExportChannel::get_state (XMLNode * node) const
62 {
63         XMLNode * port_node;
64         for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) {
65                 if ((port_node = node->add_child ("Port"))) {
66                         port_node->add_property ("name", (*it)->name());
67                 }
68         }
69 }
70
71 void
72 PortExportChannel::set_state (XMLNode * node, Session & session)
73 {
74         XMLProperty * prop;
75         XMLNodeList xml_ports = node->children ("Port");
76         for (XMLNodeList::iterator it = xml_ports.begin(); it != xml_ports.end(); ++it) {
77                 if ((prop = (*it)->property ("name"))) {
78                         std::string const & name = prop->value();
79                         AudioPort * port = dynamic_cast<AudioPort *> (session.engine().get_port_by_name (name));
80                         if (port) {
81                                 ports.insert (port);
82                         } else {
83                                 PBD::warning << string_compose (_("Could not get port for export channel \"%1\", dropping the channel"), name) << endmsg;
84                         }
85                 }
86         }
87 }
88
89 RegionExportChannelFactory::RegionExportChannelFactory (Session * session, AudioRegion const & region, AudioTrack & track, Type type) :
90   region (region),
91   track (track),
92   type (type),
93   frames_per_cycle (session->engine().frames_per_cycle ()),
94   buffers_up_to_date (false),
95   region_start (region.position()),
96   position (region_start),
97
98   mixdown_buffer (0),
99   gain_buffer (0)
100 {
101         switch (type) {
102           case Raw:
103                 n_channels = region.n_channels();
104                 break;
105           case Fades:
106                 n_channels = region.n_channels();
107
108                 mixdown_buffer = new Sample [frames_per_cycle];
109                 gain_buffer = new Sample [frames_per_cycle];
110                 memset (gain_buffer, 1.0, sizeof (Sample) * frames_per_cycle);
111
112                 break;
113           case Processed:
114                 n_channels = track.n_outputs().n_audio();
115                 break;
116           default:
117                 throw ExportFailed ("Unhandled type in ExportChannelFactory constructor");
118         }
119
120         session->ProcessExport.connect_same_thread (export_connection, boost::bind (&RegionExportChannelFactory::new_cycle_started, this, _1));
121
122         buffers.ensure_buffers (DataType::AUDIO, n_channels, frames_per_cycle);
123         buffers.set_count (ChanCount (DataType::AUDIO, n_channels));
124 }
125
126 RegionExportChannelFactory::~RegionExportChannelFactory ()
127 {
128         delete[] mixdown_buffer;
129         delete[] gain_buffer;
130 }
131
132 ExportChannelPtr
133 RegionExportChannelFactory::create (uint32_t channel)
134 {
135         assert (channel < n_channels);
136         return ExportChannelPtr (new RegionExportChannel (*this, channel));
137 }
138
139 void
140 RegionExportChannelFactory::read (uint32_t channel, Sample * data, framecnt_t frames_to_read)
141 {
142         assert (channel < n_channels);
143         assert (frames_to_read <= frames_per_cycle);
144
145         if (!buffers_up_to_date) {
146                 update_buffers(frames_to_read);
147                 buffers_up_to_date = true;
148         }
149
150         memcpy (data, buffers.get_audio (channel).data(), frames_to_read * sizeof (Sample));
151 }
152
153 void
154 RegionExportChannelFactory::update_buffers (framecnt_t frames)
155 {
156         assert (frames <= frames_per_cycle);
157
158         switch (type) {
159           case Raw:
160                 for (size_t channel = 0; channel < n_channels; ++channel) {
161                         region.read (buffers.get_audio (channel).data(), position - region_start, frames, channel);
162                 }
163                 break;
164           case Fades:
165                 assert (mixdown_buffer && gain_buffer);
166                 for (size_t channel = 0; channel < n_channels; ++channel) {
167                         memset (mixdown_buffer, 0, sizeof (Sample) * frames);
168                         region.read_at (buffers.get_audio (channel).data(), mixdown_buffer, gain_buffer, position, frames, channel);
169                 }
170                 break;
171           case Processed:
172                 track.export_stuff (buffers, position, frames);
173                 break;
174           default:
175                 throw ExportFailed ("Unhandled type in ExportChannelFactory::update_buffers");
176         }
177
178         position += frames;
179 }