X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fsound%2Fsample_openal.cxx;h=2eb4910d2bf73426a7e3ec4029c875790cb89c03;hb=c8953c6275647ab7a5593554bbb8cf5e9bd11279;hp=eaa5452afc7f5a73d4e5e6c9e0c84e9ba28fdf53;hpb=f6314d3124633a05ce249ece5fef6d4f7a07cb76;p=simgear.git diff --git a/simgear/sound/sample_openal.cxx b/simgear/sound/sample_openal.cxx index eaa5452a..2eb4910d 100644 --- a/simgear/sound/sample_openal.cxx +++ b/simgear/sound/sample_openal.cxx @@ -16,10 +16,13 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ +#ifdef HAVE_CONFIG_H +# include +#endif #if defined( __APPLE__ ) # define AL_ILLEGAL_ENUM AL_INVALID_ENUM @@ -63,11 +66,21 @@ static bool print_openal_error(const string &s = "unknown") { return error; } +// empry constructor +SGSoundSample::SGSoundSample() : + buffer(0), + source(0), + pitch(1.0), + volume(1.0), + reference_dist(500.0), + max_dist(3000.), + loop(AL_FALSE), + playing(false) +{ +} // constructor -SGSoundSample::SGSoundSample( const char *path, const char *file, - bool cleanup ) : - data(NULL), +SGSoundSample::SGSoundSample( const char *path, const char *file) : buffer(0), source(0), pitch(1.0), @@ -81,7 +94,6 @@ SGSoundSample::SGSoundSample( const char *path, const char *file, if ( strlen(file) ) { samplepath.append( file ); } - sample_name = samplepath.str(); SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = " @@ -103,36 +115,37 @@ SGSoundSample::SGSoundSample( const char *path, const char *file, } // Load the sample file -#if defined (__APPLE__) - alutLoadWAVFile( (ALbyte *)samplepath.c_str(), - &format, &data, &size, &freq ); +#if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1 + + buffer = alutCreateBufferFromFile(samplepath.c_str()); + if (buffer == AL_NONE) { + ALenum error = alutGetError (); + print_openal_error("constructor (alutCreateBufferFromFile)"); + throw sg_io_exception("Failed to load wav file: ", + sg_location(string(alutGetErrorString (error)))); + } + #else - alutLoadWAVFile( (ALbyte *)samplepath.c_str(), - &format, &data, &size, &freq, &loop ); -#endif - if ( print_openal_error("constructor (alutLoadWAVFile)") ) { - throw sg_exception("Failed to load wav file."); - } + // + // pre 1.0 alut version + // + ALvoid* data = load_file(path, file); // Copy data to the internal OpenAL buffer alBufferData( buffer, format, data, size, freq ); + if ( print_openal_error("constructor (alBufferData)") ) { throw sg_exception("Failed to buffer data."); } - if ( cleanup ) { - alutUnloadWAV( format, data, size, freq ); - data = NULL; - } + alutUnloadWAV( format, data, size, freq ); +#endif print_openal_error("constructor return"); } - // constructor -SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq, - bool cleanup) : - data(NULL), +SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) : buffer(0), source(0), pitch(1.0), @@ -163,19 +176,13 @@ SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq, format = AL_FORMAT_MONO8; size = len; - data = _data; freq = _freq; - alBufferData( buffer, format, data, size, freq ); + alBufferData( buffer, format, _data, size, freq ); if ( print_openal_error("constructor (alBufferData)") ) { throw sg_exception("Failed to buffer data."); } - if ( cleanup ) { - alutUnloadWAV( format, data, size, freq ); - data = NULL; - } - print_openal_error("constructor return"); } @@ -183,7 +190,8 @@ SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq, // destructor SGSoundSample::~SGSoundSample() { SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" ); - alDeleteBuffers(1, &buffer); + if (buffer) + alDeleteBuffers(1, &buffer); } @@ -224,6 +232,9 @@ SGSoundSample::bind_source() { if ( playing ) { return true; } + if ( buffer == 0 ) { + return false; + } // Bind buffer with a source. alGetError(); @@ -327,6 +338,9 @@ SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle, inner = inner_angle; outer = outer_angle; outergain = outer_gain; + direction[0] = dir[0]; + direction[1] = dir[1]; + direction[2] = dir[2]; if (playing) { alSourcefv( source, AL_DIRECTION, dir); alSourcef( source, AL_CONE_INNER_ANGLE, inner ); @@ -361,3 +375,39 @@ SGSoundSample::set_max_dist( ALfloat dist ) { alSourcef( source, AL_MAX_DISTANCE, max_dist ); } } + +ALvoid * +SGSoundSample::load_file(const char *path, const char *file) +{ + ALvoid* data = 0; + + SGPath samplepath( path ); + if ( strlen(file) ) { + samplepath.append( file ); + } + +#if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1 + ALfloat freqf; + data = alutLoadMemoryFromFile(samplepath.c_str(), &format, &size, &freqf ); + if (data == NULL) { + throw sg_io_exception("Failed to load wav file.", + sg_location(samplepath.str())); + } + freq = (ALsizei)freqf; +#else +# if defined (__APPLE__) + alutLoadWAVFile( (ALbyte *)samplepath.c_str(), + &format, &data, &size, &freq ); +# else + alutLoadWAVFile( (ALbyte *)samplepath.c_str(), + &format, &data, &size, &freq, &loop ); +# endif + if ( print_openal_error("constructor (alutLoadWAVFile)") ) { + throw sg_io_exception("Failed to load wav file.", + sg_location(samplepath.str())); + } +#endif + + return data; +} +