Added computation of the duration of a sound buffer (unused atm).

This commit is contained in:
hiker 2014-10-13 22:06:05 +11:00
parent f84e773738
commit 5cf7b0dca9
2 changed files with 86 additions and 38 deletions

View File

@ -36,27 +36,37 @@
#endif #endif
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
/** Creates a sfx. The parameter are taken from the parameters:
* \param file File name of the buffer.
* \param positional If the sfx is positional.
* \param rolloff Rolloff value of this sfx.
* \param max_dist Maximum distance the sfx can be heard.
* \param gain Gain value of this sfx.
*/
SFXBuffer::SFXBuffer(const std::string& file, SFXBuffer::SFXBuffer(const std::string& file,
bool positional, bool positional,
float rolloff, float rolloff,
float max_width, float max_dist,
float gain) float gain)
{ {
m_buffer = 0; m_buffer = 0;
m_gain = 1.0f; m_gain = 1.0f;
m_rolloff = 0.1f; m_rolloff = 0.1f;
m_loaded = false; m_loaded = false;
m_max_dist = max_width; m_max_dist = max_dist;
m_duration = -1.0f;
m_file = file; m_file = file;
m_rolloff = rolloff; m_rolloff = rolloff;
m_positional = positional; m_positional = positional;
m_gain = gain; m_gain = gain;
} } // SFXBuffer
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
/** Constructor getting the sfx parameters from an XML node.
* \param file File name of the data.
* \param node XML Node with the data for this sfx.
*/
SFXBuffer::SFXBuffer(const std::string& file, SFXBuffer::SFXBuffer(const std::string& file,
const XMLNode* node) const XMLNode* node)
{ {
@ -64,6 +74,7 @@ SFXBuffer::SFXBuffer(const std::string& file,
m_gain = 1.0f; m_gain = 1.0f;
m_rolloff = 0.1f; m_rolloff = 0.1f;
m_max_dist = 300.0f; m_max_dist = 300.0f;
m_duration = -1.0f;
m_positional = false; m_positional = false;
m_loaded = false; m_loaded = false;
m_file = file; m_file = file;
@ -72,10 +83,15 @@ SFXBuffer::SFXBuffer(const std::string& file,
node->get("positional", &m_positional ); node->get("positional", &m_positional );
node->get("volume", &m_gain ); node->get("volume", &m_gain );
node->get("max_dist", &m_max_dist ); node->get("max_dist", &m_max_dist );
} node->get("duration", &m_duration );
} // SFXBuffer(XMLNode)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
/** \brief load the buffer from file into OpenAL.
* \note If this buffer is already loaded, this call does nothing and
* returns false.
* \return Whether loading was successful.
*/
bool SFXBuffer::load() bool SFXBuffer::load()
{ {
if (UserConfigParams::m_sfx == false) return false; if (UserConfigParams::m_sfx == false) return false;
@ -103,9 +119,13 @@ bool SFXBuffer::load()
m_loaded = true; m_loaded = true;
return true; return true;
} } // load
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
/** \brief Frees the loaded buffer.
* Cannot appear in destructor because copy-constructors may be used,
* and the OpenAL source must not be deleted on a copy
*/
void SFXBuffer::unload() void SFXBuffer::unload()
{ {
@ -117,7 +137,7 @@ void SFXBuffer::unload()
} }
#endif #endif
m_loaded = false; m_loaded = false;
} } // unload
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
/** Load a vorbis file into an OpenAL buffer /** Load a vorbis file into an OpenAL buffer
@ -164,7 +184,7 @@ bool SFXBuffer::loadVorbisBuffer(const std::string &name, ALuint buffer)
if(!data) if(!data)
{ {
ov_clear(&oggFile); ov_clear(&oggFile);
Log::error("SFXBuffer", "[SFXBuffer] loadVorbisBuffer() - Error : LoadVorbisBuffer() - couldn't allocate decode buffer\n"); Log::error("SFXBuffer", "[SFXBuffer] Could not allocate decode buffer.");
return false; return false;
} }
@ -188,9 +208,21 @@ bool SFXBuffer::loadVorbisBuffer(const std::string &name, ALuint buffer)
ov_clear(&oggFile); ov_clear(&oggFile);
fclose(file); fclose(file);
// Allow the xml data to overwrite the duration, but if there is no
// duration (which is the norm), compute it:
if(m_duration < 0)
{
ALint buffer_size, frequency, bits_per_sample, channels;
alGetBufferi(buffer, AL_SIZE, &buffer_size );
alGetBufferi(buffer, AL_FREQUENCY, &frequency );
alGetBufferi(buffer, AL_CHANNELS, &channels );
alGetBufferi(buffer, AL_BITS, &bits_per_sample);
m_duration = float(buffer_size) / (frequency*channels*(bits_per_sample / 8));
}
return success; return success;
#else #else
return false; return false;
#endif #endif
} } // loadVorbisBuffer

View File

@ -46,18 +46,32 @@ class SFXBuffer
{ {
private: private:
LEAK_CHECK()
/** Whether the contents of the file was loaded */ /** Whether the contents of the file was loaded */
bool m_loaded; bool m_loaded;
/** The file that contains the OGG audio data */ /** The file that contains the OGG audio data */
std::string m_file; std::string m_file;
/** The openal buffer id. */
ALuint m_buffer; ALuint m_buffer;
/** If the sound is positional. */
bool m_positional; bool m_positional;
/** The roll-off value. */
float m_rolloff; float m_rolloff;
/** The volume gain value. */
float m_gain; float m_gain;
/** Maximum distance the sfx can be heard. */
float m_max_dist; float m_max_dist;
/** Duration of the sfx. */
float m_duration;
bool loadVorbisBuffer(const std::string &name, ALuint buffer); bool loadVorbisBuffer(const std::string &name, ALuint buffer);
public: public:
@ -70,41 +84,43 @@ public:
SFXBuffer(const std::string& file, SFXBuffer(const std::string& file,
const XMLNode* node); const XMLNode* node);
~SFXBuffer() ~SFXBuffer()
{ {
} } // ~SFXBuffer
/**
* \brief load the buffer from file into OpenAL.
* \note If this buffer is already loaded, this call does nothing and returns false
* \return whether loading was successful
*/
bool load();
/** bool load();
* \brief Frees the loaded buffer void unload();
* Cannot appear in destructor because copy-constructors may be used,
* and the OpenAL source must not be deleted on a copy
*/
void unload();
// ------------------------------------------------------------------------
/** \return whether this buffer was loaded from disk */ /** \return whether this buffer was loaded from disk */
bool isLoaded() const { return m_loaded; } bool isLoaded() const { return m_loaded; }
// ------------------------------------------------------------------------
/** Only returns a valid buffer if isLoaded() returned true */ /** Only returns a valid buffer if isLoaded() returned true */
ALuint getBufferID() const { return m_buffer; } ALuint getBufferID() const { return m_buffer; }
// ------------------------------------------------------------------------
/** Returns if the buffer is positional. */
bool isPositional() const { return m_positional; }
// ------------------------------------------------------------------------
/** Returns the rolloff value of this buffer. */
float getRolloff() const { return m_rolloff; }
// ------------------------------------------------------------------------
/** Returns the gain for this sfx. */
float getGain() const { return m_gain; }
// ------------------------------------------------------------------------
/** Returns the maximum distance this sfx can be heard. */
float getMaxDist() const { return m_max_dist; }
// ------------------------------------------------------------------------
/** Returns the file name of this buffer. */
const std::string& getFileName() const { return m_file; }
// ------------------------------------------------------------------------
/** Sets if this buffer is positional or not. */
void setPositional(bool positional) { m_positional = positional; }
// ------------------------------------------------------------------------
/** Returns how long this buffer will play. */
float getDuration() const { return m_duration; }
bool isPositional() const { return m_positional; } }; // class SFXBuffer
float getRolloff() const { return m_rolloff; }
float getGain() const { return m_gain; }
float getMaxDist() const { return m_max_dist; }
std::string getFileName() const { return m_file; }
void setPositional(bool positional) { m_positional = positional; }
LEAK_CHECK()
};
#endif // HEADER_SFX_BUFFER_HPP #endif // HEADER_SFX_BUFFER_HPP