Update the SoundSample api so we can request that a copy of the sample be
kept in memory and accessible.
This commit is contained in:
@@ -7,37 +7,37 @@
|
|||||||
int main( int argc, char *argv[] ) {
|
int main( int argc, char *argv[] ) {
|
||||||
SGSoundMgr sm;
|
SGSoundMgr sm;
|
||||||
|
|
||||||
SGSoundSample sample1( ".", "jet.wav" );
|
SGSoundSample sample1( ".", "jet.wav", true );
|
||||||
sample1.set_volume(0.5);
|
sample1.set_volume(0.5);
|
||||||
sample1.set_volume(0.2);
|
sample1.set_volume(0.2);
|
||||||
sample1.play_looped();
|
sample1.play_looped();
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
SGSoundSample sample2( ".", "jet.wav" );
|
SGSoundSample sample2( ".", "jet.wav", true );
|
||||||
sample2.set_volume(0.5);
|
sample2.set_volume(0.5);
|
||||||
sample2.set_pitch(0.4);
|
sample2.set_pitch(0.4);
|
||||||
sample2.play_looped();
|
sample2.play_looped();
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
SGSoundSample sample3( ".", "jet.wav" );
|
SGSoundSample sample3( ".", "jet.wav", true );
|
||||||
sample3.set_volume(0.5);
|
sample3.set_volume(0.5);
|
||||||
sample3.set_pitch(0.8);
|
sample3.set_pitch(0.8);
|
||||||
sample3.play_looped();
|
sample3.play_looped();
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
SGSoundSample sample4( ".", "jet.wav" );
|
SGSoundSample sample4( ".", "jet.wav", true );
|
||||||
sample4.set_volume(0.5);
|
sample4.set_volume(0.5);
|
||||||
sample4.set_pitch(1.2);
|
sample4.set_pitch(1.2);
|
||||||
sample4.play_looped();
|
sample4.play_looped();
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
SGSoundSample sample5( ".", "jet.wav" );
|
SGSoundSample sample5( ".", "jet.wav", true );
|
||||||
sample5.set_volume(0.5);
|
sample5.set_volume(0.5);
|
||||||
sample5.set_pitch(1.6);
|
sample5.set_pitch(1.6);
|
||||||
sample5.play_looped();
|
sample5.play_looped();
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
SGSoundSample sample6( ".", "jet.wav" );
|
SGSoundSample sample6( ".", "jet.wav", true );
|
||||||
sample6.set_volume(0.5);
|
sample6.set_volume(0.5);
|
||||||
sample6.set_pitch(2.0);
|
sample6.set_pitch(2.0);
|
||||||
sample6.play_looped();
|
sample6.play_looped();
|
||||||
|
|||||||
@@ -61,7 +61,8 @@ static void print_openal_error( ALuint error ) {
|
|||||||
|
|
||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
SGSoundSample::SGSoundSample( const char *path, const char *file ) :
|
SGSoundSample::SGSoundSample( const char *path, const char *file,
|
||||||
|
bool cleanup ) :
|
||||||
pitch(1.0),
|
pitch(1.0),
|
||||||
volume(1.0),
|
volume(1.0),
|
||||||
loop(AL_FALSE)
|
loop(AL_FALSE)
|
||||||
@@ -71,6 +72,8 @@ SGSoundSample::SGSoundSample( const char *path, const char *file ) :
|
|||||||
samplepath.append( file );
|
samplepath.append( file );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sample_name = samplepath.str();
|
||||||
|
|
||||||
SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
|
SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
|
||||||
<< samplepath.str() );
|
<< samplepath.str() );
|
||||||
|
|
||||||
@@ -108,7 +111,10 @@ SGSoundSample::SGSoundSample( const char *path, const char *file ) :
|
|||||||
throw sg_exception("Failed to buffer data.");
|
throw sg_exception("Failed to buffer data.");
|
||||||
}
|
}
|
||||||
|
|
||||||
alutUnloadWAV( format, data, size, freq );
|
if ( cleanup ) {
|
||||||
|
alutUnloadWAV( format, data, size, freq );
|
||||||
|
data = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// Bind buffer with a source.
|
// Bind buffer with a source.
|
||||||
alGenSources(1, &source);
|
alGenSources(1, &source);
|
||||||
@@ -133,13 +139,15 @@ SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) :
|
|||||||
{
|
{
|
||||||
SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
|
SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
|
||||||
|
|
||||||
|
sample_name = "unknown, generated from data";
|
||||||
|
|
||||||
source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
|
source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
|
||||||
source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
|
source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
|
||||||
|
|
||||||
// Load wav data into a buffer.
|
// Load wav data into a buffer.
|
||||||
alGenBuffers(1, &buffer);
|
alGenBuffers(1, &buffer);
|
||||||
if (alGetError() != AL_NO_ERROR) {
|
if (alGetError() != AL_NO_ERROR) {
|
||||||
SG_LOG( SG_GENERAL, SG_ALERT, "Error in alGenBuffers()" );
|
throw sg_exception("Failed to gen buffer." );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,6 +176,9 @@ SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) :
|
|||||||
// destructor
|
// destructor
|
||||||
SGSoundSample::~SGSoundSample() {
|
SGSoundSample::~SGSoundSample() {
|
||||||
SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
|
SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
|
||||||
|
if ( data != NULL ) {
|
||||||
|
delete data;
|
||||||
|
}
|
||||||
alDeleteSources(1, &source);
|
alDeleteSources(1, &source);
|
||||||
alDeleteBuffers(1, &buffer);
|
alDeleteBuffers(1, &buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,8 @@
|
|||||||
|
|
||||||
#include <simgear/compiler.h>
|
#include <simgear/compiler.h>
|
||||||
|
|
||||||
|
#include STL_STRING
|
||||||
|
|
||||||
#if defined(__APPLE__)
|
#if defined(__APPLE__)
|
||||||
# define AL_ILLEGAL_ENUM AL_INVALID_ENUM
|
# define AL_ILLEGAL_ENUM AL_INVALID_ENUM
|
||||||
# define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
|
# define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
|
||||||
@@ -46,6 +48,8 @@
|
|||||||
|
|
||||||
#include <simgear/debug/logstream.hxx>
|
#include <simgear/debug/logstream.hxx>
|
||||||
|
|
||||||
|
SG_USING_STD(string);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* manages everything we need to know for an individual sound sample
|
* manages everything we need to know for an individual sound sample
|
||||||
@@ -55,6 +59,8 @@ class SGSoundSample {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
string sample_name;
|
||||||
|
|
||||||
// Buffers hold sound data.
|
// Buffers hold sound data.
|
||||||
ALuint buffer;
|
ALuint buffer;
|
||||||
|
|
||||||
@@ -79,14 +85,22 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SGSoundSample( const char *path, const char *file );
|
/**
|
||||||
|
* Constructor
|
||||||
|
* @param path Path name to sound
|
||||||
|
* @param file File name of sound
|
||||||
|
* @param cleanup Request clean up the intermediate data (this
|
||||||
|
should usually be true unless you want to manipulate the data
|
||||||
|
later.)
|
||||||
|
*/
|
||||||
|
SGSoundSample( const char *path, const char *file, bool cleanup );
|
||||||
SGSoundSample( unsigned char *_data, int len, int _freq );
|
SGSoundSample( unsigned char *_data, int len, int _freq );
|
||||||
~SGSoundSample();
|
~SGSoundSample();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start playing this sample.
|
* Start playing this sample.
|
||||||
*
|
*
|
||||||
* @param looped Define wether the sound should be played in a loop.
|
* @param _loop Define wether the sound should be played in a loop.
|
||||||
*/
|
*/
|
||||||
void play( bool _loop );
|
void play( bool _loop );
|
||||||
|
|
||||||
@@ -118,7 +132,7 @@ public:
|
|||||||
alGetSourcei( source, AL_SOURCE_STATE, &result );
|
alGetSourcei( source, AL_SOURCE_STATE, &result );
|
||||||
if ( alGetError() != AL_NO_ERROR) {
|
if ( alGetError() != AL_NO_ERROR) {
|
||||||
SG_LOG( SG_GENERAL, SG_ALERT,
|
SG_LOG( SG_GENERAL, SG_ALERT,
|
||||||
"Oops AL error in sample is_playing()!" );
|
"Oops AL error in sample is_playing(): " << sample_name );
|
||||||
}
|
}
|
||||||
return (result == AL_PLAYING) ;
|
return (result == AL_PLAYING) ;
|
||||||
}
|
}
|
||||||
@@ -139,7 +153,8 @@ public:
|
|||||||
alSourcef( source, AL_PITCH, pitch );
|
alSourcef( source, AL_PITCH, pitch );
|
||||||
if ( alGetError() != AL_NO_ERROR) {
|
if ( alGetError() != AL_NO_ERROR) {
|
||||||
SG_LOG( SG_GENERAL, SG_ALERT,
|
SG_LOG( SG_GENERAL, SG_ALERT,
|
||||||
"Oops AL error in sample set_pitch()! " << p );
|
"Oops AL error in sample set_pitch()! " << p
|
||||||
|
<< " for " << sample_name );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +171,8 @@ public:
|
|||||||
alSourcef( source, AL_GAIN, volume );
|
alSourcef( source, AL_GAIN, volume );
|
||||||
if ( alGetError() != AL_NO_ERROR) {
|
if ( alGetError() != AL_NO_ERROR) {
|
||||||
SG_LOG( SG_GENERAL, SG_ALERT,
|
SG_LOG( SG_GENERAL, SG_ALERT,
|
||||||
"Oops AL error in sample set_volume()!" );
|
"Oops AL error in sample set_volume()! " << v
|
||||||
|
<< " for " << sample_name );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -239,7 +239,8 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr,
|
|||||||
_mgr = sndmgr;
|
_mgr = sndmgr;
|
||||||
if ( (_sample = _mgr->find(_name)) == NULL ) {
|
if ( (_sample = _mgr->find(_name)) == NULL ) {
|
||||||
_sample = new SGSoundSample( path.c_str(),
|
_sample = new SGSoundSample( path.c_str(),
|
||||||
node->getStringValue("path", "") );
|
node->getStringValue("path", ""),
|
||||||
|
true );
|
||||||
_mgr->add( _sample, _name );
|
_mgr->add( _sample, _name );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user