-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaudioBuffer.hpp
66 lines (54 loc) · 942 Bytes
/
audioBuffer.hpp
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
#ifndef AUDIOBUFFER_HPP
#define AUDIOBUFFER_HPP
class audioBuffer
{
private:
short buf[AUDIO_BUF_SIZE];
int idx_read, idx_write, lvl;
bool locked;
inline void wrap(int *p)
{
if (*p == AUDIO_BUF_SIZE)
{
*p = 0;
}
}
public:
audioBuffer()
{
locked = false;
idx_read = 0;
idx_write = 0;
lvl = 0;
}
bool putSample(int sample)
{
locked = true;
if (lvl >= AUDIO_BUF_SIZE)
{
locked = false;
return false;
}
lvl++;
buf[idx_write++] = sample;
wrap(&idx_write);
locked = false;
return true;
}
bool getSample(int *sample)
{
if (locked)
{
return false;
}
if (lvl == 0)
{
return false;
}
lvl--;
*sample = buf[idx_read++];
wrap(&idx_read);
return true;
}
};
#endif