forked from alltom/chuck
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util_buffers.h
186 lines (146 loc) · 4.89 KB
/
util_buffers.h
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
/*----------------------------------------------------------------------------
ChucK Concurrent, On-the-fly Audio Programming Language
Compiler and Virtual Machine
Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
http://chuck.cs.princeton.edu/
http://soundlab.cs.princeton.edu/
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
U.S.A.
-----------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------
// file: util_buffers.h
// desc: buffer
//
// author: Ge Wang (gewang@cs.princeton.edu)
// Perry R. Cook (prc@cs.princeton.edu)
// Ananya Misra (amisra@cs.princeton.edu)
// date: Spring 2004
// Summer 2005 - allow multiple readers
//-----------------------------------------------------------------------------
#ifndef __UTIL_BUFFERS_H__
#define __UTIL_BUFFERS_H__
#include "chuck_oo.h"
#include "util_thread.h"
#include <vector>
#include <queue>
#define DWORD__ unsigned long
#define SINT__ long
#define UINT__ DWORD__
#define BOOL__ DWORD__
#define BYTE__ unsigned char
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
//-----------------------------------------------------------------------------
// name: class CBufferAdvance
// desc: circular buffer
//-----------------------------------------------------------------------------
class CBufferAdvance
{
public:
CBufferAdvance();
~CBufferAdvance();
public:
BOOL__ initialize( UINT__ num_elem, UINT__ width );
void cleanup();
public:
UINT__ get( void * data, UINT__ num_elem, UINT__ read_offset_index );
void put( void * data, UINT__ num_elem );
BOOL__ empty( UINT__ read_offset_index );
UINT__ join( Chuck_Event * event = NULL );
void resign( UINT__ read_offset_index );
protected:
BYTE__ * m_data;
UINT__ m_data_width;
//UINT__ m_read_offset;
// this holds the offset allocated by join(), paired with an optional
// Chuck_Event to notify when things are put in the buffer
struct ReadOffset
{
SINT__ read_offset;
Chuck_Event * event;
ReadOffset( SINT__ ro, Chuck_Event * e = NULL )
{ read_offset = ro; event = e; }
};
std::vector<ReadOffset> m_read_offsets;
std::queue<UINT__> m_free;
SINT__ m_write_offset;
SINT__ m_max_elem;
// TODO: necessary?
XMutex m_mutex;
};
//-----------------------------------------------------------------------------
// name: class CBufferSimple
// desc: circular buffer - one reader one writer
//-----------------------------------------------------------------------------
class CBufferSimple
{
public:
CBufferSimple();
~CBufferSimple();
public:
BOOL__ initialize( UINT__ num_elem, UINT__ width );
void cleanup();
public:
UINT__ get( void * data, UINT__ num_elem );
void put( void * data, UINT__ num_elem );
protected:
BYTE__ * m_data;
UINT__ m_data_width;
UINT__ m_read_offset;
UINT__ m_write_offset;
UINT__ m_max_elem;
};
//-----------------------------------------------------------------------------
// name: class AccumBuffer
// desc: circular buffer for sample accumulation
//-----------------------------------------------------------------------------
class AccumBuffer
{
public:
AccumBuffer();
~AccumBuffer();
public:
t_CKINT resize( t_CKINT new_size );
void cleanup();
public:
void put( SAMPLE next );
void get( SAMPLE * buffer, t_CKINT num_elem );
protected:
SAMPLE * m_data;
t_CKUINT m_write_offset;
t_CKUINT m_max_elem;
};
//-----------------------------------------------------------------------------
// name: class DeccumBuffer
// desc: circular buffer for sample deccumulation
//-----------------------------------------------------------------------------
class DeccumBuffer
{
public:
DeccumBuffer();
~DeccumBuffer();
public:
t_CKINT resize( t_CKINT new_size );
void cleanup();
public:
void put( SAMPLE * next, t_CKINT num_elem );
void get( SAMPLE * out );
void get( SAMPLE * buffer, t_CKINT num_elem );
protected:
SAMPLE * m_data;
t_CKUINT m_read_offset;
t_CKUINT m_max_elem;
};
#endif