1 #ifndef _AUDIOMEMBUFFER__H_
2 #define _AUDIOMEMBUFFER__H_
6 #include "audio_def.hpp"
7 #include "audio_receiver.hpp"
8 #include "audio_format.hpp"
9 #include "audio_producer.hpp"
14 _AUDIO_NAMESPACE_START_
18 class audio_membuffer : public audio_receiver, public audio_producer
28 audio_format aud_info;
29 unsigned int buf_size;
30 unsigned int init_size;
35 // Protected Functions
39 //allocs N bytes for the audio buffer.
40 void alloc_mem_( unsigned int );
44 void free_mem_( void );
47 //resizes memory, and copies old
48 //audio data to new-size memory
49 void resize_mem_( unsigned int );
52 //truncates and discards unused memory.
53 //`buf_size' will be the same as `bytes_received'.
54 void truncate_( void );
62 void ( * audio_arrival )( unsigned int );
63 void ( * buffer_resized ) ( unsigned int );
70 audio_membuffer( void )
71 : audio_data( 0 ), aud_info( _AUDIO_DEFAULT_FORMAT ),
72 buf_size( 0 ), init_size( 0 )
76 // Allocs memory for at least 1 or some seconds
79 init_size = ( unsigned int )
80 (( float )aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS );
83 alloc_mem_( init_size );
90 audio_membuffer( audio_format aud_fmt )
91 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
96 // Allocs memory for at least 1 or some seconds
99 init_size = ( unsigned int )
100 (( float )aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS );
103 alloc_mem_( init_size );
110 audio_membuffer( audio_format aud_fmt, unsigned int seconds )
111 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
116 // Allocs memory for audio recording
117 // the specified number of seconds.
119 init_size = aud_info.byte_rate() * seconds;
120 alloc_mem_( init_size );
126 audio_membuffer( audio_format aud_fmt, float seconds )
127 : audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
132 // Allocs memory for audio recording
133 // the specified number of seconds.
135 init_size = ( unsigned int )(( float ) aud_info.byte_rate() *
136 seconds <= 0 ? 1 : seconds );
139 alloc_mem_( init_size );
146 audio_membuffer( unsigned int bytes )
147 : audio_data( 0 ), aud_info( _AUDIO_DEFAULT_FORMAT ),
148 buf_size( 0 ), init_size( 0 )
152 // Allocs memory for the specified bytes
155 alloc_mem_( init_size );
166 virtual ~audio_membuffer( void )
170 // Frees memory and reset values.
191 //returns the audio buffer size in bytes.
192 unsigned int mem_size( void ) const
196 //returns how many audio data has been
197 //received, in bytes.
198 unsigned int bytes_recorded( void ) const
199 { return bytes_received; }
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(); }
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(); }
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()); }
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()); }
228 unsigned int total_samples( void ) const
231 return ( aud_info.samples_in_seconds( fseconds_total() ));
236 unsigned int samples_received( void ) const
240 return ( aud_info.samples_in_bytes( bytes_received ));
246 //returns a pointer to the audio buffer
247 BYTE * audio_buffer( void ) const
248 { return audio_data; }
252 //frees memory and resets values.
256 audio_format & audinfo( void ) { return aud_info; }
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.
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
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 );
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 );
287 //resizes in bytes the current buffer,
288 //without discarding previsiously audio data received.
289 void resize_bytes( unsigned int );
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 );
306 // Inherited Functions from `audio_receiver'
309 void audio_receive( unsigned char *, unsigned int );
315 // Inherited Functions from `audio_buffer'
319 unsigned int read( BYTE *, unsigned int );
320 bool finished( void );
332 _AUDIO_NAMESPACE_END_
338 #endif //ifdef _AUDIOMEMBUFFER__H_