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