split SoundFileBrowser back into SoundFileOmega; make SoundFileChooser work again...
[ardour.git] / gtk2_ardour / editor_audio_import.cc
index d2bbfa111339e0403dec7205e4f75efade3f9ae9..72fbc4fdcbbf7741e5321d3202087d9c8df1fe72 100644 (file)
@@ -78,30 +78,34 @@ Editor::external_audio_dialog ()
                msg.run ();
                return;
        }
+       
+       if (sfbrowser == 0) {
+               sfbrowser = new SoundFileOmega (*this, _("Add existing audio"), session, selection->tracks.size());
+       } else {
+               sfbrowser->reset (selection->tracks.size());
+       }
 
-       SoundFileBrowser browser (*this, _("Add existing audio"), session, selection->tracks.size());
+       sfbrowser->show_all ();
 
-       browser.show_all ();
+       int response = sfbrowser->run ();
 
-       int response = browser.run ();
+       sfbrowser->hide ();
 
-               switch (response) {
+       switch (response) {
        case RESPONSE_OK:
                break;
        default:
                // cancel from the browser - we are done
                return;
        }
-       
-       browser.hide ();
 
        /* lets do it */
        
-       paths = browser.get_paths ();
+       paths = sfbrowser->get_paths ();
 
-       ImportPosition pos = browser.get_position ();
-       ImportMode mode = browser.get_mode ();
-       ImportChannel chns = browser.get_channel_disposition ();
+       ImportPosition pos = sfbrowser->get_position ();
+       ImportMode mode = sfbrowser->get_mode ();
+       ImportDisposition chns = sfbrowser->get_channel_disposition ();
        nframes64_t where;
 
        switch (pos) {
@@ -119,53 +123,127 @@ Editor::external_audio_dialog ()
                break;
        }
 
-       if (browser.import.get_active()) {
+       if (sfbrowser->import.get_active()) {
                do_import (paths, chns, mode, where);
        } else {
                do_embed (paths, chns, mode, where);
        }
 }
 
+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) {
+               if (dynamic_cast<AudioTimeAxisView*>(*x)) {
+                       --nth;
+               }
+       }
+       
+       if (x == selection->tracks.end()) {
+               atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
+       } else {
+               atv = dynamic_cast<AudioTimeAxisView*>(*x);
+       }
+       
+       if (!atv) {
+               return boost::shared_ptr<AudioTrack>();
+       }
+       
+       return atv->audio_track();
+}      
+
 void
-Editor::do_import (vector<ustring> paths, ImportChannel chns, ImportMode mode, nframes64_t& pos)
+Editor::do_import (vector<ustring> paths, ImportDisposition chns, ImportMode mode, 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 ();
+       }
+
        switch (chns) {
-       case Editing::ImportDistinctChannels:
-               import_status.multichan = false;
+       case Editing::ImportDistinctFiles:
+               for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
+
+                       to_import.clear ();
+                       to_import.push_back (*a);
+
+                       if (mode == Editing::ImportToTrack) {
+                               track = get_nth_selected_audio_track (nth++);
+                       }
+                       
+                       if (import_sndfiles (to_import, mode, pos, 1, -1, track)) {
+                               goto out;
+                       }
+
+               }
                break;
 
-       default:
-               import_status.multichan = true;
+       case Editing::ImportDistinctChannels:
+               for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
+
+                       to_import.clear ();
+                       to_import.push_back (*a);
+
+                       if (import_sndfiles (to_import, mode, pos, -1, -1, track)) {
+                               goto out;
+                       }
+
+               }
                break;
 
-       }
+       case Editing::ImportMergeFiles:
+               /* create 1 region from all paths, add to 1 track,
+                  ignore "track"
+               */
+               if (import_sndfiles (paths, mode, pos, 1, 1, track)) {
+                       goto out;
+               }
+               break;
 
-       if (interthread_progress_window == 0) {
-               build_interthread_progress_window ();
-       }
+       case Editing::ImportSerializeFiles:
+               for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
 
-       vector<ustring> to_import;
+                       to_import.clear ();
+                       to_import.push_back (*a);
+                       
+                       /* create 1 region from this path, add to 1 track,
+                          reuse "track" across paths
+                       */
 
-       for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
+                       if (import_sndfiles (to_import, mode, pos, 1, 1, track)) {
+                               goto out;
+                       }
 
-               to_import.clear ();
-               to_import.push_back (*a);
+               }
+               break;
+       }
 
-               import_sndfile (to_import, mode, pos);
+       ok = true;
+       
+  out: 
+       if (ok) {
+               session->save_state ("");
        }
 
        interthread_progress_window->hide_all ();
 }
 
 bool
