ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

audio_membuffer.cpp
Go to the documentation of this file.
00001 /* PROJECT:         ReactOS sndrec32
00002  * LICENSE:         GPL - See COPYING in the top level directory
00003  * FILE:            base/applications/sndrec32/audio_membuffer.cpp
00004  * PURPOSE:         Sound recording
00005  * PROGRAMMERS:     Marco Pagliaricci (irc: rendar)
00006  */
00007 
00008 
00009 
00010 #include "stdafx.h"
00011 #include "audio_membuffer.hpp"
00012 
00013 
00014 
00015 _AUDIO_NAMESPACE_START_
00016 
00017 
00018 
00019 
00023 
00024 
00025 void 
00026 audio_membuffer::alloc_mem_( unsigned int bytes )
00027 {
00028 
00029     //
00030     // Some checking
00031     //
00032 
00033     if ( bytes == 0 )
00034         return;
00035 
00036 
00037 
00038 
00039 
00040     //
00041     // Checks previsiously alloc'd memory
00042     // and frees it.
00043     //
00044 
00045     if ( audio_data )
00046         delete[] audio_data;
00047 
00048 
00049 
00050     //
00051     // Allocs new memory and zeros it.
00052     //
00053 
00054     audio_data = new BYTE[ bytes ];
00055 
00056 
00057     memset( audio_data, 0, bytes * sizeof( BYTE ));
00058 
00059 
00060 
00061     //
00062     // Sets the correct buffer size
00063     //
00064 
00065     buf_size = bytes;
00066 
00067 
00068     init_size = bytes;
00069 
00070 
00071 
00072 }
00073 
00074 
00075 void
00076 audio_membuffer::free_mem_( void )
00077 {
00078 
00079     if ( audio_data )
00080         delete[] audio_data;
00081 
00082     buf_size = 0;
00083     audio_data = 0;
00084 
00085 }
00086 
00087 
00088 void
00089 audio_membuffer::resize_mem_( unsigned int new_size )
00090 {
00091 
00092 
00093     if ( new_size == 0 )
00094         return;
00095 
00096 
00097     //
00098     // The new_size, cannot be <= of the
00099     // `bytes_received' member value of the
00100     // parent class `audio_receiver'.
00101     // We cannot touch received audio data,
00102     // so we have to alloc at least
00103     // bytes_received+1 bytes.
00104     //
00105     // But we can truncate unused memory, so
00106     // `new_size' can be < of `buf_size'.
00107     //
00108 
00109     if ( new_size <= bytes_received )
00110         return;
00111 
00112 
00113     
00114 
00115     BYTE * new_mem;
00116 
00117 
00118 
00119     //
00120     // Allocs new memory and zeros it.
00121     //
00122 
00123 
00124     new_mem = new BYTE[ new_size ];
00125 
00126     memset( new_mem, 0, new_size * sizeof( BYTE ));
00127 
00128 
00129 
00130     if ( audio_data )
00131     {
00132 
00133 
00134         //
00135         // Copies received audio data, and discard
00136         // unused memory.
00137         //
00138 
00139         memcpy( new_mem, audio_data, bytes_received );
00140 
00141 
00142 
00143         //
00144         // Frees old memory.
00145         //
00146 
00147         delete[] audio_data;
00148 
00149 
00150 
00151 
00152 
00153         //
00154         // Commit new memory.
00155         //
00156 
00157         audio_data = new_mem;
00158         buf_size = new_size;
00159 
00160 
00161 
00162 
00163     } else {
00164 
00165         audio_data = new_mem;
00166         buf_size = new_size;
00167     }
00168 
00169 
00170     if ( buffer_resized )
00171         buffer_resized( new_size );
00172 
00173 }
00174 
00175 
00176 
00177 
00178 void 
00179 audio_membuffer::truncate_( void )
00180 {
00181 
00182     //
00183     // If `buf_size' is already = to the
00184     // `bytes_received' of audio data, then
00185     // this operation is useless; simply return.
00186     //
00187 
00188     if ( bytes_received == buf_size )
00189         return;
00190 
00191 
00192 
00193     if ( audio_data )
00194     {
00195 
00196         
00197         //
00198         // Allocs a new buffer.
00199         //
00200 
00201         BYTE * newbuf = new BYTE[ bytes_received ];
00202 
00203 
00204 
00205 
00206         //
00207         // Copies audio data.
00208         //
00209 
00210         memcpy( newbuf, audio_data, bytes_received );
00211 
00212 
00213 
00214         //
00215         // Frees old memory.
00216         //
00217 
00218         delete[] audio_data;
00219 
00220 
00221 
00222         //
00223         // Commit the new buffer.
00224         //
00225 
00226         audio_data = newbuf;
00227         buf_size = bytes_received;
00228 
00229 
00230 
00231         //
00232         // Buffer truncation successfull.
00233         // Now the buffer size is exactly big
00234         // as much audio data was received.
00235         //
00236 
00237 
00238     }
00239 
00240 
00241 }
00242 
00243 
00244 
00245 
00246 
00247 
00251 
00252 
00253 
00254 
00255 void
00256 audio_membuffer::clear( void )
00257 {
00258 
00259     free_mem_();
00260 
00261     bytes_received = 0;
00262 }
00263 
00264 
00265 
00266 void 
00267 audio_membuffer::reset( void )
00268 {
00269 
00270 
00271     //
00272     // Frees memory and reset
00273     // to initial state.
00274     //
00275 
00276     clear();
00277 
00278 
00279 
00280     //
00281     // Alloc memory of size specified
00282     // at the constructor.
00283     //
00284 
00285     alloc_mem_( init_size );
00286 
00287 
00288 }
00289 
00290 void 
00291 audio_membuffer::alloc_bytes( unsigned int bytes )
00292 {
00293 
00294     alloc_mem_( bytes );
00295 
00296 }
00297 
00298 
00299 
00300         
00301 void 
00302 audio_membuffer::alloc_seconds( unsigned int secs )
00303 {
00304     
00305     alloc_mem_( aud_info.byte_rate() * secs );
00306 
00307 }
00308 
00309 
00310 void 
00311 audio_membuffer::alloc_seconds( float secs )
00312 {
00313 
00314     alloc_mem_(( unsigned int )(( float ) aud_info.byte_rate() * secs ));
00315 
00316 }
00317 
00318 
00319 
00320 
00321 void 
00322 audio_membuffer::resize_bytes( unsigned int bytes )
00323 {
00324 
00325     resize_mem_( bytes );
00326 
00327 }
00328 
00329 
00330         
00331 void 
00332 audio_membuffer::resize_seconds( unsigned int secs )
00333 {
00334 
00335     resize_mem_( aud_info.byte_rate() * secs );
00336 
00337 }
00338 
00339 
00340 void 
00341 audio_membuffer::resize_seconds( float secs )
00342 {
00343 
00344     resize_mem_(( unsigned int )
00345         (( float )aud_info.byte_rate() * secs )
00346     );
00347 
00348 }
00349 
00350 
00351 
00352 
00353 
00357 
00358 
00359 
00360 
00361 
00362 
00363 
00364 void 
00365 audio_membuffer::audio_receive
00366         ( unsigned char * data, unsigned int size )
00367 {
00368 
00369     
00370 
00371 
00372     //
00373     // If there isn't a buffer, allocs memory for
00374     // it of size*2, and copies audio data arrival.
00375     //
00376 
00377     if (( audio_data == 0 ) || ( buf_size == 0 ))
00378     {
00379         alloc_mem_( size * 2 );
00380 
00381         memcpy( audio_data, data, size );
00382 
00383         return;
00384 
00385     }
00386 
00387 
00388 
00389 
00390 
00391     //
00392     // If buffer's free memory is < of `size',
00393     // we have to realloc buffer memory of
00394     // buf_size*2, while free memory is enough
00395     // to contain `size' bytes.
00396     //
00397     // In this case free memory is represented
00398     // by `buf_size - bytes_recorded'.
00399     //
00400 
00401     unsigned int tot_mem = buf_size,
00402         free_mem = buf_size - bytes_received;
00403 
00404 
00405     if ( free_mem < size )
00406     {
00407 
00408         //
00409         // Calcs new buffer size.
00410         // TODO: flags for other behaviour?
00411 
00412         while ( free_mem < size )
00413         {
00414             tot_mem *= 2;
00415 
00416             free_mem = tot_mem - bytes_received;
00417         }
00418 
00419 
00420 
00421         //
00422         // Resize buffer memory.
00423         //
00424 
00425         resize_mem_( tot_mem );
00426 
00427     }
00428         
00429 
00430     //
00431     // Now we have enough free space in the
00432     // buffer, so let's copy audio data arrivals.
00433     //
00434 
00435     memcpy( audio_data + bytes_received, data, size );
00436 
00437 
00438     
00439     
00440     if ( audio_arrival )
00441         audio_arrival( aud_info.samples_in_bytes( size ));
00442     
00443 
00444 
00445 }
00446 
00447 
00448 unsigned int 
00449 audio_membuffer::read( BYTE * out_buf, unsigned int bytes )
00450 {
00451 
00452 
00453     //
00454     // Some checking
00455     //
00456     
00457     if ( !audio_data )
00458         return 0;
00459 
00460 
00461     if ( bytes_played_ >= bytes_received )
00462         return 0;
00463 
00464 
00465 
00466     unsigned int to_play =  
00467         bytes_received - bytes_played_;
00468 
00469 
00470     unsigned int to_copy = 
00471         bytes > to_play ? to_play : bytes;
00472     
00473 
00474     //
00475     // Copies the audio data out.
00476     //
00477 
00478     if (( out_buf ) && ( to_copy ) && ( audio_data ))
00479         memcpy( out_buf, audio_data + bytes_played_, to_copy );
00480 
00481 
00482     //
00483     // Increments the number of total bytes
00484     // played (audio data gone out from the
00485     // `audio_producer' object).
00486     //
00487 
00488     bytes_played_ += to_copy;
00489 
00490 
00491     if ( audio_arrival )
00492         audio_arrival( aud_info.samples_in_bytes( to_copy ));
00493 
00494     
00495     //
00496     // Returns the exact size of audio data
00497     // produced.
00498     //
00499 
00500     return to_copy;
00501 }
00502 
00503 
00504 bool
00505 audio_membuffer::finished( void ) 
00506 {
00507     if ( bytes_played_ < bytes_received )
00508         return false;
00509     else
00510         return true;
00511 }
00512 
00513 
00514 _AUDIO_NAMESPACE_END_

Generated on Sat May 26 2012 04:16:25 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.