Re-work main right-click context menu to operate on the selection, and remove some...
[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/chan_count.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_session()
56 {
57         if (session) {
58                 export_range (session->current_start_frame(), session->current_end_frame());
59         }
60 }
61
62 void
63 Editor::export_selection ()
64 {
65         if (session) {
66                 if (selection->time.empty()) {
67                         MessageDialog message (*this, _("There is no selection to export.\n\nSelect a selection using the range mouse mode"));
68                         message.run ();
69                         return;
70                 }
71
72                 export_range (selection->time.front().start, selection->time.front().end);
73         }
74 }
75
76 void
77 Editor::export_range (nframes_t start, nframes_t end)
78 {
79         if (session) {
80                 if (export_dialog == 0) {
81                         export_dialog = new ExportSessionDialog (*this);
82                 }
83                 
84                 export_dialog->connect_to_session (session);
85                 export_dialog->set_range (start, end);
86                 export_dialog->start_export();
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 (r->first_frame(), r->last_frame());
104         dialog->start_export();
105 }
106
107 void
108 Editor::export_range_markers ()
109 {
110         if (session) {
111
112                 if (session->locations()->num_range_markers() == 0) {
113                         MessageDialog message (*this, _("There are no ranges to export.\n\nCreate 1 or more ranges by dragging the mouse in the range bar"));
114                         message.run ();
115                         return;
116                 }
117                 
118
119                 if (export_range_markers_dialog == 0) {
120                         export_range_markers_dialog = new ExportRangeMarkersDialog(*this);
121                 }
122                 
123                 export_range_markers_dialog->connect_to_session (session);
124                 export_range_markers_dialog->start_export();
125         }
126 }       
127
128 int
129 Editor::write_region_selection (RegionSelection& regions)
130 {
131         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
132                 // FIXME
133                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
134                 if (arv)
135                         if (write_region ("", arv->audio_region()) == false)
136                                 return -1;
137         }
138
139         return 0;
140 }
141
142 void
143 Editor::bounce_region_selection ()
144 {
145         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
146                 
147                 boost::shared_ptr<Region> region ((*i)->region());
148                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
149                 Track* track = dynamic_cast<Track*>(rtv->route().get());
150
151                 InterThreadInfo itt;
152
153                 itt.done = false;
154                 itt.cancel = false;
155                 itt.progress = 0.0f;
156
157                 track->bounce_range (region->position(), region->position() + region->length(), itt);
158         }
159 }
160
161 bool
162 Editor::write_region (string path, boost::shared_ptr<AudioRegion> region)
163 {
164         boost::shared_ptr<AudioFileSource> fs;
165         const nframes_t chunk_size = 4096;
166         nframes_t to_read;
167         Sample buf[chunk_size];
168         gain_t gain_buffer[chunk_size];
169         nframes_t pos;
170         char s[PATH_MAX+1];
171         uint32_t cnt;
172         vector<boost::shared_ptr<AudioFileSource> > sources;
173         uint32_t nchans;
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()) {
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", session->sound_dir().c_str(),
191                                                   legalize_for_path(region->name()).c_str(), cnt);
192                                 }
193                                 else {
194                                         snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().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> (SourceFactory::createWritable (DataType::AUDIO, *session, path, false, session->frame_rate()));
214                         }
215                         
216                         catch (failed_constructor& err) {
217                                 goto error_out;
218                         }
219
220                         sources.push_back (fs);
221                 }
222         }
223         else {
224                 /* TODO: make filesources based on passed path */
225
226         }
227         
228         to_read = region->length();
229         pos = region->position();
230
231         while (to_read) {
232                 nframes_t this_time;
233
234                 this_time = min (to_read, chunk_size);
235
236                 for (vector<boost::shared_ptr<AudioFileSource> >::iterator src=sources.begin(); src != sources.end(); ++src) {
237                         
238                         fs = (*src);
239
240                         if (region->read_at (buf, buf, gain_buffer, pos, this_time) != this_time) {
241                                 break;
242                         }
243                         
244                         if (fs->write (buf, this_time) != this_time) {
245                                 error << "" << endmsg;
246                                 goto error_out;
247                         }
248                 }
249
250                 to_read -= this_time;
251                 pos += this_time;
252         }
253
254         time_t tnow;
255         struct tm* now;
256         time (&tnow);
257         now = localtime (&tnow);
258         
259         for (vector<boost::shared_ptr<AudioFileSource> >::iterator src = sources.begin(); src != sources.end(); ++src) {
260                 (*src)->update_header (0, *now, tnow);
261                 (*src)->mark_immutable ();
262         }
263
264         return true;
265
266 error_out:
267
268         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
269                 (*i)->mark_for_remove ();
270         }
271
272         return 0;
273 }
274
275 int
276 Editor::write_audio_selection (TimeSelection& ts)
277 {
278         int ret = 0;
279
280         if (selection->tracks.empty()) {
281                 return 0;
282         }
283
284         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
285
286                 AudioTimeAxisView* atv;
287
288                 if ((atv = dynamic_cast<AudioTimeAxisView*>(*i)) == 0) {
289                         continue;
290                 }
291
292                 if (atv->is_audio_track()) {
293
294                         boost::shared_ptr<AudioPlaylist> playlist = boost::dynamic_pointer_cast<AudioPlaylist>(atv->get_diskstream()->playlist());
295                         
296                         if (playlist && write_audio_range (*playlist, atv->get_diskstream()->n_channels(), ts) == 0) {
297                                 ret = -1;
298                                 break;
299                         }
300                 }
301         }
302
303         return ret;
304 }
305
306 bool
307 Editor::write_audio_range (AudioPlaylist& playlist, const ChanCount& count, list<AudioRange>& range)
308 {
309         boost::shared_ptr<AudioFileSource> fs;
310         const nframes_t chunk_size = 4096;
311         nframes_t nframes;
312         Sample buf[chunk_size];
313         gain_t gain_buffer[chunk_size];
314         nframes_t pos;
315         char s[PATH_MAX+1];
316         uint32_t cnt;
317         string path;
318         vector<boost::shared_ptr<AudioFileSource> > sources;
319
320         uint32_t channels = count.n_audio();
321
322         for (uint32_t n=0; n < channels; ++n) {
323                 
324                 for (cnt = 0; cnt < 999999; ++cnt) {
325                         if (channels == 1) {
326                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 ".wav", session->sound_dir().c_str(),
327                                           legalize_for_path(playlist.name()).c_str(), cnt);
328                         }
329                         else {
330                                 snprintf (s, sizeof(s), "%s/%s_%" PRIu32 "-%" PRId32 ".wav", session->sound_dir().c_str(),
331                                           legalize_for_path(playlist.name()).c_str(), cnt, n);
332                         }
333                         
334                         if (::access (s, F_OK) != 0) {
335                                 break;
336                         }
337                 }
338                 
339                 if (cnt == 999999) {
340                         error << "" << endmsg;
341                         goto error_out;
342                 }
343
344                 path = s;
345                 
346                 try {
347                         fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, *session, path, false, session->frame_rate()));
348                 }
349                 
350                 catch (failed_constructor& err) {
351                         goto error_out;
352                 }
353                 
354                 sources.push_back (fs);
355
356         }
357         
358
359         for (list<AudioRange>::iterator i = range.begin(); i != range.end();) {
360         
361                 nframes = (*i).length();
362                 pos = (*i).start;
363                 
364                 while (nframes) {
365                         nframes_t this_time;
366                         
367                         this_time = min (nframes, chunk_size);
368
369                         for (uint32_t n=0; n < channels; ++n) {
370
371                                 fs = sources[n];
372                                 
373                                 if (playlist.read (buf, buf, gain_buffer, pos, this_time, n) != this_time) {
374                                         break;
375                                 }
376                                 
377                                 if (fs->write (buf, this_time) != this_time) {
378                                         goto error_out;
379                                 }
380                         }
381                         
382                         nframes -= this_time;
383                         pos += this_time;
384                 }
385                 
386                 list<AudioRange>::iterator tmp = i;
387                 ++tmp;
388
389                 if (tmp != range.end()) {
390                         
391                         /* fill gaps with silence */
392                         
393                         nframes = (*tmp).start - (*i).end;
394
395                         while (nframes) {
396
397                                 nframes_t this_time = min (nframes, chunk_size);
398                                 memset (buf, 0, sizeof (Sample) * this_time);
399
400                                 for (uint32_t n=0; n < channels; ++n) {
401
402                                         fs = sources[n];
403                                         if (fs->write (buf, this_time) != this_time) {
404                                                 goto error_out;
405                                         }
406                                 }
407
408                                 nframes -= this_time;
409                         }
410                 }
411
412                 i = tmp;
413         }
414
415         time_t tnow;
416         struct tm* now;
417         time (&tnow);
418         now = localtime (&tnow);
419
420         for (vector<boost::shared_ptr<AudioFileSource> >::iterator s = sources.begin(); s != sources.end(); ++s) {
421                 (*s)->update_header (0, *now, tnow);
422                 (*s)->mark_immutable ();
423                 // do we need to ref it again?
424         }
425         
426         return true;
427
428 error_out:
429         /* unref created files */
430
431         for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = sources.begin(); i != sources.end(); ++i) {
432                 (*i)->mark_for_remove ();
433         }
434
435         return false;
436 }
437
438 void
439 Editor::write_selection ()
440 {
441         if (!selection->time.empty()) {
442                 write_audio_selection (selection->time);
443         } else if (!selection->regions.empty()) {
444                 write_region_selection (selection->regions);
445         }
446 }