Factor out grid beat divisions.
[ardour.git] / gtk2_ardour / sfdb_ui.cc
index 899748747d690b82334a8c5809e494fd742d7a2b..d6ba793c2f943ca5a0e5916e6c4d89232d8ab6f7 100644 (file)
@@ -21,6 +21,8 @@
 #include "gtk2ardour-config.h"
 #endif
 
+#include "i18n.h"
+
 #include <map>
 #include <cerrno>
 #include <sstream>
@@ -49,6 +51,7 @@
 #include "ardour/auditioner.h"
 #include "ardour/audioregion.h"
 #include "ardour/audiofilesource.h"
+#include "ardour/midi_region.h"
 #include "ardour/smf_source.h"
 #include "ardour/region_factory.h"
 #include "ardour/source_factory.h"
 #include "prompter.h"
 #include "sfdb_ui.h"
 #include "editing.h"
-#include "utils.h"
 #include "gain_meter.h"
 #include "main_clock.h"
 #include "public_editor.h"
+#include "timers.h"
 
 #include "sfdb_freesound_mootcher.h"
 
-#include "i18n.h"
-
 using namespace ARDOUR;
 using namespace PBD;
 using namespace std;
@@ -114,7 +115,7 @@ importmode2string (ImportMode mode)
        case ImportAsTapeTrack:
                return _("as new tape tracks");
        }
-       /*NOTREACHED*/
+       abort(); /*NOTREACHED*/
        return _("as new tracks");
 }
 
@@ -281,12 +282,17 @@ SoundFileBox::setup_labels (const string& filename)
 
        string error_msg;
 
