blob: 25bbae59d90b680ab8c16ae96bb4d16c91ef5d39 (
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
|
#include "cachemanager.h"
#include <stdio.h>
CacheManager::CacheManager()
{
}
CacheManager::~CacheManager()
{
wait_stop();
}
void CacheManager::init(int poolsize)
{
id2cache.resize(poolsize);
for(int i = 0; i < poolsize; i++) {
availableids.push_back(i);
}
running = true;
run();
}
sample_t *CacheManager::open(AudioFile *file, int initial_samples_needed, int channel, cacheid_t &id)
{
m_ids.lock();
id = availableids.front();
availableids.pop_front();
m_ids.unlock();
cache_t c;
c.file = file;
c.channel = channel;
c.pos = initial_samples_needed;
id2cache[id] = c;
event_t e;
e.id = id;
pushEvent(e);
return file->data;
}
void CacheManager::close(cacheid_t id)
{
m_ids.lock();
availableids.push_back(id);
m_ids.unlock();
}
sample_t *CacheManager::next(cacheid_t id, size_t &size)
{
cache_t *c;
{
MutexAutolock l(m_caches);
c = &id2cache[id];
}
size = 256;
sample_t *s = c->file->data + c->pos;
c->pos += size;
return s;
}
void CacheManager::loadNext(cacheid_t id)
{
printf("Loading next...\n");
if(false) {
event_t e;
e.id = id;
pushEvent(e);
}
}
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();
loadNext(e.id);
}
else {
m_events.unlock();
}
}
}
void CacheManager::pushEvent(event_t e)
{
m_events.lock();
eventqueue.push_back(e);
m_events.unlock();
}
|