use filechooser widget in export dialog, selected files set format combos, hide progr...
[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_session_dialog.h"
28 #include "export_region_dialog.h"
29 #include "export_range_markers_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
37 #include <pbd/pthread_utils.h>
38 #include <ardour/types.h>
39 #include <ardour/export.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
50 #include "i18n.h"
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55 using namespace Gtk;
56
57 void
58 Editor::export_session()
59 {
60         if (session) {
61                 export_range (session->current_start_frame(), session->current_end_frame());
62         }
63 }
64
65 void
66 Editor::export_selection ()
67 {
68         if (session) {
69                 if (selection->time.empty()) {
70                         MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
71                         message.run ();
72                         return;
73                 }
74
75                 export_range (selection->time.front().start, selection->time.front().end);
76         }
77 }
78
79 void
80 Editor::export_range (nframes_t start, nframes_t end)
81 {
82         if (session) {
83                 if (export_dialog == 0) {
84                         export_dialog = new ExportSessionDialog (*this);
85                         export_dialog->connect_to_session (session);
86                 }
87                 
88                 export_dialog->set_range (start, end);
89                 export_dialog->start_export();
90         }
91 }       
92
93 /** Export the first selected region */
94 void
95 Editor::export_region ()
96 {
97         if (selection->regions.empty()) {
98                 return;
99         }
100
101         boost::shared_ptr<Region> r = selection->regions.front()->region();
102         
103         ExportDialog* dialog = new ExportRegionDialog (*this, r);
104                 
105         dialog->connect_to_session (session);
106         dialog->set_range (r->first_frame(), r->last_frame());
107         dialog->start_export();
108 }
109
110 void
111 Editor::export_range_markers ()
112 {
113         if (session) {
114
115                 if (session->locations()->num_range_markers() == 0) {
116                         MessageDialog message (*this, _("There are no ranges to export.\n\nCreate 1 or more ranges by dragging the mouse in the range bar"));
117                         message.run ();
118                         return;
119                 }
120                 
121
122                 if (export_range_markers_dialog == 0) {
123                         export_range_markers_dialog = new ExportRangeMarkersDialog(*this);
124                         export_range_markers_dialog->connect_to_session (session);
125                 }
126
127                 export_range_markers_dialog->start_export();
128         }
129 }       
130
131 int
132 Editor::write_region_selection (RegionSelection& regions)
133 {
134         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
135                 // FIXME
136                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
137                 if (arv)
138                         if (write_region ("", arv->audio_region()) == false)
139                                 return -1;
140         }
141
142         return 0;
143 }
144
145 void
146 Editor::bounce_region_selection ()
147 {
148         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
149                 
150                 boost::shared_ptr<Region> region ((*i)->region());
151                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
152                 Track* track = dynamic_cast<Track*>(rtv->route().get());
153
154                 InterThreadInfo itt;
155
156                 itt.done = false;
157                 itt.cancel = false;
158                 itt.progress = 0.0f;
159
160                 track->bounce_range (region->position(), region->position() + region->length(), itt);
161         }
162 }
163
164 bool
165 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
166 {
167         boost::shared_ptr<AudioFileSource> fs;
168         const nframes_t chunk_size = 4096;
169         nframes_t to_read;
170         Sample buf[chunk_size];
171         gain_t gain_buffer[chunk_size];
172         nframes_t pos;
173         char s[PATH_MAX+1];
174         uint32_t cnt;
175         vector<boost::shared_ptr<AudioFileSource> > sources;
176         uint32_t nchans;
177
178         const string sound_directory = session->session_directory().sound_path().to_string();
179
180         nchans = region->n_channels();
181         
182         /* don't do duplicate of the entire source if that's what is going on here */
183
184         if (region->start() == 0 && region->length() == region->source()->length()) {
185                 /* XXX should link(2) to create a new inode with "path" */
186                 return true;
187         }
188
189         if (path.length() == 0) {
190
191                 for (uint32_t n=0; n < nchans; ++n) {
192                         
193                         for (cnt = 0; cnt < 999999; ++cnt) {
194                                 if (nchans == 1) {
195                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
196                                                   legalize_for_path(region->name()).c_str(), cnt);
197                                 }
198                                 else {
199                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
200                                                   legalize_for_path(region->name()).c_str(), cnt, n);
201                                 }
202
203                                 path = s;
204                                 
205                                 if (::access (path.c_str(), F_OK) != 0) {
206                                         break;
207                                 }
208                         }
209                         
210                         if (cnt == 999999) {
211                                 error << "" << endmsg;
212                                 goto error_out;
213                         }
214                         
215                 
216                         
217                         try {
218                                 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *session, path, false, session->frame_rate()));
219                         }
220                         
221                         catch (failed_constructor& err) {
222                                 goto error_out;
223                         }
224
225                         sources.push_back (fs);
226                 }
227         }
228         else {
229                 /* TODO: make filesources based on passed path */
230
231         }
232         
233         to_read = region->length();
234         pos = region->position();
235
236         while (to_read) {
237                 nframes_t this_time;
238
239                 this_time = min (to_read, chunk_size);
240
241                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
242                         
243                         fs = (*src);
244
245                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
246                                 break;
247                         }
248                         
249                         if (fs->write (buf, this_time) != this_time) {
250                                 error << "" << endmsg;
251                                 goto error_out;
252                         }
253                 }
254
255                 to_read -= this_time;
256                 pos += this_time;
257         }
258
259         time_t tnow;
260         struct tm* now;
261         time (&tnow);
262         now = localtime (&tnow);
263         
264         for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
265                 (*src)->update_header (0, *now, tnow);
266                 (*src)->mark_immutable ();
267         }
268
269         return true;
270
271 error_out:
272
273         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
274                 (*i)->mark_for_remove ();
275         }
276
277         return 0;
278 }
279
280 int
281 Editor::write_audio_selection (TimeSelection& ts)
282 {
283         int ret = 0;
284
285         if (selection->tracks.empty()) {
286                 return 0;
287         }
288
289         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
290
291                 AudioTimeAxisView* atv;
292
293                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
294                         continue;
295                 }
296
297                 if (atv->is_audio_track()) {
298
299                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
300                         
301                         if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
302                                 ret = -1;
303                                 break;
304                         }
305                 }
306         }
307
308         return ret;
309 }
310
311 bool
312 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
313 {
314         boost::shared_ptr<AudioFileSource> fs;
315         const nframes_t chunk_size = 4096;
316         nframes_t nframes;
317         Sample buf[chunk_size];
318         gain_t gain_buffer[chunk_size];
319         nframes_t pos;
320         char s[PATH_MAX+1];
321         uint32_t cnt;
322         string path;
323         vector<boost::shared_ptr<AudioFileSource> > sources;
324
325         const string sound_directory = session->session_directory().sound_path().to_string();
326
327         uint32_t channels = count.n_audio();
328
329         for (uint32_t n=0; n < channels; ++n) {
330                 
331                 for (cnt = 0; cnt < 999999; ++cnt) {
332                         if (channels == 1) {
333                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", sound_directory.c_str(),
334                                           legalize_for_path(playlist.name()).c_str(), cnt);
335                         }
336                         else {
337                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", sound_directory.c_str(),
338                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
339                         }
340                         
341                         if (::access (s, F_OK) != 0) {
342                                 break;
343                         }
344                 }
345                 
346                 if (cnt == 999999) {
347                         error << "" << endmsg;
348                         goto error_out;
349                 }
350
351                 path = s;
352                 
353                 try {
354                         fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *session, path, 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                         nframes_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                                 nframes_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 }