From 56b0856541446cbafee4eed9ef0ee9fb69af565a Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 8 Aug 2008 22:17:29 +0000 Subject: improved truster indicator, impulse engine sounds --- src/audio/audio.cc | 74 +++++++++++++++++++++++++++++++++++++++++++----------- src/audio/audio.h | 12 ++++++--- 2 files changed, 69 insertions(+), 17 deletions(-) (limited to 'src/audio') diff --git a/src/audio/audio.cc b/src/audio/audio.cc index 50d15c8..81a260c 100644 --- a/src/audio/audio.cc +++ b/src/audio/audio.cc @@ -81,19 +81,19 @@ void shutdown() } } -void play(const char *name) +size_t play(const char *name) { if (!audio_context) - return; + return 0; - size_t buffer = Buffers::load(std::string(name)); + size_t buffer_index = Buffers::load(std::string(name)); for (size_t i = 0; i < MAXUISOURCES; i++) { ALint srcstate = 0; ALuint source = Sources::source(i); alGetSourcei(source , AL_SOURCE_STATE , &srcstate); if (srcstate != AL_PLAYING) { - Buffers::bind(source, buffer); + Buffers::bind(source, buffer_index); alSourcef(source, AL_PITCH, 1.0); alSourcef(source, AL_GAIN, 1.0); alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE); @@ -101,30 +101,49 @@ void play(const char *name) alSourcefv(source, AL_POSITION, location); alSourceRewind(source); alSourcePlay(source); - return; + return buffer_index; } } + + return 0; } -void update_source(size_t source_index, math::Vector3f const & location, math::Vector3f const & velocity, float pitch, float gain) +size_t play(size_t source_index, size_t buffer_index, float pitch, float gain) { - if (!audio_context) - return; + if (!audio_context || !buffer_index) + return 0; ALuint source = Sources::source(source_index); - alSourcefv(source, AL_POSITION, location.ptr()); - //alSourcefv(source, AL_VELOCITY, velocity.ptr()); + ALint srcstate = 0; + alGetSourcei(source , AL_SOURCE_STATE , &srcstate); + if (srcstate == AL_PLAYING) { + alSourceStop(source); + } + Buffers::bind(source, buffer_index); + alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE); alSourcef(source, AL_PITCH, pitch); alSourcef(source, AL_GAIN, gain); + + alSourcei(source, AL_LOOPING, AL_FALSE); + alSourceRewind(source); + alSourcePlay(source); + + return buffer_index; } -void loop( size_t source_index, const char *name, float pitch, float gain) +size_t loop(size_t source_index, size_t buffer_index, float pitch, float gain) { - if (!audio_context) - return; + if (!audio_context || !buffer_index) + return 0; ALuint source = Sources::source(source_index); - Buffers::bind(source, Buffers::load(std::string(name))); + ALint srcstate = 0; + alGetSourcei(source , AL_SOURCE_STATE , &srcstate); + if (srcstate == AL_PLAYING) { + alSourceStop(source); + } + + Buffers::bind(source, buffer_index); //alSourcef(source, AL_REFERENCE_DISTANCE, 2.0f); // might be the cause of cracks in the sound alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE); @@ -134,6 +153,20 @@ void loop( size_t source_index, const char *name, float pitch, float gain) alSourcei(source, AL_LOOPING, AL_TRUE); alSourceRewind(source); alSourcePlay(source); + + return buffer_index; +} + +size_t loop(size_t source_index, const char *name, float pitch, float gain) +{ + if (!audio_context) + return 0; + + size_t buffer_index = Buffers::load(std::string(name)); + if (buffer_index) + loop(source_index, buffer_index, pitch, gain); + + return buffer_index; } void update_listener(math::Vector3f const &location, math::Axis const &axis, math::Vector3f const & velocity) @@ -151,5 +184,18 @@ void update_listener(math::Vector3f const &location, math::Axis const &axis, mat alListenerfv(AL_ORIENTATION, orientation); //alListenerfv(AL_VELOCITY, velocity.ptr()); } + + +void update_source(size_t source_index, math::Vector3f const & location, math::Vector3f const & velocity, float pitch, float gain) +{ + if (!audio_context) + return; + + ALuint source = Sources::source(source_index); + alSourcefv(source, AL_POSITION, location.ptr()); + //alSourcefv(source, AL_VELOCITY, velocity.ptr()); + alSourcef(source, AL_PITCH, pitch); + alSourcef(source, AL_GAIN, gain); +} } diff --git a/src/audio/audio.h b/src/audio/audio.h index a3d4010..c0479d3 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -32,11 +32,17 @@ void shutdown(); /// load an audio sample void load(const char *name); -/// play a previously loaded audio sample -void play(const char *name); +/// play a previously loaded audio sample on the ui channel +size_t play(const char *name); + +/// playe a sound from a specific buffer on a specific source +size_t play(size_t source_index, size_t buffer_index, float pitch=1.0f, float gain=1.0f); /// play a looping sound on a specified source -void loop( size_t source_index, const char *name, float pitch=1.0f, float gain=1.0f); +size_t loop( size_t source_index, const char *name, float pitch=1.0f, float gain=1.0f); + +/// play a looping sound from a specified buffer on a specified source +size_t loop(size_t source_index, size_t buffer_index, float pitch, float gain); /// update source parameters void update_source(size_t source_index, math::Vector3f const & location, math::Vector3f const & velocity, float pitch=1.0f, float gain=1.0f); -- cgit v1.2.3