From 9c52ab4a6688782d0db723bd411db745fed08087 Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Sat, 11 Apr 2015 19:11:31 +0200 Subject: ADded local cache. --- src/cachemanager.cc | 54 ++++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/src/cachemanager.cc b/src/cachemanager.cc index d047d66..9288ed1 100644 --- a/src/cachemanager.cc +++ b/src/cachemanager.cc @@ -81,6 +81,7 @@ void CacheManager::deinit() } // Invariant: initial_samples_needed < preloaded audio data +// Proposal: preloaded > 2 x CHUNKSIZE? So that we can fill c.front immediatly on open sample_t *CacheManager::open(AudioFile *file, size_t initial_samples_needed, int channel, cacheid_t &id) { { @@ -97,13 +98,12 @@ sample_t *CacheManager::open(AudioFile *file, size_t initial_samples_needed, int return nodata; } - cache_t c; c.file = file; c.channel = channel; c.pos = initial_samples_needed; - c.front = file->data; - c.back = nodata; + c.front = file->data + initial_samples_needed; + c.back = file->data; // Allocate buffers // Increase audio ref count @@ -116,11 +116,11 @@ sample_t *CacheManager::open(AudioFile *file, size_t initial_samples_needed, int localcache[id] = c.front; if(initial_samples_needed < file->size) { - event_t e = createLoadNextEvent(id, c.pos, LOADNEXT); + event_t e = createLoadNextEvent(id, c.pos, &c.back); pushEvent(e); } - return c.front; + return file->data; // preloaded data } void CacheManager::close(cacheid_t id) @@ -141,11 +141,16 @@ void CacheManager::close(cacheid_t id) // Decrement audiofile ref count } -const CacheManager::cache_t CacheManager::getNextCache(cacheid_t id) +CacheManager::cache_t CacheManager::getNextCache(cacheid_t id) { MutexAutolock l(m_caches); cache_t c = id2cache[id]; + + sample_t *tmp = id2cache[id].front; + id2cache[id].front = c.back; + id2cache[id].back = tmp; id2cache[id].pos += CHUNKSIZE; + return c; } @@ -157,22 +162,18 @@ sample_t *CacheManager::next(cacheid_t id, size_t &size) return nodata; } - size_t localpos = localcachepos[id]; - sample_t* localbuf = localcache[id]; - if(localpos < CHUNKSIZE) { - localcachepos[id] += FRAMESIZE; - return localbuf + FRAMESIZE; + if(localcachepos[id] < CHUNKSIZE) { + localcachepos[id] += size; + return localcache[id] + localcachepos[id]; } - localcachepos[id] = 0; - localcache[id] = NULL; - - const cache_t c = getNextCache(id); + cache_t c = getNextCache(id); + localcachepos[id] = 0; localcache[id] = c.front; if(c.pos < c.file->size) { - event_t e = createLoadNextEvent(id, c.pos + CHUNKSIZE, LOADNEXT); + event_t e = createLoadNextEvent(id, c.pos + CHUNKSIZE, &c.back); pushEvent(e); } @@ -181,18 +182,10 @@ sample_t *CacheManager::next(cacheid_t id, size_t &size) // return s; } -void CacheManager::loadNext(cacheid_t id) +void CacheManager::loadNext(event_t &e) { - m_caches.lock(); - cache_t c = id2cache[id]; - sample_t *tmp = c.front; - c.front = c.back; - c.back = tmp; - id2cache[id] = c; - m_caches.unlock(); - - // do work - tmp = tmp + CHUNKSIZE; + cache_t c = id2cache[e.id]; + *e.fillbuffer = c.file->data + e.pos; } void CacheManager::thread_main() @@ -211,7 +204,7 @@ void CacheManager::thread_main() switch(e.cmd) { case LOADNEXT: - loadNext(e.id); + loadNext(e); break; // case CLEAN: // break; @@ -233,12 +226,13 @@ void CacheManager::pushEvent(event_t e) sem.post(); } -CacheManager::event_t CacheManager::createLoadNextEvent(cacheid_t id, size_t pos, cmd_t cmd) +CacheManager::event_t CacheManager::createLoadNextEvent(cacheid_t id, size_t pos, sample_t** fillbuffer) { event_t e; e.active = true; e.id = id; - e.cmd = cmd; + e.cmd = LOADNEXT; e.pos = pos; + e.fillbuffer = fillbuffer; return e; } -- cgit v1.2.3