Work on making OpenAL a compile-time option.

ENABLE_SOUND=0 now works when configuring SimGear, and libSimGearScene can be linked without OpenAL. However, more testing of FlightGear in this mode is needed before it's officially supported!
This commit is contained in:
James Turner 2012-12-07 15:26:24 -05:00
parent fd27e7bd43
commit 24d2431522
4 changed files with 44 additions and 15 deletions

View File

@ -177,6 +177,7 @@ else()
if (ENABLE_SOUND) if (ENABLE_SOUND)
find_package(OpenAL REQUIRED) find_package(OpenAL REQUIRED)
message(STATUS "Sound support: ENABLED")
endif(ENABLE_SOUND) endif(ENABLE_SOUND)
find_package(OpenSceneGraph 3.0.0 REQUIRED osgText osgSim osgDB osgParticle osgGA osgUtil) find_package(OpenSceneGraph 3.0.0 REQUIRED osgText osgSim osgDB osgParticle osgGA osgUtil)

View File

@ -21,7 +21,7 @@ set(SOURCES
simgear_scene_component(sound sound "${SOURCES}" "${HEADERS}") simgear_scene_component(sound sound "${SOURCES}" "${HEADERS}")
if(ENABLE_TESTS) if(ENABLE_TESTS AND ENABLE_SOUND)
if (SIMGEAR_SHARED) if (SIMGEAR_SHARED)
set(SOUND_TEST_LIBS SimGearScene) set(SOUND_TEST_LIBS SimGearScene)
@ -45,4 +45,4 @@ create_test(openal_test1)
create_test(openal_test2) create_test(openal_test2)
create_test(openal_test3) create_test(openal_test3)
create_test(openal_test4) create_test(openal_test4)
endif(ENABLE_TESTS) endif()

View File

@ -284,13 +284,14 @@ ALvoid* loadWAVFromFile(const SGPath& path, ALenum& format, ALsizei& size, ALflo
ALuint createBufferFromFile(const SGPath& path) ALuint createBufferFromFile(const SGPath& path)
{ {
ALuint buffer = -1;
#ifdef ENABLE_SOUND
ALenum format; ALenum format;
ALsizei size; ALsizei size;
ALfloat sampleFrequency; ALfloat sampleFrequency;
ALvoid* data = loadWAVFromFile(path, format, size, sampleFrequency); ALvoid* data = loadWAVFromFile(path, format, size, sampleFrequency);
assert(data); assert(data);
ALuint buffer;
alGenBuffers(1, &buffer); alGenBuffers(1, &buffer);
if (alGetError() != AL_NO_ERROR) { if (alGetError() != AL_NO_ERROR) {
free(data); free(data);
@ -303,7 +304,7 @@ ALuint createBufferFromFile(const SGPath& path)
free(data); free(data);
throw sg_io_exception("OpenAL setting buffer data failed", sg_location(path.str())); throw sg_io_exception("OpenAL setting buffer data failed", sg_location(path.str()));
} }
#endif
return buffer; return buffer;
} }

View File

@ -148,8 +148,9 @@ SGSoundMgr::~SGSoundMgr() {
} }
// initialize the sound manager // initialize the sound manager
void SGSoundMgr::init() { void SGSoundMgr::init()
{
#ifdef ENABLE_SOUND
if (is_working()) if (is_working())
{ {
SG_LOG( SG_SOUND, SG_ALERT, "Oops, OpenAL sound manager is already initialized." ); SG_LOG( SG_SOUND, SG_ALERT, "Oops, OpenAL sound manager is already initialized." );
@ -247,10 +248,12 @@ void SGSoundMgr::init() {
if (d->_free_sources.empty()) { if (d->_free_sources.empty()) {
SG_LOG(SG_SOUND, SG_ALERT, "Unable to grab any OpenAL sources!"); SG_LOG(SG_SOUND, SG_ALERT, "Unable to grab any OpenAL sources!");
} }
#endif
} }
void SGSoundMgr::reinit() void SGSoundMgr::reinit()
{ {
#ifndef ENABLE_SOUND
bool was_active = _active; bool was_active = _active;
if (was_active) if (was_active)
@ -263,9 +266,12 @@ void SGSoundMgr::reinit()
if (was_active) if (was_active)
resume(); resume();
#endif
} }
void SGSoundMgr::activate() { void SGSoundMgr::activate()
{
#ifdef ENABLE_SOUND
if ( is_working() ) { if ( is_working() ) {
_active = true; _active = true;
@ -276,11 +282,13 @@ void SGSoundMgr::activate() {
sgrp->activate(); sgrp->activate();
} }
} }
#endif
} }
// stop the sound manager // stop the sound manager
void SGSoundMgr::stop() { void SGSoundMgr::stop()
{
#ifdef ENABLE_SOUND
// first stop all sample groups // first stop all sample groups
sample_group_map_iterator sample_grp_current = d->_sample_groups.begin(); sample_group_map_iterator sample_grp_current = d->_sample_groups.begin();
sample_group_map_iterator sample_grp_end = d->_sample_groups.end(); sample_group_map_iterator sample_grp_end = d->_sample_groups.end();
@ -319,9 +327,12 @@ void SGSoundMgr::stop() {
_renderer = "unknown"; _renderer = "unknown";
_vendor = "unknown"; _vendor = "unknown";
} }
#endif
} }
void SGSoundMgr::suspend() { void SGSoundMgr::suspend()
{
#ifdef ENABLE_SOUND
if (is_working()) { if (is_working()) {
sample_group_map_iterator sample_grp_current = d->_sample_groups.begin(); sample_group_map_iterator sample_grp_current = d->_sample_groups.begin();
sample_group_map_iterator sample_grp_end = d->_sample_groups.end(); sample_group_map_iterator sample_grp_end = d->_sample_groups.end();
@ -331,9 +342,12 @@ void SGSoundMgr::suspend() {
} }
_active = false; _active = false;
} }
#endif
} }
void SGSoundMgr::resume() { void SGSoundMgr::resume()
{
#ifdef ENABLE_SOUND
if (is_working()) { if (is_working()) {
sample_group_map_iterator sample_grp_current = d->_sample_groups.begin(); sample_group_map_iterator sample_grp_current = d->_sample_groups.begin();
sample_group_map_iterator sample_grp_end = d->_sample_groups.end(); sample_group_map_iterator sample_grp_end = d->_sample_groups.end();
@ -343,10 +357,13 @@ void SGSoundMgr::resume() {
} }
_active = true; _active = true;
} }
#endif
} }
// run the audio scheduler // run the audio scheduler
void SGSoundMgr::update( double dt ) { void SGSoundMgr::update( double dt )
{
#ifdef ENABLE_SOUND
if (_active) { if (_active) {
alcSuspendContext(d->_context); alcSuspendContext(d->_context);
@ -389,6 +406,7 @@ if (isNaN(_velocity.data())) printf("NaN in listener velocity\n");
alcProcessContext(d->_context); alcProcessContext(d->_context);
} }
#endif
} }
// add a sample group, return true if successful // add a sample group, return true if successful
@ -486,6 +504,7 @@ void SGSoundMgr::release_source( unsigned int source )
it = std::find(d->_sources_in_use.begin(), d->_sources_in_use.end(), source); it = std::find(d->_sources_in_use.begin(), d->_sources_in_use.end(), source);
if ( it != d->_sources_in_use.end() ) { if ( it != d->_sources_in_use.end() ) {
#ifdef ENABLE_SOUND
ALint result; ALint result;
alGetSourcei( source, AL_SOURCE_STATE, &result ); alGetSourcei( source, AL_SOURCE_STATE, &result );
@ -495,6 +514,7 @@ void SGSoundMgr::release_source( unsigned int source )
alSourcei( source, AL_BUFFER, 0 ); // detach the associated buffer alSourcei( source, AL_BUFFER, 0 ); // detach the associated buffer
testForALError("release_source"); testForALError("release_source");
#endif
d->_free_sources.push_back( source ); d->_free_sources.push_back( source );
d->_sources_in_use.erase( it ); d->_sources_in_use.erase( it );
} }
@ -503,7 +523,7 @@ void SGSoundMgr::release_source( unsigned int source )
unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample) unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample)
{ {
ALuint buffer = NO_BUFFER; ALuint buffer = NO_BUFFER;
#ifdef ENABLE_SOUND
if ( !sample->is_valid_buffer() ) { if ( !sample->is_valid_buffer() ) {
// sample was not yet loaded or removed again // sample was not yet loaded or removed again
string sample_name = sample->get_sample_name(); string sample_name = sample->get_sample_name();
@ -562,7 +582,7 @@ unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample)
else { else {
buffer = sample->get_buffer(); buffer = sample->get_buffer();
} }
#endif
return buffer; return buffer;
} }
@ -580,8 +600,10 @@ void SGSoundMgr::release_buffer(SGSoundSample *sample)
sample->no_valid_buffer(); sample->no_valid_buffer();
buffer_it->second.refctr--; buffer_it->second.refctr--;
if (buffer_it->second.refctr == 0) { if (buffer_it->second.refctr == 0) {
#ifdef ENABLE_SOUND
ALuint buffer = buffer_it->second.id; ALuint buffer = buffer_it->second.id;
alDeleteBuffers(1, &buffer); alDeleteBuffers(1, &buffer);
#endif
d->_buffers.erase( buffer_it ); d->_buffers.erase( buffer_it );
testForALError("release buffer"); testForALError("release buffer");
} }
@ -623,6 +645,7 @@ bool SGSoundMgr::load(const string &samplepath, void **dbuf, int *fmt,
vector<const char*> SGSoundMgr::get_available_devices() vector<const char*> SGSoundMgr::get_available_devices()
{ {
vector<const char*> devices; vector<const char*> devices;
#ifdef ENABLE_SOUND
const ALCchar *s; const ALCchar *s;
if (alcIsExtensionPresent(NULL, "ALC_enumerate_all_EXT") == AL_TRUE) { if (alcIsExtensionPresent(NULL, "ALC_enumerate_all_EXT") == AL_TRUE) {
@ -642,7 +665,7 @@ vector<const char*> SGSoundMgr::get_available_devices()
} }
devices.push_back(ptr); devices.push_back(ptr);
} }
#endif
return devices; return devices;
} }
@ -659,17 +682,20 @@ bool SGSoundMgr::testForError(void *p, string s)
bool SGSoundMgr::testForALError(string s) bool SGSoundMgr::testForALError(string s)
{ {
#ifdef ENABLE_SOUND
ALenum error = alGetError(); ALenum error = alGetError();
if (error != AL_NO_ERROR) { if (error != AL_NO_ERROR) {
SG_LOG( SG_SOUND, SG_ALERT, "AL Error (sound manager): " SG_LOG( SG_SOUND, SG_ALERT, "AL Error (sound manager): "
<< alGetString(error) << " at " << s); << alGetString(error) << " at " << s);
return true; return true;
} }
#endif
return false; return false;
} }
bool SGSoundMgr::testForALCError(string s) bool SGSoundMgr::testForALCError(string s)
{ {
#ifdef ENABLE_SOUND
ALCenum error; ALCenum error;
error = alcGetError(d->_device); error = alcGetError(d->_device);
if (error != ALC_NO_ERROR) { if (error != ALC_NO_ERROR) {
@ -678,6 +704,7 @@ bool SGSoundMgr::testForALCError(string s)
<< s); << s);
return true; return true;
} }
#endif
return false; return false;
} }