Make sure we don't deactivate un-activated plugins or re-activate already activated...
[ardour.git] / gtk2_ardour / editor_audio_import.cc
index 71c64bf0336e124da534c50bec8042ed7a0df5fb..0b674ad3ab3a95df19f3dd7acf34c9a063b44227 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 #include <errno.h>
 #include <unistd.h>
 
@@ -72,6 +73,7 @@ void
 Editor::external_audio_dialog ()
 {
        vector<Glib::ustring> paths;
+       uint32_t track_cnt;
 
        if (session == 0) {
                MessageDialog msg (0, _("You can't import or embed an audiofile until you have a session loaded."));
@@ -79,21 +81,41 @@ Editor::external_audio_dialog ()
                return;
        }
        
+       track_cnt = 0;
+
+       for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
+               AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
+               
+               if (!atv) {
+                       continue;
+               } else if (atv->is_audio_track()) {
+                       track_cnt++;
+               }
+       }
+
        if (sfbrowser == 0) {
-               sfbrowser = new SoundFileBrowser (*this, _("Add existing audio"), session, selection->tracks.size());
+               sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, track_cnt);
+       } else {
+               sfbrowser->reset (track_cnt);
        }
 
        sfbrowser->show_all ();
 
+  again:
        int response = sfbrowser->run ();
 
-       sfbrowser->hide ();
-
        switch (response) {
+       case RESPONSE_APPLY:
+               // leave the dialog open
+               break;
+
        case RESPONSE_OK:
+               sfbrowser->hide ();
                break;
+
        default:
                // cancel from the browser - we are done
+               sfbrowser->hide ();
                return;
        }
 
@@ -121,19 +143,57 @@ Editor::external_audio_dialog ()
                break;
        }
 
-       if (sfbrowser->import.get_active()) {
-               do_import (paths, chns, mode, where);
+       SrcQuality quality = sfbrowser->get_src_quality();
+
+       if (sfbrowser->copy_files_btn.get_active()) {
+               do_import (paths, chns, mode, quality, where);
        } else {
                do_embed (paths, chns, mode, where);
        }
+
+       if (response == RESPONSE_APPLY) {
+               sfbrowser->clear_selection ();
+               goto again;
+       }
 }
 
+boost::shared_ptr<AudioTrack>
+Editor::get_nth_selected_audio_track (int nth) const
+{
+       AudioTimeAxisView* atv;
+       TrackSelection::iterator x;
+       
+       for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
+
+               atv = dynamic_cast<AudioTimeAxisView*>(*x);
+               
+               if (!atv) {
+                       continue;
+               } else if (atv->is_audio_track()) {
+                       --nth;
+               }
+       }
+       
+       if (x == selection->tracks.end()) {
+               atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
+       } else {
+               atv = dynamic_cast<AudioTimeAxisView*>(*x);
+       }
+       
+       if (!atv || !atv->is_audio_track()) {
+               return boost::shared_ptr<AudioTrack>();
+       }
+       
+       return atv->audio_track();
+}      
+
 void
-Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
+Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, nframes64_t& pos)
 {
        boost::shared_ptr<AudioTrack> track;
        vector<ustring> to_import;
        bool ok = false;
+       int nth = 0;
 
        if (interthread_progress_window == 0) {
                build_interthread_progress_window ();
@@ -146,11 +206,11 @@ Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mod
                        to_import.clear ();
                        to_import.push_back (*a);
 
-                       /* create 1 region from this path, add to 1 track,
-                          ignore "track"
-                       */
+                       if (mode == Editing::ImportToTrack) {
+                               track = get_nth_selected_audio_track (nth++);
+                       }
                        
-                       if (import_sndfiles (to_import, mode, pos, 1, -1, track)) {
+                       if (import_sndfiles (to_import, mode, quality, pos, 1, -1, track)) {
                                goto out;
                        }
 
@@ -163,12 +223,7 @@ Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mod
                        to_import.clear ();
                        to_import.push_back (*a);
 
-                       /* create as many regions as there are channels, add them
-                          to that many tracks, ignore "track"
-                       */
-                       
-
-                       if (import_sndfiles (to_import, mode, pos, -1, -1, track)) {
+                       if (import_sndfiles (to_import, mode, quality, pos, -1, -1, track)) {
                                goto out;
                        }
 
@@ -179,7 +234,7 @@ Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mod
                /* create 1 region from all paths, add to 1 track,
                   ignore "track"
                */
-               if (import_sndfiles (paths, mode, pos, 1, 1, track)) {
+               if (import_sndfiles (paths, mode, quality, pos, 1, 1, track)) {
                        goto out;
                }
                break;
@@ -194,7 +249,7 @@ Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mod
                           reuse "track" across paths
                        */
 
-                       if (import_sndfiles (to_import, mode, pos, 1, 1, track)) {
+                       if (import_sndfiles (to_import, mode, quality, pos, 1, 1, track)) {
                                goto out;
                        }
 
@@ -237,6 +292,7 @@ Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mod
        bool ok = false;
        vector<ustring> to_embed;
        bool multi = paths.size() > 1;
+       int nth = 0;
 
        switch (chns) {
        case Editing::ImportDistinctFiles:
@@ -245,6 +301,10 @@ Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mod
                        to_embed.clear ();
                        to_embed.push_back (*a);
 
+                       if (mode == Editing::ImportToTrack) {
+                               track = get_nth_selected_audio_track (nth++);
+                       }
+
                        if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
                                goto out;
                        }
@@ -267,7 +327,7 @@ Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mod
                if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
                        goto out;
                }
-               break;
+       break;
 
        case Editing::ImportSerializeFiles:
                for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
@@ -291,7 +351,7 @@ Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mod
 }
 
 int
-Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, nframes64_t& pos, 
+Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, SrcQuality quality, nframes64_t& pos, 
                         int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track)
 {
        WindowTitle title = string_compose (_("importing %1"), paths.front());
@@ -308,7 +368,8 @@ Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, nframes64_t& po
        import_status.cancel = false;
        import_status.freeze = false;
        import_status.done = 0.0;
-       
+       import_status.quality = quality;
+
        interthread_progress_connection = Glib::signal_timeout().connect 
                (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
        
@@ -339,7 +400,7 @@ Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, nframes64_t& po
                goto out;
        }
 
-       if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track) == 0) {
+       if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track, false) == 0) {
                session->save_state ("");
        }
 
@@ -474,7 +535,8 @@ Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
                                                                                               (*session, path,  n,
                                                                                                (mode == ImportAsTapeTrack ? 
                                                                                                 AudioFileSource::Destructive : 
-                                                                                                AudioFileSource::Flag (0))));
+                                                                                                AudioFileSource::Flag (0)),
+                                                                                               true, true));
                                } else {
                                        source = boost::dynamic_pointer_cast<AudioFileSource> (s);
                                }
