summaryrefslogtreecommitdiff
path: root/src/audiofile.cc
diff options
context:
space:
mode:
authordeva <deva>2009-09-10 09:34:52 +0000
committerdeva <deva>2009-09-10 09:34:52 +0000
commit28630efa5e905f501f267e5e105d184ec5dd379a (patch)
tree0e832d4ec5d3dd15a8c5107d116f742e43ed85a3 /src/audiofile.cc
parent2b9c45b9bb1c70908c3e753425247d8eeb38afdf (diff)
Some changes regharding minimizing the memory usage when loading audio files.
Diffstat (limited to 'src/audiofile.cc')
-rw-r--r--src/audiofile.cc14
1 files changed, 12 insertions, 2 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);
+
}