summaryrefslogtreecommitdiff
path: root/src/audiofile.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2016-01-28 14:58:41 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2016-01-28 14:58:41 +0100
commit413c0703722835e48b98b85b8ded32f1f43555bb (patch)
tree9d195d00c020ed96e1a5ad2a10ed8e044d7623a9 /src/audiofile.cc
parent2087facd3727c52069ad65c5b791f24cba3101a2 (diff)
Style fixes
Diffstat (limited to 'src/audiofile.cc')
-rw-r--r--src/audiofile.cc279
1 files changed, 85 insertions, 194 deletions
diff --git a/src/audiofile.cc b/src/audiofile.cc
index 5ceff59..e9b5976 100644
--- a/src/audiofile.cc
+++ b/src/audiofile.cc
@@ -38,225 +38,116 @@
#include "configuration.h"
-AudioFile::AudioFile(std::string filename, int filechannel)
+AudioFile::AudioFile(const std::string& filename, int filechannel)
{
- is_loaded = false;
- this->filename = filename;
- this->filechannel = filechannel;
+ is_loaded = false;
+ this->filename = filename;
+ this->filechannel = filechannel;
- data = NULL;
- size = 0;
+ data = nullptr;
+ size = 0;
-#ifdef LAZYLOAD
- preloaded_data = NULL;
-#endif/*LAZYLOAD*/
-
- magic = this;
+ magic = this;
}
AudioFile::~AudioFile()
{
- magic = NULL;
- unload();
+ magic = nullptr;
+ unload();
}
bool AudioFile::isValid()
{
- return this == magic;
+ return this == magic;
}
void AudioFile::unload()
{
- // Make sure we don't unload the object while loading it...
- MutexAutolock l(mutex);
+ // Make sure we don't unload the object while loading it...
+ MutexAutolock l(mutex);
- is_loaded = false;
+ is_loaded = false;
-#ifdef LAZYLOAD
- if(data == preloaded_data) {
- delete[] data;
- data = NULL;
- size = 0;
- } else {
- size = 0;
- delete[] data;
- data = NULL;
- delete preloaded_data;
- preloaded_data = NULL;
- }
-#else
- delete[] data;
- data = NULL;
- size = 0;
-#endif/*LAZYLOAD*/
+ delete[] data;
+ data = nullptr;
+ size = 0;
}
#define BUFFER_SIZE 4092
void AudioFile::load(int num_samples)
{
- // Make sure we don't unload the object while loading it...
- MutexAutolock l(mutex);
-
- /*
- Lazy load of drum kits
- init();
- return;
- */
-
- if(data) return;
-
- SF_INFO sf_info;
- SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info);
- if(!fh) {
- ERR(audiofile,"SNDFILE Error (%s): %s\n",
- filename.c_str(), sf_strerror(fh));
- return;
- }
-
- if(num_samples == ALL_SAMPLES) {
- num_samples = sf_info.frames;
- }
-
- size = sf_info.frames;
- preloadedsize = sf_info.frames;
-
- if(preloadedsize > (size_t)num_samples) {
- preloadedsize = num_samples;
- }
-
- sample_t* data = new sample_t[preloadedsize];
- if(sf_info.channels == 1) {
- preloadedsize = sf_read_float(fh, data, preloadedsize);
- } else {
- // check filechannel exists
- if(filechannel >= sf_info.channels) {
- filechannel = sf_info.channels - 1;
- }
- sample_t buffer[BUFFER_SIZE];
- int readsize = BUFFER_SIZE / sf_info.channels;
- int totalread = 0;
- int read;
- do {
- read = sf_readf_float(fh, buffer, readsize);
- for (int i = 0; (i < read) && (totalread < num_samples); i++) {
- data[totalread++] = buffer[i * sf_info.channels + filechannel];
- }
- } while( (read > 0) &&
- (totalread < (int)preloadedsize) &&
- (totalread < num_samples) );
- // set data size to total bytes read
- preloadedsize = totalread;
- }
-
- DEBUG(audiofile,"Loaded %d samples %p\n", (int)preloadedsize, this);
-
- sf_close(fh);
-
- this->data = data;
- is_loaded = true;
-
- //DEBUG(audiofile, "Loading of %s completed.\n", filename.c_str());
+ // Make sure we don't unload the object while loading it...
+ MutexAutolock l(mutex);
+
+ if(data)
+ {
+ return;
+ }
+
+ SF_INFO sf_info;
+ SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info);
+ if(!fh)
+ {
+ ERR(audiofile,"SNDFILE Error (%s): %s\n",
+ filename.c_str(), sf_strerror(fh));
+ return;
+ }
+
+ if(num_samples == ALL_SAMPLES)
+ {
+ num_samples = sf_info.frames;
+ }
+
+ size = sf_info.frames;
+ preloadedsize = sf_info.frames;
+
+ if(preloadedsize > (size_t)num_samples)
+ {
+ preloadedsize = num_samples;
+ }
+
+ sample_t* data = new sample_t[preloadedsize];
+ if(sf_info.channels == 1)
+ {
+ preloadedsize = sf_read_float(fh, data, preloadedsize);
+ }
+ else
+ {
+ // check filechannel exists
+ if(filechannel >= sf_info.channels)
+ {
+ filechannel = sf_info.channels - 1;
+ }
+
+ sample_t buffer[BUFFER_SIZE];
+ int readsize = BUFFER_SIZE / sf_info.channels;
+ int totalread = 0;
+ int read;
+
+ do
+ {
+ read = sf_readf_float(fh, buffer, readsize);
+ for(int i = 0; (i < read) && (totalread < num_samples); ++i)
+ {
+ data[totalread++] = buffer[i * sf_info.channels + filechannel];
+ }
+ }
+ while( (read > 0) &&
+ (totalread < (int)preloadedsize) &&
+ (totalread < num_samples) );
+
+ // set data size to total bytes read
+ preloadedsize = totalread;
+ }
+
+ sf_close(fh);
+
+ this->data = data;
+ is_loaded = true;
}
bool AudioFile::isLoaded()
{
- return is_loaded;
-}
-
-#ifdef LAZYLOAD
-#define SIZE 512*4
-void AudioFile::init()
-{
- //DEBUG(audiofile,"Initializing %p\n", this);
- if(data) {
- //DEBUG(audiofile,"\t already initialized\n");
- return;
- }
-
- SF_INFO sf_info;
- SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info);
- if(!fh) {
- ERR(audiofile,"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);
-
- //DEBUG(audiofile,"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;
- mutex.unlock();
-}
-
-void AudioFile::loadNext()
-{
- if(this->data != this->preloaded_data) {
- //DEBUG(audiofile,"Already completely loaded %p\n", this);
- return;
- }
-
- SF_INFO sf_info;
- SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info);
- if(!fh) {
- ERR(audiofile,"SNDFILE Error (%s): %s\n",
- filename.c_str(), sf_strerror(fh));
- return;
- }
-
- int r;
-// int size_accum = 0;
- sample_t* data = new sample_t[sf_info.frames];
- memcpy(data, this->preloaded_data, this->size * sizeof(sample_t));
- this->data = data;
- sf_seek(fh, this->size, SEEK_SET);
-// sample_t* data_buf = new sample_t[SIZE];
- while(this->size < sf_info.frames) {
- //DEBUG(audiofile,"Accumulated %d of %llu\n", size_accum, sf_info.frames);
- //if( (r = sf_read_float(fh, data_buf, SIZE)) < 0) {
- if( (r = sf_read_float(fh, &data[this->size], SIZE)) < 0) {
- ERR(audiofile,"Error reading sound file\n");
- break;
- }
- //size_accum += r;
- //memcpy(data+size_accum, data_buf, sizeof(sample_t) * r);
- this->size += r;
- }
- //delete data_buf;
-
- //DEBUG(audiofile,"Finished loading %d samples %p\n", size, this);
- sf_close(fh);
-
- //mutex.lock();
- //this->data = data;
- //this->size = size;
- //mutex.unlock();
-}
-
-void AudioFile::reset()
-{
- //DEBUG(audiofile,"Resetting audio file %p\n", this);
- if(this->data == this->preloaded_data) {
- //DEBUG(audiofile,"\tNot completely loaded - skipping %p\n", this);
- return;
- }
-
- mutex.lock();
- volatile sample_t* old_data = data;
- this->size = SIZE;
- this->data = this->preloaded_data;
- //DEBUG(audiofile,"Deleting data %p\n", this);
- delete old_data;
- mutex.unlock();
+ return is_loaded;
}
-#endif