summaryrefslogtreecommitdiff
path: root/src/audiofile.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/audiofile.cc')
-rw-r--r--src/audiofile.cc120
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);