more gcc4 fixes to handle jumps over declarations
[ardour.git] / gtk2_ardour / editor_audio_import.cc
1 /*
2     Copyright (C) 2000-2006 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     $Id$
19 */
20
21 #include <pbd/pthread_utils.h>
22 #include <pbd/basename.h>
23
24 #include <gtkmm2ext/choice.h>
25
26 #include <ardour/session.h>
27 #include <ardour/audioplaylist.h>
28 #include <ardour/audioregion.h>
29 #include <ardour/diskstream.h>
30 #include <ardour/filesource.h>
31 #include <ardour/externalsource.h>
32 #include <ardour/utils.h>
33 #include <ardour/audio_track.h>
34 #include <ardour/audioplaylist.h>
35
36 #include "ardour_ui.h"
37 #include "editor.h"
38 #include "sfdb_ui.h"
39 #include "editing.h"
40 #include "audio_time_axis.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace ARDOUR;
46 using namespace sigc;
47 using namespace Gtk;
48 using namespace Editing;
49
50 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
51
52 void
53 Editor::add_external_audio_action (ImportMode mode)
54 {
55         jack_nframes_t& pos = edit_cursor->current_frame;
56         AudioTrack* track = 0;
57
58         if (!selection->tracks.empty()) {
59                 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
60                 if (atv) {
61                         track = atv->audio_track();
62                 }
63         }
64
65         bring_in_external_audio (mode, track, pos, false);
66 }
67
68 void
69 Editor::bring_in_external_audio (ImportMode mode, AudioTrack* track, jack_nframes_t& pos, bool prompt)
70 {
71         if (session == 0) {
72                 MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
73                 msg.run ();
74                 return;
75         }
76
77         SoundFileOmega sfdb (_("Add existing audio to session"));
78         sfdb.set_mode (mode);
79
80         switch (sfdb.run()) {
81         case SoundFileOmega::ResponseImport:
82                 do_import (sfdb.get_paths(), sfdb.get_split(), mode, track, pos, prompt);
83                 break;
84                 
85         case SoundFileOmega::ResponseEmbed:
86                 do_embed (sfdb.get_paths(), sfdb.get_split(), mode, track, pos, prompt);
87                 break;
88
89         default:
90                 break;
91         }
92 }
93
94 void
95 Editor::do_import (vector<Glib::ustring> paths, bool split, ImportMode mode, AudioTrack* track, jack_nframes_t& pos, bool prompt)
96 {
97         /* SFDB sets "multichan" to true to indicate "split channels"
98            so reverse the setting to match the way libardour
99            interprets it.
100         */
101         
102         import_status.multichan = !split;
103
104         if (interthread_progress_window == 0) {
105                 build_interthread_progress_window ();
106         }
107         
108         /* for each path that was selected, import it and then potentially create a new track
109            containing the new region as the sole contents.
110         */
111
112         for (vector<Glib::ustring>::iterator i = paths.begin(); i != paths.end(); ++i ) {
113                 import_sndfile (*i, mode, track, pos);
114         }
115
116         interthread_progress_window->hide_all ();
117 }
118
119 void
120 Editor::do_embed (vector<Glib::ustring> paths, bool split, ImportMode mode, AudioTrack* track, jack_nframes_t& pos, bool prompt)
121 {
122         bool multiple_files = paths.size() > 1;
123         bool check_sample_rate = true;
124         vector<Glib::ustring>::iterator i;
125         
126         for (i = paths.begin(); i != paths.end(); ++i) {
127                 int ret = embed_sndfile (*i, split, multiple_files, check_sample_rate, mode, track, pos, prompt);
128
129                 if (ret < -1) {
130                         break;
131                 }
132         }
133
134         if (i == paths.end()) {
135                 session->save_state ("");
136         }
137 }
138
139 int
140 Editor::import_sndfile (Glib::ustring path, ImportMode mode, AudioTrack* track, jack_nframes_t& pos)
141 {
142         interthread_progress_window->set_title (string_compose (_("ardour: importing %1"), path));
143         interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
144         interthread_progress_window->show_all ();
145         interthread_progress_bar.set_fraction (0.0f);
146         interthread_cancel_label.set_text (_("Cancel Import"));
147         current_interthread_info = &import_status;
148
149         import_status.pathname = path;
150         import_status.done = false;
151         import_status.cancel = false;
152         import_status.freeze = false;
153         import_status.done = 0.0;
154         
155         interthread_progress_connection = Glib::signal_timeout().connect 
156                 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
157         
158         track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
159         ARDOUR_UI::instance()->flush_pending ();
160
161         /* start import thread for this path. this will ultimately call Session::import_audiofile()
162            and if successful will add the file as a region to the session region list.
163         */
164         
165         pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
166         pthread_detach (import_status.thread);
167         
168         while (!(import_status.done || import_status.cancel)) {
169                 gtk_main_iteration ();
170         }
171         
172         import_status.done = true;
173         interthread_progress_connection.disconnect ();
174         
175         /* import thread finished - see if we should build a new track */
176         
177         if (!import_status.new_regions.empty()) {
178                 AudioRegion& region (*import_status.new_regions.front());
179                 finish_bringing_in_audio (region, region.n_channels(), region.n_channels(), track, pos, mode);
180         }
181
182         track_canvas.get_window()->set_cursor (*current_canvas_cursor);
183         return 0;
184 }
185
186 int
187 Editor::embed_sndfile (Glib::ustring path, bool split, bool multiple_files, bool& check_sample_rate, ImportMode mode, 
188                        AudioTrack* track, jack_nframes_t& pos, bool prompt)
189 {
190         ExternalSource *source = 0; /* keep g++ quiet */
191         AudioRegion::SourceList sources;
192         AudioRegion* region;
193         string idspec;
194         string linked_path;
195         SoundFileInfo finfo;
196         string region_name;
197         uint32_t input_chan;
198         uint32_t output_chan;
199
200         track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
201         ARDOUR_UI::instance()->flush_pending ();
202
203         /* lets see if we can link it into the session */
204         
205         linked_path = session->sound_dir();
206         linked_path += PBD::basename (path);
207
208         if (link (path.c_str(), linked_path.c_str()) == 0) {
209
210                 /* there are many reasons why link(2) might have failed.
211                    but if it succeeds, we now have a link in the
212                    session sound dir that will protect against
213                    unlinking of the original path. nice.
214                 */
215
216                 path = linked_path;
217         }
218
219         /* note that we temporarily truncated _id at the colon */
220
221         string error_msg;
222
223         if (!ExternalSource::get_soundfile_info (path, finfo, error_msg)) {
224                 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), selection, error_msg ) << endmsg;
225                 return 0;
226         }
227         
228         if (check_sample_rate  && (finfo.samplerate != (int) session->frame_rate())) {
229                 vector<string> choices;
230                 
231                 choices.push_back (_("Embed it anyway"));
232                 
233                 if (multiple_files) {
234                         choices.push_back (_("Don't embed it"));
235                         choices.push_back (_("Embed all without questions"));
236                         choices.push_back (_("Cancel entire import"));
237                 } else {
238                         choices.push_back (_("Cancel"));
239                 }
240                 
241                 Gtkmm2ext::Choice rate_choice (
242                         string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
243                         choices, false);
244                 
245                 switch (rate_choice.run()) {
246                 case 0: /* do it */
247                         break;
248                 case 1: /* don't import this one */
249                         return -1;
250                 case 2: /* do it, and the rest without asking */
251                         check_sample_rate = false;
252                         break;
253                 case 3: /* stop a multi-file import */
254                 default:
255                         return -2;
256                 }
257         }
258         
259         track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
260         ARDOUR_UI::instance()->flush_pending ();
261         
262         /* make the proper number of channels in the region */
263
264         for (int n = 0; n < finfo.channels; ++n)
265         {
266                 idspec = path;
267                 idspec += string_compose(":%1", n);
268                 
269                 try {
270                         source = ExternalSource::create (idspec.c_str());
271                         sources.push_back(source);
272                 } 
273                 
274                  catch (failed_constructor& err) {
275                          error << string_compose(_("could not open %1"), path) << endmsg;
276                          goto out;
277                  }
278                 
279                  ARDOUR_UI::instance()->flush_pending ();
280         }
281         
282         if (sources.empty()) {
283                 goto out;
284         }
285         
286         region_name = PBD::basename_nosuffix (path);
287         region_name += "-0";
288         
289         region = new AudioRegion (sources, 0, sources[0]->length(), region_name, 0,
290                                   Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
291         
292         input_chan = finfo.channels;
293
294         if (session->get_output_auto_connect() & Session::AutoConnectMaster) {
295                 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
296         } else {
297                 output_chan = input_chan;
298         }
299         
300         finish_bringing_in_audio (*region, input_chan, output_chan, track, pos, mode);
301         
302   out:
303         track_canvas.get_window()->set_cursor (*current_canvas_cursor);
304         return 0;
305 }
306
307 int
308  Editor::finish_bringing_in_audio (AudioRegion& region, uint32_t in_chans, uint32_t out_chans, AudioTrack* track, jack_nframes_t& pos, ImportMode mode)
309  {
310          switch (mode) {
311          case ImportAsRegion:
312                  /* relax, its been done */
313                  break;
314                 
315         case ImportToTrack:
316                 if (track) {
317                         Playlist* playlist  = track->disk_stream().playlist();
318                         
319                         AudioRegion* copy = new AudioRegion (region);
320                         begin_reversible_command (_("insert sndfile"));
321                         session->add_undo (playlist->get_memento());
322                         playlist->add_region (*copy, pos);
323                         session->add_redo_no_execute (playlist->get_memento());
324                         commit_reversible_command ();
325
326                         pos += region.length();
327                 }
328                 break;
329                 
330         case ImportAsTrack:
331                 AudioTrack* at = session->new_audio_track (in_chans, out_chans);
332                 AudioRegion* copy = new AudioRegion (region);
333                 at->disk_stream().playlist()->add_region (*copy, pos);
334                 break;
335         }
336
337         return 0;
338 }
339
340 void *
341 Editor::_import_thread (void *arg)
342 {
343         PBD::ThreadCreated (pthread_self(), X_("Import"));
344
345         Editor *ed = (Editor *) arg;
346         return ed->import_thread ();
347 }
348
349 void *
350 Editor::import_thread ()
351 {
352         session->import_audiofile (import_status);
353         pthread_exit_pbd (0);
354         /*NOTREACHED*/
355         return 0;
356 }
357
358 gint
359 Editor::import_progress_timeout (void *arg)
360 {
361         interthread_progress_label.set_text (import_status.doing_what);
362
363         if (import_status.freeze) {
364                 interthread_cancel_button.set_sensitive(false);
365         } else {
366                 interthread_cancel_button.set_sensitive(true);
367         }
368
369         if (import_status.doing_what == "building peak files") {
370                 interthread_progress_bar.pulse ();
371                 return FALSE;
372         } else {
373                 interthread_progress_bar.set_fraction (import_status.progress);
374         }
375
376         return !(import_status.done || import_status.cancel);
377 }
378