Make the wiimote code appear in the source package
[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 */
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <algorithm>
26
27 #include <sndfile.h>
28
29 #include <pbd/pthread_utils.h>
30 #include <pbd/basename.h>
31 #include <pbd/shortpath.h>
32
33 #include <gtkmm2ext/choice.h>
34 #include <gtkmm2ext/window_title.h>
35
36 #include <ardour/session.h>
37 #include <ardour/audioplaylist.h>
38 #include <ardour/audioregion.h>
39 #include <ardour/audio_diskstream.h>
40 #include <ardour/utils.h>
41 #include <ardour/audio_track.h>
42 #include <ardour/audioplaylist.h>
43 #include <ardour/audiofilesource.h>
44 #include <ardour/region_factory.h>
45 #include <ardour/source_factory.h>
46 #include <ardour/session.h>
47 #include <pbd/memento_command.h>
48
49 #include "ardour_ui.h"
50 #include "editor.h"
51 #include "sfdb_ui.h"
52 #include "editing.h"
53 #include "audio_time_axis.h"
54 #include "utils.h"
55 #include "gui_thread.h"
56
57 #include "i18n.h"
58
59 using namespace std;
60 using namespace ARDOUR;
61 using namespace PBD;
62 using namespace sigc;
63 using namespace Gtk;
64 using namespace Gtkmm2ext;
65 using namespace Editing;
66 using Glib::ustring;
67
68 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
69
70 void
71 Editor::add_external_audio_action (ImportMode mode_hint)
72 {
73         if (session == 0) {
74                 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
75                 msg.run ();
76                 return;
77         }
78         
79         if (sfbrowser == 0) {
80                 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, 0, true, mode_hint);
81         } else {
82                 sfbrowser->set_mode (mode_hint);
83         }
84
85         external_audio_dialog ();
86 }
87
88 void
89 Editor::external_audio_dialog ()
90 {
91         vector<Glib::ustring> paths;
92         uint32_t track_cnt;
93
94         if (session == 0) {
95                 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
96                 msg.run ();
97                 return;
98         }
99         
100         track_cnt = 0;
101
102         for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
103                 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
104                 
105                 if (!atv) {
106                         continue;
107                 } else if (atv->is_audio_track()) {
108                         track_cnt++;
109                 }
110         }
111
112         if (sfbrowser == 0) {
113                 sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, track_cnt, true);
114         } else {
115                 sfbrowser->reset (track_cnt);
116         }
117
118         sfbrowser->show_all ();
119
120
121         bool keepRunning;
122
123         do {
124                 keepRunning = false;
125
126                 int response = sfbrowser->run ();
127
128                 switch (response) {
129                         case RESPONSE_APPLY:
130                                 // leave the dialog open
131                                 break;
132
133                         case RESPONSE_OK:
134                                 sfbrowser->hide ();
135                                 break;
136
137                         default:
138                                 // cancel from the browser - we are done
139                                 sfbrowser->hide ();
140                                 return;
141                 }
142
143                 /* lets do it */
144
145                 paths = sfbrowser->get_paths ();
146
147                 ImportPosition pos = sfbrowser->get_position ();
148                 ImportMode mode = sfbrowser->get_mode ();
149                 ImportDisposition chns = sfbrowser->get_channel_disposition ();
150                 nframes64_t where;
151
152                 switch (pos) {
153                         case ImportAtEditPoint:
154                                 where = get_preferred_edit_position ();
155                                 break;
156                         case ImportAtTimestamp:
157                                 where = -1;
158                                 break;
159                         case ImportAtPlayhead:
160                                 where = playhead_cursor->current_frame;
161                                 break;
162                         case ImportAtStart:
163                                 where = session->current_start_frame();
164                                 break;
165                 }
166
167                 SrcQuality quality = sfbrowser->get_src_quality();
168
169
170                 if (sfbrowser->copy_files_btn.get_active()) {
171                         do_import (paths, chns, mode, quality, where);
172                 } else {
173                         do_embed (paths, chns, mode, where);
174                 }
175
176                 if (response == RESPONSE_APPLY) {
177                         sfbrowser->clear_selection ();
178                         keepRunning = true;
179                 }
180
181         } while (keepRunning);
182 }
183
184 typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
185
186 /**
187  * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
188  *
189  * all_or_nothing:
190  *   true  = show "Update", "Import" and "Skip"
191  *   false = show "Import", and "Cancel"
192  *
193  * Returns:
194  *     0  To update an existing source of the same name
195  *     1  To import/embed the file normally (make sure the new name will be unique)
196  *     2  If the user wants to skip this file
197  **/
198 int
199 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
200 {
201         string wave_name (Glib::path_get_basename(path));
202
203         AudioSourceList all_sources = session->get_audio_sources();
204         bool wave_name_exists = false;
205
206         for (AudioSourceList::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
207
208                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(i->second);
209
210                 string tmp (Glib::path_get_basename (afs->path()));
211
212                 if (tmp == wave_name) {
213                         wave_name_exists = true;
214                         break;
215                 }
216         }
217
218         int function = 1;
219
220         if (wave_name_exists) {
221                 string message;
222                 if (all_or_nothing) {
223                         // updating is still disabled
224                         //message = string_compose(_("The session already contains a source file named %1. Do you want to update that file (and thus all regions using the file) or import this file as a new file?"),wave_name);
225                         message = string_compose(_("The session already contains a source file named %1. This file will be imported as a new file, please confirm."),wave_name);
226                 } else {
227                         message = string_compose(_("A source file %1 already exists. This operation will not update that source but import the file %2 as a new source, please confirm."), wave_name, wave_name);
228
229                 }
230                 MessageDialog dialog(message, false,Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
231
232                 if (all_or_nothing) {
233                         // disabled
234                         //dialog.add_button("Update", 0);
235                         dialog.add_button("Import", 1);
236                         dialog.add_button("Skip",   2);
237                 } else {
238                         dialog.add_button("Import", 1);
239                         dialog.add_button("Cancel", 2);
240                 }
241                 
242                 //dialog.add_button("Skip all", 4); // All or rest?
243
244                 dialog.show();
245
246                 function = dialog.run ();
247
248                 dialog.hide();
249         }
250
251         return function;
252 }
253
254 boost::shared_ptr<AudioTrack>
255 Editor::get_nth_selected_audio_track (int nth) const
256 {
257         AudioTimeAxisView* atv;
258         TrackSelection::iterator x;
259         
260         for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
261
262                 atv = dynamic_cast<AudioTimeAxisView*>(*x);
263                 
264                 if (!atv) {
265                         continue;
266                 } else if (atv->is_audio_track()) {
267                         --nth;
268                 }
269         }
270         
271         if (x == selection->tracks.end()) {
272                 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
273         } else {
274                 atv = dynamic_cast<AudioTimeAxisView*>(*x);
275         }
276         
277         if (!atv || !atv->is_audio_track()) {
278                 return boost::shared_ptr<AudioTrack>();
279         }
280         
281         return atv->audio_track();
282 }       
283
284 void
285 Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
286 {
287         boost::shared_ptr<AudioTrack> track;
288         vector<ustring> to_import;
289         int nth = 0;
290
291         if (interthread_progress_window == 0) {
292                 build_interthread_progress_window ();
293         }
294
295         if (chns == Editing::ImportMergeFiles) {
296
297                 /* create 1 region from all paths, add to 1 track,
298                    ignore "track"
299                 */
300
301                 bool cancel = false;
302                 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
303                         int check = check_whether_and_how_to_import(*a, false);
304                         if (check == 2) {
305                                 cancel = true;
306                                 break;
307                         }
308                 }
309
310                 if (!cancel) {
311                         import_sndfiles (paths, mode, quality, pos, 1, 1, track, false, paths.size());
312                 }
313
314         } else {
315
316                 bool replace = false;
317                 bool ok = true;
318                 vector<ustring>::size_type total = paths.size();
319
320                 for (vector<ustring>::iterator a = paths.begin(); a != paths.end() && ok; ++a) {
321
322                         int check = check_whether_and_how_to_import (*a, true);
323
324                         switch (check) {
325                         case 2:
326                                 // user said skip
327                                 continue;
328                         case 0:
329                                 fatal << "Updating existing sources should be disabled!" << endmsg;
330                                 /* NOTREACHED*/
331                                 break;
332                         case 1:
333                                 replace = false;
334                                 break;
335                         default:
336                                 fatal << "Illegal return " << check <<  " from check_whether_and_how_to_import()!" << endmsg;
337                                 /* NOTREACHED*/
338                         }
339
340
341                         switch (chns) {
342                         case Editing::ImportDistinctFiles:
343                                 
344                                 to_import.clear ();
345                                 to_import.push_back (*a);
346                                 
347                                 if (mode == Editing::ImportToTrack) {
348                                         track = get_nth_selected_audio_track (nth++);
349                                 }
350                                 
351                                 ok = (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, replace, total) == 0);
352                                 break;
353                                 
354                         case Editing::ImportDistinctChannels:
355                                 
356                                 to_import.clear ();
357                                 to_import.push_back (*a);
358                                 
359                                 ok = (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, replace, total) == 0);
360                                 break;
361                                 
362                         case Editing::ImportSerializeFiles:
363                                 
364                                 to_import.clear ();
365                                 to_import.push_back (*a);
366
367                                 ok = (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, replace, total) == 0);
368                                 break;
369
370                         case Editing::ImportMergeFiles:
371                                 // Not entered, handled in earlier if() branch
372                                 break;
373                         }
374                 }
375         }
376
377         interthread_progress_window->hide_all ();
378 }
379
380 void
381 Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
382 {
383         boost::shared_ptr<AudioTrack> track;
384         bool check_sample_rate = true;
385         bool ok = false;
386         vector<ustring> to_embed;
387         bool multi = paths.size() > 1;
388         int nth = 0;
389
390         switch (chns) {
391         case Editing::ImportDistinctFiles:
392                 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
393
394                         to_embed.clear ();
395                         to_embed.push_back (*a);
396
397                         if (mode == Editing::ImportToTrack) {
398                                 track = get_nth_selected_audio_track (nth++);
399                         }
400
401                         if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
402                                 goto out;
403                         }
404                 }
405                 break;
406                 
407         case Editing::ImportDistinctChannels:
408                 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
409
410                         to_embed.clear ();
411                         to_embed.push_back (*a);
412
413                         if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
414                                 goto out;
415                         }
416                 }
417                 break;
418
419         case Editing::ImportMergeFiles:
420                 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
421                         goto out;
422                 }
423         break;
424
425         case Editing::ImportSerializeFiles:
426                 for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
427
428                         to_embed.clear ();
429                         to_embed.push_back (*a);
430
431                         if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
432                                 goto out;
433                         }
434                 }
435                 break;
436         }
437
438         ok = true;
439         
440   out:  
441         if (ok) {
442                 session->save_state ("");
443         }
444 }
445
446 int
447 Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos, 
448                          int target_regions, int target_tracks, boost::shared_ptr<AudioTrack> track, bool replace,
449                          uint32_t total)
450 {
451         WindowTitle title = string_compose (_("importing %1"), paths.front());
452
453         interthread_progress_window->set_title (title.get_string());
454         interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
455         interthread_progress_bar.set_fraction (0.0f);
456         interthread_cancel_label.set_text (_("Cancel Import"));
457         current_interthread_info = &import_status;
458
459         import_status.paths = paths;
460         import_status.done = false;
461         import_status.cancel = false;
462         import_status.freeze = false;
463         import_status.done = 0.0;
464         import_status.quality = quality;
465         import_status.replace_existing_source = replace;
466         import_status.total = total;
467
468         import_status.mode = mode;
469         import_status.pos = pos;
470         import_status.target_tracks = target_tracks;
471         import_status.target_regions = target_regions;
472         import_status.track = track;
473         import_status.replace = replace;
474         interthread_progress_connection = Glib::signal_timeout().connect 
475                 (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 500);
476         
477         track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
478         gdk_flush ();
479
480         /* start import thread for this spec. this will ultimately call Session::import_audiofile()
481            which, if successful, will add the files as regions to the region list. its up to us
482            (the GUI) to direct additional steps after that.
483         */
484
485         pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
486         pthread_detach (import_status.thread);
487         
488         while (!import_status.done && !import_status.cancel) {
489                 gtk_main_iteration ();
490         }
491
492         interthread_progress_window->hide ();
493         import_status.done = true;
494         interthread_progress_connection.disconnect ();
495         
496         if (!import_status.cancel && !import_status.sources.empty()) {
497                 if (add_sources (import_status.paths, 
498                                  import_status.sources, 
499                                  import_status.pos, 
500                                  import_status.mode, 
501                                  import_status.target_regions, 
502                                  import_status.target_tracks, 
503                                  import_status.track, false) == 0) {
504                         session->save_state ("");
505                 }
506                 
507                 /* update position from results */
508                 
509                 pos = import_status.pos;
510         }
511
512
513         track_canvas->get_window()->set_cursor (*current_canvas_cursor);
514         return 0;
515 }
516
517 int
518 Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
519                         bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
520                         boost::shared_ptr<AudioTrack> track)
521 {
522         boost::shared_ptr<AudioFileSource> source;
523         SourceList sources;
524         string linked_path;
525         SoundFileInfo finfo;
526         int ret = 0;
527
528         track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
529         gdk_flush ();
530
531         for (vector<Glib::ustring>::iterator p = paths.begin(); p != paths.end(); ++p) {
532
533                 ustring path = *p;
534
535                 /* lets see if we can link it into the session */
536                 
537                 linked_path = session->sound_dir();
538                 linked_path += '/';
539                 linked_path += Glib::path_get_basename (path);
540                 
541                 if (link (path.c_str(), linked_path.c_str()) == 0) {
542
543                         /* there are many reasons why link(2) might have failed.
544                            but if it succeeds, we now have a link in the
545                            session sound dir that will protect against
546                            unlinking of the original path. nice.
547                         */
548                         
549                         path = linked_path;
550
551                 } else {
552
553                         /* one possible reason is that its already linked */
554
555                         if (errno == EEXIST) {
556                                 struct stat sb;
557
558                                 if (stat (linked_path.c_str(), &sb) == 0) {
559                                         if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
560                                                 path = linked_path;
561                                         }
562                                 }
563                         }
564                 }
565                 
566                 /* note that we temporarily truncated _id at the colon */
567                 
568                 string error_msg;
569
570                 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
571                         error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
572                         goto out;
573                 }
574                 
575                 if (check_sample_rate  && (finfo.samplerate != (int) session->frame_rate())) {
576                         vector<string> choices;
577                         
578                         if (multifile) {
579                                 choices.push_back (_("Cancel entire import"));
580                                 choices.push_back (_("Don't embed it"));
581                                 choices.push_back (_("Embed all without questions"));
582                         
583                                 Gtkmm2ext::Choice rate_choice (
584                                         string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), 
585                                                         short_path (path, 40)),
586                                         choices, false);
587                                 
588                                 int resx = rate_choice.run ();
589                                 
590                                 switch (resx) {
591                                 case 0: /* stop a multi-file import */
592                                         ret = -2;
593                                         goto out;
594                                 case 1: /* don't embed this one */
595                                         ret = -1;
596                                         goto out;
597                                 case 2: /* do it, and the rest without asking */
598                                         check_sample_rate = false;
599                                         break;
600                                 case 3: /* do it */
601                                         break;
602                                 default:
603                                         ret = -2;
604                                         goto out;
605                                 }
606                         } else {
607                                 choices.push_back (_("Cancel"));
608                                 choices.push_back (_("Embed it anyway"));
609                         
610                                 Gtkmm2ext::Choice rate_choice (
611                                         string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
612                                         choices, false);
613                                 
614                                 int resx = rate_choice.run ();
615                                 
616                                 switch (resx) {
617                                 case 0: /* don't import */
618                                         ret = -1;
619                                         goto out;
620                                 case 1: /* do it */
621                                         break;
622                                 default:
623                                         ret = -2;
624                                         goto out;
625                                 }
626                         }
627                 }
628                 
629                 track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
630
631                 for (int n = 0; n < finfo.channels; ++n) {
632                         try {
633
634                                 /* check if we have this thing embedded already */
635
636                                 boost::shared_ptr<Source> s;
637
638                                 if ((s = session->source_by_path_and_channel (path, n)) == 0) {
639
640                                         source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable 
641                                                                                                (*session, path,  n,
642                                                                                                 (mode == ImportAsTapeTrack ? 
643                                                                                                  AudioFileSource::Destructive : 
644                                                                                                  AudioFileSource::Flag (0)),
645                                                                                                 true, true));
646                                 } else {
647                                         source = boost::dynamic_pointer_cast<AudioFileSource> (s);
648                                 }
649
650                                 sources.push_back(source);
651                         } 
652                         
653                         catch (failed_constructor& err) {
654                                 error << string_compose(_("could not open %1"), path) << endmsg;
655                                 goto out;
656                         }
657                         
658                         ARDOUR_UI::instance()->flush_pending ();
659                 }
660         }
661
662         if (sources.empty()) {
663                 goto out;
664         }
665
666         ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
667
668   out:
669         track_canvas->get_window()->set_cursor (*current_canvas_cursor);
670         return ret;
671 }
672
673 int
674 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode, 
675                      int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool add_channel_suffix)
676 {
677         vector<boost::shared_ptr<AudioRegion> > regions;
678         ustring region_name;
679         uint32_t input_chan = 0;
680         uint32_t output_chan = 0;
681
682         if (pos == -1) { // "use timestamp"
683                 if (sources[0]->natural_position() != 0) {
684                         pos = sources[0]->natural_position();
685                 } else {
686                         pos = get_preferred_edit_position ();
687                 }
688         }
689
690         if (target_regions == 1) {
691
692                 /* take all the sources we have and package them up as a region */
693
694                 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
695                 
696                 regions.push_back (boost::dynamic_pointer_cast<AudioRegion> 
697                                    (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
698                                                            Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
699                 
700         } else if (target_regions == -1 || target_regions > 1) {
701
702                 /* take each source and create a region for each one */
703
704                 SourceList just_one;
705                 SourceList::iterator x;
706                 uint32_t n;
707
708                 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
709
710                         just_one.clear ();
711                         just_one.push_back (*x);
712                         
713                         boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
714
715                         region_name = region_name_from_path (afs->path(), false, true, sources.size(), n);
716
717                         regions.push_back (boost::dynamic_pointer_cast<AudioRegion> 
718                                            (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
719                                                                    Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
720
721                 }
722         }
723
724         if (target_regions == 1) {
725                 input_chan = regions.front()->n_channels();
726         } else {
727                 if (target_tracks == 1) {
728                         input_chan = regions.size();
729                 } else {
730                         input_chan = 1;
731                 }
732         }
733
734         if (Config->get_output_auto_connect() & AutoConnectMaster) {
735                 output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
736         } else {
737                 output_chan = input_chan;
738         }
739
740         int n = 0;
741
742         for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
743
744                 finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
745
746                 if (target_tracks != 1) {
747                         track.reset ();
748                 } else {
749                         pos += (*r)->length();
750                 } 
751         }
752
753         /* setup peak file building in another thread */
754
755         for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
756                 SourceFactory::setup_peakfile (*x, true);
757         }
758
759         return 0;
760 }
761         
762 int
763 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos, 
764                                   ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track)
765 {
766         switch (mode) {
767         case ImportAsRegion:
768                 /* relax, its been done */
769                 break;
770                 
771         case ImportToTrack:
772         {
773                 if (!existing_track) {
774
775                         existing_track = get_nth_selected_audio_track (0);
776
777                         if (!existing_track) {
778                                 return -1;
779                         }
780                 }
781
782                 boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
783                 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
784                 begin_reversible_command (_("insert sndfile"));
785                 XMLNode &before = playlist->get_state();
786                 playlist->add_region (copy, pos);
787                 session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
788                 commit_reversible_command ();
789                 break;
790         }
791
792         case ImportAsTrack:
793         { 
794                 if (!existing_track) {
795                         list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
796
797                         if (at.empty()) {
798                                 return -1;
799                         }
800
801                         existing_track = at.front();
802                         existing_track->set_name (region->name(), this);
803                 }
804
805                 boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
806                 existing_track->diskstream()->playlist()->add_region (copy, pos);
807                 break;
808         }
809
810
811         case ImportAsTapeTrack:
812         {
813                 list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Destructive));
814                 if (!at.empty()) {
815                         boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
816                         at.front()->set_name (basename_nosuffix (copy->name()), this);
817                         at.front()->diskstream()->playlist()->add_region (copy, pos);
818                 }
819                 break;
820         }
821         }
822
823         return 0;
824 }
825
826 void *
827 Editor::_import_thread (void *arg)
828 {
829         PBD::notify_gui_about_thread_creation (pthread_self(), X_("Import"));
830
831         Editor *ed = (Editor *) arg;
832         return ed->import_thread ();
833 }
834
835 void *
836 Editor::import_thread ()
837 {
838         session->import_audiofiles (import_status);
839         pthread_exit_pbd (0);
840         /*NOTREACHED*/
841         return 0;
842 }
843
844 gint
845 Editor::import_progress_timeout (void *arg)
846 {
847         bool reset = false;
848
849         if (!interthread_progress_window->is_visible()) {
850                 interthread_progress_window->show_all ();
851                 reset = true;
852         }
853         
854         interthread_progress_label.set_text (import_status.doing_what);
855
856         if (import_status.freeze) {
857                 interthread_cancel_button.set_sensitive(false);
858         } else {
859                 interthread_cancel_button.set_sensitive(true);
860         }
861
862         if (import_status.doing_what == "building peak files") {
863                 interthread_progress_bar.pulse ();
864                 return FALSE;
865         } else {
866                 float val = import_status.progress;
867                 interthread_progress_bar.set_fraction (min (max (0.0f, val), 1.0f));
868         }
869
870         if (reset) {
871
872                 /* the window is now visible, speed up the updates */
873                 
874                 interthread_progress_connection.disconnect ();
875                 interthread_progress_connection = Glib::signal_timeout().connect 
876                         (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
877                 return false;
878         } else {
879                 return !(import_status.done || import_status.cancel);
880         }
881 }
882