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