Remove use of i18n macros in headers. Prevents our gettext.h being included before...
[ardour.git] / gtk2_ardour / editor_export_audio.cc
1 /*
2     Copyright (C) 2001 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /* Note: public Editor methods are documented in public_editor.h */
21
22 #define __STDC_FORMAT_MACROS 1
23 #include <inttypes.h>
24 #include <unistd.h>
25 #include <climits>
26
27 #include <gtkmm/messagedialog.h>
28
29 #include "export_dialog.h"
30 #include "editor.h"
31 #include "public_editor.h"
32 #include "selection.h"
33 #include "time_axis_view.h"
34 #include "audio_time_axis.h"
35 #include "audio_region_view.h"
36 #include "midi_region_view.h"
37
38 #include "pbd/pthread_utils.h"
39 #include "ardour/types.h"
40 #include "ardour/audio_track.h"
41 #include "ardour/audiofilesource.h"
42 #include "ardour/audio_diskstream.h"
43 #include "ardour/audioregion.h"
44 #include "ardour/audioplaylist.h"
45 #include "ardour/chan_count.h"
46 #include "ardour/session_directory.h"
47 #include "ardour/source_factory.h"
48 #include "ardour/audiofilesource.h"
49 #include "ardour/session.h"
50
51 #include "i18n.h"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace Gtk;
57
58 void
59 Editor::export_audio ()
60 {
61         ExportDialog dialog (*this, _("Export"));
62         dialog.set_session (_session);
63         dialog.run();
64 }
65
66 void
67 Editor::export_selection ()
68 {
69         ExportSelectionDialog dialog (*this);
70         dialog.set_session (_session);
71         dialog.run();
72 }
73
74 void
75 Editor::export_range ()
76 {
77         Marker* marker;
78
79         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
80                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
81                 /*NOTREACHED*/
82         }
83
84         Location* l;
85         bool is_start;
86
87         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
88                 ExportRangeDialog dialog (*this, l->id().to_s());
89                 dialog.set_session (_session);
90                 dialog.run();
91         }
92 }
93
94 /** Export the first selected region */
95 void
96 Editor::export_region ()
97 {
98         if (selection->regions.empty()) {
99                 return;
100         }
101
102         try {
103                 boost::shared_ptr<Region> r = selection->regions.front()->region();
104                 AudioRegion & region (dynamic_cast<AudioRegion &> (*r));
105
106                 RouteTimeAxisView & rtv (dynamic_cast<RouteTimeAxisView &> (selection->regions.front()->get_time_axis_view()));
107                 AudioTrack & track (dynamic_cast<AudioTrack &> (*rtv.route()));
108
109                 ExportRegionDialog dialog (*this, region, track);
110                 dialog.set_session (_session);
111                 dialog.run();
112
113         } catch (std::bad_cast & e) {
114                 error << "Exporting Region failed!" << endmsg;
115                 return;
116         }
117 }
118
119 int
120 Editor::write_region_selection (RegionSelection& regions)
121 {
122         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
123                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
124                 if (arv) {
125                         if (write_region ("", arv->audio_region()) == false)
126                                 return -1;
127                 }
128
129                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
130                 if (mrv) {
131                         warning << "MIDI region export not implemented" << endmsg;
132                 }
133         }
134
135         return 0;
136 }
137
138 void
139 Editor::bounce_region_selection ()
140 {
141         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
142
143                 boost::shared_ptr<Region> region ((*i)->region());
144                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
145                 Track* track = dynamic_cast<Track*>(rtv->route().get());
146
147                 InterThreadInfo itt;
148
149                 boost::shared_ptr<Region> r = track->bounce_range (region->position(), region->position() + region->length(), itt);
150                 cerr << "Result of bounce of "
151                      << region->name() << " len = " << region->length()
152                      << " was "
153                      << r->name() << " len = " << r->length()
154                      << endl;
155         }
156 }
157
158 bool
159 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
160 {
161         boost::shared_ptr<AudioFileSource> fs;
162         const nframes64_t chunk_size = 4096;
163         nframes64_t to_read;
164         Sample buf[chunk_size];
165         gain_t gain_buffer[chunk_size];
166         nframes64_t pos;
167         char s[PATH_MAX+1];
168         uint32_t cnt;
169         vector<boost::shared_ptr<AudioFileSource> > sources;
170         uint32_t nchans;
171
172         const string sound_directory = _session->session_directory().sound_path().to_string();
173
174         nchans = region->n_channels();
175
176         /* don't do duplicate of the entire source if that's what is going on here */
177
178         if (region->start() == 0 && region->length() == region->source_length(0)) {
179                 /* XXX should link(2) to create a new inode with "path" */
180                 return true;
181         }
182
183         if (path.length() == 0) {
184
185                 for (uint32_t n=0; n < nchans; ++n) {
186
187                         for (cnt = 0; cnt < 999999; ++cnt) {
188                                 if (nchans == 1) {
189                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
190                                                   legalize_for_path(region->name()).c_str(), cnt);
191                                 }
192                                 else {
193                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
194                                                   legalize_for_path(region->name()).c_str(), cnt, n);
195                                 }
196
197                                 path = s;
198
199                                 if (::access (path.c_str(), F_OK) != 0) {
200                                         break;
201                                 }
202                         }
203
204                         if (cnt == 999999) {
205                                 error << "" << endmsg;
206                                 goto error_out;
207                         }
208
209
210
211                         try {
212                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (
213                                                 SourceFactory::createWritable (DataType::AUDIO, *_session,
214                                                                 path, true,
215                                                                 false, _session->frame_rate()));
216                         }
217
218                         catch (failed_constructor& err) {
219                                 goto error_out;
220                         }
221
222                         sources.push_back (fs);
223                 }
224         }
225         else {
226                 /* TODO: make filesources based on passed path */
227
228         }
229
230         to_read = region->length();
231         pos = region->position();
232
233         while (to_read) {
234                 nframes64_t this_time;
235
236                 this_time = min (to_read, chunk_size);
237
238                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
239
240                         fs = (*src);
241
242                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
243                                 break;
244                         }
245
246                         if (fs->write (buf, this_time) != this_time) {
247                                 error << "" << endmsg;
248                                 goto error_out;
249                         }
250                 }
251
252                 to_read -= this_time;
253                 pos += this_time;
254         }
255
256         time_t tnow;
257         struct tm* now;
258         time (&tnow);
259         now = localtime (&tnow);
260
261         for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
262                 (*src)->update_header (0, *now, tnow);
263                 (*src)->mark_immutable ();
264         }
265
266         return true;
267
268 error_out:
269
270         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
271                 (*i)->mark_for_remove ();
272         }
273
274         return 0;
275 }
276
277 int
278 Editor::write_audio_selection (TimeSelection& ts)
279 {
280         int ret = 0;
281
282         if (selection->tracks.empty()) {
283                 return 0;
284         }
285
286         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
287
288                 AudioTimeAxisView* atv;
289
290                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
291                         continue;
292                 }
293
294                 if (atv->is_audio_track()) {
295
296                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->track()->playlist());
297
298                         if (playlist && write_audio_range (*playlist, atv->track()->n_channels(), ts) == 0) {
299                                 ret = -1;
300                                 break;
301                         }
302                 }
303         }
304
305         return ret;
306 }
307
308 bool
309 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
310 {
311         boost::shared_ptr<AudioFileSource> fs;
312         const nframes64_t chunk_size = 4096;
313         nframes64_t nframes;
314         Sample buf[chunk_size];
315         gain_t gain_buffer[chunk_size];
316         nframes64_t pos;
317         char s[PATH_MAX+1];
318         uint32_t cnt;
319         string path;
320         vector<boost::shared_ptr<AudioFileSource> > sources;
321
322         const string sound_directory = _session->session_directory().sound_path().to_string();
323
324         uint32_t channels = count.n_audio();
325
326         for (uint32_t n=0; n < channels; ++n) {
327
328                 for (cnt = 0; cnt < 999999; ++cnt) {
329                         if (channels == 1) {
330                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
331                                           legalize_for_path(playlist.name()).c_str(), cnt);
332                         }
333                         else {
334                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
335                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
336                         }
337
338                         if (::access (s, F_OK) != 0) {
339                                 break;
340                         }
341                 }
342
343                 if (cnt == 999999) {
344                         error << "" << endmsg;
345                         goto error_out;
346                 }
347
348                 path = s;
349
350                 try {
351                         fs = boost::dynamic_pointer_cast<AudioFileSource> (
352                                         SourceFactory::createWritable (DataType::AUDIO, *_session,
353                                                         path, true,
354                                                         false, _session->frame_rate()));
355                 }
356
357                 catch (failed_constructor& err) {
358                         goto error_out;
359                 }
360
361                 sources.push_back (fs);
362
363         }
364
365
366         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
367
368                 nframes = (*i).length();
369                 pos = (*i).start;
370
371                 while (nframes) {
372                         nframes64_t this_time;
373
374                         this_time = min (nframes, chunk_size);
375
376                         for (uint32_t n=0; n < channels; ++n) {
377
378                                 fs = sources[n];
379
380                                 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
381                                         break;
382                                 }
383
384                                 if (fs->write (buf, this_time) != this_time) {
385                                         goto error_out;
386                                 }
387                         }
388
389                         nframes -= this_time;
390                         pos += this_time;
391                 }
392
393                 list<AudioRange>::iterator tmp = i;
394                 ++tmp;
395
396                 if (tmp != range.end()) {
397
398                         /* fill gaps with silence */
399
400                         nframes = (*tmp).start - (*i).end;
401
402                         while (nframes) {
403
404                                 nframes64_t this_time = min (nframes, chunk_size);
405                                 memset (buf, 0, sizeof (Sample) * this_time);
406
407                                 for (uint32_t n=0; n < channels; ++n) {
408
409                                         fs = sources[n];
410                                         if (fs->write (buf, this_time) != this_time) {
411                                                 goto error_out;
412                                         }
413                                 }
414
415                                 nframes -= this_time;
416                         }
417                 }
418
419                 i = tmp;
420         }
421
422         time_t tnow;
423         struct tm* now;
424         time (&tnow);
425         now = localtime (&tnow);
426
427         for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
428                 (*s)->update_header (0, *now, tnow);
429                 (*s)->mark_immutable ();
430                 // do we need to ref it again?
431         }
432
433         return true;
434
435 error_out:
436         /* unref created files */
437
438         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
439                 (*i)->mark_for_remove ();
440         }
441
442         return false;
443 }
444
445 void
446 Editor::write_selection ()
447 {
448         if (!selection->time.empty()) {
449                 write_audio_selection (selection->time);
450         } else if (!selection->regions.empty()) {
451                 write_region_selection (selection->regions);
452         }
453 }