From 331a4e44063fc4edab4c9639e5c1bc0d42e94fa0 Mon Sep 17 00:00:00 2001 From: curt Date: Mon, 10 May 2004 21:22:50 +0000 Subject: [PATCH] I had overlooked a few memory allocation/deallocation issues for audio buffers. Hopefully this helps clean those up. --- simgear/sound/Makefile.am | 1 + simgear/sound/sample_openal.cxx | 16 +++++++++++----- simgear/sound/sample_openal.hxx | 13 ++++++++++++- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/simgear/sound/Makefile.am b/simgear/sound/Makefile.am index c570677c..86aeebea 100644 --- a/simgear/sound/Makefile.am +++ b/simgear/sound/Makefile.am @@ -24,6 +24,7 @@ openal_test2_SOURCES = openal_test2.cxx openal_test1_LDADD = \ $(top_builddir)/simgear/debug/libsgdebug.a \ $(openal_LIBS) + openal_test2_LDADD = \ $(top_builddir)/simgear/sound/libsgsound.a \ $(top_builddir)/simgear/debug/libsgdebug.a \ diff --git a/simgear/sound/sample_openal.cxx b/simgear/sound/sample_openal.cxx index 77f0737a..ffedaab4 100644 --- a/simgear/sound/sample_openal.cxx +++ b/simgear/sound/sample_openal.cxx @@ -80,7 +80,7 @@ SGSoundSample::SGSoundSample( const char *path, const char *file, SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = " << samplepath.str() ); - 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; offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0; source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0; @@ -138,7 +138,8 @@ SGSoundSample::SGSoundSample( const char *path, const char *file, // constructor -SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) : +SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq, + bool cleanup) : data(NULL), pitch(1.0), volume(1.0), @@ -172,6 +173,14 @@ SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) : freq = _freq; alBufferData( buffer, format, data, size, freq ); + if (alGetError() != AL_NO_ERROR) { + throw sg_exception("Failed to buffer data."); + } + + if ( cleanup ) { + alutUnloadWAV( format, data, size, freq ); + data = NULL; + } // Bind buffer with a source. alGenSources(1, &source); @@ -195,9 +204,6 @@ SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) : // destructor SGSoundSample::~SGSoundSample() { SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" ); - if ( data != NULL ) { - free(data); - } alDeleteSources(1, &source); alDeleteBuffers(1, &buffer); } diff --git a/simgear/sound/sample_openal.hxx b/simgear/sound/sample_openal.hxx index 0a5afdac..8646e60d 100644 --- a/simgear/sound/sample_openal.hxx +++ b/simgear/sound/sample_openal.hxx @@ -101,7 +101,18 @@ public: later.) */ SGSoundSample( const char *path, const char *file, bool cleanup ); - SGSoundSample( unsigned char *_data, int len, int _freq ); + + /** + * Constructor. + * @param _data Pointer to a memory buffer containing the sample data + * @param len Byte length of array + * @param _freq Frequency of the provided data (bytes per second) + * @param cleanup Request clean up the intermediate data (this + should usually be true unless you want to manipulate the data + later.) + */ + SGSoundSample( unsigned char *_data, int len, int _freq, bool cleanup ); + ~SGSoundSample(); /** -- 2.39.5