new files from sakari, missed last time
[ardour.git] / libs / ardour / export_channel_configuration.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/export_channel_configuration.h>
22
23 #include <ardour/export_handler.h>
24 #include <ardour/export_filename.h>
25 #include <ardour/export_processor.h>
26 #include <ardour/export_timespan.h>
27
28 #include <ardour/audio_port.h>
29 #include <ardour/export_failed.h>
30 #include <ardour/midi_port.h>
31 #include <pbd/pthread_utils.h>
32
33 namespace ARDOUR
34 {
35
36 /* ExportChannel */
37
38 ExportChannel::ExportChannel ()
39 {
40
41 }
42
43 ExportChannel::~ExportChannel ()
44 {
45
46 }
47
48 void
49 ExportChannel::read_ports (float * data, nframes_t frames) const
50 {
51         memset (data, 0, frames * sizeof (float));
52
53         for (iterator it = begin(); it != end(); ++it) {
54                 if (*it != 0) {
55                         Sample* port_buffer = (*it)->get_audio_buffer().data();
56                         
57                         for (uint32_t i = 0; i < frames; ++i) {
58                                 data[i] += (float) port_buffer[i];
59                         }
60                 }
61         }
62 }
63
64 /* ExportChannelConfiguration */
65
66 ExportChannelConfiguration::ExportChannelConfiguration (ExportStatus & status) :
67   writer_thread (*this),
68   status (status),
69   files_written (false),
70   split (false)
71 {
72
73 }
74
75 ExportChannelConfiguration::~ExportChannelConfiguration ()
76 {
77
78 }
79
80 bool
81 ExportChannelConfiguration::all_channels_have_ports ()
82 {
83         for (ChannelList::iterator it = channels.begin(); it != channels.end(); ++it) {
84                 if ((*it)->empty ()) { return false; }
85         }
86         
87         return true;
88 }
89
90 bool
91 ExportChannelConfiguration::write_files (boost::shared_ptr<ExportProcessor> new_processor)
92 {
93         if (files_written || writer_thread.running) {
94                 return false;
95         }
96         
97         files_written = true;
98
99         if (!timespan) {
100                 throw ExportFailed (_("Export failed due to a programming error"), _("No timespan registered to channel configuration when requesting files to be written"));
101         }
102         
103         /* Take a local copy of the processor to be used in the thread that is created below */
104         
105         processor.reset (new_processor->copy());
106         
107         /* Create new thread for post processing */
108         
109         pthread_create (&writer_thread.thread, 0, _write_files, &writer_thread);
110         writer_thread.running = true;
111         pthread_detach (writer_thread.thread);
112         
113         return true;
114 }
115
116 void
117 ExportChannelConfiguration::write_file ()
118 {
119         timespan->rewind ();
120         nframes_t progress = 0;
121         nframes_t timespan_length = timespan->get_length();
122
123         nframes_t frames = 2048; // TODO good block size ?
124         nframes_t frames_read = 0;
125         
126         float * channel_buffer = new float [frames];
127         float * file_buffer = new float [channels.size() * frames];
128         uint32_t channel_count = channels.size();
129         uint32_t channel;
130         
131         do {
132                 if (status.aborted()) { break; }
133         
134                 channel = 0;
135                 for (ChannelList::iterator it = channels.begin(); it != channels.end(); ++it) {
136                         
137                         /* Get channel data */
138                         
139                         frames_read = timespan->get_data (channel_buffer, frames, **it);
140                         
141                         /* Interleave into file buffer */
142                         
143                         for (uint32_t i = 0; i < frames_read; ++i) {
144                                 file_buffer[channel + (channel_count * i)] = channel_buffer[i];
145                         }
146                         
147                         ++channel;
148                 }
149                 
150                 progress += frames_read;
151                 status.progress = (float) progress / timespan_length;
152                 
153         } while (processor->process (file_buffer, frames_read) > 0);
154         
155         delete [] channel_buffer;
156         delete [] file_buffer;
157 }
158
159 void *
160 ExportChannelConfiguration::_write_files (void *arg)
161 {
162
163         PBD::ThreadCreated (pthread_self(), "Export post-processing");
164         
165         // cc can be trated like 'this'
166         WriterThread & cc (*((WriterThread *) arg));
167         
168         for (FileConfigList::iterator it = cc->file_configs.begin(); it != cc->file_configs.end(); ++it) {
169                 if (cc->status.aborted()) {
170                         break;
171                 }
172                 cc->processor->prepare (it->first, it->second, cc->channels.size(), cc->split, cc->timespan->get_start());
173                 cc->write_file (); // Writes tempfile
174                 cc->processor->prepare_post_processors ();
175                 cc->processor->write_files();
176         }
177         
178         cc.running = false;
179         cc->files_written = true;
180         cc->FilesWritten();
181         
182         return 0; // avoid compiler warnings
183 }
184
185 void
186 ExportChannelConfiguration::register_with_timespan (TimespanPtr new_timespan)
187 {
188         timespan = new_timespan;
189         
190         for (ChannelList::iterator it = channels.begin(); it != channels.end(); ++it) {
191                 timespan->register_channel (**it);
192         }
193 }
194
195 void
196 ExportChannelConfiguration::unregister_all ()
197 {
198         timespan.reset();
199         processor.reset();
200         file_configs.clear();
201         files_written = false;
202 }
203
204 } // namespace ARDOUR