From 48efe6c2933c00b02df66b4795ecc92baf06f6b6 Mon Sep 17 00:00:00 2001 From: Colin Fletcher Date: Fri, 14 Jun 2013 18:59:57 +0100 Subject: [PATCH 1/5] Add config variable for Freesound download folder location. --- libs/ardour/ardour/rc_configuration_vars.h | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/ardour/ardour/rc_configuration_vars.h b/libs/ardour/ardour/rc_configuration_vars.h index f19f8fe808..d8440f24f3 100644 --- a/libs/ardour/ardour/rc_configuration_vars.h +++ b/libs/ardour/ardour/rc_configuration_vars.h @@ -192,6 +192,7 @@ CONFIG_VARIABLE (bool, sound_midi_notes, "sound-midi-notes", false) CONFIG_VARIABLE (bool, use_plugin_own_gui, "use-plugin-own-gui", true) CONFIG_VARIABLE (uint32_t, max_recent_sessions, "max-recent-sessions", 10) CONFIG_VARIABLE (double, automation_thinning_factor, "automation-thinning-factor", 20.0) +CONFIG_VARIABLE (std::string, freesound_download_dir, "freesound-download-dir", Glib::get_home_dir() + "/Freesound/snd") /* denormal management */ From 1eff5a8215319c680ea511e90eba7fd5e4e66edd Mon Sep 17 00:00:00 2001 From: Colin Fletcher Date: Fri, 14 Jun 2013 19:09:40 +0100 Subject: [PATCH 2/5] Use config variable for Freesound download folder location. Use the new freesound-download-dir config variable to decide the location of sound files downloaded from Freesound. Move the Windows-specific logic to replace '/'s with '\'s from Mootcher::changeWorkingDir() to Mootcher::ensureWorkingDir(), and remove the now unused Mootcher::changeWorkingDir(). Use Glib::build_filename to construct the path. --- gtk2_ardour/sfdb_freesound_mootcher.cc | 40 ++++++++++++-------------- gtk2_ardour/sfdb_freesound_mootcher.h | 1 - 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/gtk2_ardour/sfdb_freesound_mootcher.cc b/gtk2_ardour/sfdb_freesound_mootcher.cc index e46fb12391..e8b44ff4a7 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.cc +++ b/gtk2_ardour/sfdb_freesound_mootcher.cc @@ -53,6 +53,7 @@ #include "i18n.h" #include "ardour/audio_library.h" +#include "ardour/rc_configuration.h" using namespace PBD; @@ -63,9 +64,12 @@ static const std::string api_key = "9d77cb8d841b4bcfa960e1aae62224eb"; // ardour Mootcher::Mootcher() : curl(curl_easy_init()) { - std::string path; - path = Glib::get_home_dir() + "/Freesound/"; - changeWorkingDir ( path.c_str() ); + cancel_download_btn.set_label (_("Cancel")); + progress_hbox.pack_start (progress_bar, true, true); + progress_hbox.pack_end (cancel_download_btn, false, false); + progress_bar.show(); + cancel_download_btn.show(); + cancel_download_btn.signal_clicked().connect(sigc::mem_fun (*this, &Mootcher::cancelDownload)); }; //------------------------------------------------------------------------ Mootcher:: ~Mootcher() @@ -74,9 +78,17 @@ Mootcher:: ~Mootcher() } //------------------------------------------------------------------------ -void Mootcher::changeWorkingDir(const char *saveLocation) + +void Mootcher::ensureWorkingDir () { - basePath = saveLocation; + std::string p = ARDOUR::Config->get_freesound_download_dir(); + + if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) { + if (g_mkdir_with_parents (p.c_str(), 0775) != 0) { + PBD::error << "Unable to create Mootcher working dir" << endmsg; + } + } + basePath = p; #ifdef __WIN32__ std::string replace = "/"; size_t pos = basePath.find("\\"); @@ -85,20 +97,6 @@ void Mootcher::changeWorkingDir(const char *saveLocation) pos = basePath.find("\\"); } #endif - // - size_t pos2 = basePath.find_last_of("/"); - if(basePath.length() != (pos2+1)) basePath += "/"; -} - -void Mootcher::ensureWorkingDir () -{ - std::string p = Glib::build_filename (basePath, "snd"); - - if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) { - if (g_mkdir_with_parents (p.c_str(), 0775) != 0) { - PBD::error << "Unable to create Mootcher working dir" << endmsg; - } - } } @@ -264,7 +262,7 @@ std::string Mootcher::getSoundResourceFile(std::string ID) // get the file name and size from xml file if (name) { - audioFileName = basePath + "snd/" + ID + "-" + name->child("text")->content(); + audioFileName = Glib::build_filename (basePath, ID + "-" + name->child("text")->content()); //store all the tags in the database XMLNode *tags = freesound->child("tags"); @@ -299,7 +297,7 @@ int audioFileWrite(void *buffer, size_t size, size_t nmemb, void *file) std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller) { ensureWorkingDir(); - std::string audioFileName = basePath + "snd/" + ID + "-" + originalFileName; + audioFileName = Glib::build_filename (basePath, ID + "-" + originalFileName); // check to see if audio file already exists FILE *testFile = g_fopen(audioFileName.c_str(), "r"); diff --git a/gtk2_ardour/sfdb_freesound_mootcher.h b/gtk2_ardour/sfdb_freesound_mootcher.h index 7e39ba4ae4..8956e349eb 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.h +++ b/gtk2_ardour/sfdb_freesound_mootcher.h @@ -76,7 +76,6 @@ public: private: - void changeWorkingDir(const char *saveLocation); void ensureWorkingDir(); std::string doRequest(std::string uri, std::string params); From 0483803186c83e27db708f77189a4dc9974f1712 Mon Sep 17 00:00:00 2001 From: Colin Fletcher Date: Fri, 14 Jun 2013 19:17:24 +0100 Subject: [PATCH 3/5] Further Freesound tweaks. Make download of sound files multi-threaded. Each sound file download takes place in its own thread, and has its own progress bar and cancel button, which stack up from the bottom of the list of results. Sound files download into a file with a '.part' suffix, which is then renamed to the intended name on success. Add a 'Similar' button, which searches Freesound for sounds similar to the currently-selected sound in the results list. --- gtk2_ardour/sfdb_freesound_mootcher.cc | 189 ++++++++++++++++------ gtk2_ardour/sfdb_freesound_mootcher.h | 20 ++- gtk2_ardour/sfdb_ui.cc | 213 ++++++++++++++----------- gtk2_ardour/sfdb_ui.h | 14 +- 4 files changed, 283 insertions(+), 153 deletions(-) diff --git a/gtk2_ardour/sfdb_freesound_mootcher.cc b/gtk2_ardour/sfdb_freesound_mootcher.cc index e8b44ff4a7..38d53aaa70 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.cc +++ b/gtk2_ardour/sfdb_freesound_mootcher.cc @@ -54,6 +54,7 @@ #include "ardour/audio_library.h" #include "ardour/rc_configuration.h" +#include "pbd/pthread_utils.h" using namespace PBD; @@ -119,7 +120,8 @@ size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void //------------------------------------------------------------------------ -std::string Mootcher::sortMethodString(enum sortMethod sort) { +std::string Mootcher::sortMethodString(enum sortMethod sort) +{ // given a sort type, returns the string value to be passed to the API to // sort the results in the requested way. @@ -200,6 +202,18 @@ std::string Mootcher::doRequest(std::string uri, std::string params) } +std::string Mootcher::searchSimilar(std::string id) +{ + std::string params = ""; + + params += "&fields=id,original_filename,duration,filesize,samplerate,license,serve"; + params += "&num_results=100"; + + return doRequest("/sounds/" + id + "/similar", params); +} + +//------------------------------------------------------------------------ + std::string Mootcher::searchText(std::string query, int page, std::string filter, enum sortMethod sort) { std::string params = ""; @@ -294,9 +308,62 @@ int audioFileWrite(void *buffer, size_t size, size_t nmemb, void *file) }; //------------------------------------------------------------------------ -std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller) + +void * +Mootcher::threadFunc() { +CURLcode res; + + res = curl_easy_perform (curl); + fclose (theFile); + curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1); // turn off the progress bar + + if (res != CURLE_OK) { + /* it's not an error if the user pressed the stop button */ + if (res != CURLE_ABORTED_BY_CALLBACK) { + error << string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg; + } + remove ( (audioFileName+".part").c_str() ); + } else { + rename ( (audioFileName+".part").c_str(), audioFileName.c_str() ); + // now download the tags &c. + getSoundResourceFile(ID); + } + + return (void *) res; +} + +static int +donewithMootcher(void *arg) +{ + Mootcher *thisMootcher = (Mootcher *) arg; + + // update the sound info pane if the selection in the list box is still us + thisMootcher->sfb->refresh_display(thisMootcher->ID, thisMootcher->audioFileName); + + delete(thisMootcher); + return 0; +} + +static void * +freesound_download_thread_func(void *arg) +{ + Mootcher *thisMootcher = (Mootcher *) arg; + void *res; + + // std::cerr << "freesound_download_thread_func(" << arg << ")" << std::endl; + res = thisMootcher->threadFunc(); + g_idle_add(donewithMootcher, thisMootcher); + + return res; +} + + +//------------------------------------------------------------------------ + +bool Mootcher::checkAudioFile(std::string originalFileName, std::string theID) { ensureWorkingDir(); + ID = theID; audioFileName = Glib::build_filename (basePath, ID + "-" + originalFileName); // check to see if audio file already exists @@ -305,29 +372,31 @@ std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID, fseek (testFile , 0 , SEEK_END); if (ftell (testFile) > 256) { fclose (testFile); - return audioFileName; + return true; } - // else file was small, probably an error, delete it and try again + // else file was small, probably an error, delete it fclose(testFile); remove( audioFileName.c_str() ); } + return false; +} + + +bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, std::string audioURL, SoundFileBrowser *caller) +{ + ensureWorkingDir(); + ID = theID; + audioFileName = Glib::build_filename (basePath, ID + "-" + originalFileName); if (!curl) { - return ""; + return false; } - - // if already cancelling a previous download, bail out here ( this can happen b/c getAudioFile gets called by various UI update funcs ) - if ( caller->freesound_download_cancel ) { - return ""; - } - // now download the actual file - FILE* theFile; - theFile = g_fopen( audioFileName.c_str(), "wb" ); + theFile = g_fopen( (audioFileName + ".part").c_str(), "wb" ); if (!theFile) { - return ""; + return false; } // create the download url @@ -338,57 +407,71 @@ std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID, curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, audioFileWrite); curl_easy_setopt(curl, CURLOPT_WRITEDATA, theFile); - /* hack to get rid of the barber-pole stripes */ - caller->freesound_progress_bar.hide(); - caller->freesound_progress_bar.show(); - std::string prog; prog = string_compose (_("%1"), originalFileName); - caller->freesound_progress_bar.set_text(prog); + progress_bar.set_text(prog); + + Gtk::VBox *freesound_vbox = dynamic_cast (caller->notebook.get_nth_page(2)); + freesound_vbox->pack_start(progress_hbox, Gtk::PACK_SHRINK); + progress_hbox.show(); + cancel_download = false; + sfb = caller; curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); // turn on the progress bar curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback); - curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, caller); + curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this); - CURLcode res = curl_easy_perform(curl); - fclose(theFile); + pthread_t freesound_download_thread; + pthread_create_and_store("freesound_import", &freesound_download_thread, freesound_download_thread_func, this); - curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1); // turn off the progress bar - caller->freesound_progress_bar.set_fraction(0.0); - caller->freesound_progress_bar.set_text(""); - - if( res != 0 ) { - /* it's not an error if the user pressed the stop button */ - if (res != CURLE_ABORTED_BY_CALLBACK) { - error << string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg; - } - remove( audioFileName.c_str() ); - return ""; - } else { - // now download the tags &c. - getSoundResourceFile(ID); - } - - return audioFileName; + return true; } //--------- -int Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double /*ultotal*/, double /*ulnow*/) -{ +struct progressInfo { + Gtk::ProgressBar *bar; + double dltotal; + double dlnow; +}; -SoundFileBrowser *sfb = (SoundFileBrowser *) caller; - //XXX I hope it's OK to do GTK things in this callback. Otherwise - // I'll have to do stuff like in interthread_progress_window. - if (sfb->freesound_download_cancel) { - return -1; - } - - - sfb->freesound_progress_bar.set_fraction(dlnow/dltotal); - /* Make sure the progress widget gets updated */ - while (Glib::MainContext::get_default()->iteration (false)) { - /* do nothing */ +static int +updateProgress(void *arg) +{ + struct progressInfo *progress = (struct progressInfo *) arg; + if (progress->dltotal > 0) { + double fraction = progress->dlnow / progress->dltotal; + // std::cerr << "progress idle: " << progress->bar->get_text() << ". " << progress->dlnow << " / " << progress->dltotal << " = " << fraction << std::endl; + if (fraction > 1.0) { + fraction = 1.0; + } else if (fraction < 0.0) { + fraction = 0.0; + } + progress->bar->set_fraction(fraction); } + + delete progress; + return 0; +} + +int +Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double /*ultotal*/, double /*ulnow*/) +{ + // It may seem curious to pass a pointer to an instance of an object to a static + // member function, but we can't use a normal member function as a curl progress callback, + // and we want access to some private members of Mootcher. + + Mootcher *thisMootcher = (Mootcher *) caller; + + if (thisMootcher->cancel_download) { + return -1; + } + + struct progressInfo *progress = new struct progressInfo; + progress->bar = &thisMootcher->progress_bar; + progress->dltotal = dltotal; + progress->dlnow = dlnow; + + g_idle_add(updateProgress, progress); return 0; } diff --git a/gtk2_ardour/sfdb_freesound_mootcher.h b/gtk2_ardour/sfdb_freesound_mootcher.h index 8956e349eb..ee65020021 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.h +++ b/gtk2_ardour/sfdb_freesound_mootcher.h @@ -71,8 +71,14 @@ public: Mootcher(); ~Mootcher(); - std::string getAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller); + bool checkAudioFile(std::string originalFileName, std::string ID); + bool fetchAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller); std::string searchText(std::string query, int page, std::string filter, enum sortMethod sort); + std::string searchSimilar(std::string id); + void * threadFunc(); + SoundFileBrowser *sfb; + std::string audioFileName; + std::string ID; private: @@ -89,6 +95,18 @@ private: CURL *curl; char errorBuffer[CURL_ERROR_SIZE]; // storage for cUrl error message + FILE* theFile; + + Gtk::HBox progress_hbox; + Gtk::ProgressBar progress_bar; + Gtk::Button cancel_download_btn; + + bool cancel_download; + void cancelDownload() { + cancel_download = true; + progress_hbox.hide(); + } + std::string basePath; std::string xmlLocation; }; diff --git a/gtk2_ardour/sfdb_ui.cc b/gtk2_ardour/sfdb_ui.cc index 85b4f7d7ef..e8db9d4939 100644 --- a/gtk2_ardour/sfdb_ui.cc +++ b/gtk2_ardour/sfdb_ui.cc @@ -80,6 +80,7 @@ using namespace Editing; using std::string; string SoundFileBrowser::persistent_folder; +typedef TreeView::Selection::ListHandle_Path ListPath; static ImportMode string2importmode (string str) @@ -512,8 +513,6 @@ SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s, bool persi found_search_btn.signal_clicked().connect(sigc::mem_fun(*this, &SoundFileBrowser::found_search_clicked)); found_entry.signal_activate().connect(sigc::mem_fun(*this, &SoundFileBrowser::found_search_clicked)); - freesound_stop_btn.signal_clicked().connect(sigc::mem_fun(*this, &SoundFileBrowser::freesound_stop_clicked)); - notebook.append_page (*vbox, _("Search Tags")); #ifdef FREESOUND @@ -555,9 +554,9 @@ SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s, bool persi freesound_more_btn.set_label(_("More")); freesound_more_btn.set_sensitive(false); - passbox->pack_end (freesound_stop_btn, false, false); - freesound_stop_btn.set_label(_("Stop")); - freesound_stop_btn.set_sensitive(false); + passbox->pack_start (freesound_similar_btn, false, false); + freesound_similar_btn.set_label(_("Similar")); + freesound_similar_btn.set_sensitive(false); scroll = manage(new ScrolledWindow); scroll->add(freesound_list_view); @@ -566,9 +565,8 @@ SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s, bool persi vbox = manage(new VBox); vbox->set_spacing (3); vbox->pack_start (*passbox, PACK_SHRINK); - vbox->pack_start (freesound_progress_bar, PACK_SHRINK); vbox->pack_start (*scroll); - + freesound_list_view.append_column(_("ID") , freesound_list_columns.id); freesound_list_view.append_column(_("Filename"), freesound_list_columns.filename); // freesound_list_view.append_column(_("URI") , freesound_list_columns.uri); @@ -577,20 +575,22 @@ SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s, bool persi freesound_list_view.append_column(_("Samplerate"), freesound_list_columns.smplrate); freesound_list_view.append_column(_("License"), freesound_list_columns.license); freesound_list_view.get_column(0)->set_alignment(0.5); - freesound_list_view.get_column(1)->set_expand(true); + freesound_list_view.get_column(1)->set_expand(true); // filename + freesound_list_view.get_column(1)->set_resizable(true); // filename freesound_list_view.get_column(2)->set_alignment(0.5); freesound_list_view.get_column(3)->set_alignment(0.5); freesound_list_view.get_column(4)->set_alignment(0.5); freesound_list_view.get_column(5)->set_alignment(0.5); freesound_list_view.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &SoundFileBrowser::freesound_list_view_selected)); + freesound_list_view.set_tooltip_column(1); freesound_list_view.get_selection()->set_mode (SELECTION_MULTIPLE); freesound_list_view.signal_row_activated().connect (sigc::mem_fun (*this, &SoundFileBrowser::freesound_list_view_activated)); freesound_search_btn.signal_clicked().connect(sigc::mem_fun(*this, &SoundFileBrowser::freesound_search_clicked)); freesound_entry.signal_activate().connect(sigc::mem_fun(*this, &SoundFileBrowser::freesound_search_clicked)); freesound_more_btn.signal_clicked().connect(sigc::mem_fun(*this, &SoundFileBrowser::freesound_more_clicked)); - freesound_stop_btn.signal_clicked().connect(sigc::mem_fun(*this, &SoundFileBrowser::freesound_stop_clicked)); + freesound_similar_btn.signal_clicked().connect(sigc::mem_fun(*this, &SoundFileBrowser::freesound_similar_clicked)); notebook.append_page (*vbox, _("Search Freesound")); #endif @@ -787,7 +787,7 @@ SoundFileBrowser::found_list_view_selected () } else { string file; - TreeView::Selection::ListHandle_Path rows = found_list_view.get_selection()->get_selected_rows (); + ListPath rows = found_list_view.get_selection()->get_selected_rows (); if (!rows.empty()) { TreeIter iter = found_list->get_iter(*rows.begin()); @@ -802,55 +802,6 @@ SoundFileBrowser::found_list_view_selected () } } -void -SoundFileBrowser::freesound_list_view_selected () -{ - freesound_download_cancel = false; - freesound_stop_btn.set_sensitive(true); - -#ifdef FREESOUND - if (!reset_options ()) { - set_action_sensitive (false); - } else { - Mootcher mootcher; - string file; - - TreeView::Selection::ListHandle_Path rows = freesound_list_view.get_selection()->get_selected_rows (); - - if (!rows.empty()) { - TreeIter iter = freesound_list->get_iter(*rows.begin()); - - string id = (*iter)[freesound_list_columns.id]; - string uri = (*iter)[freesound_list_columns.uri]; - string ofn = (*iter)[freesound_list_columns.filename]; - - // download the sound file - GdkCursor *prev_cursor; - prev_cursor = gdk_window_get_cursor (get_window()->gobj()); - gdk_window_set_cursor (get_window()->gobj(), gdk_cursor_new(GDK_WATCH)); - gdk_flush(); - - file = mootcher.getAudioFile(ofn, id, uri, this); - - gdk_window_set_cursor (get_window()->gobj(), prev_cursor); - - if (file != "") { - chooser.set_filename (file); - set_action_sensitive (true); - } - } else { - set_action_sensitive (false); - } - - freesound_progress_bar.set_text( - string_compose(P_("found %1 match", "found %1 matches", matches), matches)); - - preview.setup_labels (file); - } -#endif - freesound_stop_btn.set_sensitive(false); -} - void SoundFileBrowser::found_search_clicked () { @@ -875,6 +826,91 @@ SoundFileBrowser::found_search_clicked () } } + +std::string +SoundFileBrowser::freesound_get_audio_file(Gtk::TreeIter iter) +{ + + Mootcher *mootcher = new Mootcher; + std::string file; + + string id = (*iter)[freesound_list_columns.id]; + string uri = (*iter)[freesound_list_columns.uri]; + string ofn = (*iter)[freesound_list_columns.filename]; + + if (mootcher->checkAudioFile(ofn, id)) { + // file already exists, no need to download it again + file = mootcher->audioFileName; + delete mootcher; + (*iter)[freesound_list_columns.started] = false; + return file; + } + if (!(*iter)[freesound_list_columns.started]) { + // start downloading the sound file + (*iter)[freesound_list_columns.started] = true; + mootcher->fetchAudioFile(ofn, id, uri, this); + } + return ""; +} + +void +SoundFileBrowser::freesound_list_view_selected () +{ + + if (!reset_options ()) { + set_action_sensitive (false); + } else { + std::string file; + ListPath rows = freesound_list_view.get_selection()->get_selected_rows (); + for (ListPath::iterator i = rows.begin() ; i != rows.end(); ++i) { + file = freesound_get_audio_file (freesound_list->get_iter(*i)); + } + + switch (rows.size()) { + case 0: + // nothing selected + freesound_similar_btn.set_sensitive(false); + set_action_sensitive (false); + break; + case 1: + // exactly one item selected + if (file != "") { + // file exists on disk already + chooser.set_filename (file); + preview.setup_labels (file); + set_action_sensitive (true); + } + freesound_similar_btn.set_sensitive(true); + break; + default: + // multiple items selected + preview.setup_labels (""); + freesound_similar_btn.set_sensitive(false); + break; + } + + } +} + +void +SoundFileBrowser::refresh_display(std::string ID, std::string file) +{ + // called when the mootcher has finished downloading a file + ListPath rows = freesound_list_view.get_selection()->get_selected_rows (); + if (rows.size() == 1) { + // there's a single item selected in the freesound list + //XXX make a function to be used to construct the actual file name both here and in the mootcher + Gtk::TreeIter row = freesound_list->get_iter(*rows.begin()); + std::string selected_ID = (*row)[freesound_list_columns.id]; + if (ID == selected_ID) { + // the selected item in the freesound list is the item that has just finished downloading + chooser.set_filename(file); + preview.setup_labels (file); + set_action_sensitive (true); + } + } +} + void SoundFileBrowser::freesound_search_clicked () { @@ -895,18 +931,32 @@ SoundFileBrowser::freesound_more_clicked () } void -SoundFileBrowser::freesound_stop_clicked () +SoundFileBrowser::freesound_similar_clicked () { - freesound_download_cancel = true; -} + ListPath rows = freesound_list_view.get_selection()->get_selected_rows (); + if (rows.size() == 1) { + Mootcher mootcher; + string id; + Gtk::TreeIter iter = freesound_list->get_iter(*rows.begin()); + id = (*iter)[freesound_list_columns.id]; + freesound_list->clear(); + GdkCursor *prev_cursor; + prev_cursor = gdk_window_get_cursor (get_window()->gobj()); + gdk_window_set_cursor (get_window()->gobj(), gdk_cursor_new(GDK_WATCH)); + gdk_flush(); + + std::string theString = mootcher.searchSimilar(id); + + gdk_window_set_cursor (get_window()->gobj(), prev_cursor); + handle_freesound_results(theString); + } +} void SoundFileBrowser::freesound_search() { -#ifdef FREESOUND Mootcher mootcher; - freesound_list_view.get_column(1)->set_sizing(TREE_VIEW_COLUMN_GROW_ONLY); string search_string = freesound_entry.get_text (); enum sortMethod sort_method = (enum sortMethod) freesound_sort.get_active_row_number(); @@ -914,7 +964,6 @@ SoundFileBrowser::freesound_search() GdkCursor *prev_cursor; prev_cursor = gdk_window_get_cursor (get_window()->gobj()); gdk_window_set_cursor (get_window()->gobj(), gdk_cursor_new(GDK_WATCH)); - freesound_progress_bar.set_fraction(0.0); gdk_flush(); std::string theString = mootcher.searchText( @@ -929,7 +978,11 @@ SoundFileBrowser::freesound_search() ); gdk_window_set_cursor (get_window()->gobj(), prev_cursor); + handle_freesound_results(theString); +} +void +SoundFileBrowser::handle_freesound_results(std::string theString) { XMLTree doc; doc.read_buffer( theString ); XMLNode *root = doc.root(); @@ -1065,14 +1118,6 @@ SoundFileBrowser::freesound_search() matches++; } } - - if (matches == 0) { - freesound_progress_bar.set_text(_("Search returned no results.")); - } else { - freesound_progress_bar.set_text(string_compose(P_("Found %1 match", "Found %1 matches", matches), matches)); - } - freesound_list_view.get_column(1)->set_sizing(TREE_VIEW_COLUMN_AUTOSIZE); -#endif } vector @@ -1093,9 +1138,7 @@ SoundFileBrowser::get_paths () } } - } else if (n==1){ - - typedef TreeView::Selection::ListHandle_Path ListPath; + } else if (n == 1) { ListPath rows = found_list_view.get_selection()->get_selected_rows (); for (ListPath::iterator i = rows.begin() ; i != rows.end(); ++i) { @@ -1106,28 +1149,12 @@ SoundFileBrowser::get_paths () } } else { #ifdef FREESOUND - typedef TreeView::Selection::ListHandle_Path ListPath; - Mootcher mootcher; - ListPath rows = freesound_list_view.get_selection()->get_selected_rows (); for (ListPath::iterator i = rows.begin() ; i != rows.end(); ++i) { - TreeIter iter = freesound_list->get_iter(*i); - string id = (*iter)[freesound_list_columns.id]; - string uri = (*iter)[freesound_list_columns.uri]; - string ofn = (*iter)[freesound_list_columns.filename]; - - GdkCursor *prev_cursor; - prev_cursor = gdk_window_get_cursor (get_window()->gobj()); - gdk_window_set_cursor (get_window()->gobj(), gdk_cursor_new(GDK_WATCH)); - gdk_flush(); - - string str = mootcher.getAudioFile(ofn, id, uri, this); + string str = freesound_get_audio_file (freesound_list->get_iter(*i)); if (str != "") { results.push_back (str); } - - gdk_window_set_cursor (get_window()->gobj(), prev_cursor); - } #endif } diff --git a/gtk2_ardour/sfdb_ui.h b/gtk2_ardour/sfdb_ui.h index bed800ad73..5d8decddf4 100644 --- a/gtk2_ardour/sfdb_ui.h +++ b/gtk2_ardour/sfdb_ui.h @@ -131,6 +131,7 @@ class SoundFileBrowser : public ArdourWindow Gtk::TreeModelColumn filesize; Gtk::TreeModelColumn smplrate; Gtk::TreeModelColumn license; + Gtk::TreeModelColumn started; FreesoundColumns() { add(id); @@ -140,6 +141,7 @@ class SoundFileBrowser : public ArdourWindow add(filesize); add(smplrate); add(license); + add(started); } }; @@ -150,8 +152,9 @@ class SoundFileBrowser : public ArdourWindow Glib::RefPtr freesound_list; Gtk::Button freesound_more_btn; - Gtk::Button freesound_stop_btn; + Gtk::Button freesound_similar_btn; + void handle_freesound_results(std::string theString); public: SoundFileBrowser (std::string title, ARDOUR::Session* _s, bool persistent); virtual ~SoundFileBrowser (); @@ -177,11 +180,10 @@ class SoundFileBrowser : public ArdourWindow Gtk::Button freesound_search_btn; Gtk::TreeView freesound_list_view; - Gtk::ProgressBar freesound_progress_bar; - - bool freesound_download_cancel; + Gtk::Notebook notebook; void freesound_search(); + void refresh_display(std::string ID, std::string file); protected: bool resetting_ourselves; @@ -203,7 +205,6 @@ class SoundFileBrowser : public ArdourWindow static std::string persistent_folder; - Gtk::Notebook notebook; GainMeter* gm; Gtk::VBox meter_packer; @@ -224,10 +225,11 @@ class SoundFileBrowser : public ArdourWindow void freesound_list_view_activated (const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn*); void freesound_search_clicked (); void freesound_more_clicked (); - void freesound_stop_clicked (); + void freesound_similar_clicked (); int freesound_page; void chooser_file_activated (); + std::string freesound_get_audio_file(Gtk::TreeIter iter); bool on_audio_filter (const Gtk::FileFilter::Info& filter_info); bool on_midi_filter (const Gtk::FileFilter::Info& filter_info); From 8ad4924b7f6d12037753e0a93b5f861b443614ae Mon Sep 17 00:00:00 2001 From: Colin Fletcher Date: Sun, 16 Jun 2013 15:43:43 +0100 Subject: [PATCH 4/5] Use connect(..., gui_thread()) rather than g_idle_add(). Use the proper functions to ensure things happen in the main gui thread, instead of fudging around with g_idle_add(). --- gtk2_ardour/sfdb_freesound_mootcher.cc | 43 +++++++++----------------- gtk2_ardour/sfdb_freesound_mootcher.h | 13 +++++++- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/gtk2_ardour/sfdb_freesound_mootcher.cc b/gtk2_ardour/sfdb_freesound_mootcher.cc index 38d53aaa70..e6861cd1f4 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.cc +++ b/gtk2_ardour/sfdb_freesound_mootcher.cc @@ -55,6 +55,7 @@ #include "ardour/audio_library.h" #include "ardour/rc_configuration.h" #include "pbd/pthread_utils.h" +#include "gui_thread.h" using namespace PBD; @@ -331,17 +332,15 @@ CURLcode res; return (void *) res; } - -static int -donewithMootcher(void *arg) + +void +Mootcher::doneWithMootcher() { - Mootcher *thisMootcher = (Mootcher *) arg; // update the sound info pane if the selection in the list box is still us - thisMootcher->sfb->refresh_display(thisMootcher->ID, thisMootcher->audioFileName); + sfb->refresh_display(ID, audioFileName); - delete(thisMootcher); - return 0; + delete this; // XXX is this a good idea? } static void * @@ -352,8 +351,8 @@ freesound_download_thread_func(void *arg) // std::cerr << "freesound_download_thread_func(" << arg << ")" << std::endl; res = thisMootcher->threadFunc(); - g_idle_add(donewithMootcher, thisMootcher); + thisMootcher->Finished(); /* EMIT SIGNAL */ return res; } @@ -421,6 +420,8 @@ bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, s curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback); curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this); + Progress.connect(*this, invalidator (*this), boost::bind(&Mootcher::updateProgress, this, _1, _2), gui_context()); + Finished.connect(*this, invalidator (*this), boost::bind(&Mootcher::doneWithMootcher, this), gui_context()); pthread_t freesound_download_thread; pthread_create_and_store("freesound_import", &freesound_download_thread, freesound_download_thread_func, this); @@ -428,29 +429,20 @@ bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, s } //--------- -struct progressInfo { - Gtk::ProgressBar *bar; - double dltotal; - double dlnow; -}; -static int -updateProgress(void *arg) +void +Mootcher::updateProgress(double dlnow, double dltotal) { - struct progressInfo *progress = (struct progressInfo *) arg; - if (progress->dltotal > 0) { - double fraction = progress->dlnow / progress->dltotal; + if (dltotal > 0) { + double fraction = dlnow / dltotal; // std::cerr << "progress idle: " << progress->bar->get_text() << ". " << progress->dlnow << " / " << progress->dltotal << " = " << fraction << std::endl; if (fraction > 1.0) { fraction = 1.0; } else if (fraction < 0.0) { fraction = 0.0; } - progress->bar->set_fraction(fraction); + progress_bar.set_fraction(fraction); } - - delete progress; - return 0; } int @@ -466,12 +458,7 @@ Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double / return -1; } - struct progressInfo *progress = new struct progressInfo; - progress->bar = &thisMootcher->progress_bar; - progress->dltotal = dltotal; - progress->dlnow = dlnow; - - g_idle_add(updateProgress, progress); + thisMootcher->Progress(dlnow, dltotal); /* EMIT SIGNAL */ return 0; } diff --git a/gtk2_ardour/sfdb_freesound_mootcher.h b/gtk2_ardour/sfdb_freesound_mootcher.h index ee65020021..48fb11b638 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.h +++ b/gtk2_ardour/sfdb_freesound_mootcher.h @@ -65,7 +65,7 @@ enum sortMethod { }; -class Mootcher +class Mootcher: public sigc::trackable, public PBD::ScopedConnectionList { public: Mootcher(); @@ -80,6 +80,14 @@ public: std::string audioFileName; std::string ID; + /** signal emitted when mootcher reports progress updates during download. + * The parameters are current and total numbers of bytes downloaded. + */ + PBD::Signal2 Progress; + /** signal emitted when the mootcher has finished downloading. */ + PBD::Signal0 Finished; + + private: void ensureWorkingDir(); @@ -97,6 +105,9 @@ private: FILE* theFile; + void updateProgress(double dlnow, double dltotal); + void doneWithMootcher(); + Gtk::HBox progress_hbox; Gtk::ProgressBar progress_bar; Gtk::Button cancel_download_btn; From 7ba817e36fb72c28077cc8143d14d2c09cd6addf Mon Sep 17 00:00:00 2001 From: Colin Fletcher Date: Mon, 17 Jun 2013 16:46:18 +0100 Subject: [PATCH 5/5] Amend comment to 'delete this' Amend comment to 'delete this' to describe why I think it's OK. --- gtk2_ardour/sfdb_freesound_mootcher.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtk2_ardour/sfdb_freesound_mootcher.cc b/gtk2_ardour/sfdb_freesound_mootcher.cc index e6861cd1f4..8e57d9a0c4 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.cc +++ b/gtk2_ardour/sfdb_freesound_mootcher.cc @@ -340,7 +340,7 @@ Mootcher::doneWithMootcher() // update the sound info pane if the selection in the list box is still us sfb->refresh_display(ID, audioFileName); - delete this; // XXX is this a good idea? + delete this; // this should be OK to do as long as Progress and Finished signals are always received in the order in which they are emitted } static void *