-Editor::idle_do_embed (vector<ustring> paths, ImportChannel chns, ImportMode mode, nframes64_t& pos)
+Editor::idle_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
 {
        _do_embed (paths, chns, mode, pos);
        return false;
 }
 
 void
-Editor::do_embed (vector<ustring> paths, ImportChannel chns, ImportMode mode, nframes64_t& pos)
+Editor::do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
 {
 #ifdef GTKOSX
        Glib::signal_idle().connect (bind (mem_fun (*this, &Editor::idle_do_embed), paths, chns, mode, pos));
@@ -175,33 +253,60 @@ Editor::do_embed (vector<ustring> paths, ImportChannel chns, ImportMode mode, nf
 }
 
 void
-Editor::_do_embed (vector<ustring> paths, ImportChannel chns, ImportMode mode, nframes64_t& pos)
+Editor::_do_embed (vector<ustring> paths, ImportDisposition chns, ImportMode mode, nframes64_t& pos)
 {
-       bool multiple_files = paths.size() > 1;
+       boost::shared_ptr<AudioTrack> track;
        bool check_sample_rate = true;
        bool ok = false;
        vector<ustring> to_embed;
-       
+       bool multi = paths.size() > 1;
+       int nth = 0;
 
        switch (chns) {
        case Editing::ImportDistinctFiles:
+               for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
+
+                       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;
+                       }
+               }
+               break;
+               
        case Editing::ImportDistinctChannels:
                for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
 
                        to_embed.clear ();
                        to_embed.push_back (*a);
 
-                       if (embed_sndfile (to_embed, chns, multiple_files, check_sample_rate, mode, pos) < -1) {
+                       if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
                                goto out;
                        }
                }
                break;
 
-       case Editing::ImportSerializeFiles:
        case Editing::ImportMergeFiles:
-               if (embed_sndfile (paths, chns, multiple_files, check_sample_rate, mode, pos) < -1) {
+               if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
                        goto out;
                }
+       break;
+
+       case Editing::ImportSerializeFiles:
+               for (vector<ustring>::iterator a = paths.begin(); a != paths.end(); ++a) {
+
+                       to_embed.clear ();
+                       to_embed.push_back (*a);
+
+                       if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
+                               goto out;
+                       }
+               }
                break;
        }
 
@@ -214,7 +319,8 @@ Editor::_do_embed (vector<ustring> paths, ImportChannel chns, ImportMode mode, n
 }
 
 int
