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