47 #include <sys/types.h>
51 #include <glib/gstdio.h>
62 static const std::string
base_url =
"http://www.freesound.org/api";
63 static const std::string
api_key =
"9d77cb8d841b4bcfa960e1aae62224eb";
67 : curl(curl_easy_init())
79 curl_easy_cleanup(
curl);
88 if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
89 if (g_mkdir_with_parents (p.c_str(), 0775) != 0) {
94 #ifdef PLATFORM_WINDOWS
95 std::string replace =
"/";
97 while( pos != std::string::npos ){
108 int realsize = (int)(size * nmemb);
114 memcpy(&(mem->
memory[mem->
size]), ptr, realsize);
115 mem->
size += realsize;
146 curl_global_init(CURL_GLOBAL_ALL);
148 curl_easy_setopt(
curl, CURLOPT_USERAGENT,
"libcurl-agent/1.0");
152 curl_easy_setopt(
curl, CURLOPT_FOLLOWLOCATION, 1);
155 curl_easy_setopt(
curl, CURLOPT_NOSIGNAL, 1);
156 curl_easy_setopt(
curl, CURLOPT_CONNECTTIMEOUT, 30);
170 curl_easy_setopt(
curl, CURLOPT_WRITEDATA, (
void *) &xml_page);
177 std::string url =
base_url + uri +
"?";
179 url += params +
"&api_key=" +
api_key +
"&format=xml";
181 url +=
"api_key=" +
api_key +
"&format=xml";
184 curl_easy_setopt(
curl, CURLOPT_URL, url.c_str() );
187 CURLcode res = curl_easy_perform(
curl);
208 std::string params =
"";
210 params +=
"&fields=id,original_filename,duration,filesize,samplerate,license,serve";
211 params +=
"&num_results=100";
213 return doRequest(
"/sounds/" +
id +
"/similar", params);
220 std::string params =
"";
224 snprintf(buf, 23,
"p=%d&", page);
228 char *eq = curl_easy_escape(
curl, query.c_str(), query.length());
229 params +=
"q=\"" + std::string(eq) +
"\"";
233 char *ef = curl_easy_escape(
curl, filter.c_str(), filter.length());
234 params +=
"&f=" + std::string(ef);
241 params +=
"&fields=id,original_filename,duration,filesize,samplerate,license,serve";
242 params +=
"&sounds_per_page=100";
244 return doRequest(
"/sounds/search", params);
252 std::string originalSoundURI;
265 if (freesound == NULL) {
266 error <<
_(
"getSoundResourceFile: There is no valid root in the xml file") <<
endmsg;
270 if (strcmp(doc.
root()->
name().c_str(),
"response") != 0) {
287 std::vector<std::string> strings;
288 for (niter = children.begin(); niter != children.end(); ++niter) {
290 if( strcmp( node->
name().c_str(),
"resource") == 0 ) {
294 strings.push_back(text->
content());
308 return (
int)fwrite(buffer, size, nmemb, (FILE*) file);
317 res = curl_easy_perform (
curl);
319 curl_easy_setopt (
curl, CURLOPT_NOPROGRESS, 1);
321 if (res != CURLE_OK) {
323 if (res != CURLE_ABORTED_BY_CALLBACK) {
371 fseek (testFile , 0 , SEEK_END);
372 if (ftell (testFile) > 256) {
402 audioURL +=
"?api_key=" +
api_key;
405 curl_easy_setopt(
curl, CURLOPT_URL, audioURL.c_str() );
407 curl_easy_setopt(
curl, CURLOPT_WRITEDATA,
theFile);
413 Gtk::VBox *freesound_vbox =
dynamic_cast<Gtk::VBox *
> (caller->
notebook.get_nth_page(2));
419 curl_easy_setopt (
curl, CURLOPT_NOPROGRESS, 0);
421 curl_easy_setopt (
curl, CURLOPT_PROGRESSDATA,
this);
425 pthread_t freesound_download_thread;
437 double fraction = dlnow / dltotal;
439 if (fraction > 1.0) {
441 }
else if (fraction < 0.0) {
461 thisMootcher->
Progress(dlnow, dltotal);
void refresh_display(std::string ID, std::string file)
std::string doRequest(std::string uri, std::string params)
const std::string & content() const
PBD::Signal2< void, double, double > Progress
std::string searchSimilar(std::string id)
static const std::string base_url
const std::string & name() const
LIBPBD_API Transmitter error
bool fetchAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller)
const XMLNodeList & children(const std::string &str=std::string()) const
PBD::Signal0< void > Finished
std::ostream & endmsg(std::ostream &ostr)
bool read_buffer(const std::string &)
static const std::string api_key
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
Gtk::ProgressBar progress_bar
int audioFileWrite(void *buffer, size_t size, size_t nmemb, void *file)
std::list< XMLNode * > XMLNodeList
std::string searchText(std::string query, int page, std::string filter, enum sortMethod sort)
char errorBuffer[CURL_ERROR_SIZE]
LIBARDOUR_API RCConfiguration * Config
static int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
static void * freesound_download_thread_func(void *arg)
Gtk::Button cancel_download_btn
void updateProgress(double dlnow, double dltotal)
void set_tags(std::string member, std::vector< std::string > tags)
LIBARDOUR_API AudioLibrary * Library
XMLNode * child(const char *) const
std::string audioFileName
std::string getSoundResourceFile(std::string ID)
std::string sortMethodString(enum sortMethod sort)
XMLNodeList::const_iterator XMLNodeConstIterator
bool checkAudioFile(std::string originalFileName, std::string ID)
LIBPBD_API int pthread_create_and_store(std::string name, pthread_t *thread, void *(*start_routine)(void *), void *arg)
std::string string_compose(const std::string &fmt, const T1 &o1)