-Editor::import_sndfile (vector<ustring> paths, ImportMode mode, nframes64_t& pos)
+Editor::import_sndfiles (vector<ustring> paths, ImportMode mode, nframes64_t& pos, 
+                        int target_regions, int target_tracks, boost::shared_ptr<AudioTrack>& track)
 {
        WindowTitle title = string_compose (_("importing %1"), paths.front());
 
@@ -255,29 +361,30 @@ Editor::import_sndfile (vector<ustring> paths, ImportMode mode, nframes64_t& pos
        
        /* import thread finished - see if we should build a new track */
 
-       boost::shared_ptr<AudioTrack> track;
+       boost::shared_ptr<AudioRegion> r;
        
-       if (!import_status.new_regions.empty()) {
-               boost::shared_ptr<AudioRegion> region (import_status.new_regions.front());
-               finish_bringing_in_audio (region, region->n_channels(), region->n_channels(), pos, mode, track, 0);
+       if (import_status.cancel || import_status.sources.empty()) {
+               goto out;
        }
 
+       if (add_sources (paths, import_status.sources, pos, mode, target_regions, target_tracks, track) == 0) {
+               session->save_state ("");
+       }
+
+  out:
        track_canvas.get_window()->set_cursor (*current_canvas_cursor);
        return 0;
 }
 
 int
-Editor::embed_sndfile (vector<Glib::ustring> paths, Editing::ImportChannel chns, bool multiple_files, bool& check_sample_rate, ImportMode mode, nframes64_t& pos)
+Editor::embed_sndfiles (vector<Glib::ustring> paths, bool multifile,
+                       bool& check_sample_rate, ImportMode mode, nframes64_t& pos, int target_regions, int target_tracks,
+                       boost::shared_ptr<AudioTrack>& track)
 {
-       boost::shared_ptr<AudioTrack> track;
        boost::shared_ptr<AudioFileSource> source;
        SourceList sources;
-       boost::shared_ptr<AudioRegion> region;
        string linked_path;
        SoundFileInfo finfo;
-       ustring region_name;
-       uint32_t input_chan = 0;
-       uint32_t output_chan = 0;
        int ret = 0;
 
        track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
@@ -330,7 +437,7 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, Editing::ImportChannel chns,
                if (check_sample_rate  && (finfo.samplerate != (int) session->frame_rate())) {
                        vector<string> choices;
                        
-                       if (multiple_files) {
+                       if (multifile) {
                                choices.push_back (_("Cancel entire import"));
                                choices.push_back (_("Don't embed it"));
                                choices.push_back (_("Embed all without questions"));
@@ -382,14 +489,8 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, Editing::ImportChannel chns,
                }
                
                track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
-               // ARDOUR_UI::instance()->flush_pending ();
-       
-               /* make the proper number of channels in the region */
-               
-               input_chan += finfo.channels;
 
-               for (int n = 0; n < finfo.channels; ++n)
-               {
+               for (int n = 0; n < finfo.channels; ++n) {
                        try {
 
                                /* check if we have this thing embedded already */
@@ -422,6 +523,21 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, Editing::ImportChannel chns,
                goto out;
        }
 
+       ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track);
+
+  out:
+       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
+       return ret;
+}
+
+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)
+{
+       vector<boost::shared_ptr<AudioRegion> > regions;
+       ustring region_name;
+       uint32_t input_chan = 0;
+       uint32_t output_chan = 0;
 
        if (pos == -1) { // "use timestamp"
                if (sources[0]->natural_position() != 0) {
@@ -432,82 +548,73 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, Editing::ImportChannel chns,
                }
        }
 
-       if (chns == Editing::ImportMergeFiles) {
+       if (target_regions == 1) {
 
                /* 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 = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
-                                                                                         Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External)));
-
-               if (Config->get_output_auto_connect() & AutoConnectMaster) {
-                       output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
-               } else {
-                       output_chan = input_chan;
-               }
-               
-               finish_bringing_in_audio (region, input_chan, output_chan, pos, mode, track, 0);
+               regions.push_back (boost::dynamic_pointer_cast<AudioRegion> 
+                                  (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
+                                                          Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External))));
                
-       } else { // SerializeFiles, DistinctFiles, DistinctChannels
+       } else if (target_regions == -1) {
 
                /* take each source and create a region for each one */
 
                SourceList just_one;
                SourceList::iterator x;
-               vector<Glib::ustring>::iterator p = paths.begin();
-               vector<Glib::ustring>::iterator next_path;
-               int nth;
 
-               for (nth = 0, x = sources.begin(); x != sources.end(); ++x, ++nth) {
+               for (x = sources.begin(); x != sources.end(); ++x) {
 
                        just_one.clear ();
                        just_one.push_back (*x);
 
-                       region_name = region_name_from_path ((*p), false);
+                       boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*x);
+
+                       region_name = region_name_from_path (afs->path(), false);
                        
-                       region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (just_one, 0, (*x)->length(), region_name, 0,
-                                                                                                 Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External)));
+                       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))));
 
-                       if (Config->get_output_auto_connect() & AutoConnectMaster) {
-                               output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
-                       } else {
-                               output_chan = input_chan;
-                       }
-               
-                       finish_bringing_in_audio (region, 1, output_chan, pos, mode, track, nth);
+               }
+       }
 
-                       if (chns == ImportSerializeFiles) {
-                               pos += region->length();
-                       }
+       if (target_regions == 1) {
+               input_chan = regions.front()->n_channels();
+       } else {
+               if (target_tracks == 1) {
+                       input_chan = regions.size();
+               } else {
+                       input_chan = 1;
+               }
+       }
 
-                       if (chns == ImportDistinctChannels || chns == ImportDistinctFiles) {
-                               /* make a new track for the next region */
-                               track.reset ();
-                       }
+       if (Config->get_output_auto_connect() & AutoConnectMaster) {
+               output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
+       } else {
+               output_chan = input_chan;
+       }
 
-                       /* don't run out of paths */
-                       
-                       next_path = p;
-                       next_path++;
+       for (vector<boost::shared_ptr<AudioRegion> >::iterator r = regions.begin(); r != regions.end(); ++r) {
 
-                       if (next_path != paths.end()) {
-                               p = next_path;
-                       }
-               }
+               finish_bringing_in_audio (*r, input_chan, output_chan, pos, mode, track);
+
+               if (target_tracks != 1) {
+                       track.reset ();
+               } else {
+                       pos += (*r)->length();
+               } 
        }
 
-       
-  out:
-       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
-       return ret;
+       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:
@@ -516,19 +623,22 @@ 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 (!existing_track) {
 
-               if (!atv) {
-                       return -1;
+                       if (selection->tracks.empty()) {
+                               return -1;
+                       }
+                       
+                       AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front());
+                       
+                       if (!atv) {
+                               return -1;
+                       }
+                       
+                       existing_track = atv->audio_track();
                }
 
-               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();