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