# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

buffer_al.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdopenal.h"
00027 
00028 #include "buffer_al.h"
00029 
00030 namespace NLSOUND {
00031 
00032 
00033 /*
00034  * Constructor
00035  */
00036 CBufferAL::CBufferAL( ALuint buffername ) :
00037         IBuffer(), _BufferName(buffername), _SampleFormat(AL_INVALID), _Frequency(0)
00038 {
00039 }
00040 
00041 
00042 /*
00043  * Destructor
00044  */
00045 CBufferAL::~CBufferAL()
00046 {
00047         CSoundDriverAL *sdal = CSoundDriverAL::instance();
00048         //nlinfo( "Deleting buffer (name %u)", _BufferName );
00049         sdal->removeBuffer( this );
00050 }
00051 
00052 void CBufferAL::presetName(const std::string &bufferName)
00053 {
00054         _Name = bufferName;
00055 }
00056 
00057 
00058 /*
00059  * Return the size of the buffer, in bytes
00060  */
00061 uint32          CBufferAL::getSize() const
00062 {
00063         ALint value;
00064         alGetBufferi( _BufferName, AL_SIZE, &value );
00065         nlassert( alGetError() == AL_NO_ERROR );
00066         return value;
00067 }
00068 
00069 
00070 /*
00071  * Return true if the buffer is stereo, false if mono
00072  */
00073 bool            CBufferAL::isStereo() const
00074 {
00075         return (_SampleFormat==AL_FORMAT_STEREO8) || (_SampleFormat==AL_FORMAT_STEREO16);
00076 }
00077 
00078 
00079 /*
00080  * Return the format and frequency
00081  */
00082 void            CBufferAL::getFormat( TSampleFormat& format, uint& freq ) const
00083 {
00084         switch ( _SampleFormat )
00085         {
00086         case AL_FORMAT_MONO8 : format = Mono8; break;
00087         case AL_FORMAT_MONO16 : format = Mono16; break;
00088         case AL_FORMAT_STEREO8 : format = Stereo8; break;
00089         case AL_FORMAT_STEREO16 : format = Stereo16; break;
00090         default : nlstop;
00091         }
00092         freq = _Frequency;
00093 }
00094 
00095 
00096 /*
00097  * Return the duration (in ms) of the sample in the buffer
00098  */
00099 float           CBufferAL::getDuration() const
00100 {
00101         if ( _Frequency == 0 )
00102         {
00103                 return 0;
00104         }
00105         uint32 bytespersample;
00106         switch ( _SampleFormat )
00107         {
00108         case AL_FORMAT_MONO8 : bytespersample = 1; break;
00109         case AL_FORMAT_MONO16 : bytespersample = 2; break;
00110         case AL_FORMAT_STEREO8 : bytespersample = 2; break;
00111         case  AL_FORMAT_STEREO16 : bytespersample = 4; break;
00112         default : return 0;
00113         }
00114         return (float)(getSize()) * 1000.0f / (float)_Frequency / (float)bytespersample;
00115 }
00116 
00117 
00118 /*
00119  * Set the sample format. Example: freq=44100
00120  */
00121 void            CBufferAL::setFormat( TSampleFormat format, uint freq )
00122 {
00123         switch ( format )
00124         {
00125         case Mono8 : _SampleFormat = AL_FORMAT_MONO8; break;
00126         case Mono16 : _SampleFormat = AL_FORMAT_MONO16; break;
00127         case Stereo8 : _SampleFormat = AL_FORMAT_STEREO8; break;
00128         case Stereo16 : _SampleFormat = AL_FORMAT_STEREO16; break;
00129         default            : nlstop; _SampleFormat = AL_INVALID;
00130         }
00131         _Frequency = freq;
00132 }
00133 
00134 
00135 /*
00136  * Set the buffer size and fill the buffer. Return true if ok.
00137  */
00138 bool            CBufferAL::fillBuffer( void *src, uint32 bufsize )
00139 {
00140         nlassert( src != NULL );
00141         nlassert( (_SampleFormat != AL_INVALID) && (_Frequency != 0 ) );
00142 
00143         // Fill buffer
00144         alBufferData( _BufferName, _SampleFormat, src, bufsize, _Frequency );
00145 
00146         // Error handling
00147         return ( alGetError() == AL_NO_ERROR );
00148 }
00149 
00150 
00151 } // NLSOUND