int main( int argc, char *argv[] ) {
SGSoundMgr sm;
- SGSoundSample sample1( ".", "jet.wav", true );
+ SGSoundSample sample1( ".", "jet.wav" );
sample1.set_volume(0.5);
sample1.set_volume(0.2);
sample1.play_looped();
sleep(1);
- SGSoundSample sample2( ".", "jet.wav", true );
+ SGSoundSample sample2( ".", "jet.wav" );
sample2.set_volume(0.5);
sample2.set_pitch(0.4);
sample2.play_looped();
sleep(1);
- SGSoundSample sample3( ".", "jet.wav", true );
+ SGSoundSample sample3( ".", "jet.wav" );
sample3.set_volume(0.5);
sample3.set_pitch(0.8);
sample3.play_looped();
sleep(1);
- SGSoundSample sample4( ".", "jet.wav", true );
+ SGSoundSample sample4( ".", "jet.wav" );
sample4.set_volume(0.5);
sample4.set_pitch(1.2);
sample4.play_looped();
sleep(1);
- SGSoundSample sample5( ".", "jet.wav", true );
+ SGSoundSample sample5( ".", "jet.wav" );
sample5.set_volume(0.5);
sample5.set_pitch(1.6);
sample5.play_looped();
sleep(1);
- SGSoundSample sample6( ".", "jet.wav", true );
+ SGSoundSample sample6( ".", "jet.wav" );
sample6.set_volume(0.5);
sample6.set_pitch(2.0);
sample6.play_looped();
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),
if ( strlen(file) ) {
samplepath.append( file );
}
-
sample_name = samplepath.str();
SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
//
// pre 1.0 alut version
//
-# 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_exception("Failed to load wav file.");
- }
+ ALvoid* data = load_file(path, file)
// Copy data to the internal OpenAL buffer
alBufferData( buffer, format, data, size, freq );
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),
buffer(0),
source(0),
pitch(1.0),
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;
+ free(_data);
}
print_openal_error("constructor return");
// destructor
SGSoundSample::~SGSoundSample() {
SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
- alDeleteBuffers(1, &buffer);
+ if (buffer)
+ alDeleteBuffers(1, &buffer);
}
if ( playing ) {
return true;
}
+ if ( buffer == 0 ) {
+ return false;
+ }
// Bind buffer with a source.
alGetError();
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_exception("Failed to load wav file.");
+ }
+ 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_exception("Failed to load wav file.");
+ }
+#endif
+
+ return data;
+}
+
// configuration values
ALenum format;
ALsizei size;
- ALvoid* data;
ALsizei freq;
double pitch;
public:
+ /**
+ * Empty constructor, can be used to read data to the systems
+ * memory and not to the driver.
+ */
+ SGSoundSample();
+
/**
* 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( const char *path, const char *file );
/**
* Constructor.
return size;
}
- /**
- * Return a pointer to the raw data
- */
- inline char *get_data() {
- return (char *)data;
- }
-
/**
* Set position of sound source (uses same coordinate system as opengl)
*/
* no longer audible.
*/
void set_max_dist( ALfloat dist );
+
+ /**
+ * Load a sound file into a memory buffer only.
+ */
+ ALvoid* load_file(const char *path, const char *file);
};