-       if (SMFSource::safe_midi_file_extension (path)) {
+       if (SMFSource::valid_midi_file (path)) {
 
-               boost::shared_ptr<SMFSource> ms =
-                       boost::dynamic_pointer_cast<SMFSource> (
-                                       SourceFactory::createExternal (DataType::MIDI, *_session,
-                                                                                        path, 0, Source::Flag (0), false));
+               boost::shared_ptr<SMFSource> ms;
+               try {
+                       ms = boost::dynamic_pointer_cast<SMFSource> (
+                               SourceFactory::createExternal (DataType::MIDI, *_session,
+                                                              path, 0, Source::Flag (0), false));
+               } catch (const std::exception& e) {
+                       error << string_compose(_("Could not read file: %1 (%2)."),
+                                               path, e.what()) << endmsg;
+               }
 
                preview_label.set_markup (_("<b>Midi File Information</b>"));
 
@@ -406,7 +412,7 @@ SoundFileBox::audition ()
 
        boost::shared_ptr<Region> r;
 
-       if (SMFSource::safe_midi_file_extension (path)) {
+       if (SMFSource::valid_midi_file (path)) {
 
                boost::shared_ptr<SMFSource> ms =
                        boost::dynamic_pointer_cast<SMFSource> (
@@ -474,6 +480,23 @@ SoundFileBox::audition ()
                r = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, plist, false));
        }
 
+       frameoffset_t audition_position = 0;
+       switch(_import_position) {
+               case ImportAtTimestamp:
+                       audition_position = 0;
+                       break;
+               case ImportAtPlayhead:
+                       audition_position = _session->transport_frame();
+                       break;
+               case ImportAtStart:
+                       audition_position = _session->current_start_frame();
+                       break;
+               case ImportAtEditPoint:
+                       audition_position = PublicEditor::instance().get_preferred_edit_position ();
+                       break;
+       }
+       r->set_position(audition_position);
+
        _session->audition_region(r);
 }
 
@@ -834,7 +857,7 @@ SoundFileBrowser::remove_gain_meter ()
 void
 SoundFileBrowser::start_metering ()
 {
-       metering_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (sigc::mem_fun(*this, &SoundFileBrowser::meter));
+       metering_connection = Timers::super_rapid_connect (sigc::mem_fun(*this, &SoundFileBrowser::meter));
 }
 
 void
@@ -1294,7 +1317,7 @@ SoundFileOmega::reset_options ()
                   to do embedding (or if we are importing a MIDI file).
                */
 
-               if (Config->get_only_copy_imported_files()) {
+               if (ARDOUR_UI::config()->get_only_copy_imported_files()) {
                        copy_files_btn.set_sensitive (false);
                } else {
                        copy_files_btn.set_sensitive (false);
@@ -1309,7 +1332,7 @@ SoundFileOmega::reset_options ()
 
        /* See if we are thinking about importing any MIDI files */
        vector<string>::iterator i = paths.begin ();
-       while (i != paths.end() && SMFSource::safe_midi_file_extension (*i) == false) {
+       while (i != paths.end() && SMFSource::valid_midi_file (*i) == false) {
                ++i;
        }
        bool const have_a_midi_file = (i != paths.end ());
@@ -1468,7 +1491,7 @@ SoundFileOmega::reset_options ()
         * or any file if we are under nsm control */
        bool const must_copy = _session->get_nsm_state() || have_a_midi_file || notebook.get_current_page() == 2;
        
-       if (Config->get_only_copy_imported_files()) {
+       if (ARDOUR_UI::config()->get_only_copy_imported_files()) {
 
                if (selection_can_be_embedded_with_links && !must_copy) {
                        copy_files_btn.set_sensitive (true);
@@ -1537,7 +1560,7 @@ SoundFileOmega::check_info (const vector<string>& paths, bool& same_size, bool&
                                src_needed = true;
                        }
 
-               } else if (SMFSource::safe_midi_file_extension (*i)) {
+               } else if (SMFSource::valid_midi_file (*i)) {
 
                        Evoral::SMF reader;
                        reader.open(*i);
@@ -1662,6 +1685,7 @@ SoundFileOmega::SoundFileOmega (string title, ARDOUR::Session* s,
        str.push_back (_("session start"));
        set_popdown_strings (where_combo, str);
        where_combo.set_active_text (str.front());
+       where_combo.signal_changed().connect (sigc::mem_fun (*this, &SoundFileOmega::where_combo_changed));
 
        Label* l = manage (new Label);
        l->set_markup (_("<b>Add files as ...</b>"));
@@ -1728,6 +1752,18 @@ SoundFileOmega::SoundFileOmega (string title, ARDOUR::Session* s,
        hbox->pack_start (*vbox, false, false);
        options.pack_start (*hbox, false, false);
 
+       l = manage (new Label);
+       l->set_markup (_("<b>Instrument</b>"));
+
+       vbox = manage (new VBox);
+       vbox->set_border_width (12);
+       vbox->set_spacing (6);
+       vbox->pack_start (*l, false, false);
+       vbox->pack_start (instrument_combo, false, false);
+       hbox = manage (new HBox);
+       hbox->pack_start (*vbox, false, false);
+       options.pack_start (*hbox, false, false);
+
        str.clear ();
        str.push_back (_("Best"));
        str.push_back (_("Good"));
@@ -1855,6 +1891,12 @@ SoundFileOmega::src_combo_changed()
        preview.set_src_quality(get_src_quality());
 }
 
+void
+SoundFileOmega::where_combo_changed()
+{
+       preview.set_import_position(get_position());
+}
+
 ImportDisposition
 SoundFileOmega::get_channel_disposition () const
 {
@@ -1868,7 +1910,7 @@ SoundFileOmega::get_channel_disposition () const
 
        if (x == disposition_map.end()) {
                fatal << string_compose (_("programming error: %1 (%2)"), "unknown string for import disposition", str) << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        return x->second;
@@ -1925,6 +1967,7 @@ SoundFileOmega::do_something (int action)
        ImportPosition pos = get_position ();
        ImportMode mode = get_mode ();
        ImportDisposition chns = get_channel_disposition ();
+       PluginInfoPtr instrument = instrument_combo.selected_instrument();
        framepos_t where;
        
        switch (pos) {
@@ -1945,9 +1988,9 @@ SoundFileOmega::do_something (int action)
        SrcQuality quality = get_src_quality();
        
        if (copy_files_btn.get_active()) {
-               PublicEditor::instance().do_import (paths, chns, mode, quality, where);
+               PublicEditor::instance().do_import (paths, chns, mode, quality, where, instrument);
        } else {
-               PublicEditor::instance().do_embed (paths, chns, mode, where);
+               PublicEditor::instance().do_embed (paths, chns, mode, where, instrument);
        }
        
        if (action == RESPONSE_OK) {