0f6580e27d3eefaa222b64e4b32d708264f49294
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"
12 _AUDIO_NAMESPACE_START_
16 audio_membuffer::alloc_mem_( unsigned int bytes
)
31 // Checks previsiously alloc'd memory
41 // Allocs new memory and zeros it.
44 audio_data
= new BYTE
[ bytes
];
47 memset( audio_data
, 0, bytes
* sizeof( BYTE
));
52 // Sets the correct buffer size
66 audio_membuffer::free_mem_( void )
79 audio_membuffer::resize_mem_( unsigned int new_size
)
88 // The new_size, cannot be <= of the
89 // `bytes_received' member value of the
90 // parent class `audio_receiver'.
91 // We cannot touch received audio data,
92 // so we have to alloc at least
93 // bytes_received+1 bytes.
95 // But we can truncate unused memory, so
96 // `new_size' can be < of `buf_size'.
99 if ( new_size
<= bytes_received
)
110 // Allocs new memory and zeros it.
114 new_mem
= new BYTE
[ new_size
];
116 memset( new_mem
, 0, new_size
* sizeof( BYTE
));
125 // Copies received audio data, and discard
129 memcpy( new_mem
, audio_data
, bytes_received
);
144 // Commit new memory.
147 audio_data
= new_mem
;
155 audio_data
= new_mem
;
160 if ( buffer_resized
)
161 buffer_resized( new_size
);
169 audio_membuffer::truncate_( void )
173 // If `buf_size' is already = to the
174 // `bytes_received' of audio data, then
175 // this operation is useless; simply return.
178 if ( bytes_received
== buf_size
)
188 // Allocs a new buffer.
191 BYTE
* newbuf
= new BYTE
[ bytes_received
];
197 // Copies audio data.
200 memcpy( newbuf
, audio_data
, bytes_received
);
213 // Commit the new buffer.
217 buf_size
= bytes_received
;
222 // Buffer truncation successfull.
223 // Now the buffer size is exactly big
224 // as much audio data was received.
238 //////////////////////////////////////
239 /////// Public Functions ///////////
240 //////////////////////////////////////
246 audio_membuffer::clear( void )
257 audio_membuffer::reset( void )
262 // Frees memory and reset
271 // Alloc memory of size specified
272 // at the constructor.
275 alloc_mem_( init_size
);
281 audio_membuffer::alloc_bytes( unsigned int bytes
)
292 audio_membuffer::alloc_seconds( unsigned int secs
)
295 alloc_mem_( aud_info
.byte_rate() * secs
);
301 audio_membuffer::alloc_seconds( float secs
)
304 alloc_mem_(( unsigned int )(( float ) aud_info
.byte_rate() * secs
));
312 audio_membuffer::resize_bytes( unsigned int bytes
)
315 resize_mem_( bytes
);
322 audio_membuffer::resize_seconds( unsigned int secs
)
325 resize_mem_( aud_info
.byte_rate() * secs
);
331 audio_membuffer::resize_seconds( float secs
)
334 resize_mem_(( unsigned int )
335 (( float )aud_info
.byte_rate() * secs
)
345 ///////////////////////////////////////
346 /////// Inherited Functions /////////
347 ///////////////////////////////////////
356 audio_membuffer::audio_receive
357 ( unsigned char * data
, unsigned int size
)
362 // If there isn't a buffer, allocs memory for
363 // it of size*2, and copies audio data arrival.
366 if (( audio_data
== 0 ) || ( buf_size
== 0 ))
368 alloc_mem_( size
* 2 );
370 memcpy( audio_data
, data
, size
);
381 // If buffer's free memory is < of `size',
382 // we have to realloc buffer memory of
383 // buf_size*2, while free memory is enough
384 // to contain `size' bytes.
386 // In this case free memory is represented
387 // by `buf_size - bytes_recorded'.
390 unsigned int tot_mem
= buf_size
,
391 free_mem
= buf_size
- bytes_received
;
394 if ( free_mem
< size
)
398 // Calcs new buffer size.
399 // TODO: flags for other behaviour?
401 while ( free_mem
< size
)
405 free_mem
= tot_mem
- bytes_received
;
411 // Resize buffer memory.
414 resize_mem_( tot_mem
);
420 // Now we have enough free space in the
421 // buffer, so let's copy audio data arrivals.
424 memcpy( audio_data
+ bytes_received
, data
, size
);
430 audio_arrival( aud_info
.samples_in_bytes( size
));
438 audio_membuffer::read( BYTE
* out_buf
, unsigned int bytes
)
449 if ( bytes_played_
>= bytes_received
)
454 unsigned int to_play
=
455 bytes_received
- bytes_played_
;
458 unsigned int to_copy
=
459 bytes
> to_play
? to_play
: bytes
;
463 // Copies the audio data out.
466 if (( out_buf
) && ( to_copy
) && ( audio_data
))
467 memcpy( out_buf
, audio_data
+ bytes_played_
, to_copy
);
471 // Increments the number of total bytes
472 // played (audio data gone out from the
473 // `audio_producer' object).
476 bytes_played_
+= bytes
;
480 audio_arrival( aud_info
.samples_in_bytes( bytes
));
484 // Returns the exact size of audio data
493 audio_membuffer::finished( void )
495 if ( bytes_played_
< bytes_received
)
501 _AUDIO_NAMESPACE_END_