diff options
author | - <nemo@alice.(none)> | 2013-05-09 17:54:45 +0200 |
---|---|---|
committer | - <nemo@alice.(none)> | 2013-05-09 17:54:45 +0200 |
commit | 774815a97901fe9d5ce2767a8c479c52a5ad0ce8 (patch) | |
tree | 325793f2c2f76d2efe3af3d980005d074af035f8 /src/audiofile.cc | |
parent | a4d73c0cb2321bd61bd31d2922ae469389e5825e (diff) |
Lazy load of audio. Unloads when there's nothing else to do.
Diffstat (limited to 'src/audiofile.cc')
-rw-r--r-- | src/audiofile.cc | 120 |
1 files changed, 116 insertions, 4 deletions
diff --git a/src/audiofile.cc b/src/audiofile.cc index 5a9cdb5..c9641dc 100644 --- a/src/audiofile.cc +++ b/src/audiofile.cc @@ -33,15 +33,23 @@ #include <hugin.hpp> +#define LAZYLOAD + AudioFile::AudioFile(std::string filename) { is_loaded = false; //printf("new AudioFile %p\n", this); this->filename = filename; + locked = false; data = NULL; size = 0; - +//#ifdef LAZY_LOAD + fh = NULL; + preloaded_data = NULL; + completely_loaded = false; +//#endif/*LAZYLOAD*/ + ref_count = 0; magic = this; //load(); @@ -61,15 +69,116 @@ bool AudioFile::isValid() void AudioFile::unload() { - if(data) { + if(data == preloaded_data) { delete data; data = NULL; size = 0; } + else { + size = 0; + delete data; + data = NULL; + delete preloaded_data; + preloaded_data = NULL; + } + sf_close(this->fh); +} + +#define SIZE 512*4 +void AudioFile::init() { + printf("Initializing %p\n", this); + if(data) { + printf("\t already initialized\n"); + return; + } + + SF_INFO sf_info; + SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info); + if(!fh) { + printf("SNDFILE Error (%s): %s\n", filename.c_str(), sf_strerror(fh)); + return; + } + + int size = SIZE; + + sample_t* data = new sample_t[size]; + + size = sf_read_float(fh, data, size); + + printf("Lazy loaded %d samples\n", size); + sf_close(fh); + + mutex.lock(); + this->data = data; + this->size = size; + this->preloaded_data = data; + this->is_loaded = true; + this->fh = fh; +// if(sf_info.frames <= size) { +// printf("Sample completely loaded\n"); +// completely_loaded = true; +// } + mutex.unlock(); +} + +void AudioFile::loadNext() +{ + if(this->data != this->preloaded_data) { + printf("Already completely loaded %p\n", this); + return; + } + + SF_INFO sf_info; + SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info); +// SF_INFO sf_info = this->sf_info; +// SNDFILE *fh = this->fh; + if(!fh) { + printf("SNDFILE Error (%s): %s\n", filename.c_str(), sf_strerror(fh)); + return; + } + +// sf_seek(fh, 0, SEEK_SET) ; + + int size = sf_info.frames; + + sample_t* data = new sample_t[size]; + + size = sf_read_float(fh, data, size); + + printf("Finished loading %d samples %p\n", size, this); + sf_close(fh); + + mutex.lock(); + this->data = data; + this->size = size; + mutex.unlock(); +} + +void AudioFile::reset() { + printf("Resetting audio file %p\n", this); + if(this->data == this->preloaded_data) { + printf("\tNot completely loaded - skipping %p\n", this); + return; + } + + mutex.lock(); + sample_t* old_data = data; + this->size = SIZE; + this->data = this->preloaded_data; +// if() { + printf("Deleting data %p\n", this); + delete old_data; +// } + mutex.unlock(); } void AudioFile::load() { +#ifdef LAZYLOAD + init(); + return; +#endif + if(data) return; SF_INFO sf_info; @@ -78,11 +187,14 @@ void AudioFile::load() printf("SNDFILE Error (%s): %s\n", filename.c_str(), sf_strerror(fh)); return; } - + size = sf_info.frames; + data = new sample_t[size]; - sf_read_float(fh, data, size); + size = sf_read_float(fh, data, size); + + printf("Loaded %d samples %p\n", size, this); sf_close(fh); |