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
//----------------------------------------------------------------------------
/** 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,
bool positional,
float rolloff,
float max_width,
float max_dist,
float gain)
{
m_buffer = 0;
m_gain = 1.0f;
m_rolloff = 0.1f;
m_loaded = false;
m_max_dist = max_width;
m_max_dist = max_dist;
m_duration = -1.0f;
m_file = file;
m_rolloff = rolloff;
m_positional = positional;
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,
const XMLNode* node)
{
@ -64,6 +74,7 @@ SFXBuffer::SFXBuffer(const std::string& file,
m_gain = 1.0f;
m_rolloff = 0.1f;
m_max_dist = 300.0f;
m_duration = -1.0f;
m_positional = false;
m_loaded = false;
m_file = file;
@ -72,10 +83,15 @@ SFXBuffer::SFXBuffer(const std::string& file,
node->get("positional", &m_positional );
node->get("volume", &m_gain );
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()
{
if (UserConfigParams::m_sfx == false) return false;
@ -103,9 +119,13 @@ bool SFXBuffer::load()
m_loaded = 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()
{
@ -117,7 +137,7 @@ void SFXBuffer::unload()
}
#endif
m_loaded = false;
}
} // unload
//----------------------------------------------------------------------------
/** Load a vorbis file into an OpenAL buffer
@ -164,7 +184,7 @@ bool SFXBuffer::loadVorbisBuffer(const std::string &name, ALuint buffer)
if(!data)
{
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;
}
@ -188,9 +208,21 @@ bool SFXBuffer::loadVorbisBuffer(const std::string &name, ALuint buffer)
ov_clear(&oggFile);
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;
#else
return false;
#endif
}
} // loadVorbisBuffer

View File

@ -46,18 +46,32 @@ class SFXBuffer
{
private:
LEAK_CHECK()
/** Whether the contents of the file was loaded */
bool m_loaded;
/** The file that contains the OGG audio data */
std::string m_file;
/** The openal buffer id. */
ALuint m_buffer;
/** If the sound is positional. */
bool m_positional;
/** The roll-off value. */
float m_rolloff;
/** The volume gain value. */
float m_gain;
/** Maximum distance the sfx can be heard. */
float m_max_dist;
/** Duration of the sfx. */
float m_duration;
bool loadVorbisBuffer(const std::string &name, ALuint buffer);
public:
@ -70,41 +84,43 @@ public:
SFXBuffer(const std::string& file,
const XMLNode* node);
~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();
/**
* \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 unload();
bool load();
void unload();
// ------------------------------------------------------------------------
/** \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 */
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; }
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()
};
}; // class SFXBuffer
#endif // HEADER_SFX_BUFFER_HPP