diff options
author | Ben Loftis <ben@glw.com> | 2012-04-05 21:31:39 +0000 |
---|---|---|
committer | Ben Loftis <ben@glw.com> | 2012-04-05 21:31:39 +0000 |
commit | 54e7185296752b597484bff00dc20f58e86d1886 (patch) | |
tree | fd6628fb935da2cb73f6b9f16c44dcdeb9c1e288 /gtk2_ardour | |
parent | b2637c54bd9351f7daf3b69b103e2d252f72ac1d (diff) |
various tweaks to Freesound UI and code. keep a single mootcher to speed up operations; add text to progress bar, stop storing xml files, auto-increment pages until user clicks stop.
git-svn-id: svn://localhost/ardour2/branches/2.0-ongoing@11800 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'gtk2_ardour')
-rw-r--r-- | gtk2_ardour/sfdb_freesound_mootcher.cc | 621 | ||||
-rw-r--r-- | gtk2_ardour/sfdb_freesound_mootcher.h | 57 | ||||
-rw-r--r-- | gtk2_ardour/sfdb_ui.cc | 272 | ||||
-rw-r--r-- | gtk2_ardour/sfdb_ui.h | 43 |
4 files changed, 501 insertions, 492 deletions
diff --git a/gtk2_ardour/sfdb_freesound_mootcher.cc b/gtk2_ardour/sfdb_freesound_mootcher.cc index f23698b4bf..bb2d92188d 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.cc +++ b/gtk2_ardour/sfdb_freesound_mootcher.cc @@ -1,7 +1,8 @@ /* sfdb_freesound_mootcher.cpp **********************************************************************
-
+
Adapted for Ardour by Ben Loftis, March 2008
-
+ Updated to new Freesound API by Colin Fletcher, November 2011
+
Mootcher 23-8-2005
Mootcher Online Access to thefreesoundproject website
@@ -39,62 +40,71 @@ *************************************************************************************/
#include "sfdb_freesound_mootcher.h"
-#include <pbd/xml++.h>
+#include "pbd/xml++.h"
+//#include "pbd/filesystem.h"
#include <sys/stat.h>
#include <sys/types.h>
+#include <iostream>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include "i18n.h"
-#include <ardour/audio_library.h>
+#include "ardour/audio_library.h"
+
+static const std::string base_url = "http://www.freesound.org/api";
+static const std::string api_key = "9d77cb8d841b4bcfa960e1aae62224eb"; // ardour3
-#define TRUE 1
//------------------------------------------------------------------------
-Mootcher:: Mootcher(const char *saveLocation)
- : curl( NULL )
- , connection( 0 )
+Mootcher::Mootcher()
+ : curl(curl_easy_init())
{
- changeWorkingDir(saveLocation);
+ std::string path;
+ path = Glib::get_home_dir() + "/Freesound/";
+ changeWorkingDir ( path.c_str() );
};
//------------------------------------------------------------------------
-Mootcher:: ~Mootcher()
+Mootcher:: ~Mootcher()
{
- remove( "cookiejar.txt" );
+ curl_easy_cleanup(curl);
}
+
//------------------------------------------------------------------------
-const char* Mootcher::changeWorkingDir(const char *saveLocation)
+void Mootcher::changeWorkingDir(const char *saveLocation)
{
basePath = saveLocation;
#ifdef __WIN32__
std::string replace = "/";
- int pos = (int)basePath.find("\\");
+ size_t pos = basePath.find("\\");
while( pos != std::string::npos ){
basePath.replace(pos, 1, replace);
- pos = (int)basePath.find("\\");
+ pos = basePath.find("\\");
}
#endif
- //
- int pos2 = basePath.find_last_of("/");
+ //
+ size_t pos2 = basePath.find_last_of("/");
if(basePath.length() != (pos2+1)) basePath += "/";
-
- // create Freesound directory and sound dir
+}
+
+void Mootcher::ensureWorkingDir ()
+{
std::string sndLocation = basePath;
- mkdir(sndLocation.c_str(), 0777);
+ g_mkdir(sndLocation.c_str(), 0777);
sndLocation += "snd";
- mkdir(sndLocation.c_str(), 0777);
-
- return basePath.c_str();
+ g_mkdir(sndLocation.c_str(), 0777);
}
+
//------------------------------------------------------------------------
-size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
+size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
register int realsize = (int)(size * nmemb);
struct MemoryStruct *mem = (struct MemoryStruct *)data;
- // There might be a realloc() out there that doesn't like
- // reallocing NULL pointers, so we take care of it here
- if(mem->memory) mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
- else mem->memory = (char *)malloc(mem->size + realsize + 1);
+ mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
@@ -106,326 +116,168 @@ size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void //------------------------------------------------------------------------
-void Mootcher::toLog(std::string input)
-{
-printf("%s\n", input.c_str());// for debugging
-}
+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.
+
+ switch (sort) {
+ case sort_duration_desc: return "duration_desc";
+ case sort_duration_asc: return "duration_asc";
+ case sort_created_desc: return "created_desc";
+ case sort_created_asc: return "created_asc";
+ case sort_downloads_desc: return "downloads_desc";
+ case sort_downloads_asc: return "downloads_asc";
+ case sort_rating_desc: return "rating_desc";
+ case sort_rating_asc: return "rating_asc";
+ default: return "";
+ }
+}
//------------------------------------------------------------------------
-void Mootcher::setcUrlOptions()
+void Mootcher::setcUrlOptions()
{
// basic init for curl
curl_global_init(CURL_GLOBAL_ALL);
// some servers don't like requests that are made without a user-agent field, so we provide one
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
// setup curl error buffer
- CURLcode res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
- // always use the cookie with session id which is received at the login
- curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookiejar.txt");
+ curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
// Allow redirection
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+
+ // Allow connections to time out (without using signals)
+ curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+ curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
+
+
}
-//------------------------------------------------------------------------
-int Mootcher::doLogin(std::string login, std::string password)
+std::string Mootcher::doRequest(std::string uri, std::string params)
{
- if(connection==1)
- return 1;
-
+ std::string result;
struct MemoryStruct xml_page;
xml_page.memory = NULL;
xml_page.size = 0;
- // create the post message from the login and password
- std::string postMessage;
- postMessage += "username=";
- postMessage += curl_escape(login.c_str(), 0);
- postMessage += "&password=";
- postMessage += curl_escape(password.c_str(), 0);
- postMessage += "&login=";
- postMessage += curl_escape("1", 0);
- postMessage += "&redirect=";
- postMessage += curl_escape("../tests/login.php", 0);
-
- // Do the setup for libcurl
- curl = curl_easy_init();
-
- if(curl)
- {
- setcUrlOptions();
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&xml_page);
- // save the sessoin id that is given back by the server in a cookie
- curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookiejar.txt");
- // use POST for login variables
- curl_easy_setopt(curl, CURLOPT_POST, TRUE);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postMessage.c_str());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1);
-
- // the url to get
- std::string login_url = "http://www.freesound.org/forum/login.php";
- curl_easy_setopt(curl, CURLOPT_URL, login_url.c_str() );
-
- // perform online request
- connection = 1;
- CURLcode res = curl_easy_perform(curl);
- if( res != 0 ) {
- toLog("curl login error\n");
- toLog(curl_easy_strerror(res));
- connection = 0;
- }
-
- if (connection == 1){
- std::string check_page = xml_page.memory;
- int test = (int)check_page.find("login"); //logged
- if( strcmp(xml_page.memory, "login") == 0 )
- toLog("Logged in.\n");
- else {
- toLog("Login failed: Check username and password.\n");
- connection = 0;
- }
- }
+ setcUrlOptions();
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &xml_page);
- // free the memory
- if(xml_page.memory){
- free( xml_page.memory );
- xml_page.memory = NULL;
- xml_page.size = 0;
- }
+ // curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
+ // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postMessage.c_str());
+ // curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1);
- std::cerr << "Login was cool, connection = " << connection << std::endl;
- return connection;
+ // the url to get
+ std::string url = base_url + uri + "?";
+ if (params != "") {
+ url += params + "&api_key=" + api_key + "&format=xml";
+ } else {
+ url += "api_key=" + api_key + "&format=xml";
}
- else return 3; // will be returned if a curl related problem ocurrs
-}
-//------------------------------------------------------------------------
-std::string Mootcher::searchText(std::string word)
-{
- struct MemoryStruct xml_page;
- xml_page.memory = NULL;
- xml_page.size = 0;
-
- std::string result;
-
- if(connection != 0)
- {
- // create a url encoded post message
- std::string postMessage;
- char tempString[ 128 ];
- char *tempPointer = &tempString[0];
-
- postMessage = "search=";
- postMessage += curl_escape(word.c_str(), 0);
- sprintf( tempPointer, "&searchDescriptions=1");
- postMessage += tempPointer;
- sprintf( tempPointer, "&searchtags=1");
- postMessage += tempPointer;
-
- // Ref: http://www.freesound.org/forum/viewtopic.php?p=19081
- // const ORDER_DEFAULT = 0;
- // const ORDER_DOWNLOADS_DESC = 1;
- // const ORDER_DOWNLOADS_ASC = 2;
- // const ORDER_USERNAME_DESC = 3;
- // const ORDER_USERNAME_ASC = 4;
- // const ORDER_DATE_DESC = 5;
- // const ORDER_DATE_ASC = 6;
- // const ORDER_DURATION_DESC = 7;
- // const ORDER_DURATION_ASC = 8;
- // const ORDER_FILEFORMAT_DESC = 9;
- // const ORDER_FILEFORMAT_ASC = 10;
- sprintf( tempPointer, "&order=1");
- postMessage += tempPointer;
- sprintf( tempPointer, "&start=0");
- postMessage += tempPointer;
- sprintf( tempPointer, "&limit=10");
- postMessage += tempPointer;
- // The limit of 10 samples is arbitrary, but seems
- // reasonable in light of the fact that all of the returned
- // samples get downloaded, and downloads are s-l-o-w.
- if(curl)
- {
- // basic init for curl
- setcUrlOptions();
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&xml_page);
- // setup the post message
- curl_easy_setopt(curl, CURLOPT_POST, TRUE);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postMessage.c_str());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1);
-
- // the url to get
- std::string search_url = "http://www.freesound.org/searchTextXML.php";
- curl_easy_setopt(curl, CURLOPT_URL, search_url.c_str());
-
- // perform the online search
- connection = 1;
- CURLcode res = curl_easy_perform(curl);
- if( res != 0 ) {
- toLog("curl login error\n");
- toLog(curl_easy_strerror(res));
- connection = 0;
- }
-
- result = xml_page.memory;
- toLog( result.c_str() );
-
- // free the memory
- if(xml_page.memory){
- free( xml_page.memory );
- xml_page.memory = NULL;
- xml_page.size = 0;
- }
+ curl_easy_setopt(curl, CURLOPT_URL, url.c_str() );
+ std::cerr << "doRequest: " << url << std::endl;
+
+ // perform online request
+ CURLcode res = curl_easy_perform(curl);
+ if( res != 0 ) {
+ std::cerr << "curl error " << res << " (" << curl_easy_strerror(res) << ")" << std::endl;
+ return "";
+ }
- }
+ result = xml_page.memory;
+ // free the memory
+ if(xml_page.memory){
+ free( xml_page.memory );
+ xml_page.memory = NULL;
+ xml_page.size = 0;
}
+//printf("freesound result: %s\n", result.c_str());
return result;
-}
-//------------------------------------------------------------------------
-std::string Mootcher::changeExtension(std::string filename)
-{
- std::string aiff = ".aiff";
- std::string aif = ".aif";
- std::string wav = ".wav";
- std::string mp3 = ".mp3";
- std::string ogg = ".ogg";
- std::string flac = ".flac";
-
- std::string replace = ".xml";
- int pos = 0;
-
- pos = (int)filename.find(aiff);
- if(pos != std::string::npos) filename.replace(pos, aiff.size(), replace);
- pos = (int)filename.find(aif);
- if(pos != std::string::npos) filename.replace(pos, aif.size(), replace);
- pos = (int)filename.find(wav);
- if(pos != std::string::npos) filename.replace(pos, wav.size(), replace);
- pos = (int)filename.find(mp3);
- if(pos != std::string::npos) filename.replace(pos, mp3.size(), replace);
- pos = (int)filename.find(ogg);
- if(pos != std::string::npos) filename.replace(pos, ogg.size(), replace);
- pos = (int)filename.find(flac);
- if(pos != std::string::npos) filename.replace(pos, flac.size(), replace);
-
- return filename;
}
-//------------------------------------------------------------------------
-void Mootcher::GetXml(std::string ID, struct MemoryStruct &xml_page)
+
+
+std::string Mootcher::searchText(std::string query, int page, std::string filter, enum sortMethod sort)
{
+ std::string params = "";
+ char buf[24];
- if(curl) {
- setcUrlOptions();
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&xml_page);
-
- // URL to get
- std::string getxml_url = "http://www.freesound.org/samplesViewSingleXML.php?id=";
- getxml_url += ID;
-
- curl_easy_setopt(curl, CURLOPT_URL, getxml_url.c_str() );
-
- // get it!
- connection = 1;
- CURLcode res = curl_easy_perform(curl);
- if( res != 0 ) {
- toLog("curl login error\n");
- toLog(curl_easy_strerror(res));
- connection = 0;
- }
+ if (page > 1) {
+ snprintf(buf, 23, "p=%d&", page);
+ params += buf;
}
+
+ params += "q=" + query;
+
+ if (filter != "")
+ params += "&f=" + filter;
+
+ if (sort)
+ params += "&s=" + sortMethodString(sort);
+
+ params += "&fields=id,original_filename,duration,serve";
+
+ return doRequest("/sounds/search", params);
}
+
//------------------------------------------------------------------------
-std::string Mootcher::getXmlFile(std::string ID, int &length)
+
+std::string Mootcher::getSoundResourceFile(std::string ID)
{
- struct MemoryStruct xml_page;
- xml_page.memory = NULL;
- xml_page.size = NULL;
- std::string xmlFileName;
+ std::string originalSoundURI;
std::string audioFileName;
- std::string filename;
-
- if(connection != 0) {
- // download the xmlfile into xml_page
- GetXml(ID, xml_page);
-
- // if sample ID does not exist on the freesound website
- if(strcmp(xml_page.memory, "sample non existant") == 0){
- free( xml_page.memory );
- sprintf(message, "getXmlFile: sample with ID:%s does not exist!\n", ID.c_str() );
- toLog(message);
- return filename;
- } else {
- XMLTree doc;
- doc.read_buffer( xml_page.memory );
- XMLNode *freesound = doc.root();
-
- // if the page is not a valid xml document with a 'freesound' root
- if( freesound == NULL){
- sprintf(message, "getXmlFile: There is no valid root in the xml file");
- toLog(message);
- } else {
- XMLNode *sample = freesound->child("sample");
- XMLNode *name = NULL;
- XMLNode *filesize = NULL;
- if (sample) {
- name = sample->child("originalFilename");
- filesize = sample->child("filesize");
- }
-
- // get the file name and size from xml file
- if (sample && name && filesize) {
-
- audioFileName = name->child("text")->content();
- sprintf( message, "getXmlFile: %s needs to be downloaded\n", audioFileName.c_str() );
- toLog(message);
-
- length = atoi(filesize->child("text")->content().c_str());
-
- // create new filename with the ID number
- filename = basePath;
- filename += "snd/";
- filename += sample->property("id")->value();
- filename += "-";
- filename += audioFileName;
- // change the extention into .xml
- xmlFileName = changeExtension( filename );
-
- sprintf(message, "getXmlFile: saving XML: %s\n", xmlFileName.c_str() );
- toLog(message);
-
- // save the xml file to disk
- doc.write(xmlFileName.c_str());
-
- //store all the tags in the database
- XMLNode *tags = sample->child("tags");
- if (tags) {
- XMLNodeList children = tags->children();
- XMLNodeConstIterator niter;
- vector<string> strings;
- for (niter = children.begin(); niter != children.end(); ++niter) {
- XMLNode *node = *niter;
- if( strcmp( node->name().c_str(), "tag") == 0 ) {
- XMLNode *text = node->child("text");
- if (text) strings.push_back(text->content());
- }
- }
- ARDOUR::Library->set_tags (string("//")+filename, strings);
- ARDOUR::Library->save_changes ();
+ std::string xml;
+
+
+ std::cerr << "getSoundResourceFile(" << ID << ")" << std::endl;
+
+ // download the xmlfile into xml_page
+ xml = doRequest("/sounds/" + ID, "");
+
+ XMLTree doc;
+ doc.read_buffer( xml.c_str() );
+ XMLNode *freesound = doc.root();
+
+ // if the page is not a valid xml document with a 'freesound' root
+ if (freesound == NULL) {
+ std::cerr << "getSoundResourceFile: There is no valid root in the xml file" << std::endl;
+ return "";
+ }
+
+ if (strcmp(doc.root()->name().c_str(), "response") != 0) {
+ std::cerr << "getSoundResourceFile: root =" << doc.root()->name() << ", != response" << std::endl;
+ return "";
+ }
+
+ XMLNode *name = freesound->child("original_filename");
+
+ // get the file name and size from xml file
+ if (name) {
+
+ audioFileName = basePath + "snd/" + ID + "-" + name->child("text")->content();
+
+ //store all the tags in the database
+ XMLNode *tags = freesound->child("tags");
+ if (tags) {
+ XMLNodeList children = tags->children();
+ XMLNodeConstIterator niter;
+ std::vector<std::string> strings;
+ for (niter = children.begin(); niter != children.end(); ++niter) {
+ XMLNode *node = *niter;
+ if( strcmp( node->name().c_str(), "resource") == 0 ) {
+ XMLNode *text = node->child("text");
+ if (text) {
+ // std::cerr << "tag: " << text->content() << std::endl;
+ strings.push_back(text->content());
}
}
-
- // clear the memory
- if(xml_page.memory){
- free( xml_page.memory );
- xml_page.memory = NULL;
- xml_page.size = 0;
- }
- return audioFileName;
}
+ ARDOUR::Library->set_tags (std::string("//")+audioFileName, strings);
+ ARDOUR::Library->save_changes ();
}
}
@@ -438,85 +290,102 @@ int audioFileWrite(void *buffer, size_t size, size_t nmemb, void *file) };
//------------------------------------------------------------------------
-std::string Mootcher::getFile(std::string ID)
+std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller)
{
- CURLcode result_curl;
+ ensureWorkingDir();
+ std::string audioFileName = basePath + "snd/" + ID + "-" + originalFileName;
+
+ // check to see if audio file already exists
+ FILE *testFile = g_fopen(audioFileName.c_str(), "r");
+ if (testFile) {
+ fseek (testFile , 0 , SEEK_END);
+ if (ftell (testFile) > 256) {
+// std::cerr << "audio file " << audioFileName << " already exists" << std::endl;
+ fclose (testFile);
+ return audioFileName;
+ }
+
+ // else file was small, probably an error, delete it and try again
+ fclose(testFile);
+ remove( audioFileName.c_str() );
+ }
- std::string audioFileName;
+ if (!curl) {
+ return "";
+ }
- if(connection != 0)
- {
- int length;
- std::string name = getXmlFile(ID, length);
- if( name != "" ){
-
- // create new filename with the ID number
- audioFileName += basePath;
- audioFileName += "snd/";
- audioFileName += ID;
- audioFileName += "-";
- audioFileName += name;
-
- //check to see if audio file already exists
- FILE *testFile = fopen(audioFileName.c_str(), "r");
- if (testFile) { //TODO: should also check length to see if file is complete
- fseek (testFile , 0 , SEEK_END);
- if (ftell (testFile) == length) {
- sprintf(message, "%s already exists\n", audioFileName.c_str() );
- toLog(message);
- fclose (testFile);
- return audioFileName;
- } else {
- remove( audioFileName.c_str() ); //file was not correct length, delete it and try again
- }
- }
-
-
- //now download the actual file
- if (curl) {
-
- FILE* theFile;
- theFile = fopen( audioFileName.c_str(), "wb" );
-
- // create the download url, this url will also update the download statics on the site
- std::string audioURL;
- audioURL += "http://www.freesound.org/samplesDownload.php?id=";
- audioURL += ID;
-
- setcUrlOptions();
- curl_easy_setopt(curl, CURLOPT_URL, audioURL.c_str() );
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, audioFileWrite);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, theFile);
-
- connection = 1;
- CURLcode res = curl_easy_perform(curl);
- if( res != 0 ) {
- toLog("curl login error\n");
- toLog(curl_easy_strerror(res));
- connection = 0;
- }
+ //if already canceling 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" );
- fclose(theFile);
- }
+ if (!theFile) {
+ return "";
+ }
-/*
- bar.dlnowMoo = 0;
- bar.dltotalMoo = 0;
- curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); // turn on the process bar thingy
- curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
- curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, &bar);
-*/
- }
+ // create the download url
+ audioURL += "?api_key=" + api_key;
+
+ setcUrlOptions();
+ curl_easy_setopt(curl, CURLOPT_URL, audioURL.c_str() );
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, audioFileWrite);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, theFile);
+
+ std::cerr << "downloading " << audioFileName << " from " << audioURL << "..." << std::endl;
+ /* hack to get rid of the barber-pole stripes */
+ caller->freesound_progress_bar.hide();
+ caller->freesound_progress_bar.show();
+
+ string prog;
+ prog = string_compose (_("%1: click Stop to cancel -->"), originalFileName);
+ caller->freesound_progress_bar.set_text(prog);
+
+ 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);
+
+ CURLcode res = curl_easy_perform(curl);
+ fclose(theFile);
+
+ 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 ) {
+ std::cerr << "curl error " << res << " (" << curl_easy_strerror(res) << ")" << std::endl;
+ remove( audioFileName.c_str() );
+ return "";
+ } else {
+ std::cerr << "done!" << std::endl;
+ // now download the tags &c.
+ getSoundResourceFile(ID);
}
return audioFileName;
}
//---------
-int Mootcher::progress_callback(void *bar, double dltotal, double dlnow, double ultotal, double ulnow)
+int Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double ultotal, double ulnow)
{
- struct dlprocess *lbar = (struct dlprocess *) bar;
- lbar->dltotalMoo = dltotal;
- lbar->dlnowMoo = 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 */
+ }
+
+ std::cerr << "Freesound download progress: " << dlnow << " of " << dltotal << " \r";
return 0;
}
+
diff --git a/gtk2_ardour/sfdb_freesound_mootcher.h b/gtk2_ardour/sfdb_freesound_mootcher.h index 3cfbd414d4..b6f3d7889d 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.h +++ b/gtk2_ardour/sfdb_freesound_mootcher.h @@ -1,6 +1,7 @@ /*sfdb_freesound_mootcher.h****************************************************************************
-
+
Adapted for Ardour by Ben Loftis, March 2008
+ Updated to new Freesound API by Colin Fletcher, November 2011
Mootcher Online Access to thefreesoundproject website
http://freesound.iua.upf.edu/
@@ -19,60 +20,58 @@ #include <string>
#include <sstream>
#include <vector>
+#include <gtkmm/progressbar.h>
//#include <ctime>
-#include "curl/curl.h"
-
+#include "sfdb_ui.h"
-// mootcher version
-#define ___VERSION___ 1.3
+#include "curl/curl.h"
-//--- struct to store XML file
+//--- struct to store XML file
struct MemoryStruct {
char *memory;
size_t size;
};
-//--- for download process viewing
-struct dlprocess {
- double dltotalMoo;
- double dlnowMoo;
+enum sortMethod {
+ sort_none, // no sort
+ sort_duration_desc, // Sort by the duration of the sounds, longest sounds first.
+ sort_duration_asc, // Same as above, but shortest sounds first.
+ sort_created_desc, // Sort by the date of when the sound was added. newest sounds first.
+ sort_created_asc, // Same as above, but oldest sounds first.
+ sort_downloads_desc, // Sort by the number of downloads, most downloaded sounds first.
+ sort_downloads_asc, // Same as above, but least downloaded sounds first.
+ sort_rating_desc, // Sort by the average rating given to the sounds, highest rated first.
+ sort_rating_asc // Same as above, but lowest rated sounds first.
};
+
class Mootcher
{
public:
- Mootcher(const char *saveLocation);
+ Mootcher();
~Mootcher();
- int doLogin(std::string login, std::string password);
- std::string getFile(std::string ID);
- std::string searchText(std::string word);
+ std::string getAudioFile(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);
-
- struct dlprocess bar;
-
private:
- const char* changeWorkingDir(const char *saveLocation);
-
- std::string getXmlFile(std::string ID, int &length);
- void GetXml(std::string ID, struct MemoryStruct &xml_page);
- std::string changeExtension(std::string filename);
-
- void toLog(std::string input);
+ void changeWorkingDir(const char *saveLocation);
+ void ensureWorkingDir();
+ std::string doRequest(std::string uri, std::string params);
void setcUrlOptions();
- static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data);
- static int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
+ static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data);
+ static int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
+ std::string sortMethodString(enum sortMethod sort);
+ std::string getSoundResourceFile(std::string ID);
CURL *curl;
char errorBuffer[CURL_ERROR_SIZE]; // storage for cUrl error message
- int connection; // is 0 if no connection
- char message[128]; // storage for messages that are send to the logfile
-
std::string basePath;
std::string xmlLocation;
};
+
diff --git a/gtk2_ardour/sfdb_ui.cc b/gtk2_ardour/sfdb_ui.cc index fa2d50afc6..4fed84e853 100644 --- a/gtk2_ardour/sfdb_ui.cc +++ b/gtk2_ardour/sfdb_ui.cc @@ -178,10 +178,10 @@ SoundFileBox::SoundFileBox (bool persistent) main_box.pack_start(bottom_box, false, false); play_btn.set_image (*(manage (new Image (Stock::MEDIA_PLAY, ICON_SIZE_BUTTON)))); - play_btn.set_label (_("Play (double click)")); +// play_btn.set_label (_("Play (double click)")); stop_btn.set_image (*(manage (new Image (Stock::MEDIA_STOP, ICON_SIZE_BUTTON)))); - stop_btn.set_label (_("Stop")); +// stop_btn.set_label (_("Stop")); bottom_box.set_homogeneous (false); bottom_box.set_spacing (6); @@ -399,13 +399,12 @@ SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::S preview (persistent), found_search_btn (_("Search")), found_list_view (found_list), - freesound_search_btn (_("Start Downloading")), + freesound_search_btn (_("Search")), freesound_list_view (freesound_list) { resetting_ourselves = false; gm = 0; - #ifdef GTKOSX chooser.add_shortcut_folder_uri("file:///Library/GarageBand/Apple Loops"); chooser.add_shortcut_folder_uri("file:///Library/Audio/Apple Loops"); @@ -414,6 +413,10 @@ SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::S chooser.add_shortcut_folder_uri("file:///Volumes"); #endif +#ifdef FREESOUND + mootcher = new Mootcher(); +#endif + //add the file chooser { chooser.set_border_width (12); @@ -472,6 +475,8 @@ SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::S found_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked)); found_entry.signal_activate().connect(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")); } @@ -485,21 +490,36 @@ SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::S passbox = manage(new HBox); passbox->set_border_width (12); passbox->set_spacing (6); - - label = manage (new Label); - label->set_text (_("User:")); - passbox->pack_start (*label, false, false); - passbox->pack_start (freesound_name_entry); - label = manage (new Label); - label->set_text (_("Password:")); - passbox->pack_start (*label, false, false); - passbox->pack_start (freesound_pass_entry); label = manage (new Label); label->set_text (_("Tags:")); passbox->pack_start (*label, false, false); passbox->pack_start (freesound_entry, false, false); + + label = manage (new Label); + label->set_text (_("Sort:")); + passbox->pack_start (*label, false, false); + passbox->pack_start (freesound_sort, false, false); +// freesound_sort.clear_items(); + + // Order of the following must correspond with enum sortMethod + // in sfdb_freesound_mootcher.h + freesound_sort.append_text(_("None")); + freesound_sort.append_text(_("Longest")); + freesound_sort.append_text(_("Shortest")); + freesound_sort.append_text(_("Newest")); + freesound_sort.append_text(_("Oldest")); + freesound_sort.append_text(_("Most downloaded")); + freesound_sort.append_text(_("Least downloaded")); + freesound_sort.append_text(_("Highest rated")); + freesound_sort.append_text(_("Lowest rated")); + freesound_sort.set_active(0); + passbox->pack_start (freesound_search_btn, false, false); + passbox->pack_start (freesound_progress_bar); + passbox->pack_end (freesound_stop_btn, false, false); + freesound_stop_btn.set_label(_("Stop")); + Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow); scroll->add(freesound_list_view); scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); @@ -510,11 +530,15 @@ SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::S //vbox->pack_start (freesound_list_view); - freesound_list_view.append_column(_("Paths"), freesound_list_columns.pathname); + 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); + freesound_list_view.append_column(_("Duration"), freesound_list_columns.duration); + freesound_list_view.get_column(1)->set_expand(true); + freesound_list_view.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &SoundFileBrowser::freesound_list_view_selected)); + freesound_list_view.get_selection()->set_mode (SELECTION_MULTIPLE); - freesound_list_view.get_selection()->signal_changed().connect(mem_fun(*this, &SoundFileBrowser::freesound_list_view_selected)); - - //freesound_list_view.get_selection()->set_mode (SELECTION_MULTIPLE); + freesound_list_view.get_selection()->set_mode (SELECTION_MULTIPLE); freesound_list_view.signal_row_activated().connect (mem_fun (*this, &SoundFileBrowser::freesound_list_view_activated)); freesound_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::freesound_search_clicked)); @@ -522,6 +546,7 @@ SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::S notebook.append_page (*vbox, _("Search Freesound")); } + #endif @@ -658,7 +683,7 @@ SoundFileBrowser::found_list_view_selected () string file; TreeView::Selection::ListHandle_Path rows = found_list_view.get_selection()->get_selected_rows (); - + if (!rows.empty()) { TreeIter iter = found_list->get_iter(*rows.begin()); file = (*iter)[found_list_columns.pathname]; @@ -667,7 +692,7 @@ SoundFileBrowser::found_list_view_selected () } else { set_response_sensitive (RESPONSE_OK, false); } - + preview.setup_labels (file); } } @@ -675,24 +700,45 @@ SoundFileBrowser::found_list_view_selected () void SoundFileBrowser::freesound_list_view_selected () { + freesound_download_cancel = false; + +#ifdef FREESOUND if (!reset_options ()) { set_response_sensitive (RESPONSE_OK, false); } else { + 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()); - file = (*iter)[freesound_list_columns.pathname]; - chooser.set_filename (file); - set_response_sensitive (RESPONSE_OK, true); + + 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_response_sensitive (RESPONSE_OK, true); + } } else { set_response_sensitive (RESPONSE_OK, false); } - + preview.setup_labels (file); } +#endif } void @@ -719,85 +765,131 @@ SoundFileBrowser::found_search_clicked () } } -void* -freesound_search_thread_entry (void* arg) +void +SoundFileBrowser::freesound_search_clicked () { - PBD::notify_gui_about_thread_creation (pthread_self(), X_("Freesound Search")); - - static_cast<SoundFileBrowser*>(arg)->freesound_search_thread (); - - return 0; + freesound_search_cancel = false; + freesound_search(); } -bool searching = false; -bool canceling = false; - void -SoundFileBrowser::freesound_search_clicked () +SoundFileBrowser::freesound_stop_clicked () { - if (canceling) //already canceling, button does nothing - return; - - if ( searching ) { - freesound_search_btn.set_label(_("Cancelling..")); - canceling = true; - } else { - searching = true; - freesound_search_btn.set_label(_("Cancel")); - pthread_t freesound_thr; - pthread_create_and_store ("freesound_search", &freesound_thr, 0, freesound_search_thread_entry, this); - } + freesound_download_cancel = true; + freesound_search_cancel = true; } void -SoundFileBrowser::freesound_search_thread() +SoundFileBrowser::freesound_search() { #ifdef FREESOUND freesound_list->clear(); - string path; - path = Glib::get_home_dir(); - path += "/Freesound/"; - Mootcher theMootcher(path.c_str()); - - string name_string = freesound_name_entry.get_text (); - string pass_string = freesound_pass_entry.get_text (); string search_string = freesound_entry.get_text (); + enum sortMethod sort_method = (enum sortMethod) freesound_sort.get_active_row_number(); - if ( theMootcher.doLogin( name_string, pass_string ) ) { + 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(); + for (int page = 1; page <= 99; page++ ) { + + string prog; + prog = string_compose (_("Searching Page %1, click Stop to cancel -->"), page); + freesound_progress_bar.set_text(prog); + while (Glib::MainContext::get_default()->iteration (false)) { + /* do nothing */ + } - string theString = theMootcher.searchText(search_string); + string theString = mootcher->searchText( + search_string, + page, + "", // filter, could do, e.g. "type:wav" + sort_method + ); XMLTree doc; doc.read_buffer( theString ); XMLNode *root = doc.root(); - if (root==NULL) return; - - if ( strcmp(root->name().c_str(), "freesound") == 0) { - - XMLNode *node = 0; - XMLNodeList children = root->children(); - XMLNodeConstIterator niter; - for (niter = children.begin(); niter != children.end() && !canceling; ++niter) { - node = *niter; - if( strcmp( node->name().c_str(), "sample") == 0 ){ - XMLProperty *prop=node->property ("id"); - string filename = theMootcher.getFile( prop->value().c_str() ); - if ( filename != "" ) { - TreeModel::iterator new_row = freesound_list->append(); - TreeModel::Row row = *new_row; - string path = Glib::filename_from_uri (string ("file:") + filename); - row[freesound_list_columns.pathname] = path; - } + if (!root) { + cerr << "no root XML node!" << endl; + break; + } + + if ( strcmp(root->name().c_str(), "response") != 0) { + cerr << "root node name == " << root->name() << ", != \"response\"!" << endl; + break; + } + + XMLNode *sounds_root = root->child("sounds"); + + if (!sounds_root) { + cerr << "no child node \"sounds\" found!" << endl; + break; + } + + XMLNodeList sounds = sounds_root->children(); + XMLNodeConstIterator niter; + XMLNode *node; + for (niter = sounds.begin(); niter != sounds.end(); ++niter) { + node = *niter; + if( strcmp( node->name().c_str(), "resource") != 0 ){ + cerr << "node->name()=" << node->name() << ",!= \"resource\"!" << endl; + freesound_search_cancel = true; + break; + } + + // node->dump(cerr, "node:"); + + XMLNode *id_node = node->child ("id"); + XMLNode *uri_node = node->child ("serve"); + XMLNode *ofn_node = node->child ("original_filename"); + XMLNode *dur_node = node->child ("duration"); + + if (id_node && uri_node && ofn_node) { + + std::string id = id_node->child("text")->content(); + std::string uri = uri_node->child("text")->content(); + std::string ofn = ofn_node->child("text")->content(); + std::string dur = dur_node->child("text")->content(); + + std::string r; + // cerr << "id=" << id << ",uri=" << uri << ",ofn=" << ofn << ",dur=" << dur << endl; + + double duration_seconds = atof(dur.c_str()); + double h, m, s; + char duration_hhmmss[16]; + if (duration_seconds >= 99 * 60 * 60) { + strcpy(duration_hhmmss, ">99h"); + } else { + s = modf(duration_seconds/60, &m) * 60; + m = modf(m/60, &h) * 60; + sprintf(duration_hhmmss, "%02.fh:%02.fm:%04.1fs", + h, m, s + ); } + + TreeModel::iterator new_row = freesound_list->append(); + TreeModel::Row row = *new_row; + + row[freesound_list_columns.id ] = id; + row[freesound_list_columns.uri ] = uri; + row[freesound_list_columns.filename] = ofn; + row[freesound_list_columns.duration] = duration_hhmmss; + } } - } + + if (freesound_search_cancel) + break; + + } //page "for" loop + + gdk_window_set_cursor (get_window()->gobj(), prev_cursor); + + freesound_progress_bar.set_text(""); - searching = false; - canceling = false; - freesound_search_btn.set_label(_("Start Downloading")); #endif } @@ -831,16 +923,30 @@ SoundFileBrowser::get_paths () results.push_back (str); } } else { - +#ifdef FREESOUND typedef TreeView::Selection::ListHandle_Path ListPath; - + 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 str = (*iter)[freesound_list_columns.pathname]; + 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); + if (str != "") { + results.push_back (str); + } - results.push_back (str); + gdk_window_set_cursor (get_window()->gobj(), prev_cursor); + } +#endif } return results; diff --git a/gtk2_ardour/sfdb_ui.h b/gtk2_ardour/sfdb_ui.h index 611214e83c..28c6a7a8ba 100644 --- a/gtk2_ardour/sfdb_ui.h +++ b/gtk2_ardour/sfdb_ui.h @@ -35,10 +35,16 @@ #include <gtkmm/filechooserwidget.h> #include <gtkmm/frame.h> #include <gtkmm/label.h> +#include <gtkmm/table.h> +#include <gtkmm/liststore.h> +#include <gtkmm/textview.h> +#include <gtkmm/spinbutton.h> +#include <gtkmm/notebook.h> #include <ardour/session.h> #include <ardour/audiofilesource.h> +#include "audio_clock.h" #include "ardour_dialog.h" #include "editing.h" @@ -47,6 +53,7 @@ namespace ARDOUR { }; class GainMeter; +class Mootcher; class SoundFileBox : public Gtk::VBox { @@ -102,6 +109,7 @@ class SoundFileBox : public Gtk::VBox void stop_audition (); }; + class SoundFileBrowser : public ArdourDialog { private: @@ -113,12 +121,30 @@ class SoundFileBrowser : public ArdourDialog FoundTagColumns() { add(pathname); } }; + class FreesoundColumns : public Gtk::TreeModel::ColumnRecord + { + public: + Gtk::TreeModelColumn<std::string> id; + Gtk::TreeModelColumn<std::string> uri; + Gtk::TreeModelColumn<std::string> filename; + Gtk::TreeModelColumn<std::string> duration; + + FreesoundColumns() { + add(id); + add(filename); + add(uri); + add(duration); + } + }; + FoundTagColumns found_list_columns; Glib::RefPtr<Gtk::ListStore> found_list; - FoundTagColumns freesound_list_columns; + FreesoundColumns freesound_list_columns; Glib::RefPtr<Gtk::ListStore> freesound_list; + Gtk::Button freesound_stop_btn; + public: SoundFileBrowser (Gtk::Window& parent, std::string title, ARDOUR::Session* _s, bool persistent); virtual ~SoundFileBrowser (); @@ -136,13 +162,21 @@ class SoundFileBrowser : public ArdourDialog Gtk::Button found_search_btn; Gtk::TreeView found_list_view; - Gtk::Entry freesound_name_entry; - Gtk::Entry freesound_pass_entry; Gtk::Entry freesound_entry; Gtk::Button freesound_search_btn; + Gtk::ComboBoxText freesound_sort; + Gtk::SpinButton freesound_page; Gtk::TreeView freesound_list_view; + Gtk::ProgressBar freesound_progress_bar; - void freesound_search_thread(); + bool freesound_search_cancel; + bool freesound_download_cancel; + + void freesound_search(); + +#ifdef FREESOUND + Mootcher *mootcher; +#endif protected: bool resetting_ourselves; @@ -173,6 +207,7 @@ class SoundFileBrowser : public ArdourDialog void freesound_list_view_selected (); void freesound_list_view_activated (const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn*); void freesound_search_clicked (); + void freesound_stop_clicked (); void chooser_file_activated (); |