From cb3f289fa64a93f9bb6ef653cad2c3b5ea5d15a4 Mon Sep 17 00:00:00 2001 From: Mad Mike <3929826-fluential@users.noreply.gitlab.com> Date: Tue, 12 Oct 2021 21:13:22 +0200 Subject: [PATCH] Add feature Pull Requests. #38 #34. https://github.com/spotify/echoprint-codegen/pull/38 https://github.com/spotify/echoprint-codegen/pull/34 --- src/AudioStreamInput.h | 4 +- src/functions.cxx | 3 + src/main.cxx | 151 +++++++++++++++++++++-------------------- 3 files changed, 81 insertions(+), 77 deletions(-) diff --git a/src/AudioStreamInput.h b/src/AudioStreamInput.h index 47b9227..b2c9086 100644 --- a/src/AudioStreamInput.h +++ b/src/AudioStreamInput.h @@ -62,10 +62,10 @@ class FfmpegStreamInput : public AudioStreamInput { // TODO: Windows char message[4096] = {0}; if (_Offset_s == 0 && _Seconds == 0) - snprintf(message, NELEM(message), "ffmpeg -i \"%s\" -ac %d -ar %d -f s16le - 2>%s", + snprintf(message, NELEM(message), "ffmpeg -i '%s' -ac %d -ar %d -f s16le - 2>%s", filename, Params::AudioStreamInput::Channels, (uint) Params::AudioStreamInput::SamplingRate, DEVNULL); else - snprintf(message, NELEM(message), "ffmpeg -i \"%s\" -ac %d -ar %d -f s16le -t %d -ss %d - 2>%s", + snprintf(message, NELEM(message), "ffmpeg -i '%s' -ac %d -ar %d -f s16le -t %d -ss %d - 2>%s", filename, Params::AudioStreamInput::Channels, (uint) Params::AudioStreamInput::SamplingRate, _Seconds, _Offset_s, DEVNULL); return std::string(message); diff --git a/src/functions.cxx b/src/functions.cxx index 48fd1ed..a82aa2e 100644 --- a/src/functions.cxx +++ b/src/functions.cxx @@ -53,12 +53,14 @@ codegen_response_t *codegen_file(char* filename, int start_offset, int duration, codegen_response_t *response = (codegen_response_t *)malloc(sizeof(codegen_response_t)); response->error = NULL; response->codegen = NULL; + fprintf(stderr,"Processing \t%s\n", escape(filename).c_str()); auto_ptr pAudio(new FfmpegStreamInput()); pAudio->ProcessFile(filename, start_offset, duration); if (pAudio.get() == NULL) { // Unable to decode! char* output = (char*) malloc(16384); + fprintf(stderr,"{\"error\":\"could not create decoder\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}\n", tag, escape(filename).c_str()); sprintf(output,"{\"error\":\"could not create decoder\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}", tag, escape(filename).c_str()); @@ -70,6 +72,7 @@ codegen_response_t *codegen_file(char* filename, int start_offset, int duration, if (numSamples < 1) { char* output = (char*) malloc(16384); + fprintf(stderr,"{\"error\":\"could not decode\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}\n", tag, escape(filename).c_str()); sprintf(output,"{\"error\":\"could not decode\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}", tag, escape(filename).c_str()); diff --git a/src/main.cxx b/src/main.cxx index 7d2dc7e..4d27fe0 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -157,92 +157,93 @@ int main(int argc, char** argv) { if(count == 0) throw std::runtime_error("No files given.\n"); - #ifdef _WIN32 // Threading doesn't work in windows yet. - for(int i=0;icodegen) { - delete response->codegen; - } - free(response); - free(output); - } - return 0; - + bool use_threads = false; #else + bool use_threads = (count > 1); +#endif - // Figure out how many threads to use based on # of cores - int num_threads = getNumCores(); - if (num_threads > 8) num_threads = 8; - if (num_threads < 2) num_threads = 2; - if (num_threads > count) num_threads = count; - - // Setup threading - pthread_t *t = (pthread_t*)malloc(sizeof(pthread_t)*num_threads); - thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads); - pthread_attr_t *attr = (pthread_attr_t*)malloc(sizeof(pthread_attr_t)*num_threads); - - // Kick off the first N threads - int still_left = count-1-already; - for(int i=0;ifilename = (char*)files[still_left].c_str(); - parm[i]->start_offset = start_offset; - parm[i]->tag = still_left; - parm[i]->duration = duration; - parm[i]->done = 0; - still_left--; - pthread_attr_init(&attr[i]); - pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_DETACHED); - // Kick off the thread - if (pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i])) - throw std::runtime_error("Problem creating thread\n"); - } - - int done = 0; - // Now wait for the threads to come back, and also kick off new ones - while(donecodegen) { + delete response->codegen; + } + free(response); + free(output); + } + return 0; + } else { + // Figure out how many threads to use based on # of cores + int num_threads = getNumCores(); + if (num_threads > 8) num_threads = 8; + if (num_threads < 2) num_threads = 2; + if (num_threads > count) num_threads = count; + + // Setup threading + pthread_t *t = (pthread_t*)malloc(sizeof(pthread_t)*num_threads); + thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads); + pthread_attr_t *attr = (pthread_attr_t*)malloc(sizeof(pthread_attr_t)*num_threads); + + // Kick off the first N threads + int still_left = count-1-already; for(int i=0;idone) { - parm[i]->done = 0; - done++; - codegen_response_t *response = (codegen_response_t*)parm[i]->response; - char *json = make_json_string(response, human_readable_code); - print_json_to_screen(json, count, done); - if (response->codegen) { - delete response->codegen; - } - free(parm[i]->response); - free(json); - // More to do? Start a new one on this just finished thread - if(still_left >= 0) { - parm[i]->tag = still_left; - parm[i]->filename = (char*)files[still_left].c_str(); - still_left--; - int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]); - if(err) - throw std::runtime_error("Problem creating thread\n"); + parm[i] = (thread_parm_t *)malloc(sizeof(thread_parm_t)); + parm[i]->filename = (char*)files[still_left].c_str(); + parm[i]->start_offset = start_offset; + parm[i]->tag = still_left; + parm[i]->duration = duration; + parm[i]->done = 0; + still_left--; + pthread_attr_init(&attr[i]); + pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_DETACHED); + // Kick off the thread + if (pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i])) + throw std::runtime_error("Problem creating thread\n"); + } + int done = 0; + // Now wait for the threads to come back, and also kick off new ones + while(donedone) { + parm[i]->done = 0; + done++; + codegen_response_t *response = (codegen_response_t*)parm[i]->response; + char *json = make_json_string(response,human_readable_code); + print_json_to_screen(json, count, done); + if (response->codegen) { + delete response->codegen; + } + free(parm[i]->response); + free(json); + // More to do? Start a new one on this just finished thread + if(still_left >= 0) { + parm[i]->tag = still_left; + parm[i]->filename = (char*)files[still_left].c_str(); + still_left--; + int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]); + if(err) + throw std::runtime_error("Problem creating thread\n"); + + } } } } - } - // Clean up threads - for(int i=0;i