summaryrefslogtreecommitdiff
path: root/src/cachemanager.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2015-04-12 20:24:16 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2015-04-12 20:24:16 +0200
commitd4b217d3c2902bab3daf438714bcb7cb3ccbca51 (patch)
tree79ddd7f4599f84bcdfa647e137d4385a1a2fa637 /src/cachemanager.cc
parentfcfa4676a2be89fff6ea55ff13c5e3e73ba59a74 (diff)
Fix file loading.
Diffstat (limited to 'src/cachemanager.cc')
-rw-r--r--src/cachemanager.cc26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/cachemanager.cc b/src/cachemanager.cc
index 51323d1..3984447 100644
--- a/src/cachemanager.cc
+++ b/src/cachemanager.cc
@@ -33,13 +33,12 @@
#include <hugin.hpp>
-#define FRAMESIZE 256
-#define CHUNKSIZE FRAMESIZE*100
-
static sample_t nodata[FRAMESIZE];
-#define BUFFER_SIZE FRAMESIZE
-static size_t readChunk(std::string filename, int filechannel, size_t from, size_t num_samples, sample_t* buf)
+#define BUFFER_SIZE 4092
+
+static size_t readChunk(std::string filename, int filechannel, size_t pos,
+ size_t num_samples, sample_t* buf)
{
SF_INFO sf_info;
SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info);
@@ -49,14 +48,17 @@ static size_t readChunk(std::string filename, int filechannel, size_t from, size
return 0;
}
- sf_seek(fh, from, SEEK_SET);
+ if(pos > sf_info.frames) return 0;
+
+ sf_seek(fh, pos, SEEK_SET);
- size_t size = num_samples;
- sample_t* data = buf;
+ size_t size = sf_info.frames - pos;
+ if(size > num_samples) size = num_samples;
+
+ sample_t* data = buf;
if(sf_info.channels == 1) {
size = sf_read_float(fh, data, size);
- }
- else {
+ } else {
// check filechannel exists
if(filechannel >= sf_info.channels) {
filechannel = sf_info.channels - 1;
@@ -67,10 +69,10 @@ static size_t readChunk(std::string filename, int filechannel, size_t from, size
int read;
do {
read = sf_readf_float(fh, buffer, readsize);
- for (int i = 0; i < read; i++) {
+ for (int i = 0; i < read && totalread < (int)size; i++) {
data[totalread++] = buffer[i * sf_info.channels + filechannel];
}
- } while(read > 0 && totalread < (int)size);
+ } while(read > 0 && totalread < (int)size && totalread < sf_info.frames);
// set data size to total bytes read
size = totalread;
}