@@ -495,7 +557,7 @@ Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
                goto out;
        }
 
-       ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track);
+       ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
 
   out:
        track_canvas.get_window()->set_cursor (*current_canvas_cursor);
@@ -504,7 +566,7 @@ Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
 
 int
 Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64_t& pos, ImportMode mode, 
-                    int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track)
+                    int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track, bool add_channel_suffix)
 {
        vector<boost::shared_ptr<AudioRegion> > regions;
        ustring region_name;
@@ -524,7 +586,7 @@ Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64
 
                /* take all the sources we have and package them up as a region */
 
-               region_name = region_name_from_path (paths.front(), (sources.size() > 1));
+               region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
                
                regions.push_back (boost::dynamic_pointer_cast<AudioRegion> 
                                   (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
@@ -536,16 +598,17 @@ Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64
 
                SourceList just_one;
                SourceList::iterator x;
+               uint32_t n;
 
-               for (x = sources.begin(); x != sources.end(); ++x) {
+               for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
 
                        just_one.clear ();
                        just_one.push_back (*x);
-
+                       
                        boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
 
-                       region_name = region_name_from_path (afs->path(), false);
-                       
+                       region_name = region_name_from_path (afs->path(), false, true, sources.size(), n);
+
                        regions.push_back (boost::dynamic_pointer_cast<AudioRegion> 
                                           (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
                                                                   Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
@@ -553,7 +616,15 @@ Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64
                }
        }
 
-       input_chan = sources.size();
+       if (target_regions == 1) {
+               input_chan = regions.front()->n_channels();
+       } else {
+               if (target_tracks == 1) {
+                       input_chan = regions.size();
+               } else {
+                       input_chan = 1;
+               }
+       }
 
        if (Config->get_output_auto_connect() & AutoConnectMaster) {
                output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
@@ -565,7 +636,7 @@ Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64
 
        for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
 
-               finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track, n);
+               finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
 
                if (target_tracks != 1) {
                        track.reset ();
@@ -574,15 +645,19 @@ Editor::add_sources (vector<Glib::ustring> paths, SourceList& sources, nframes64
                } 
        }
 
+       /* setup peak file building in another thread */
+
+       for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
+               SourceFactory::setup_peakfile (*x, true);
+       }
+
        return 0;
 }
        
 int
 Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_t in_chans, uint32_t out_chans, nframes64_t& pos, 
-                                 ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track, int nth)
+                                 ImportMode mode, boost::shared_ptr<AudioTrack>& existing_track)
 {
-       boost::shared_ptr<AudioTrack> track;
-
        switch (mode) {
        case ImportAsRegion:
                /* relax, its been done */
@@ -590,19 +665,16 @@ Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_
                
        case ImportToTrack:
        {
-               if (selection->tracks.empty()) {
-                       return -1;
-               }
-                       
-               AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
-               
-               if (!atv) {
-                       return -1;
+               if (!existing_track) {
+
+                       existing_track = get_nth_selected_audio_track (0);
+
+                       if (!existing_track) {
+                               return -1;
+                       }
                }
-               
-               track = atv->audio_track();
-               
-               boost::shared_ptr<Playlist> playlist = track->diskstream()->playlist();
+
+               boost::shared_ptr<Playlist> playlist = existing_track->diskstream()->playlist();
                boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
                begin_reversible_command (_("insert sndfile"));
                XMLNode &before = playlist->get_state();
@@ -616,12 +688,15 @@ Editor::finish_bringing_in_audio (boost::shared_ptr<AudioRegion> region, uint32_
        { 
                if (!existing_track) {
                        list<boost::shared_ptr<AudioTrack> > at (session->new_audio_track (in_chans, out_chans, Normal, 1));
+
                        if (at.empty()) {
                                return -1;
                        }
+
                        existing_track = at.front();
-                       existing_track->set_name (basename_nosuffix (region->name()), this);
+                       existing_track->set_name (region->name(), this);
                }
+
                boost::shared_ptr<AudioRegion> copy (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
                existing_track->diskstream()->playlist()->add_region (copy, pos);
                break;