13
0

Freesound tweaks from colinf (#4761).

git-svn-id: svn://localhost/ardour2/branches/3.0@11636 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Carl Hetherington 2012-03-09 22:02:48 +00:00
parent c25b921888
commit c23a34a5a5
4 changed files with 75 additions and 52 deletions

View File

@ -54,10 +54,12 @@ static const std::string api_key = "9d77cb8d841b4bcfa960e1aae62224eb"; // ardour
//------------------------------------------------------------------------
Mootcher::Mootcher(const char *saveLocation)
Mootcher::Mootcher()
: curl(curl_easy_init())
{
changeWorkingDir(saveLocation);
std::string path;
path = Glib::get_home_dir() + "/Freesound/";
changeWorkingDir ( path.c_str() );
};
//------------------------------------------------------------------------
Mootcher:: ~Mootcher()
@ -111,6 +113,8 @@ size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void
//------------------------------------------------------------------------
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";
@ -300,7 +304,7 @@ std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID,
ensureWorkingDir();
std::string audioFileName = basePath + "snd/" + ID + "-" + originalFileName;
//check to see if audio file already exists
// check to see if audio file already exists
FILE *testFile = fopen(audioFileName.c_str(), "r");
if (testFile) {
fseek (testFile , 0 , SEEK_END);
@ -315,47 +319,49 @@ std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID,
remove( audioFileName.c_str() );
}
if (!curl) {
return "";
}
//now download the actual file
if (curl) {
FILE* theFile;
theFile = fopen( audioFileName.c_str(), "wb" );
FILE* theFile;
theFile = fopen( audioFileName.c_str(), "wb" );
if (!theFile) {
return "";
}
// create the download url
audioURL += "?api_key=" + api_key;
if (theFile) {
// 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);
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->progress_bar.hide();
caller->progress_bar.show();
std::cerr << "downloading " << audioFileName << " from " << audioURL << "..." << std::endl;
/* hack to get rid of the barber-pole stripes */
caller->progress_bar.hide();
caller->progress_bar.show();
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_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);
CURLcode res = curl_easy_perform(curl);
fclose(theFile);
curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1); // turn off the progress bar
caller->progress_bar.set_fraction(0.0);
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);
}
}
curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1); // turn off the progress bar
caller->progress_bar.set_fraction(0.0);
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;

View File

@ -49,7 +49,7 @@ enum sortMethod {
class Mootcher
{
public:
Mootcher(const char *saveLocation);
Mootcher();
~Mootcher();
std::string getAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller);

View File

@ -177,6 +177,7 @@ SoundFileBox::SoundFileBox (bool persistent)
main_box.pack_start (table, false, false);
tags_entry.set_editable (true);
tags_entry.set_wrap_mode(Gtk::WRAP_WORD);
tags_entry.signal_focus_out_event().connect (sigc::mem_fun (*this, &SoundFileBox::tags_entry_left));
Label* label = manage (new Label (_("Tags:")));
@ -562,6 +563,9 @@ SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::S
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);
@ -737,10 +741,7 @@ SoundFileBrowser::freesound_list_view_selected ()
set_response_sensitive (RESPONSE_OK, false);
} else {
string path;
path = Glib::get_home_dir();
path += "/Freesound/";
Mootcher theMootcher(path.c_str()); // XXX should be a member of SoundFileBrowser
Mootcher theMootcher; // XXX should be a member of SoundFileBrowser
string file;
@ -820,10 +821,7 @@ SoundFileBrowser::freesound_search()
#ifdef FREESOUND
freesound_list->clear();
string path;
path = Glib::get_home_dir();
path += "/Freesound/";
Mootcher theMootcher(path.c_str());
Mootcher theMootcher;
string search_string = freesound_entry.get_text ();
enum sortMethod sort_method = (enum sortMethod) freesound_sort.get_active_row_number();
@ -879,15 +877,30 @@ SoundFileBrowser::freesound_search()
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 << endl;
// 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;
@ -895,6 +908,7 @@ SoundFileBrowser::freesound_search()
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;
}
}
@ -934,10 +948,7 @@ SoundFileBrowser::get_paths ()
#ifdef FREESOUND
typedef TreeView::Selection::ListHandle_Path ListPath;
string path;
path = Glib::get_home_dir();
path += "/Freesound/";
Mootcher theMootcher(path.c_str()); // XXX should be a member of SoundFileBrowser
Mootcher theMootcher; // XXX should be a member of SoundFileBrowser
ListPath rows = freesound_list_view.get_selection()->get_selected_rows ();

View File

@ -126,8 +126,14 @@ class SoundFileBrowser : public ArdourDialog
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); }
FreesoundColumns() {
add(id);
add(filename);
add(uri);
add(duration);
}
};
FoundTagColumns found_list_columns;