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