/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Class for loading sound files into a memory buffer.
 *//*
 * LEGAL:   COPYRIGHT (C) 2009 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define SOUND_SOUND_BUFFER_CC 1
#include "base/module.hh"
#include "base/conf.hh"
#include "base/file.hh"
#include "base/stream.hh"
using namespace base;
#include "sound/module.hh"
#include "sound/sound_buffer.hh"

namespace sound {

////////////////////////////////////////////////////////////////////////////////
///////////////////////////////  SoundBuffer  //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*****************************************************************************
 * Load sound file into a memory buffer else throw exception.
 * Supports compressed sound files.
 * @param   soundFilename
 *          Will be expanded to full pathname.
 *****************************************************************************/
SoundBuffer::SoundBuffer( const string& soundFilename )
:   mBufAndSize(ReadFileGzipIntoMemory(SoundBase::ComputePathnameSoundFile(soundFilename)))
#if SOUND_VERBOSE
    ,mName(soundFilename)
#endif
{
    // Buffer invalid?
    if ( (mBufAndSize.first == NULL)  // mBufAndSize is an STL pair
      or (mBufAndSize.second <= 0) )
    {
        const string msg = string("ERROR[SoundBuffer] failed to load sound file ") + soundFilename;
        #if LOG_SOUND_ERRORS
        CERROR << msg << std::endl;
        #endif
        throw Exception( msg );
    }
}

SoundBuffer::~SoundBuffer()
{
#if SOUND_FREE
    #if SOUND_VERBOSE
    CLOG << "Freeing SoundBuffer " << mName << '.' << std::endl;
    #endif
    free( GetBuf() );
#endif
}

} // namespace sound
