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