summaryrefslogtreecommitdiff
path: root/src/cachemanager.cc
blob: d047d66f3dd47931c2694020de0ff4ced84809a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            cachemanager.cc
 *
 *  Fri Apr 10 10:39:24 CEST 2015
 *  Copyright 2015 Jonas Suhr Christensen 
 *  jsc@umbraculum.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "cachemanager.h"

#include <stdio.h>

#define FRAMESIZE 256
#define CHUNKSIZE FRAMESIZE*8

sample_t nodata[FRAMESIZE];

static std::vector<size_t> localcachepos;
static std::vector<sample_t*> localcache;

CacheManager::CacheManager()
{

}

CacheManager::~CacheManager()
{
  deinit();
}

void CacheManager::init(size_t poolsize)
{
  for(size_t i = 0; i < FRAMESIZE; i++) {
    nodata[i] = 0;
  }

  id2cache.resize(poolsize);
  for(size_t i = 0; i < poolsize; i++) {
    availableids.push_back(i);
  }

  localcachepos.resize(poolsize);
  for(size_t i = 0; i < poolsize; i++) {
    localcachepos[i] = 0;
  }

  localcache.resize(poolsize);
  for(size_t i = 0; i < poolsize; i++) {
    localcache[i] = NULL;
  } 
  
  running = true;
  run();

  // TODO: Add semaphore
}

void CacheManager::deinit()
{
  if(!running) return;
  running = false;
  wait_stop();
}

// Invariant: initial_samples_needed < preloaded audio data 
sample_t *CacheManager::open(AudioFile *file, size_t initial_samples_needed, int channel, cacheid_t &id) 
{
  {
    MutexAutolock l(m_ids);
    if(availableids.empty()) {
      id = CACHE_DUMMYID;
    } else {
      id = availableids.front();
      availableids.pop_front();
    }
  }

  if(id == CACHE_DUMMYID) {
    return nodata;
  }


  cache_t c;
  c.file = file;
  c.channel = channel;
  c.pos = initial_samples_needed;
  c.front = file->data;
  c.back = nodata;
  // Allocate buffers
  // Increase audio ref count

  {
    MutexAutolock l(m_ids);
    id2cache[id] = c;
  }

  localcachepos[id] = 0;
  localcache[id] = c.front;

  if(initial_samples_needed < file->size) {
    event_t e = createLoadNextEvent(id, c.pos, LOADNEXT);
    pushEvent(e);
  }

  return c.front;
}

void CacheManager::close(cacheid_t id)
{
  if(id == CACHE_DUMMYID) return;

  {
//    event_t e = createEvent(id, CLEAN);
//    MutexAutolock l(m_events); 
//    eventqueue.push_front(e);
  }
  
  {
    MutexAutolock l(m_ids);
    availableids.push_back(id);
  }
  // Clean cache_t mapped to event
  // Decrement audiofile ref count
}

const CacheManager::cache_t CacheManager::getNextCache(cacheid_t id)
{
  MutexAutolock l(m_caches);
  cache_t c = id2cache[id];
  id2cache[id].pos += CHUNKSIZE;
  return c;
}

sample_t *CacheManager::next(cacheid_t id, size_t &size) 
{
  size = FRAMESIZE;

  if(id == CACHE_DUMMYID) {
    return nodata;
  }

  size_t localpos = localcachepos[id];
  sample_t* localbuf = localcache[id];
  if(localpos < CHUNKSIZE) {
    localcachepos[id] += FRAMESIZE;
    return localbuf + FRAMESIZE;
  }

  localcachepos[id] = 0;
  localcache[id] = NULL;

  const cache_t c = getNextCache(id);
  
  localcache[id] = c.front;

  if(c.pos < c.file->size) {
    event_t e = createLoadNextEvent(id, c.pos + CHUNKSIZE, LOADNEXT); 
    pushEvent(e);
  } 

//  sample_t *s = c.file->data + (c.pos - size);
  return c.front;
//  return s;
}

void CacheManager::loadNext(cacheid_t id) 
{
  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;
}

void CacheManager::thread_main()
{
  while(running) {
    sem.wait();

    m_events.lock();
    if(!eventqueue.empty()) {
      event_t e = eventqueue.front();
      eventqueue.pop_front();
      m_events.unlock();

      // TODO: Skip event if e.pos < cache.pos 
//      if(!e.active) continue;

      switch(e.cmd) {  
        case LOADNEXT:
          loadNext(e.id);
          break;
//        case CLEAN:
//          break;
      }      
    }
    else {
      m_events.unlock();
    }
  }
}

void CacheManager::pushEvent(event_t e)
{
  // Check that if event should be merged (Maybe by event queue (ie. push in front).
  {
  MutexAutolock l(m_events);
  eventqueue.push_back(e);
  }
  sem.post();
}

CacheManager::event_t CacheManager::createLoadNextEvent(cacheid_t id, size_t pos, cmd_t cmd)
{
  event_t e;
  e.active = true;
  e.id = id;
  e.cmd = cmd;
  e.pos = pos;
  return e; 
}