*** empty log message ***
[chuck-blob.git] / v2 / util_buffers.h
blobcac0bb0c6ca22bf1795d11602c851f7274d986e9
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: util_buffers.h
27 // desc: buffer
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // Ananya Misra (amisra@cs.princeton.edu)
32 // date: Spring 2004
33 // Summer 2005 - allow multiple readers
34 //-----------------------------------------------------------------------------
35 #ifndef __UTIL_BUFFERS_H__
36 #define __UTIL_BUFFERS_H__
38 #include "chuck_oo.h"
39 #include "util_thread.h"
40 #include <vector>
41 #include <queue>
43 #define DWORD__ unsigned long
44 #define SINT__ long
45 #define UINT__ DWORD__
46 #define BOOL__ DWORD__
47 #define BYTE__ unsigned char
49 #ifndef TRUE
50 #define TRUE 1
51 #define FALSE 0
52 #endif
55 //-----------------------------------------------------------------------------
56 // name: class CBufferAdvance
57 // desc: circular buffer
58 //-----------------------------------------------------------------------------
59 class CBufferAdvance
61 public:
62 CBufferAdvance();
63 ~CBufferAdvance();
65 public:
66 BOOL__ initialize( UINT__ num_elem, UINT__ width );
67 void cleanup();
69 public:
70 UINT__ get( void * data, UINT__ num_elem, UINT__ read_offset_index );
71 void put( void * data, UINT__ num_elem );
72 BOOL__ empty( UINT__ read_offset_index );
73 UINT__ join( Chuck_Event * event = NULL );
74 void resign( UINT__ read_offset_index );
76 protected:
77 BYTE__ * m_data;
78 UINT__ m_data_width;
79 //UINT__ m_read_offset;
81 // this holds the offset allocated by join(), paired with an optional
82 // Chuck_Event to notify when things are put in the buffer
83 struct ReadOffset
85 SINT__ read_offset;
86 Chuck_Event * event;
87 ReadOffset( SINT__ ro, Chuck_Event * e = NULL )
88 { read_offset = ro; event = e; }
90 std::vector<ReadOffset> m_read_offsets;
91 std::queue<UINT__> m_free;
93 SINT__ m_write_offset;
94 SINT__ m_max_elem;
96 // TODO: necessary?
97 XMutex m_mutex;
102 //-----------------------------------------------------------------------------
103 // name: class CBufferSimple
104 // desc: circular buffer - one reader one writer
105 //-----------------------------------------------------------------------------
106 class CBufferSimple
108 public:
109 CBufferSimple();
110 ~CBufferSimple();
112 public:
113 BOOL__ initialize( UINT__ num_elem, UINT__ width );
114 void cleanup();
116 public:
117 UINT__ get( void * data, UINT__ num_elem );
118 void put( void * data, UINT__ num_elem );
120 protected:
121 BYTE__ * m_data;
122 UINT__ m_data_width;
123 UINT__ m_read_offset;
124 UINT__ m_write_offset;
125 UINT__ m_max_elem;
131 //-----------------------------------------------------------------------------
132 // name: class AccumBuffer
133 // desc: circular buffer for sample accumulation
134 //-----------------------------------------------------------------------------
135 class AccumBuffer
137 public:
138 AccumBuffer();
139 ~AccumBuffer();
141 public:
142 t_CKINT resize( t_CKINT new_size );
143 void cleanup();
145 public:
146 void put( SAMPLE next );
147 void get( SAMPLE * buffer, t_CKINT num_elem );
149 protected:
150 SAMPLE * m_data;
151 t_CKUINT m_write_offset;
152 t_CKUINT m_max_elem;
158 //-----------------------------------------------------------------------------
159 // name: class DeccumBuffer
160 // desc: circular buffer for sample deccumulation
161 //-----------------------------------------------------------------------------
162 class DeccumBuffer
164 public:
165 DeccumBuffer();
166 ~DeccumBuffer();
168 public:
169 t_CKINT resize( t_CKINT new_size );
170 void cleanup();
172 public:
173 void put( SAMPLE * next, t_CKINT num_elem );
174 void get( SAMPLE * out );
175 void get( SAMPLE * buffer, t_CKINT num_elem );
177 protected:
178 SAMPLE * m_data;
179 t_CKUINT m_read_offset;
180 t_CKUINT m_max_elem;
186 #endif