7cd1dc40dcfb66aac66a1de79c842f1b91766705
[reactos.git] / reactos / base / applications / sndrec32 / audio_membuffer.hpp
1 #ifndef _AUDIOMEMBUFFER__H_
2 #define _AUDIOMEMBUFFER__H_
3
4
5
6 #include "audio_def.hpp"
7 #include "audio_receiver.hpp"
8 #include "audio_format.hpp"
9 #include "audio_producer.hpp"
10
11
12
13
14 _AUDIO_NAMESPACE_START_
15
16
17
18 class audio_membuffer : public audio_receiver, public audio_producer
19 {
20
21
22
23
24
25 protected:
26
27 BYTE * audio_data;
28 audio_format aud_info;
29 unsigned int buf_size;
30 unsigned int init_size;
31
32
33
34 //
35 // Protected Functions
36 //
37
38
39 //allocs N bytes for the audio buffer.
40 void alloc_mem_( unsigned int );
41
42
43 //frees memory
44 void free_mem_( void );
45
46
47 //resizes memory, and copies old
48 //audio data to new-size memory
49 void resize_mem_( unsigned int );
50
51
52 //truncates and discards unused memory.
53 //`buf_size' will be the same as `bytes_received'.
54 void truncate_( void );
55
56
57
58
59 public:
60
61
62 void ( * audio_arrival )( unsigned int );
63 void ( * buffer_resized ) ( unsigned int );
64
65
66 //
67 // Ctors
68 //
69
70 audio_membuffer( void )
71 : audio_data( 0 ), aud_info( _AUDIO_DEFAULT_FORMAT ),
72 buf_size( 0 ), init_size( 0 )
73 {
74
75 //
76 // Allocs memory for at least 1 or some seconds
77 // of recording.
78 //
79 init_size = ( unsigned int )
80 (( float )aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS );
81
82
83 alloc_mem_( init_size );
84
85
86 }
87
88
89
90 audio_membuffer( audio_format aud_fmt )
91 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
92 init_size( 0 )
93 {
94
95 //
96 // Allocs memory for at least 1 or some seconds
97 // of recording.
98 //
99 init_size = ( unsigned int )
100 (( float )aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS );
101
102
103 alloc_mem_( init_size );
104
105 }
106
107
108
109
110 audio_membuffer( audio_format aud_fmt, unsigned int seconds )
111 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
112 init_size( 0 )
113 {
114
115 //
116 // Allocs memory for audio recording
117 // the specified number of seconds.
118 //
119 init_size = aud_info.byte_rate() * seconds;
120 alloc_mem_( init_size );
121
122 }
123
124
125
126 audio_membuffer( audio_format aud_fmt, float seconds )
127 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
128 init_size( 0 )
129 {
130
131 //
132 // Allocs memory for audio recording
133 // the specified number of seconds.
134 //
135 init_size = ( unsigned int )(( float ) aud_info.byte_rate() *
136 seconds <= 0 ? 1 : seconds );
137
138
139 alloc_mem_( init_size );
140
141 }
142
143
144
145
146 audio_membuffer( unsigned int bytes )
147 : audio_data( 0 ), aud_info( _AUDIO_DEFAULT_FORMAT ),
148 buf_size( 0 ), init_size( 0 )
149 {
150
151 //
152 // Allocs memory for the specified bytes
153 //
154 init_size = bytes;
155 alloc_mem_( init_size );
156
157 }
158
159
160
161
162 //
163 // Dtor
164 //
165
166 virtual ~audio_membuffer( void )
167 {
168
169 //
170 // Frees memory and reset values.
171 //
172
173 clear();
174
175 }
176
177
178
179
180
181
182
183
184
185 //
186 // Public functions
187 //
188
189
190
191 //returns the audio buffer size in bytes.
192 unsigned int mem_size( void ) const
193 { return buf_size; }
194
195
196 //returns how many audio data has been
197 //received, in bytes.
198 unsigned int bytes_recorded( void ) const
199 { return bytes_received; }
200
201
202 //returns the integer number of seconds
203 //that the buffer can record
204 unsigned int seconds_total( void ) const
205 { return buf_size / aud_info.byte_rate(); }
206
207
208 //returns the integer number of seconds
209 //that the buffer can record
210 unsigned int seconds_recorded( void ) const
211 { return bytes_received / aud_info.byte_rate(); }
212
213
214 //returns the float number of seconds
215 //that the buffer can record
216 float fseconds_total( void ) const
217 { return ( float )(( float ) buf_size /
218 ( float ) aud_info.byte_rate()); }
219
220
221 //returns the float number of seconds
222 //that has been recorded
223 float fseconds_recorded( void ) const
224 { return ( float )(( float ) bytes_received /
225 ( float ) aud_info.byte_rate()); }
226
227
228 unsigned int total_samples( void ) const
229 {
230
231 return ( aud_info.samples_in_seconds( fseconds_total() ));
232
233 }
234
235
236 unsigned int samples_received( void ) const
237 {
238
239
240 return ( aud_info.samples_in_bytes( bytes_received ));
241
242 }
243
244
245
246 //returns a pointer to the audio buffer
247 BYTE * audio_buffer( void ) const
248 { return audio_data; }
249
250
251
252 //frees memory and resets values.
253 void clear( void );
254
255
256 audio_format & audinfo( void ) { return aud_info; }
257
258
259 //discard audio data, resets values,
260 //but, instead of clear() which frees memory,
261 //reset the memory to the initial size, ready
262 //for receiving "new" audio data.
263 void reset( void );
264
265
266 //truncates and discards unused memory.
267 //`buf_size' will be the same as `bytes_received'.
268 void truncate( void )
269 { truncate_( ); }//TODO: fare truncate N bytes
270
271
272 //if there is a buffer, discards current buffer
273 //memory and realloc a new memory buffer with a
274 //new size expressed in bytes.
275 void alloc_bytes( unsigned int );
276
277
278
279 //if there is a buffer, discards current buffer
280 //memory and realloc a new memory buffer with a
281 //new size expressed in seconds, integer and float.
282 void alloc_seconds( unsigned int );
283 void alloc_seconds( float );
284
285
286
287 //resizes in bytes the current buffer,
288 //without discarding previsiously audio data received.
289 void resize_bytes( unsigned int );
290
291
292 //resizes in seconds the current buffer,
293 //without discarding previsiously audio data received.
294 void resize_seconds( unsigned int );
295 void resize_seconds( float );
296
297
298
299
300
301
302
303
304
305 //
306 // Inherited Functions from `audio_receiver'
307 //
308
309 void audio_receive( unsigned char *, unsigned int );
310
311
312
313
314 //
315 // Inherited Functions from `audio_buffer'
316 //
317
318
319 unsigned int read( BYTE *, unsigned int );
320 bool finished( void );
321
322
323
324 };
325
326
327
328
329
330
331
332 _AUDIO_NAMESPACE_END_
333
334
335
336
337
338 #endif //ifdef _AUDIOMEMBUFFER__H_