summaryrefslogtreecommitdiff
path: root/src/cachemanager.cc
blob: 4053f05734c76d4f349d66aad169cd95028eafc4 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* -*- 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 <string.h>
#include <stdio.h>
#include <assert.h>

#include <sndfile.h>

#include <hugin.hpp>

#define	BUFFER_SIZE	4092

#define CHUNKSIZE(x) (x * CHUNK_MULTIPLIER)

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);
  if(!fh) {
    ERR(audiofile,"SNDFILE Error (%s): %s\n",
        filename.c_str(), sf_strerror(fh));
    return 0;
  }

  if(pos > sf_info.frames) {
    return 0;
  }

  sf_seek(fh, pos, SEEK_SET);

  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_readf_float(fh, data, size);
  } 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 < (int)size; i++) {
        data[totalread++] = buffer[i * sf_info.channels + filechannel];
      }
    } while(read > 0 && totalread < (int)size && totalread < sf_info.frames);
    // set data size to total bytes read
    size = totalread;
  }

  sf_close(fh);

  return size;
}

CacheManager::CacheManager()
  : framesize(0)
  , nodata(NULL)
{
}

CacheManager::~CacheManager()
{
  deinit();
  delete[] nodata;
}

void CacheManager::init(size_t poolsize, bool threaded)
{
  this->threaded = threaded;

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

  running = true;
  if(threaded) {
    run();
    sem_run.wait();
  }
}

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

// 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)
{
  {
    MutexAutolock l(m_ids);
    if(availableids.empty()) {
      id = CACHE_DUMMYID;
    } else {
      id = availableids.front();
      availableids.pop_front();
    }
  }

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

  cache_t c;
  c.file = file;
  c.channel = channel;
  c.pos = initial_samples_needed;
  c.localpos = 0;
  c.front = new sample_t[CHUNKSIZE(framesize)];
  c.back = new sample_t[CHUNKSIZE(framesize)];

  size_t size = CHUNKSIZE(framesize);
  if(size > (file->preloadedsize - c.pos)) size = (file->preloadedsize - c.pos);
  memcpy(c.front, c.file->data + c.pos, size * sizeof(sample_t));
  c.ready = false;
  c.pos += size;

  // Increase audio ref count

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

  // Only load next buffer if there are more data in the file to be loaded...
  if(c.pos < file->size) {
    cevent_t e =
      createLoadNextEvent(c.file, c.channel, c.pos, c.back);
    e.ready = &id2cache[id].ready;
    pushEvent(e);
  }

  return file->data; // preloaded data
}

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

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

  cache_t& c = id2cache[id];
  if(c.localpos < CHUNKSIZE(framesize)) {
    sample_t *s = c.front + c.localpos;
    c.localpos += size;
    return s;
  }

  if(!c.ready) {
    //printf("#%d: NOT READY!\n", id); // TODO: Count and show in UI?
  }

  // Swap buffers
  sample_t *tmp = c.front;
  c.front = c.back;
  c.back = tmp;

  c.localpos = size; // Next time we go here we have already read the first frame.

  c.pos += CHUNKSIZE(framesize);
  
  if(c.pos < c.file->size) {
    cevent_t e = createLoadNextEvent(c.file, c.channel, c.pos, c.back);
    c.ready = false;
    e.ready = &c.ready;
    pushEvent(e);
  } 

  return c.front;
}

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

  cevent_t e = createCloseEvent(id);
  pushEvent(e);
}

void CacheManager::setFrameSize(size_t framesize)
{
  this->framesize = framesize;
  delete[] nodata;
  nodata = new sample_t[framesize];

  for(size_t i = 0; i < framesize; i++) {
    nodata[i] = 0;
  }
}

void CacheManager::handleLoadNextEvent(cevent_t &e) 
{
#if 0 // memcpy
  size_t size = CHUNKSIZE(framesize);
  if(size > (e.file->size - e.pos)) {
    size = (e.file->size - e.pos);
  }
  memcpy(e.buffer, e.file->data + e.pos, size * sizeof(sample_t));
#elif 1 // diskread
  //memset(e.buffer, 0, CHUNKSIZE(framesize) * sizeof(sample_t));
  readChunk(e.file->filename, e.channel, e.pos, CHUNKSIZE(framesize), e.buffer);
#endif
  *e.ready = true;
}

void CacheManager::handleCloseEvent(cevent_t &e) 
{
  cache_t& c = id2cache[e.id];
  delete[] c.front;
  delete[] c.back;

  {
    MutexAutolock l(m_ids);
    availableids.push_back(e.id);
  }

  // TODO: Count down ref counter on c.file and close it if 0.
}


void CacheManager::handleEvent(cevent_t &e)
{
  switch(e.cmd) {
  case LOADNEXT:
    handleLoadNextEvent(e);
    break;
  case CLOSE:
    handleCloseEvent(e);
    break;
  }
}

void CacheManager::thread_main()
{
  sem_run.post(); // Signal that the thread has been started

  while(running) {
    sem.wait();

    m_events.lock();
    if(eventqueue.empty()) {
      m_events.unlock();
      continue;
    }

    cevent_t e = eventqueue.front();
    eventqueue.pop_front();
    m_events.unlock();

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

    handleEvent(e);
  }
}

void CacheManager::pushEvent(cevent_t e)
{
  if(!threaded) {
    handleEvent(e);
    return;
  }

  // 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::cevent_t
CacheManager::createLoadNextEvent(AudioFile *file, size_t channel, size_t pos,
                                  sample_t* buffer)
{
  cevent_t e;
  e.cmd = LOADNEXT;
  e.pos = pos;
  e.buffer = buffer;
  e.file = file;
  e.channel = channel;
  return e; 
}

CacheManager::cevent_t
CacheManager::createCloseEvent(cacheid_t id)
{
  cevent_t e;
  e.cmd = CLOSE;
  e.id = id;
  return e; 
}