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)
9 #ifndef _AUDIOMEMBUFFER__H_
10 #define _AUDIOMEMBUFFER__H_
14 #include "audio_def.hpp"
15 #include "audio_receiver.hpp"
16 #include "audio_format.hpp"
17 #include "audio_producer.hpp"
22 _AUDIO_NAMESPACE_START_
26 class audio_membuffer : public audio_receiver, public audio_producer
36 audio_format aud_info;
37 unsigned int buf_size;
38 unsigned int init_size;
43 // Protected Functions
47 //allocs N bytes for the audio buffer.
48 void alloc_mem_( unsigned int );
52 void free_mem_( void );
55 //resizes memory, and copies old
56 //audio data to new-size memory
57 void resize_mem_( unsigned int );
60 //truncates and discards unused memory.
61 //`buf_size' will be the same as `bytes_received'.
62 void truncate_( void );
70 void ( * audio_arrival )( unsigned int );
71 void ( * buffer_resized ) ( unsigned int );
78 audio_membuffer( void )
79 : audio_data( 0 ), aud_info( _AUDIO_DEFAULT_FORMAT ),
80 buf_size( 0 ), init_size( 0 )
84 // Allocs memory for at least 1 or some seconds
87 init_size = ( unsigned int )
88 (( float )aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS );
91 alloc_mem_( init_size );
98 audio_membuffer( audio_format aud_fmt )
99 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
104 // Allocs memory for at least 1 or some seconds
107 init_size = ( unsigned int )
108 (( float )aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS );
111 alloc_mem_( init_size );
118 audio_membuffer( audio_format aud_fmt, unsigned int seconds )
119 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
124 // Allocs memory for audio recording
125 // the specified number of seconds.
127 init_size = aud_info.byte_rate() * seconds;
128 alloc_mem_( init_size );
134 audio_membuffer( audio_format aud_fmt, float seconds )
135 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
140 // Allocs memory for audio recording
141 // the specified number of seconds.
143 init_size = ( unsigned int )(( float ) aud_info.byte_rate() *
144 seconds <= 0 ? 1 : seconds );
147 alloc_mem_( init_size );
154 audio_membuffer( unsigned int bytes )
155 : audio_data( 0 ), aud_info( _AUDIO_DEFAULT_FORMAT ),
156 buf_size( 0 ), init_size( 0 )
160 // Allocs memory for the specified bytes
163 alloc_mem_( init_size );
174 virtual ~audio_membuffer( void )
178 // Frees memory and reset values.
199 //returns the audio buffer size in bytes.
200 unsigned int mem_size( void ) const
204 //returns how many audio data has been
205 //received, in bytes.
206 unsigned int bytes_recorded( void ) const
207 { return bytes_received; }
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(); }
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(); }
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()); }
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()); }
236 unsigned int total_samples( void ) const
239 return ( aud_info.samples_in_seconds( fseconds_total() ));
244 unsigned int samples_received( void ) const
248 return ( aud_info.samples_in_bytes( bytes_received ));
254 //returns a pointer to the audio buffer
255 BYTE * audio_buffer( void ) const
256 { return audio_data; }
260 //frees memory and resets values.
264 audio_format & audinfo( void ) { return aud_info; }
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.
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
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 );
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 );
295 //resizes in bytes the current buffer,
296 //without discarding previsiously audio data received.
297 void resize_bytes( unsigned int );
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 );
314 // Inherited Functions from `audio_receiver'
317 void audio_receive( unsigned char *, unsigned int );
323 // Inherited Functions from `audio_buffer'
327 unsigned int read( BYTE *, unsigned int );
328 bool finished( void );
340 _AUDIO_NAMESPACE_END_
346 #endif //ifdef _AUDIOMEMBUFFER__H_