From 28630efa5e905f501f267e5e105d184ec5dd379a Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 10 Sep 2009 09:34:52 +0000 Subject: Some changes regharding minimizing the memory usage when loading audio files. --- src/audiofile.cc | 14 ++++++++++++-- src/drumgizmo.cc | 26 ++++++++++++++++---------- src/drumkitparser.cc | 2 +- src/saxparser.cc | 1 + src/velocity.cc | 2 ++ 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/audiofile.cc b/src/audiofile.cc index b8b4ef9..a556bca 100644 --- a/src/audiofile.cc +++ b/src/audiofile.cc @@ -63,6 +63,7 @@ void AudioFile::unload() size = 0; } +#define THRESHOLD 0.0001 void AudioFile::load() { if(data) return; @@ -71,15 +72,24 @@ void AudioFile::load() SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info); size = sf_seek(fh, 0, SEEK_END); - data = (jack_default_audio_sample_t*)malloc(sizeof(jack_default_audio_sample_t)*size); + jack_default_audio_sample_t* tmp_data = (jack_default_audio_sample_t*)malloc(sizeof(jack_default_audio_sample_t)*size); // printf("Loading %s, %d samples\n", filename.c_str(), size); sf_seek(fh, 0, SEEK_SET); - sf_read_float(fh, data, size); + sf_read_float(fh, tmp_data, size); // for(size_t i = 0; i < size; i++) data[i] *= 0.1; sf_close(fh); + + // Find real size (crop 'silence') + while(size > 0 && tmp_data[size--] < THRESHOLD) {} + data = (jack_default_audio_sample_t*)malloc(sizeof(jack_default_audio_sample_t)*size); + for(size_t i = 0; i < size; i++) data[i] = tmp_data[i]; + // ramp down + // if(size > 1024) for(size_t i = 1024; i >= 0; i--) data[size - i] *= (float)i/1024.0; + free(tmp_data); + } diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc index cfc8ad5..a4d863f 100644 --- a/src/drumgizmo.cc +++ b/src/drumgizmo.cc @@ -29,6 +29,7 @@ #include "jackclient.h" #include "drumkitparser.h" #include "midiplayer.h" +#include static const char version_str[] = "DrumGizmo v" VERSION "\n" @@ -59,7 +60,7 @@ int main(int argc, char *argv[]) char *midifile = NULL; bool preload = true; - int min_velocity = 18; + int min_velocity = 0;//18; int option_index = 0; while(1) { @@ -107,19 +108,24 @@ int main(int argc, char *argv[]) } } - if(argc < option_index + 2) { + std::string kitfile; + + if(option_index < argc) { + printf("non-option ARGV-elements: "); + while (optind < argc) { + if(kitfile != "") { + fprintf(stderr, "Can only handle a single kitfile.\n"); + printf(usage_str, argv[0]); + return 1; + } + kitfile = argv[optind++]; + } + printf("\n"); + } else { fprintf(stderr, "Missing kitfile.\n"); printf(usage_str, argv[0]); return 1; } - - if(argc > option_index + 2) { - fprintf(stderr, "Can only handle a single kitfile.\n"); - printf(usage_str, argv[0]); - return 1; - } - - std::string kitfile = argv[option_index + 1]; printf("Using kitfile: %s\n", kitfile.c_str()); diff --git a/src/drumkitparser.cc b/src/drumkitparser.cc index ae557dd..e18d262 100644 --- a/src/drumkitparser.cc +++ b/src/drumkitparser.cc @@ -25,7 +25,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "drumkitparser.h" - +#include #define DIR_SEPERATOR '/' DrumKitParser::DrumKitParser(std::string kitfile, bool preload, int min_velocity) diff --git a/src/saxparser.cc b/src/saxparser.cc index 89f426b..19c6142 100644 --- a/src/saxparser.cc +++ b/src/saxparser.cc @@ -25,6 +25,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "saxparser.h" +#include static void character_hndl(void *p, const XML_Char *s, int len) { diff --git a/src/velocity.cc b/src/velocity.cc index 4ff398c..319dc82 100644 --- a/src/velocity.cc +++ b/src/velocity.cc @@ -26,6 +26,8 @@ */ #include "velocity.h" +#include + Velocity::Velocity(unsigned int lower, unsigned int upper) { this->lower = lower; -- cgit v1.2.3