]> git.mxchange.org Git - simgear.git/commitdiff
I had overlooked a few memory allocation/deallocation issues for audio buffers.
authorcurt <curt>
Mon, 10 May 2004 21:22:50 +0000 (21:22 +0000)
committercurt <curt>
Mon, 10 May 2004 21:22:50 +0000 (21:22 +0000)
Hopefully this helps clean those up.

simgear/sound/Makefile.am
simgear/sound/sample_openal.cxx
simgear/sound/sample_openal.hxx

index c570677c92f0da521c2f77c7622ada1dfe2f36bc..86aeebea329d917ee1766fa73056f7634ef945e9 100644 (file)
@@ -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 \
index 77f0737a5926141051b773a148f3852245506046..ffedaab4269c12858321c1b3689eacbb72b0d9d3 100644 (file)
@@ -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);
 }
index 0a5afdacd117b474fcd130bbf27ac7b8223b17db..8646e60d8e484512f2b61366b88404e520ded0dd 100644 (file)
@@ -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();
 
     /**