2 * PROJECT: ReactOS Sound Record Application
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/sndrec32/audio_membuffer.cpp
5 * PURPOSE: Audio MemBuffer
6 * PROGRAMMERS: Marco Pagliaricci <ms_blue (at) hotmail (dot) it>
10 #include "audio_membuffer.hpp"
14 _AUDIO_NAMESPACE_START_
19 //////////////////////////////////////
20 /////// Protected Functions /////////
21 //////////////////////////////////////
25 audio_membuffer::alloc_mem_( unsigned int bytes
)
40 // Checks previsiously alloc'd memory
50 // Allocs new memory and zeros it.
53 audio_data
= new BYTE
[ bytes
];
56 memset( audio_data
, 0, bytes
* sizeof( BYTE
));
61 // Sets the correct buffer size
75 audio_membuffer::free_mem_( void )
88 audio_membuffer::resize_mem_( unsigned int new_size
)
97 // The new_size, cannot be <= of the
98 // `bytes_received' member value of the
99 // parent class `audio_receiver'.
100 // We cannot touch received audio data,
101 // so we have to alloc at least
102 // bytes_received+1 bytes.
104 // But we can truncate unused memory, so
105 // `new_size' can be < of `buf_size'.
108 if ( new_size
<= bytes_received
)
119 // Allocs new memory and zeros it.
123 new_mem
= new BYTE
[ new_size
];
125 memset( new_mem
, 0, new_size
* sizeof( BYTE
));
134 // Copies received audio data, and discard
138 memcpy( new_mem
, audio_data
, bytes_received
);
153 // Commit new memory.
156 audio_data
= new_mem
;
164 audio_data
= new_mem
;
169 if ( buffer_resized
)
170 buffer_resized( new_size
);
178 audio_membuffer::truncate_( void )
182 // If `buf_size' is already = to the
183 // `bytes_received' of audio data, then
184 // this operation is useless; simply return.
187 if ( bytes_received
== buf_size
)
197 // Allocs a new buffer.
200 BYTE
* newbuf
= new BYTE
[ bytes_received
];
206 // Copies audio data.
209 memcpy( newbuf
, audio_data
, bytes_received
);
222 // Commit the new buffer.
226 buf_size
= bytes_received
;
231 // Buffer truncation successfull.
232 // Now the buffer size is exactly big
233 // as much audio data was received.
247 //////////////////////////////////////
248 /////// Public Functions ///////////
249 //////////////////////////////////////
255 audio_membuffer::clear( void )
266 audio_membuffer::reset( void )
271 // Frees memory and reset
280 // Alloc memory of size specified
281 // at the constructor.
284 alloc_mem_( init_size
);
290 audio_membuffer::alloc_bytes( unsigned int bytes
)
301 audio_membuffer::alloc_seconds( unsigned int secs
)
304 alloc_mem_( aud_info
.byte_rate() * secs
);
310 audio_membuffer::alloc_seconds( float secs
)
313 alloc_mem_(( unsigned int )(( float ) aud_info
.byte_rate() * secs
));
321 audio_membuffer::resize_bytes( unsigned int bytes
)
324 resize_mem_( bytes
);
331 audio_membuffer::resize_seconds( unsigned int secs
)
334 resize_mem_( aud_info
.byte_rate() * secs
);
340 audio_membuffer::resize_seconds( float secs
)
343 resize_mem_(( unsigned int )
344 (( float )aud_info
.byte_rate() * secs
)
354 ///////////////////////////////////////
355 /////// Inherited Functions /////////
356 ///////////////////////////////////////
365 audio_membuffer::audio_receive
366 ( unsigned char * data
, unsigned int size
)
371 // If there isn't a buffer, allocs memory for
372 // it of size*2, and copies audio data arrival.
375 if (( audio_data
== 0 ) || ( buf_size
== 0 ))
377 alloc_mem_( size
* 2 );
379 memcpy( audio_data
, data
, size
);
390 // If buffer's free memory is < of `size',
391 // we have to realloc buffer memory of
392 // buf_size*2, while free memory is enough
393 // to contain `size' bytes.
395 // In this case free memory is represented
396 // by `buf_size - bytes_recorded'.
399 unsigned int tot_mem
= buf_size
,
400 free_mem
= buf_size
- bytes_received
;
403 if ( free_mem
< size
)
407 // Calcs new buffer size.
408 // TODO: flags for other behaviour?
410 while ( free_mem
< size
)
414 free_mem
= tot_mem
- bytes_received
;
420 // Resize buffer memory.
423 resize_mem_( tot_mem
);
429 // Now we have enough free space in the
430 // buffer, so let's copy audio data arrivals.
433 memcpy( audio_data
+ bytes_received
, data
, size
);
439 audio_arrival( aud_info
.samples_in_bytes( size
));
447 audio_membuffer::read( BYTE
* out_buf
, unsigned int bytes
)
458 if ( bytes_played_
>= bytes_received
)
463 unsigned int to_play
=
464 bytes_received
- bytes_played_
;
467 unsigned int to_copy
=
468 bytes
> to_play
? to_play
: bytes
;
472 // Copies the audio data out.
475 if (( out_buf
) && ( to_copy
) && ( audio_data
))
476 memcpy( out_buf
, audio_data
+ bytes_played_
, to_copy
);
480 // Increments the number of total bytes
481 // played (audio data gone out from the
482 // `audio_producer' object).
485 bytes_played_
+= bytes
;
489 audio_arrival( aud_info
.samples_in_bytes( bytes
));
493 // Returns the exact size of audio data
502 audio_membuffer::finished( void )
504 if ( bytes_played_
< bytes_received
)
511 _AUDIO_NAMESPACE_END_