From 28d7a4cd5b4bc3df5d034ed9472231d91710a6b6 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 14 Jul 2008 20:40:20 +0000 Subject: failed attempt at multiple simultanious sounds --- src/audio/audio.cc | 23 ++++++++++++++++++----- src/audio/buffers.cc | 11 +++++++---- src/audio/buffers.h | 3 +++ src/audio/sources.cc | 17 +++++++++++++++-- src/audio/sources.h | 9 +++------ 5 files changed, 46 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/audio/audio.cc b/src/audio/audio.cc index 729f3d2..8880780 100644 --- a/src/audio/audio.cc +++ b/src/audio/audio.cc @@ -71,12 +71,25 @@ void shutdown() void play(const char *name) { - Buffers::load(std::string(name)); + if (!audio_context) + return; - if (Sources::available(0)) { - alSourceRewind(Sources::ui()); - Buffers::bind(Sources::ui(), std::string(name)); - alSourcePlay(Sources::ui()); + size_t buffer = Buffers::load(std::string(name)); + + ALint state; + for (size_t i = 0; i < MAXUISOURCES; i++) { + ALuint source = Sources::source(i); + alGetSourcei(source , AL_SOURCE_STATE , &state); + if (state != AL_PLAYING) { + //alSourceRewind(Sources::source(i)); + Buffers::bind(source, buffer); + alSourcef(source, AL_PITCH, 1.0); + alSourcef(source, AL_GAIN, 1.0); + alSourcePlay(source); + + } else { + con_debug << "source " << Sources::source(i) << " playing!" << std::endl; + } } } diff --git a/src/audio/buffers.cc b/src/audio/buffers.cc index 5ea1b05..dd83000 100644 --- a/src/audio/buffers.cc +++ b/src/audio/buffers.cc @@ -99,15 +99,18 @@ size_t Buffers::find(std::string name) return id; } -void Buffers::bind(ALuint source, std::string name) +void Buffers::bind(ALuint source, size_t id) { int error; - size_t id = find(name); alSourcei(source, AL_BUFFER, buffers[id]); if ((error = alGetError()) != AL_NO_ERROR) { - con_warn << "Error " << std::hex << error << " binding " << name - << " source " << source << " buffer " << buffers[id] << std::endl; + con_warn << "Error " << std::hex << error << " binding buffer " << buffers[id] << " to source " << source << std::endl; } } +void Buffers::bind(ALuint source, std::string name) +{ + bind(source, find(name)); +} + } diff --git a/src/audio/buffers.h b/src/audio/buffers.h index 53137e9..cc7a6c8 100644 --- a/src/audio/buffers.h +++ b/src/audio/buffers.h @@ -33,6 +33,9 @@ public: /// bind a buffer to a source static void bind(ALuint source, std::string name); + /// bind a specific buffer to a source + static void bind(ALuint source, size_t id); + private: static void clear(); diff --git a/src/audio/sources.cc b/src/audio/sources.cc index 209bc94..dd736e7 100644 --- a/src/audio/sources.cc +++ b/src/audio/sources.cc @@ -24,7 +24,16 @@ void Sources::init() con_warn << "Error " << std::hex << error << " initializing OpenAL sources!" << std::endl; return; } - source_available[0] = true; + + // all sources are available for use + for (size_t i=MAXUISOURCES; i < MAXSOURCES; i++) { + source_available[i] = true; + } + + // reserve ui sound sources + for (size_t i=0; i < MAXUISOURCES; i++) { + source_available[i] = false; + } } void Sources::shutdown() @@ -36,7 +45,11 @@ void Sources::shutdown() void Sources::clear() { memset(sources,0, sizeof(sources)); - memset(source_available, 0 , sizeof(source_available)); + + // all sources are available for use + for (size_t i=0; i < MAXSOURCES; i++) { + source_available[i] = false; + } } diff --git a/src/audio/sources.h b/src/audio/sources.h index 08a2a41..302d5d5 100644 --- a/src/audio/sources.h +++ b/src/audio/sources.h @@ -16,6 +16,7 @@ namespace audio { const size_t MAXSOURCES = 32; +const size_t MAXUISOURCES = 4; /// OpenAL sources wrapper class class Sources { @@ -23,13 +24,9 @@ public: static void init(); static void shutdown(); - static bool available(size_t index) { return source_available[index]; } - - static ALuint source(size_t index) { return sources[index]; } - - /// the sources for user interface sounds - static inline ALuint ui() { return sources[0]; } + static inline bool available(size_t index) { return source_available[index]; } + static inline ALuint source(size_t index) { return sources[index]; } private: static void clear(); -- cgit v1.2.3