From 9b127fc0a349972b1312866766f288534b1d47e4 Mon Sep 17 00:00:00 2001 From: Didier Raboud Date: Fri, 14 Dec 2012 15:47:46 +0100 Subject: Add FindLTDL cmake module As mentionned in FindLTDL.cmake, this is code that is hereby relicensed under GPLv3. closes #68 --- src/sound/plugins/oalsound/test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sound') diff --git a/src/sound/plugins/oalsound/test/CMakeLists.txt b/src/sound/plugins/oalsound/test/CMakeLists.txt index d10169b..f7b165c 100644 --- a/src/sound/plugins/oalsound/test/CMakeLists.txt +++ b/src/sound/plugins/oalsound/test/CMakeLists.txt @@ -8,4 +8,4 @@ add_executable(plugin_test plugin_test.cpp ../../../../common/iman.cpp ../../../ include_directories(".") include_directories("../../../../") -target_link_libraries(plugin_test ltdl) +target_link_libraries(plugin_test ${LTDL_LIBRARY}) -- cgit v1.2.3-1-g7c22 From 3c976c040b48caa9f3e75d12a6502e39193ce9a6 Mon Sep 17 00:00:00 2001 From: Didier Raboud Date: Wed, 19 Dec 2012 16:52:11 +0100 Subject: Don't enforce CMake build type on users. Only set CMAKE_BUILD_TYPE to Debug when no value is provided. --- src/sound/plugins/oalsound/test/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/sound') diff --git a/src/sound/plugins/oalsound/test/CMakeLists.txt b/src/sound/plugins/oalsound/test/CMakeLists.txt index f7b165c..27ab756 100644 --- a/src/sound/plugins/oalsound/test/CMakeLists.txt +++ b/src/sound/plugins/oalsound/test/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 2.8) -set(CMAKE_BUILD_TYPE debug) +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE debug) +endif(NOT CMAKE_BUILD_TYPE) set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0 -std=c++11 -rdynamic") add_executable(plugin_test plugin_test.cpp ../../../../common/iman.cpp ../../../../common/logger.cpp ../../../../plugins/pluginloader.cpp) -- cgit v1.2.3-1-g7c22 From 7e9ef0c042fd14373a129ca8a5ac94e9679daee9 Mon Sep 17 00:00:00 2001 From: Didier Raboud Date: Wed, 19 Dec 2012 16:54:58 +0100 Subject: Put common CXX flags in common variables CMAKE_CXX_FLAGS is taken into account in all builds; build-type-specific flags are appended to it. --- src/sound/plugins/oalsound/CMakeLists.txt | 3 ++- src/sound/plugins/oalsound/test/CMakeLists.txt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/sound') diff --git a/src/sound/plugins/oalsound/CMakeLists.txt b/src/sound/plugins/oalsound/CMakeLists.txt index 0bc9482..bb7e9ff 100644 --- a/src/sound/plugins/oalsound/CMakeLists.txt +++ b/src/sound/plugins/oalsound/CMakeLists.txt @@ -6,7 +6,8 @@ set(SOURCES channel.cpp ) -SET (CMAKE_CXX_FLAGS "-Wall -g -std=c++0x -fPIC") +SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x -fPIC") +SET (CMAKE_CXX_FLAGS_DEBUG "-g") include(FindPkgConfig) include(FindOpenAL) diff --git a/src/sound/plugins/oalsound/test/CMakeLists.txt b/src/sound/plugins/oalsound/test/CMakeLists.txt index 27ab756..dd208ea 100644 --- a/src/sound/plugins/oalsound/test/CMakeLists.txt +++ b/src/sound/plugins/oalsound/test/CMakeLists.txt @@ -3,7 +3,8 @@ cmake_minimum_required(VERSION 2.8) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE debug) endif(NOT CMAKE_BUILD_TYPE) -set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0 -std=c++11 -rdynamic") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -rdynamic") +set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") add_executable(plugin_test plugin_test.cpp ../../../../common/iman.cpp ../../../../common/logger.cpp ../../../../plugins/pluginloader.cpp) -- cgit v1.2.3-1-g7c22 From a6ff654ae37ca372d785c1e155fbfe67a3a25fed Mon Sep 17 00:00:00 2001 From: erihel Date: Thu, 20 Dec 2012 20:59:11 +0100 Subject: removing plugins for gold version (for mxe cross compiling) --- src/sound/oalsound/CMakeLists.txt | 24 ++ src/sound/oalsound/alsound.cpp | 540 ++++++++++++++++++++++++ src/sound/oalsound/alsound.h | 94 +++++ src/sound/oalsound/buffer.cpp | 80 ++++ src/sound/oalsound/buffer.h | 48 +++ src/sound/oalsound/channel.cpp | 304 +++++++++++++ src/sound/oalsound/channel.h | 99 +++++ src/sound/oalsound/check.h | 39 ++ src/sound/oalsound/test/CMakeLists.txt | 14 + src/sound/oalsound/test/plugin_test.cpp | 40 ++ src/sound/plugins/oalsound/CMakeLists.txt | 24 -- src/sound/plugins/oalsound/alsound.cpp | 540 ------------------------ src/sound/plugins/oalsound/alsound.h | 94 ----- src/sound/plugins/oalsound/buffer.cpp | 80 ---- src/sound/plugins/oalsound/buffer.h | 48 --- src/sound/plugins/oalsound/channel.cpp | 304 ------------- src/sound/plugins/oalsound/channel.h | 99 ----- src/sound/plugins/oalsound/check.h | 39 -- src/sound/plugins/oalsound/test/CMakeLists.txt | 14 - src/sound/plugins/oalsound/test/plugin_test.cpp | 40 -- src/sound/sound.h | 4 +- 21 files changed, 1283 insertions(+), 1285 deletions(-) create mode 100644 src/sound/oalsound/CMakeLists.txt create mode 100644 src/sound/oalsound/alsound.cpp create mode 100644 src/sound/oalsound/alsound.h create mode 100644 src/sound/oalsound/buffer.cpp create mode 100644 src/sound/oalsound/buffer.h create mode 100644 src/sound/oalsound/channel.cpp create mode 100644 src/sound/oalsound/channel.h create mode 100644 src/sound/oalsound/check.h create mode 100644 src/sound/oalsound/test/CMakeLists.txt create mode 100644 src/sound/oalsound/test/plugin_test.cpp delete mode 100644 src/sound/plugins/oalsound/CMakeLists.txt delete mode 100644 src/sound/plugins/oalsound/alsound.cpp delete mode 100644 src/sound/plugins/oalsound/alsound.h delete mode 100644 src/sound/plugins/oalsound/buffer.cpp delete mode 100644 src/sound/plugins/oalsound/buffer.h delete mode 100644 src/sound/plugins/oalsound/channel.cpp delete mode 100644 src/sound/plugins/oalsound/channel.h delete mode 100644 src/sound/plugins/oalsound/check.h delete mode 100644 src/sound/plugins/oalsound/test/CMakeLists.txt delete mode 100644 src/sound/plugins/oalsound/test/plugin_test.cpp (limited to 'src/sound') diff --git a/src/sound/oalsound/CMakeLists.txt b/src/sound/oalsound/CMakeLists.txt new file mode 100644 index 0000000..bb7e9ff --- /dev/null +++ b/src/sound/oalsound/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 2.8) + +set(SOURCES + alsound.cpp + buffer.cpp + channel.cpp +) + +SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x -fPIC") +SET (CMAKE_CXX_FLAGS_DEBUG "-g") + +include(FindPkgConfig) +include(FindOpenAL) +pkg_check_modules(OPENAL_LIB REQUIRED openal) + +set(OPENAL_LIBRARIES + openal + alut +) + +include_directories(../../..) +include_directories(.) +add_library(openalsound SHARED ${SOURCES}) +target_link_libraries(openalsound ${OPENAL_LIBRARIES}) diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp new file mode 100644 index 0000000..83a4def --- /dev/null +++ b/src/sound/oalsound/alsound.cpp @@ -0,0 +1,540 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012 Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// alsound.cpp + + +#include "alsound.h" + + +#define MIN(a, b) (a > b ? b : a) + + +PLUGIN_INTERFACE(ALSound) + + +std::string ALSound::PluginName() +{ + return "Sound plugin using OpenAL library to play sounds."; +} + + +int ALSound::PluginVersion() +{ + return 2; +} + + +void ALSound::InstallPlugin() +{ + auto pointer = CInstanceManager::GetInstancePointer(); + if (pointer != nullptr) + CInstanceManager::GetInstancePointer()->AddInstance(CLASS_SOUND, this); +} + + +bool ALSound::UninstallPlugin(std::string &reason) +{ + auto pointer = CInstanceManager::GetInstancePointer(); + if (pointer != nullptr) + CInstanceManager::GetInstancePointer()->DeleteInstance(CLASS_SOUND, this); + CleanUp(); + return true; +} + + +ALSound::ALSound() +{ + mEnabled = false; + m3D = false; + mAudioVolume = MAXVOLUME; + mMute = false; +} + + +ALSound::~ALSound() +{ + CleanUp(); +} + + +void ALSound::CleanUp() +{ + if (mEnabled) { + GetLogger()->Info("Unloading files and closing device...\n"); + StopAll(); + + for (auto item : mSounds) + delete item.second; + + mEnabled = false; + alutExit(); + } +} + + +bool ALSound::Create(bool b3D) +{ + CleanUp(); + + if (mEnabled) + return true; + + GetLogger()->Info("Opening audio device...\n"); + if (!alutInit(NULL, NULL)) { + ALenum error = alutGetError(); + GetLogger()->Error("Could not open audio device! Reason: %s\n", alutGetErrorString(error)); + return false; + } + GetLogger()->Info("Done.\n"); + + mEnabled = true; + return true; +} + + +void ALSound::SetSound3D(bool bMode) +{ + // TODO stub! need to be implemented + m3D = bMode; +} + + +bool ALSound::RetSound3D() +{ + // TODO stub! need to be implemented + return true; +} + + +bool ALSound::RetSound3DCap() +{ + // TODO stub! need to be implemented + return true; +} + + +bool ALSound::RetEnable() +{ + return mEnabled; +} + + +void ALSound::SetAudioVolume(int volume) +{ + alListenerf(AL_GAIN, MIN(volume, MAXVOLUME) * 0.01f); + mAudioVolume = MIN(volume, MAXVOLUME); +} + + +int ALSound::RetAudioVolume() +{ + float volume; + if ( !mEnabled ) + return 0; + + alGetListenerf(AL_GAIN, &volume); + return volume * MAXVOLUME; +} + + +void ALSound::SetMusicVolume(int volume) +{ + // TODO stub! Add music support +} + + +int ALSound::RetMusicVolume() +{ + // TODO stub! Add music support + if ( !mEnabled ) + return 0; + + return 0; +} + + +bool ALSound::Cache(Sound sound, std::string filename) +{ + Buffer *buffer = new Buffer(); + if (buffer->LoadFromFile(filename, sound)) { + mSounds[sound] = buffer; + return true; + } + return false; +} + + +int ALSound::RetPriority(Sound sound) +{ + if ( sound == SOUND_FLYh || + sound == SOUND_FLY || + sound == SOUND_MOTORw || + sound == SOUND_MOTORt || + sound == SOUND_MOTORr || + sound == SOUND_MOTORs || + sound == SOUND_SLIDE || + sound == SOUND_ERROR ) + { + return 30; + } + + if ( sound == SOUND_CONVERT || + sound == SOUND_ENERGY || + sound == SOUND_DERRICK || + sound == SOUND_STATION || + sound == SOUND_REPAIR || + sound == SOUND_RESEARCH || + sound == SOUND_BURN || + sound == SOUND_BUILD || + sound == SOUND_TREMBLE || + sound == SOUND_NUCLEAR || + sound == SOUND_EXPLO || + sound == SOUND_EXPLOl || + sound == SOUND_EXPLOlp || + sound == SOUND_EXPLOp || + sound == SOUND_EXPLOi ) + { + return 20; + } + + if ( sound == SOUND_BLUP || + sound == SOUND_INSECTs || + sound == SOUND_INSECTa || + sound == SOUND_INSECTb || + sound == SOUND_INSECTw || + sound == SOUND_INSECTm || + sound == SOUND_PSHHH || + sound == SOUND_EGG ) + { + return 0; + } + + return 10; +} + + +bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) +{ + int priority = RetPriority(sound); + + // Seeks a channel used which sound is stopped. + for (auto it : mChannels) { + if (it.second->IsPlaying()) + continue; + if (it.second->GetSoundType() != sound) + continue; + + it.second->SetPriority(priority); + channel = it.first; + bAlreadyLoaded = true; + return true; + } + + // just add a new channel if we dont have any + if (mChannels.size() == 0) { + Channel *chn = new Channel(); + // check if we channel ready to play music, if not report error + if (chn->IsReady()) { + chn->SetPriority(priority); + mChannels[1] = chn; + channel = 1; + bAlreadyLoaded = false; + return true; + } + delete chn; + GetLogger()->Error("Could not open channel to play sound!"); + return false; + } + + // Seeks a channel completely free. + if (mChannels.size() < 64) { + auto it = mChannels.end(); + it--; + int i = (*it).first; + while (++i) + { + if (mChannels.find(i) == mChannels.end()) { + Channel *chn = new Channel(); + // check if channel is ready to play music, if not destroy it and seek free one + if (chn->IsReady()) { + chn->SetPriority(priority); + mChannels[++i] = chn; + channel = i; + bAlreadyLoaded = false; + return true; + } + delete chn; + GetLogger()->Warn("Could not open additional channel to play sound!"); + } + } + } + + int lowerOrEqual = -1; + for (auto it : mChannels) { + if (it.second->GetPriority() < priority) { + GetLogger()->Info("Sound channel with lower priority will be reused."); + channel = it.first; + return true; + } + if (it.second->GetPriority() <= priority) + lowerOrEqual = it.first; + } + + if (lowerOrEqual != -1) { + channel = lowerOrEqual; + GetLogger()->Info("Sound channel with lower or equal priority will be reused."); + return true; + } + + GetLogger()->Warn("Could not find free buffer to use.\n"); + return false; +} + + +int ALSound::Play(Sound sound, float amplitude, float frequency, bool bLoop) +{ + return Play(sound, Math::Vector(), amplitude, frequency, bLoop); +} + + +int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequency, bool bLoop) +{ + if (!mEnabled) + return -1; + + if (mAudioVolume <= 0.0f) + return -1; + + if (mSounds.find(sound) == mSounds.end()) { + GetLogger()->Warn("Sound %d was not loaded!\n", sound); + return -1; + } + + int channel; + bool bAlreadyLoaded; + if (!SearchFreeBuffer(sound, channel, bAlreadyLoaded)) + return -1; + if ( !bAlreadyLoaded ) { + mChannels[channel]->SetBuffer(mSounds[sound]); + } + + Position(channel, pos); + + // setting initial values + mChannels[channel]->SetStartAmplitude(amplitude); + mChannels[channel]->SetStartFrequency(frequency); + mChannels[channel]->SetChangeFrequency(1.0f); + mChannels[channel]->ResetOper(); + mChannels[channel]->AdjustFrequency(frequency); + mChannels[channel]->AdjustVolume(mAudioVolume); + mChannels[channel]->Play(); + return channel; +} + + +bool ALSound::FlushEnvelope(int channel) +{ + if (mChannels.find(channel) == mChannels.end()) { + return false; + } + + mChannels[channel]->ResetOper(); + return true; +} + + +bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) +{ + if (!mEnabled) + return false; + + if (mChannels.find(channel) == mChannels.end()) { + return false; + } + + SoundOper op; + op.finalAmplitude = amplitude; + op.finalFrequency = frequency; + op.totalTime = time; + op.nextOper = oper; + mChannels[channel]->AddOper(op); + + return false; +} + + +bool ALSound::Position(int channel, Math::Vector pos) +{ + if (!mEnabled) + return false; + + if (mChannels.find(channel) == mChannels.end()) { + return false; + } + + mChannels[channel]->SetPosition(pos); + return true; +} + + +bool ALSound::Frequency(int channel, float frequency) +{ + if (!mEnabled) + return false; + + if (mChannels.find(channel) == mChannels.end()) { + return false; + } + + mChannels[channel]->SetFrequency(frequency); + return true; +} + +bool ALSound::Stop(int channel) +{ + if (!mEnabled) + return false; + + if (mChannels.find(channel) == mChannels.end()) { + return false; + } + + mChannels[channel]->Stop(); + mChannels[channel]->ResetOper(); + + return true; +} + + +bool ALSound::StopAll() +{ + if (!mEnabled) + return false; + + for (auto channel : mChannels) { + channel.second->Stop(); + channel.second->ResetOper(); + } + + return true; +} + + +bool ALSound::MuteAll(bool bMute) +{ + if (!mEnabled) + return false; + + float volume; + mMute = bMute; + if (mMute) + volume = 0; + else + volume = mAudioVolume; + + for (auto channel : mChannels) { + channel.second->SetVolume(volume); + } + + return true; +} + + +void ALSound::FrameMove(float delta) +{ + if (!mEnabled) + return; + + float progress; + float volume, frequency; + for (auto it : mChannels) { + if (!it.second->IsPlaying()) + continue; + + if (!it.second->HasEnvelope()) + continue; + + //it.second->GetEnvelope().currentTime += delta; + SoundOper oper = it.second->GetEnvelope(); + progress = it.second->GetCurrentTime() / oper.totalTime; + progress = MIN(progress, 1.0f); + + // setting volume + volume = progress * abs(oper.finalAmplitude - it.second->GetStartAmplitude()); + it.second->AdjustVolume(volume * mAudioVolume); + + // setting frequency + frequency = progress * (oper.finalFrequency - it.second->GetStartFrequency()) * it.second->GetStartFrequency() * it.second->GetChangeFrequency(); + it.second->AdjustFrequency(frequency); + + if (it.second->GetEnvelope().totalTime <= it.second->GetCurrentTime()) { + + if (oper.nextOper == SOPER_LOOP) { + GetLogger()->Info("Replay.\n"); + it.second->SetCurrentTime(0.0f); + it.second->Play(); + } else { + GetLogger()->Info("Next.\n"); + it.second->SetStartAmplitude(oper.finalAmplitude); + it.second->SetStartFrequency(oper.finalFrequency); + it.second->PopEnvelope(); + } + } + } +} + + +void ALSound::SetListener(Math::Vector eye, Math::Vector lookat) +{ + float orientation[] = {lookat.x, lookat.y, lookat.z, 0.f, 1.f, 0.f}; + alListener3f(AL_POSITION, eye.x, eye.y, eye.z); + alListenerfv(AL_ORIENTATION, orientation); +} + + +bool ALSound::PlayMusic(int rank, bool bRepeat) +{ + // TODO stub! Add music support + return true; +} + + +bool ALSound::RestartMusic() +{ + // TODO stub! Add music support + return true; +} + +void ALSound::StopMusic() +{ + // TODO stub! Add music support + SuspendMusic(); +} + + +bool ALSound::IsPlayingMusic() +{ + // TODO stub! Add music support + return true; +} + + +void ALSound::SuspendMusic() +{ + // TODO stub! Add music support +} diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h new file mode 100644 index 0000000..a1128e0 --- /dev/null +++ b/src/sound/oalsound/alsound.h @@ -0,0 +1,94 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// alsound.h + +#pragma once + +#include +#include + +#include + +#include "common/iman.h" +#include "common/logger.h" +#include "sound/sound.h" + +#include "buffer.h" +#include "channel.h" +#include "check.h" + + +class ALSound : public CSoundInterface +{ + public: + ALSound(); + ~ALSound(); + + bool Create(bool b3D); + bool Cache(Sound, std::string); + + bool RetEnable(); + + void SetSound3D(bool bMode); + bool RetSound3D(); + bool RetSound3DCap(); + + void SetAudioVolume(int volume); + int RetAudioVolume(); + void SetMusicVolume(int volume); + int RetMusicVolume(); + + void SetListener(Math::Vector eye, Math::Vector lookat); + void FrameMove(float rTime); + + int Play(Sound sound, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false); + int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false); + bool FlushEnvelope(int channel); + bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper); + bool Position(int channel, Math::Vector pos); + bool Frequency(int channel, float frequency); + bool Stop(int channel); + bool StopAll(); + bool MuteAll(bool bMute); + + bool PlayMusic(int rank, bool bRepeat); + bool RestartMusic(); + void SuspendMusic(); + void StopMusic(); + bool IsPlayingMusic(); + + // plugin interface + std::string PluginName(); + int PluginVersion(); + void InstallPlugin(); + bool UninstallPlugin(std::string &); + + private: + void CleanUp(); + int RetPriority(Sound); + bool SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded); + + bool mEnabled; + bool m3D; + bool mMute; + int mAudioVolume; + ALCdevice* audioDevice; + ALCcontext* audioContext; + std::map mSounds; + std::map mChannels; +}; diff --git a/src/sound/oalsound/buffer.cpp b/src/sound/oalsound/buffer.cpp new file mode 100644 index 0000000..37211e9 --- /dev/null +++ b/src/sound/oalsound/buffer.cpp @@ -0,0 +1,80 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// buffer.cpp + +#include "buffer.h" + +Buffer::Buffer() { + mLoaded = false; + mDuration = 0; +} + + +Buffer::~Buffer() { + if (mLoaded) { + alDeleteBuffers(1, &mBuffer); + if (alCheck()) + GetLogger()->Warn("Failed to unload buffer. Code %d\n", alGetCode()); + } +} + + +bool Buffer::LoadFromFile(std::string filename, Sound sound) { + mSound = sound; + + GetLogger()->Info("Loading audio file: %s\n", filename.c_str()); + mBuffer = alutCreateBufferFromFile(filename.c_str()); + + ALenum error = alutGetError(); + if (error) { + GetLogger()->Warn("Failed to load file. Reason: %s\n", alutGetErrorString(error)); + mLoaded = false; + return false; + } + + ALint size, bits, channels, freq; + + alGetBufferi(mBuffer, AL_SIZE, &size); + alGetBufferi(mBuffer, AL_BITS, &bits); + alGetBufferi(mBuffer, AL_CHANNELS, &channels); + alGetBufferi(mBuffer, AL_FREQUENCY, &freq); + + mDuration = (ALfloat)size / channels / bits / 8 / (ALfloat)freq; + + mLoaded = true; + return true; +} + + +Sound Buffer::GetSoundType() { + return mSound; +} + + +ALuint Buffer::GetBuffer() { + return mBuffer; +} + + +bool Buffer::IsLoaded() { + return mLoaded; +} + + +float Buffer::GetDuration() { + return mDuration; +} diff --git a/src/sound/oalsound/buffer.h b/src/sound/oalsound/buffer.h new file mode 100644 index 0000000..8c4a2d3 --- /dev/null +++ b/src/sound/oalsound/buffer.h @@ -0,0 +1,48 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// buffer.h + +#pragma once + +#include + +#include + +#include "sound/sound.h" +#include "common/logger.h" + +#include "check.h" + +class Buffer +{ + public: + Buffer(); + ~Buffer(); + + bool LoadFromFile(std::string, Sound); + bool IsLoaded(); + + Sound GetSoundType(); + ALuint GetBuffer(); + float GetDuration(); + + private: + ALuint mBuffer; + Sound mSound; + bool mLoaded; + float mDuration; +}; diff --git a/src/sound/oalsound/channel.cpp b/src/sound/oalsound/channel.cpp new file mode 100644 index 0000000..4476dee --- /dev/null +++ b/src/sound/oalsound/channel.cpp @@ -0,0 +1,304 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// channel.cpp + +#include "channel.h" + + +Channel::Channel() { + alGenSources(1, &mSource); + + if (alCheck()) { + GetLogger()->Warn("Failed to create sound source. Code: %d\n", alGetCode()); + mReady = false; + } else { + mReady = true; + } + + mPriority = 0; + mBuffer = nullptr; +} + + +Channel::~Channel() { + if (mReady) { + alSourcei(mSource, AL_BUFFER, 0); + alDeleteSources(1, &mSource); + if (alCheck()) + GetLogger()->Warn("Failed to delete sound source. Code: %s\n", alGetCode()); + } +} + + +bool Channel::Play() { + if (!mReady) + return false; + + alSourcePlay(mSource); + if (alCheck()) + GetLogger()->Warn("Could not play audio sound source. Code: %s\n", alGetCode()); + return true; +} + + +bool Channel::SetPosition(Math::Vector pos) { + if (!mReady) + return false; + + alSource3f(mSource, AL_POSITION, pos.x, pos.y, pos.z); + if (alCheck()) { + GetLogger()->Warn("Could not set sound position. Code: %s\n", alGetCode()); + return false; + } + return true; +} + + +bool Channel::SetFrequency(float freq) +{ + if (!mReady) + return false; + + alSourcef(mSource, AL_PITCH, freq); + if (alCheck()) { + GetLogger()->Warn("Could not set sound pitch. Code: %s\n", alGetCode()); + return false; + } + return true; +} + + +float Channel::GetFrequency() +{ + ALfloat freq; + if (!mReady) + return 0; + + alGetSourcef(mSource, AL_PITCH, &freq); + if (alCheck()) { + GetLogger()->Warn("Could not get sound pitch. Code: %s\n", alGetCode()); + return 0; + } + + return freq; +} + + +bool Channel::SetVolume(float vol) +{ + if (!mReady || vol < 0) + return false; + + alSourcef(mSource, AL_GAIN, vol / MAXVOLUME); + if (alCheck()) { + GetLogger()->Warn("Could not set sound volume. Code: %s\n", alGetCode()); + return false; + } + return true; +} + + +float Channel::GetVolume() +{ + ALfloat vol; + if (!mReady) + return 0; + + alGetSourcef(mSource, AL_GAIN, &vol); + if (alCheck()) { + GetLogger()->Warn("Could not get sound volume. Code: %s\n", alGetCode()); + return 0; + } + + return vol * MAXVOLUME; +} + + +int Channel::GetPriority() +{ + return mPriority; +} + + +void Channel::SetPriority(int pri) +{ + mPriority = pri; +} + + +void Channel::SetStartAmplitude(float gain) +{ + mStartAmplitude = gain; +} + + +void Channel::SetStartFrequency(float freq) +{ + mStartFrequency = freq; +} + + +void Channel::SetChangeFrequency(float freq) +{ + mChangeFrequency = freq; +} + + +void Channel::SetInitFrequency(float freq) +{ + mInitFrequency = freq; +} + + +float Channel::GetStartAmplitude() +{ + return mStartAmplitude; +} + + +float Channel::GetStartFrequency() +{ + return mStartFrequency; +} + + +float Channel::GetChangeFrequency() +{ + return mChangeFrequency; +} + + +float Channel::GetInitFrequency() +{ + return mInitFrequency; +} + + +void Channel::AddOper(SoundOper oper) +{ + mOper.push_back(oper); +} + + +void Channel::ResetOper() +{ + mOper.clear(); +} + + +Sound Channel::GetSoundType() { + return mBuffer->GetSoundType(); +} + + +bool Channel::SetBuffer(Buffer *buffer) { + if (!mReady) + return false; + + assert(buffer); + mBuffer = buffer; + alSourcei(mSource, AL_BUFFER, buffer->GetBuffer()); + if (alCheck()) { + GetLogger()->Warn("Could not set sound buffer. Code: %s\n", alGetCode()); + return false; + } + mInitFrequency = GetFrequency(); + return true; +} + + +void Channel::AdjustFrequency(float freq) { + SetFrequency(freq * mInitFrequency); +} + + +void Channel::AdjustVolume(float volume) { + SetVolume(mStartAmplitude * (float) volume); +} + + +bool Channel::IsPlaying() { + ALint status; + if (!mReady) return false; + + alGetSourcei(mSource, AL_SOURCE_STATE, &status); + if (alCheck()) { + GetLogger()->Warn("Could not get sound status. Code: %s\n", alGetCode()); + return false; + } + + return status == AL_PLAYING; +} + + +bool Channel::IsReady() { + return mReady; +} + + +bool Channel::Stop() { + alSourceStop(mSource); + if (alCheck()) { + GetLogger()->Warn("Could not stop sound. Code: %s\n", alGetCode()); + return false; + } + return true; +} + + +float Channel::GetCurrentTime() +{ + ALfloat current; + alGetSourcef(mSource, AL_SEC_OFFSET, ¤t); + if (alCheck()) { + GetLogger()->Warn("Could not get source current play time. Code: %s\n", alGetCode()); + return 0.0f; + } + return current; +} + + +void Channel::SetCurrentTime(float current) +{ + alSourcef(mSource, AL_SEC_OFFSET, current); + if (alCheck()) + GetLogger()->Warn("Could not get source current play time. Code: %s\n", alGetCode()); +} + + +float Channel::GetDuration() +{ + return mBuffer->GetDuration(); +} + + +bool Channel::HasEnvelope() +{ + return mOper.size() > 0; +} + + +SoundOper& Channel::GetEnvelope() +{ + return mOper.front(); +} + + +void Channel::PopEnvelope() +{ + mOper.pop_front(); +} diff --git a/src/sound/oalsound/channel.h b/src/sound/oalsound/channel.h new file mode 100644 index 0000000..165ff50 --- /dev/null +++ b/src/sound/oalsound/channel.h @@ -0,0 +1,99 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// channel.h + +#pragma once + +#include +#include +#include + +#include +#include + +#include "sound/sound.h" + +#include "buffer.h" +#include "check.h" + +struct SoundOper +{ + float finalAmplitude; + float finalFrequency; + float totalTime; + SoundNext nextOper; +}; + + +class Channel +{ + public: + Channel(); + ~Channel(); + + bool Play(); + bool Stop(); + bool SetPosition(Math::Vector); + + bool SetFrequency(float); + float GetFrequency(); + + float GetCurrentTime(); + void SetCurrentTime(float); + float GetDuration(); + + bool SetVolume(float); + float GetVolume(); + bool IsPlaying(); + bool IsReady(); + + bool SetBuffer(Buffer *); + bool HasEnvelope(); + SoundOper& GetEnvelope(); + void PopEnvelope(); + + int GetPriority(); + void SetPriority(int); + + void SetStartAmplitude(float); + void SetStartFrequency(float); + void SetChangeFrequency(float); + void SetInitFrequency(float); + + float GetStartAmplitude(); + float GetStartFrequency(); + float GetChangeFrequency(); + float GetInitFrequency(); + + void AddOper(SoundOper); + void ResetOper(); + Sound GetSoundType(); + void AdjustFrequency(float); + void AdjustVolume(float); + + private: + Buffer *mBuffer; + ALuint mSource; + + int mPriority; + float mStartAmplitude; + float mStartFrequency; + float mChangeFrequency; + float mInitFrequency; + std::deque mOper; + bool mReady; +}; diff --git a/src/sound/oalsound/check.h b/src/sound/oalsound/check.h new file mode 100644 index 0000000..cf3e468 --- /dev/null +++ b/src/sound/oalsound/check.h @@ -0,0 +1,39 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// check.h + +#pragma once + +#include +#include + +#include "common/logger.h" + +static ALenum CODE = AL_NO_ERROR; + +inline bool alCheck() +{ + CODE = alGetError(); + return CODE != AL_NO_ERROR; +} + +inline ALenum alGetCode() +{ + ALenum ret = CODE; + CODE = AL_NO_ERROR; + return ret; +} diff --git a/src/sound/oalsound/test/CMakeLists.txt b/src/sound/oalsound/test/CMakeLists.txt new file mode 100644 index 0000000..dd208ea --- /dev/null +++ b/src/sound/oalsound/test/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.8) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE debug) +endif(NOT CMAKE_BUILD_TYPE) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -rdynamic") +set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") + +add_executable(plugin_test plugin_test.cpp ../../../../common/iman.cpp ../../../../common/logger.cpp ../../../../plugins/pluginloader.cpp) + +include_directories(".") +include_directories("../../../../") + +target_link_libraries(plugin_test ${LTDL_LIBRARY}) diff --git a/src/sound/oalsound/test/plugin_test.cpp b/src/sound/oalsound/test/plugin_test.cpp new file mode 100644 index 0000000..40c1cd2 --- /dev/null +++ b/src/sound/oalsound/test/plugin_test.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +#include +#include +#include +#include + + +int main() { + new CLogger(); + new CInstanceManager(); + + lt_dlinit(); + + CPluginLoader *plugin = new CPluginLoader("libopenalsound"); + if (plugin->LoadPlugin()) { + CSoundInterface *sound = static_cast(CInstanceManager::GetInstancePointer()->SearchInstance(CLASS_SOUND)); + + sound->Create(true); + sound->CacheAll(); + sound->Play((Sound)8); + sound->Play((Sound)18); + + sleep(10); + /* + while (1) + { + // just a test, very slow + plugin->FrameMove(0); + //if ('n' == getchar()) + // break; + }*/ + plugin->UnloadPlugin(); + } + + lt_dlexit(); + return 0; +} diff --git a/src/sound/plugins/oalsound/CMakeLists.txt b/src/sound/plugins/oalsound/CMakeLists.txt deleted file mode 100644 index bb7e9ff..0000000 --- a/src/sound/plugins/oalsound/CMakeLists.txt +++ /dev/null @@ -1,24 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -set(SOURCES - alsound.cpp - buffer.cpp - channel.cpp -) - -SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x -fPIC") -SET (CMAKE_CXX_FLAGS_DEBUG "-g") - -include(FindPkgConfig) -include(FindOpenAL) -pkg_check_modules(OPENAL_LIB REQUIRED openal) - -set(OPENAL_LIBRARIES - openal - alut -) - -include_directories(../../..) -include_directories(.) -add_library(openalsound SHARED ${SOURCES}) -target_link_libraries(openalsound ${OPENAL_LIBRARIES}) diff --git a/src/sound/plugins/oalsound/alsound.cpp b/src/sound/plugins/oalsound/alsound.cpp deleted file mode 100644 index 83a4def..0000000 --- a/src/sound/plugins/oalsound/alsound.cpp +++ /dev/null @@ -1,540 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012 Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -// alsound.cpp - - -#include "alsound.h" - - -#define MIN(a, b) (a > b ? b : a) - - -PLUGIN_INTERFACE(ALSound) - - -std::string ALSound::PluginName() -{ - return "Sound plugin using OpenAL library to play sounds."; -} - - -int ALSound::PluginVersion() -{ - return 2; -} - - -void ALSound::InstallPlugin() -{ - auto pointer = CInstanceManager::GetInstancePointer(); - if (pointer != nullptr) - CInstanceManager::GetInstancePointer()->AddInstance(CLASS_SOUND, this); -} - - -bool ALSound::UninstallPlugin(std::string &reason) -{ - auto pointer = CInstanceManager::GetInstancePointer(); - if (pointer != nullptr) - CInstanceManager::GetInstancePointer()->DeleteInstance(CLASS_SOUND, this); - CleanUp(); - return true; -} - - -ALSound::ALSound() -{ - mEnabled = false; - m3D = false; - mAudioVolume = MAXVOLUME; - mMute = false; -} - - -ALSound::~ALSound() -{ - CleanUp(); -} - - -void ALSound::CleanUp() -{ - if (mEnabled) { - GetLogger()->Info("Unloading files and closing device...\n"); - StopAll(); - - for (auto item : mSounds) - delete item.second; - - mEnabled = false; - alutExit(); - } -} - - -bool ALSound::Create(bool b3D) -{ - CleanUp(); - - if (mEnabled) - return true; - - GetLogger()->Info("Opening audio device...\n"); - if (!alutInit(NULL, NULL)) { - ALenum error = alutGetError(); - GetLogger()->Error("Could not open audio device! Reason: %s\n", alutGetErrorString(error)); - return false; - } - GetLogger()->Info("Done.\n"); - - mEnabled = true; - return true; -} - - -void ALSound::SetSound3D(bool bMode) -{ - // TODO stub! need to be implemented - m3D = bMode; -} - - -bool ALSound::RetSound3D() -{ - // TODO stub! need to be implemented - return true; -} - - -bool ALSound::RetSound3DCap() -{ - // TODO stub! need to be implemented - return true; -} - - -bool ALSound::RetEnable() -{ - return mEnabled; -} - - -void ALSound::SetAudioVolume(int volume) -{ - alListenerf(AL_GAIN, MIN(volume, MAXVOLUME) * 0.01f); - mAudioVolume = MIN(volume, MAXVOLUME); -} - - -int ALSound::RetAudioVolume() -{ - float volume; - if ( !mEnabled ) - return 0; - - alGetListenerf(AL_GAIN, &volume); - return volume * MAXVOLUME; -} - - -void ALSound::SetMusicVolume(int volume) -{ - // TODO stub! Add music support -} - - -int ALSound::RetMusicVolume() -{ - // TODO stub! Add music support - if ( !mEnabled ) - return 0; - - return 0; -} - - -bool ALSound::Cache(Sound sound, std::string filename) -{ - Buffer *buffer = new Buffer(); - if (buffer->LoadFromFile(filename, sound)) { - mSounds[sound] = buffer; - return true; - } - return false; -} - - -int ALSound::RetPriority(Sound sound) -{ - if ( sound == SOUND_FLYh || - sound == SOUND_FLY || - sound == SOUND_MOTORw || - sound == SOUND_MOTORt || - sound == SOUND_MOTORr || - sound == SOUND_MOTORs || - sound == SOUND_SLIDE || - sound == SOUND_ERROR ) - { - return 30; - } - - if ( sound == SOUND_CONVERT || - sound == SOUND_ENERGY || - sound == SOUND_DERRICK || - sound == SOUND_STATION || - sound == SOUND_REPAIR || - sound == SOUND_RESEARCH || - sound == SOUND_BURN || - sound == SOUND_BUILD || - sound == SOUND_TREMBLE || - sound == SOUND_NUCLEAR || - sound == SOUND_EXPLO || - sound == SOUND_EXPLOl || - sound == SOUND_EXPLOlp || - sound == SOUND_EXPLOp || - sound == SOUND_EXPLOi ) - { - return 20; - } - - if ( sound == SOUND_BLUP || - sound == SOUND_INSECTs || - sound == SOUND_INSECTa || - sound == SOUND_INSECTb || - sound == SOUND_INSECTw || - sound == SOUND_INSECTm || - sound == SOUND_PSHHH || - sound == SOUND_EGG ) - { - return 0; - } - - return 10; -} - - -bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) -{ - int priority = RetPriority(sound); - - // Seeks a channel used which sound is stopped. - for (auto it : mChannels) { - if (it.second->IsPlaying()) - continue; - if (it.second->GetSoundType() != sound) - continue; - - it.second->SetPriority(priority); - channel = it.first; - bAlreadyLoaded = true; - return true; - } - - // just add a new channel if we dont have any - if (mChannels.size() == 0) { - Channel *chn = new Channel(); - // check if we channel ready to play music, if not report error - if (chn->IsReady()) { - chn->SetPriority(priority); - mChannels[1] = chn; - channel = 1; - bAlreadyLoaded = false; - return true; - } - delete chn; - GetLogger()->Error("Could not open channel to play sound!"); - return false; - } - - // Seeks a channel completely free. - if (mChannels.size() < 64) { - auto it = mChannels.end(); - it--; - int i = (*it).first; - while (++i) - { - if (mChannels.find(i) == mChannels.end()) { - Channel *chn = new Channel(); - // check if channel is ready to play music, if not destroy it and seek free one - if (chn->IsReady()) { - chn->SetPriority(priority); - mChannels[++i] = chn; - channel = i; - bAlreadyLoaded = false; - return true; - } - delete chn; - GetLogger()->Warn("Could not open additional channel to play sound!"); - } - } - } - - int lowerOrEqual = -1; - for (auto it : mChannels) { - if (it.second->GetPriority() < priority) { - GetLogger()->Info("Sound channel with lower priority will be reused."); - channel = it.first; - return true; - } - if (it.second->GetPriority() <= priority) - lowerOrEqual = it.first; - } - - if (lowerOrEqual != -1) { - channel = lowerOrEqual; - GetLogger()->Info("Sound channel with lower or equal priority will be reused."); - return true; - } - - GetLogger()->Warn("Could not find free buffer to use.\n"); - return false; -} - - -int ALSound::Play(Sound sound, float amplitude, float frequency, bool bLoop) -{ - return Play(sound, Math::Vector(), amplitude, frequency, bLoop); -} - - -int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequency, bool bLoop) -{ - if (!mEnabled) - return -1; - - if (mAudioVolume <= 0.0f) - return -1; - - if (mSounds.find(sound) == mSounds.end()) { - GetLogger()->Warn("Sound %d was not loaded!\n", sound); - return -1; - } - - int channel; - bool bAlreadyLoaded; - if (!SearchFreeBuffer(sound, channel, bAlreadyLoaded)) - return -1; - if ( !bAlreadyLoaded ) { - mChannels[channel]->SetBuffer(mSounds[sound]); - } - - Position(channel, pos); - - // setting initial values - mChannels[channel]->SetStartAmplitude(amplitude); - mChannels[channel]->SetStartFrequency(frequency); - mChannels[channel]->SetChangeFrequency(1.0f); - mChannels[channel]->ResetOper(); - mChannels[channel]->AdjustFrequency(frequency); - mChannels[channel]->AdjustVolume(mAudioVolume); - mChannels[channel]->Play(); - return channel; -} - - -bool ALSound::FlushEnvelope(int channel) -{ - if (mChannels.find(channel) == mChannels.end()) { - return false; - } - - mChannels[channel]->ResetOper(); - return true; -} - - -bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) -{ - if (!mEnabled) - return false; - - if (mChannels.find(channel) == mChannels.end()) { - return false; - } - - SoundOper op; - op.finalAmplitude = amplitude; - op.finalFrequency = frequency; - op.totalTime = time; - op.nextOper = oper; - mChannels[channel]->AddOper(op); - - return false; -} - - -bool ALSound::Position(int channel, Math::Vector pos) -{ - if (!mEnabled) - return false; - - if (mChannels.find(channel) == mChannels.end()) { - return false; - } - - mChannels[channel]->SetPosition(pos); - return true; -} - - -bool ALSound::Frequency(int channel, float frequency) -{ - if (!mEnabled) - return false; - - if (mChannels.find(channel) == mChannels.end()) { - return false; - } - - mChannels[channel]->SetFrequency(frequency); - return true; -} - -bool ALSound::Stop(int channel) -{ - if (!mEnabled) - return false; - - if (mChannels.find(channel) == mChannels.end()) { - return false; - } - - mChannels[channel]->Stop(); - mChannels[channel]->ResetOper(); - - return true; -} - - -bool ALSound::StopAll() -{ - if (!mEnabled) - return false; - - for (auto channel : mChannels) { - channel.second->Stop(); - channel.second->ResetOper(); - } - - return true; -} - - -bool ALSound::MuteAll(bool bMute) -{ - if (!mEnabled) - return false; - - float volume; - mMute = bMute; - if (mMute) - volume = 0; - else - volume = mAudioVolume; - - for (auto channel : mChannels) { - channel.second->SetVolume(volume); - } - - return true; -} - - -void ALSound::FrameMove(float delta) -{ - if (!mEnabled) - return; - - float progress; - float volume, frequency; - for (auto it : mChannels) { - if (!it.second->IsPlaying()) - continue; - - if (!it.second->HasEnvelope()) - continue; - - //it.second->GetEnvelope().currentTime += delta; - SoundOper oper = it.second->GetEnvelope(); - progress = it.second->GetCurrentTime() / oper.totalTime; - progress = MIN(progress, 1.0f); - - // setting volume - volume = progress * abs(oper.finalAmplitude - it.second->GetStartAmplitude()); - it.second->AdjustVolume(volume * mAudioVolume); - - // setting frequency - frequency = progress * (oper.finalFrequency - it.second->GetStartFrequency()) * it.second->GetStartFrequency() * it.second->GetChangeFrequency(); - it.second->AdjustFrequency(frequency); - - if (it.second->GetEnvelope().totalTime <= it.second->GetCurrentTime()) { - - if (oper.nextOper == SOPER_LOOP) { - GetLogger()->Info("Replay.\n"); - it.second->SetCurrentTime(0.0f); - it.second->Play(); - } else { - GetLogger()->Info("Next.\n"); - it.second->SetStartAmplitude(oper.finalAmplitude); - it.second->SetStartFrequency(oper.finalFrequency); - it.second->PopEnvelope(); - } - } - } -} - - -void ALSound::SetListener(Math::Vector eye, Math::Vector lookat) -{ - float orientation[] = {lookat.x, lookat.y, lookat.z, 0.f, 1.f, 0.f}; - alListener3f(AL_POSITION, eye.x, eye.y, eye.z); - alListenerfv(AL_ORIENTATION, orientation); -} - - -bool ALSound::PlayMusic(int rank, bool bRepeat) -{ - // TODO stub! Add music support - return true; -} - - -bool ALSound::RestartMusic() -{ - // TODO stub! Add music support - return true; -} - -void ALSound::StopMusic() -{ - // TODO stub! Add music support - SuspendMusic(); -} - - -bool ALSound::IsPlayingMusic() -{ - // TODO stub! Add music support - return true; -} - - -void ALSound::SuspendMusic() -{ - // TODO stub! Add music support -} diff --git a/src/sound/plugins/oalsound/alsound.h b/src/sound/plugins/oalsound/alsound.h deleted file mode 100644 index a1128e0..0000000 --- a/src/sound/plugins/oalsound/alsound.h +++ /dev/null @@ -1,94 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -// alsound.h - -#pragma once - -#include -#include - -#include - -#include "common/iman.h" -#include "common/logger.h" -#include "sound/sound.h" - -#include "buffer.h" -#include "channel.h" -#include "check.h" - - -class ALSound : public CSoundInterface -{ - public: - ALSound(); - ~ALSound(); - - bool Create(bool b3D); - bool Cache(Sound, std::string); - - bool RetEnable(); - - void SetSound3D(bool bMode); - bool RetSound3D(); - bool RetSound3DCap(); - - void SetAudioVolume(int volume); - int RetAudioVolume(); - void SetMusicVolume(int volume); - int RetMusicVolume(); - - void SetListener(Math::Vector eye, Math::Vector lookat); - void FrameMove(float rTime); - - int Play(Sound sound, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false); - int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false); - bool FlushEnvelope(int channel); - bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper); - bool Position(int channel, Math::Vector pos); - bool Frequency(int channel, float frequency); - bool Stop(int channel); - bool StopAll(); - bool MuteAll(bool bMute); - - bool PlayMusic(int rank, bool bRepeat); - bool RestartMusic(); - void SuspendMusic(); - void StopMusic(); - bool IsPlayingMusic(); - - // plugin interface - std::string PluginName(); - int PluginVersion(); - void InstallPlugin(); - bool UninstallPlugin(std::string &); - - private: - void CleanUp(); - int RetPriority(Sound); - bool SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded); - - bool mEnabled; - bool m3D; - bool mMute; - int mAudioVolume; - ALCdevice* audioDevice; - ALCcontext* audioContext; - std::map mSounds; - std::map mChannels; -}; diff --git a/src/sound/plugins/oalsound/buffer.cpp b/src/sound/plugins/oalsound/buffer.cpp deleted file mode 100644 index 37211e9..0000000 --- a/src/sound/plugins/oalsound/buffer.cpp +++ /dev/null @@ -1,80 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -// buffer.cpp - -#include "buffer.h" - -Buffer::Buffer() { - mLoaded = false; - mDuration = 0; -} - - -Buffer::~Buffer() { - if (mLoaded) { - alDeleteBuffers(1, &mBuffer); - if (alCheck()) - GetLogger()->Warn("Failed to unload buffer. Code %d\n", alGetCode()); - } -} - - -bool Buffer::LoadFromFile(std::string filename, Sound sound) { - mSound = sound; - - GetLogger()->Info("Loading audio file: %s\n", filename.c_str()); - mBuffer = alutCreateBufferFromFile(filename.c_str()); - - ALenum error = alutGetError(); - if (error) { - GetLogger()->Warn("Failed to load file. Reason: %s\n", alutGetErrorString(error)); - mLoaded = false; - return false; - } - - ALint size, bits, channels, freq; - - alGetBufferi(mBuffer, AL_SIZE, &size); - alGetBufferi(mBuffer, AL_BITS, &bits); - alGetBufferi(mBuffer, AL_CHANNELS, &channels); - alGetBufferi(mBuffer, AL_FREQUENCY, &freq); - - mDuration = (ALfloat)size / channels / bits / 8 / (ALfloat)freq; - - mLoaded = true; - return true; -} - - -Sound Buffer::GetSoundType() { - return mSound; -} - - -ALuint Buffer::GetBuffer() { - return mBuffer; -} - - -bool Buffer::IsLoaded() { - return mLoaded; -} - - -float Buffer::GetDuration() { - return mDuration; -} diff --git a/src/sound/plugins/oalsound/buffer.h b/src/sound/plugins/oalsound/buffer.h deleted file mode 100644 index 8c4a2d3..0000000 --- a/src/sound/plugins/oalsound/buffer.h +++ /dev/null @@ -1,48 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -// buffer.h - -#pragma once - -#include - -#include - -#include "sound/sound.h" -#include "common/logger.h" - -#include "check.h" - -class Buffer -{ - public: - Buffer(); - ~Buffer(); - - bool LoadFromFile(std::string, Sound); - bool IsLoaded(); - - Sound GetSoundType(); - ALuint GetBuffer(); - float GetDuration(); - - private: - ALuint mBuffer; - Sound mSound; - bool mLoaded; - float mDuration; -}; diff --git a/src/sound/plugins/oalsound/channel.cpp b/src/sound/plugins/oalsound/channel.cpp deleted file mode 100644 index 4476dee..0000000 --- a/src/sound/plugins/oalsound/channel.cpp +++ /dev/null @@ -1,304 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -// channel.cpp - -#include "channel.h" - - -Channel::Channel() { - alGenSources(1, &mSource); - - if (alCheck()) { - GetLogger()->Warn("Failed to create sound source. Code: %d\n", alGetCode()); - mReady = false; - } else { - mReady = true; - } - - mPriority = 0; - mBuffer = nullptr; -} - - -Channel::~Channel() { - if (mReady) { - alSourcei(mSource, AL_BUFFER, 0); - alDeleteSources(1, &mSource); - if (alCheck()) - GetLogger()->Warn("Failed to delete sound source. Code: %s\n", alGetCode()); - } -} - - -bool Channel::Play() { - if (!mReady) - return false; - - alSourcePlay(mSource); - if (alCheck()) - GetLogger()->Warn("Could not play audio sound source. Code: %s\n", alGetCode()); - return true; -} - - -bool Channel::SetPosition(Math::Vector pos) { - if (!mReady) - return false; - - alSource3f(mSource, AL_POSITION, pos.x, pos.y, pos.z); - if (alCheck()) { - GetLogger()->Warn("Could not set sound position. Code: %s\n", alGetCode()); - return false; - } - return true; -} - - -bool Channel::SetFrequency(float freq) -{ - if (!mReady) - return false; - - alSourcef(mSource, AL_PITCH, freq); - if (alCheck()) { - GetLogger()->Warn("Could not set sound pitch. Code: %s\n", alGetCode()); - return false; - } - return true; -} - - -float Channel::GetFrequency() -{ - ALfloat freq; - if (!mReady) - return 0; - - alGetSourcef(mSource, AL_PITCH, &freq); - if (alCheck()) { - GetLogger()->Warn("Could not get sound pitch. Code: %s\n", alGetCode()); - return 0; - } - - return freq; -} - - -bool Channel::SetVolume(float vol) -{ - if (!mReady || vol < 0) - return false; - - alSourcef(mSource, AL_GAIN, vol / MAXVOLUME); - if (alCheck()) { - GetLogger()->Warn("Could not set sound volume. Code: %s\n", alGetCode()); - return false; - } - return true; -} - - -float Channel::GetVolume() -{ - ALfloat vol; - if (!mReady) - return 0; - - alGetSourcef(mSource, AL_GAIN, &vol); - if (alCheck()) { - GetLogger()->Warn("Could not get sound volume. Code: %s\n", alGetCode()); - return 0; - } - - return vol * MAXVOLUME; -} - - -int Channel::GetPriority() -{ - return mPriority; -} - - -void Channel::SetPriority(int pri) -{ - mPriority = pri; -} - - -void Channel::SetStartAmplitude(float gain) -{ - mStartAmplitude = gain; -} - - -void Channel::SetStartFrequency(float freq) -{ - mStartFrequency = freq; -} - - -void Channel::SetChangeFrequency(float freq) -{ - mChangeFrequency = freq; -} - - -void Channel::SetInitFrequency(float freq) -{ - mInitFrequency = freq; -} - - -float Channel::GetStartAmplitude() -{ - return mStartAmplitude; -} - - -float Channel::GetStartFrequency() -{ - return mStartFrequency; -} - - -float Channel::GetChangeFrequency() -{ - return mChangeFrequency; -} - - -float Channel::GetInitFrequency() -{ - return mInitFrequency; -} - - -void Channel::AddOper(SoundOper oper) -{ - mOper.push_back(oper); -} - - -void Channel::ResetOper() -{ - mOper.clear(); -} - - -Sound Channel::GetSoundType() { - return mBuffer->GetSoundType(); -} - - -bool Channel::SetBuffer(Buffer *buffer) { - if (!mReady) - return false; - - assert(buffer); - mBuffer = buffer; - alSourcei(mSource, AL_BUFFER, buffer->GetBuffer()); - if (alCheck()) { - GetLogger()->Warn("Could not set sound buffer. Code: %s\n", alGetCode()); - return false; - } - mInitFrequency = GetFrequency(); - return true; -} - - -void Channel::AdjustFrequency(float freq) { - SetFrequency(freq * mInitFrequency); -} - - -void Channel::AdjustVolume(float volume) { - SetVolume(mStartAmplitude * (float) volume); -} - - -bool Channel::IsPlaying() { - ALint status; - if (!mReady) return false; - - alGetSourcei(mSource, AL_SOURCE_STATE, &status); - if (alCheck()) { - GetLogger()->Warn("Could not get sound status. Code: %s\n", alGetCode()); - return false; - } - - return status == AL_PLAYING; -} - - -bool Channel::IsReady() { - return mReady; -} - - -bool Channel::Stop() { - alSourceStop(mSource); - if (alCheck()) { - GetLogger()->Warn("Could not stop sound. Code: %s\n", alGetCode()); - return false; - } - return true; -} - - -float Channel::GetCurrentTime() -{ - ALfloat current; - alGetSourcef(mSource, AL_SEC_OFFSET, ¤t); - if (alCheck()) { - GetLogger()->Warn("Could not get source current play time. Code: %s\n", alGetCode()); - return 0.0f; - } - return current; -} - - -void Channel::SetCurrentTime(float current) -{ - alSourcef(mSource, AL_SEC_OFFSET, current); - if (alCheck()) - GetLogger()->Warn("Could not get source current play time. Code: %s\n", alGetCode()); -} - - -float Channel::GetDuration() -{ - return mBuffer->GetDuration(); -} - - -bool Channel::HasEnvelope() -{ - return mOper.size() > 0; -} - - -SoundOper& Channel::GetEnvelope() -{ - return mOper.front(); -} - - -void Channel::PopEnvelope() -{ - mOper.pop_front(); -} diff --git a/src/sound/plugins/oalsound/channel.h b/src/sound/plugins/oalsound/channel.h deleted file mode 100644 index 165ff50..0000000 --- a/src/sound/plugins/oalsound/channel.h +++ /dev/null @@ -1,99 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -// channel.h - -#pragma once - -#include -#include -#include - -#include -#include - -#include "sound/sound.h" - -#include "buffer.h" -#include "check.h" - -struct SoundOper -{ - float finalAmplitude; - float finalFrequency; - float totalTime; - SoundNext nextOper; -}; - - -class Channel -{ - public: - Channel(); - ~Channel(); - - bool Play(); - bool Stop(); - bool SetPosition(Math::Vector); - - bool SetFrequency(float); - float GetFrequency(); - - float GetCurrentTime(); - void SetCurrentTime(float); - float GetDuration(); - - bool SetVolume(float); - float GetVolume(); - bool IsPlaying(); - bool IsReady(); - - bool SetBuffer(Buffer *); - bool HasEnvelope(); - SoundOper& GetEnvelope(); - void PopEnvelope(); - - int GetPriority(); - void SetPriority(int); - - void SetStartAmplitude(float); - void SetStartFrequency(float); - void SetChangeFrequency(float); - void SetInitFrequency(float); - - float GetStartAmplitude(); - float GetStartFrequency(); - float GetChangeFrequency(); - float GetInitFrequency(); - - void AddOper(SoundOper); - void ResetOper(); - Sound GetSoundType(); - void AdjustFrequency(float); - void AdjustVolume(float); - - private: - Buffer *mBuffer; - ALuint mSource; - - int mPriority; - float mStartAmplitude; - float mStartFrequency; - float mChangeFrequency; - float mInitFrequency; - std::deque mOper; - bool mReady; -}; diff --git a/src/sound/plugins/oalsound/check.h b/src/sound/plugins/oalsound/check.h deleted file mode 100644 index cf3e468..0000000 --- a/src/sound/plugins/oalsound/check.h +++ /dev/null @@ -1,39 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -// check.h - -#pragma once - -#include -#include - -#include "common/logger.h" - -static ALenum CODE = AL_NO_ERROR; - -inline bool alCheck() -{ - CODE = alGetError(); - return CODE != AL_NO_ERROR; -} - -inline ALenum alGetCode() -{ - ALenum ret = CODE; - CODE = AL_NO_ERROR; - return ret; -} diff --git a/src/sound/plugins/oalsound/test/CMakeLists.txt b/src/sound/plugins/oalsound/test/CMakeLists.txt deleted file mode 100644 index dd208ea..0000000 --- a/src/sound/plugins/oalsound/test/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE debug) -endif(NOT CMAKE_BUILD_TYPE) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -rdynamic") -set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") - -add_executable(plugin_test plugin_test.cpp ../../../../common/iman.cpp ../../../../common/logger.cpp ../../../../plugins/pluginloader.cpp) - -include_directories(".") -include_directories("../../../../") - -target_link_libraries(plugin_test ${LTDL_LIBRARY}) diff --git a/src/sound/plugins/oalsound/test/plugin_test.cpp b/src/sound/plugins/oalsound/test/plugin_test.cpp deleted file mode 100644 index 40c1cd2..0000000 --- a/src/sound/plugins/oalsound/test/plugin_test.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - - -int main() { - new CLogger(); - new CInstanceManager(); - - lt_dlinit(); - - CPluginLoader *plugin = new CPluginLoader("libopenalsound"); - if (plugin->LoadPlugin()) { - CSoundInterface *sound = static_cast(CInstanceManager::GetInstancePointer()->SearchInstance(CLASS_SOUND)); - - sound->Create(true); - sound->CacheAll(); - sound->Play((Sound)8); - sound->Play((Sound)18); - - sleep(10); - /* - while (1) - { - // just a test, very slow - plugin->FrameMove(0); - //if ('n' == getchar()) - // break; - }*/ - plugin->UnloadPlugin(); - } - - lt_dlexit(); - return 0; -} diff --git a/src/sound/sound.h b/src/sound/sound.h index c2b890f..566f415 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -28,8 +28,6 @@ #include "common/iman.h" #include "common/logger.h" -#include "plugins/plugininterface.h" - #include #include #include @@ -152,7 +150,7 @@ enum SoundNext * \brief Sound plugin interface * */ -class CSoundInterface : public CPluginInterface +class CSoundInterface { public: inline CSoundInterface() { -- cgit v1.2.3-1-g7c22 From f0e76ad446e58ea9b1564517fc3a823e61fd7410 Mon Sep 17 00:00:00 2001 From: erihel Date: Thu, 20 Dec 2012 21:57:57 +0100 Subject: sound fix --- src/sound/oalsound/CMakeLists.txt | 24 -------------------- src/sound/oalsound/alsound.cpp | 40 +++++---------------------------- src/sound/oalsound/test/CMakeLists.txt | 14 ------------ src/sound/oalsound/test/plugin_test.cpp | 40 --------------------------------- 4 files changed, 6 insertions(+), 112 deletions(-) delete mode 100644 src/sound/oalsound/CMakeLists.txt delete mode 100644 src/sound/oalsound/test/CMakeLists.txt delete mode 100644 src/sound/oalsound/test/plugin_test.cpp (limited to 'src/sound') diff --git a/src/sound/oalsound/CMakeLists.txt b/src/sound/oalsound/CMakeLists.txt deleted file mode 100644 index bb7e9ff..0000000 --- a/src/sound/oalsound/CMakeLists.txt +++ /dev/null @@ -1,24 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -set(SOURCES - alsound.cpp - buffer.cpp - channel.cpp -) - -SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x -fPIC") -SET (CMAKE_CXX_FLAGS_DEBUG "-g") - -include(FindPkgConfig) -include(FindOpenAL) -pkg_check_modules(OPENAL_LIB REQUIRED openal) - -set(OPENAL_LIBRARIES - openal - alut -) - -include_directories(../../..) -include_directories(.) -add_library(openalsound SHARED ${SOURCES}) -target_link_libraries(openalsound ${OPENAL_LIBRARIES}) diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 83a4def..5f416da 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -23,51 +23,23 @@ #define MIN(a, b) (a > b ? b : a) - -PLUGIN_INTERFACE(ALSound) - - -std::string ALSound::PluginName() -{ - return "Sound plugin using OpenAL library to play sounds."; -} - - -int ALSound::PluginVersion() -{ - return 2; -} - - -void ALSound::InstallPlugin() -{ - auto pointer = CInstanceManager::GetInstancePointer(); - if (pointer != nullptr) - CInstanceManager::GetInstancePointer()->AddInstance(CLASS_SOUND, this); -} - - -bool ALSound::UninstallPlugin(std::string &reason) -{ - auto pointer = CInstanceManager::GetInstancePointer(); - if (pointer != nullptr) - CInstanceManager::GetInstancePointer()->DeleteInstance(CLASS_SOUND, this); - CleanUp(); - return true; -} - - ALSound::ALSound() { mEnabled = false; m3D = false; mAudioVolume = MAXVOLUME; mMute = false; + auto pointer = CInstanceManager::GetInstancePointer(); + if (pointer != nullptr) + CInstanceManager::GetInstancePointer()->AddInstance(CLASS_SOUND, this); } ALSound::~ALSound() { + auto pointer = CInstanceManager::GetInstancePointer(); + if (pointer != nullptr) + CInstanceManager::GetInstancePointer()->DeleteInstance(CLASS_SOUND, this); CleanUp(); } diff --git a/src/sound/oalsound/test/CMakeLists.txt b/src/sound/oalsound/test/CMakeLists.txt deleted file mode 100644 index dd208ea..0000000 --- a/src/sound/oalsound/test/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE debug) -endif(NOT CMAKE_BUILD_TYPE) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -rdynamic") -set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") - -add_executable(plugin_test plugin_test.cpp ../../../../common/iman.cpp ../../../../common/logger.cpp ../../../../plugins/pluginloader.cpp) - -include_directories(".") -include_directories("../../../../") - -target_link_libraries(plugin_test ${LTDL_LIBRARY}) diff --git a/src/sound/oalsound/test/plugin_test.cpp b/src/sound/oalsound/test/plugin_test.cpp deleted file mode 100644 index 40c1cd2..0000000 --- a/src/sound/oalsound/test/plugin_test.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - - -int main() { - new CLogger(); - new CInstanceManager(); - - lt_dlinit(); - - CPluginLoader *plugin = new CPluginLoader("libopenalsound"); - if (plugin->LoadPlugin()) { - CSoundInterface *sound = static_cast(CInstanceManager::GetInstancePointer()->SearchInstance(CLASS_SOUND)); - - sound->Create(true); - sound->CacheAll(); - sound->Play((Sound)8); - sound->Play((Sound)18); - - sleep(10); - /* - while (1) - { - // just a test, very slow - plugin->FrameMove(0); - //if ('n' == getchar()) - // break; - }*/ - plugin->UnloadPlugin(); - } - - lt_dlexit(); - return 0; -} -- cgit v1.2.3-1-g7c22 From 1fa5f7a96fb4a1700847ebcf0b07a557da630b10 Mon Sep 17 00:00:00 2001 From: erihel Date: Tue, 25 Dec 2012 17:46:48 +0100 Subject: * Changed log level in sound module * Fixed warnings in sound module * Fixed problem with map loading when locale with different number separator --- src/sound/oalsound/alsound.cpp | 8 ++++---- src/sound/oalsound/buffer.cpp | 4 ++-- src/sound/oalsound/channel.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/sound') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 5f416da..00cb9b2 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -259,7 +259,7 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) int lowerOrEqual = -1; for (auto it : mChannels) { if (it.second->GetPriority() < priority) { - GetLogger()->Info("Sound channel with lower priority will be reused."); + GetLogger()->Debug("Sound channel with lower priority will be reused."); channel = it.first; return true; } @@ -269,7 +269,7 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) if (lowerOrEqual != -1) { channel = lowerOrEqual; - GetLogger()->Info("Sound channel with lower or equal priority will be reused."); + GetLogger()->Debug("Sound channel with lower or equal priority will be reused."); return true; } @@ -457,11 +457,11 @@ void ALSound::FrameMove(float delta) if (it.second->GetEnvelope().totalTime <= it.second->GetCurrentTime()) { if (oper.nextOper == SOPER_LOOP) { - GetLogger()->Info("Replay.\n"); + GetLogger()->Trace("Sound oper: replay.\n"); it.second->SetCurrentTime(0.0f); it.second->Play(); } else { - GetLogger()->Info("Next.\n"); + GetLogger()->Trace("Sound oper: next.\n"); it.second->SetStartAmplitude(oper.finalAmplitude); it.second->SetStartFrequency(oper.finalFrequency); it.second->PopEnvelope(); diff --git a/src/sound/oalsound/buffer.cpp b/src/sound/oalsound/buffer.cpp index 37211e9..dbfdca2 100644 --- a/src/sound/oalsound/buffer.cpp +++ b/src/sound/oalsound/buffer.cpp @@ -36,7 +36,7 @@ Buffer::~Buffer() { bool Buffer::LoadFromFile(std::string filename, Sound sound) { mSound = sound; - GetLogger()->Info("Loading audio file: %s\n", filename.c_str()); + GetLogger()->Debug("Loading audio file: %s\n", filename.c_str()); mBuffer = alutCreateBufferFromFile(filename.c_str()); ALenum error = alutGetError(); @@ -53,7 +53,7 @@ bool Buffer::LoadFromFile(std::string filename, Sound sound) { alGetBufferi(mBuffer, AL_CHANNELS, &channels); alGetBufferi(mBuffer, AL_FREQUENCY, &freq); - mDuration = (ALfloat)size / channels / bits / 8 / (ALfloat)freq; + mDuration = static_cast(size) / channels / bits / 8 / static_cast(freq); mLoaded = true; return true; diff --git a/src/sound/oalsound/channel.cpp b/src/sound/oalsound/channel.cpp index 4476dee..2285414 100644 --- a/src/sound/oalsound/channel.cpp +++ b/src/sound/oalsound/channel.cpp @@ -227,7 +227,7 @@ void Channel::AdjustFrequency(float freq) { void Channel::AdjustVolume(float volume) { - SetVolume(mStartAmplitude * (float) volume); + SetVolume(mStartAmplitude * volume); } -- cgit v1.2.3-1-g7c22 From c793d2d4c67a5556e39300988f42a630fc51e68d Mon Sep 17 00:00:00 2001 From: erihel Date: Tue, 25 Dec 2012 21:36:50 +0100 Subject: * Changed Ret to Get methods in sound module * Added video settings to profile * Resolution change is now posibble but restart is required --- src/sound/oalsound/alsound.cpp | 12 ++++++------ src/sound/oalsound/alsound.h | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/sound') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 00cb9b2..0201417 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -86,14 +86,14 @@ void ALSound::SetSound3D(bool bMode) } -bool ALSound::RetSound3D() +bool ALSound::GetSound3D() { // TODO stub! need to be implemented return true; } -bool ALSound::RetSound3DCap() +bool ALSound::GetSound3DCap() { // TODO stub! need to be implemented return true; @@ -113,7 +113,7 @@ void ALSound::SetAudioVolume(int volume) } -int ALSound::RetAudioVolume() +int ALSound::GetAudioVolume() { float volume; if ( !mEnabled ) @@ -130,7 +130,7 @@ void ALSound::SetMusicVolume(int volume) } -int ALSound::RetMusicVolume() +int ALSound::GetMusicVolume() { // TODO stub! Add music support if ( !mEnabled ) @@ -151,7 +151,7 @@ bool ALSound::Cache(Sound sound, std::string filename) } -int ALSound::RetPriority(Sound sound) +int ALSound::GetPriority(Sound sound) { if ( sound == SOUND_FLYh || sound == SOUND_FLY || @@ -202,7 +202,7 @@ int ALSound::RetPriority(Sound sound) bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) { - int priority = RetPriority(sound); + int priority = GetPriority(sound); // Seeks a channel used which sound is stopped. for (auto it : mChannels) { diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h index a1128e0..7d24ba6 100644 --- a/src/sound/oalsound/alsound.h +++ b/src/sound/oalsound/alsound.h @@ -45,13 +45,13 @@ class ALSound : public CSoundInterface bool RetEnable(); void SetSound3D(bool bMode); - bool RetSound3D(); - bool RetSound3DCap(); + bool GetSound3D(); + bool GetSound3DCap(); void SetAudioVolume(int volume); - int RetAudioVolume(); + int GetAudioVolume(); void SetMusicVolume(int volume); - int RetMusicVolume(); + int GetMusicVolume(); void SetListener(Math::Vector eye, Math::Vector lookat); void FrameMove(float rTime); @@ -80,7 +80,7 @@ class ALSound : public CSoundInterface private: void CleanUp(); - int RetPriority(Sound); + int GetPriority(Sound); bool SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded); bool mEnabled; -- cgit v1.2.3-1-g7c22 From c9ca8f2b62b4994fa797272aecaf33f23253acae Mon Sep 17 00:00:00 2001 From: erihel Date: Fri, 28 Dec 2012 21:19:50 +0100 Subject: * Fixed segault when could not set sound pitch (bad logger call) * All 81 audio files should be loaded instead of 69 high quality * Changed volume ajustment formula to proper one --- src/sound/oalsound/alsound.cpp | 14 ++++++++------ src/sound/oalsound/channel.cpp | 24 ++++++++++++------------ src/sound/sound.h | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) (limited to 'src/sound') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 0201417..f683a62 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -296,6 +296,8 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc GetLogger()->Warn("Sound %d was not loaded!\n", sound); return -1; } + + GetLogger()->Trace("ALSound::Play sound: %d volume: %f frequency: %f\n", sound, amplitude, frequency); int channel; bool bAlreadyLoaded; @@ -308,12 +310,12 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc Position(channel, pos); // setting initial values - mChannels[channel]->SetStartAmplitude(amplitude); + mChannels[channel]->SetStartAmplitude(mAudioVolume); mChannels[channel]->SetStartFrequency(frequency); mChannels[channel]->SetChangeFrequency(1.0f); mChannels[channel]->ResetOper(); - mChannels[channel]->AdjustFrequency(frequency); - mChannels[channel]->AdjustVolume(mAudioVolume); + mChannels[channel]->AdjustFrequency(frequency); + mChannels[channel]->AdjustVolume(amplitude * mAudioVolume); mChannels[channel]->Play(); return channel; } @@ -451,17 +453,17 @@ void ALSound::FrameMove(float delta) it.second->AdjustVolume(volume * mAudioVolume); // setting frequency - frequency = progress * (oper.finalFrequency - it.second->GetStartFrequency()) * it.second->GetStartFrequency() * it.second->GetChangeFrequency(); + frequency = progress * abs(oper.finalFrequency - it.second->GetStartFrequency()) * it.second->GetStartFrequency() * it.second->GetChangeFrequency(); it.second->AdjustFrequency(frequency); if (it.second->GetEnvelope().totalTime <= it.second->GetCurrentTime()) { if (oper.nextOper == SOPER_LOOP) { - GetLogger()->Trace("Sound oper: replay.\n"); + GetLogger()->Trace("ALSound::FrameMove oper: replay.\n"); it.second->SetCurrentTime(0.0f); it.second->Play(); } else { - GetLogger()->Trace("Sound oper: next.\n"); + GetLogger()->Trace("ALSound::FrameMove oper: next.\n"); it.second->SetStartAmplitude(oper.finalAmplitude); it.second->SetStartFrequency(oper.finalFrequency); it.second->PopEnvelope(); diff --git a/src/sound/oalsound/channel.cpp b/src/sound/oalsound/channel.cpp index 2285414..7d8244b 100644 --- a/src/sound/oalsound/channel.cpp +++ b/src/sound/oalsound/channel.cpp @@ -39,7 +39,7 @@ Channel::~Channel() { alSourcei(mSource, AL_BUFFER, 0); alDeleteSources(1, &mSource); if (alCheck()) - GetLogger()->Warn("Failed to delete sound source. Code: %s\n", alGetCode()); + GetLogger()->Warn("Failed to delete sound source. Code: %d\n", alGetCode()); } } @@ -50,7 +50,7 @@ bool Channel::Play() { alSourcePlay(mSource); if (alCheck()) - GetLogger()->Warn("Could not play audio sound source. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not play audio sound source. Code: %d\n", alGetCode()); return true; } @@ -61,7 +61,7 @@ bool Channel::SetPosition(Math::Vector pos) { alSource3f(mSource, AL_POSITION, pos.x, pos.y, pos.z); if (alCheck()) { - GetLogger()->Warn("Could not set sound position. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not set sound position. Code: %d\n", alGetCode()); return false; } return true; @@ -75,7 +75,7 @@ bool Channel::SetFrequency(float freq) alSourcef(mSource, AL_PITCH, freq); if (alCheck()) { - GetLogger()->Warn("Could not set sound pitch. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not set sound pitch to '%f'. Code: %d\n", freq, alGetCode()); return false; } return true; @@ -90,7 +90,7 @@ float Channel::GetFrequency() alGetSourcef(mSource, AL_PITCH, &freq); if (alCheck()) { - GetLogger()->Warn("Could not get sound pitch. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not get sound pitch. Code: %d\n", alGetCode()); return 0; } @@ -105,7 +105,7 @@ bool Channel::SetVolume(float vol) alSourcef(mSource, AL_GAIN, vol / MAXVOLUME); if (alCheck()) { - GetLogger()->Warn("Could not set sound volume. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not set sound volume to '%f'. Code: %d\n", vol, alGetCode()); return false; } return true; @@ -120,7 +120,7 @@ float Channel::GetVolume() alGetSourcef(mSource, AL_GAIN, &vol); if (alCheck()) { - GetLogger()->Warn("Could not get sound volume. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not get sound volume. Code: %d\n", alGetCode()); return 0; } @@ -213,7 +213,7 @@ bool Channel::SetBuffer(Buffer *buffer) { mBuffer = buffer; alSourcei(mSource, AL_BUFFER, buffer->GetBuffer()); if (alCheck()) { - GetLogger()->Warn("Could not set sound buffer. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not set sound buffer. Code: %d\n", alGetCode()); return false; } mInitFrequency = GetFrequency(); @@ -237,7 +237,7 @@ bool Channel::IsPlaying() { alGetSourcei(mSource, AL_SOURCE_STATE, &status); if (alCheck()) { - GetLogger()->Warn("Could not get sound status. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not get sound status. Code: %d\n", alGetCode()); return false; } @@ -253,7 +253,7 @@ bool Channel::IsReady() { bool Channel::Stop() { alSourceStop(mSource); if (alCheck()) { - GetLogger()->Warn("Could not stop sound. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not stop sound. Code: %d\n", alGetCode()); return false; } return true; @@ -265,7 +265,7 @@ float Channel::GetCurrentTime() ALfloat current; alGetSourcef(mSource, AL_SEC_OFFSET, ¤t); if (alCheck()) { - GetLogger()->Warn("Could not get source current play time. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not get source current play time. Code: %d\n", alGetCode()); return 0.0f; } return current; @@ -276,7 +276,7 @@ void Channel::SetCurrentTime(float current) { alSourcef(mSource, AL_SEC_OFFSET, current); if (alCheck()) - GetLogger()->Warn("Could not get source current play time. Code: %s\n", alGetCode()); + GetLogger()->Warn("Could not get source current play time. Code: %d\n", alGetCode()); } diff --git a/src/sound/sound.h b/src/sound/sound.h index 566f415..518e2ad 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -168,7 +168,7 @@ class CSoundInterface * Function calls \link CSoundInterface::Cache() \endlink for each file */ inline void CacheAll(std::string path) { - for ( int i = 1; i < 69; i++ ) { + for ( int i = 1; i <= 81; i++ ) { std::stringstream filename; filename << path << "/sound" << std::setfill('0') << std::setw(3) << i << ".wav"; if ( !Cache(static_cast(i), filename.str()) ) -- cgit v1.2.3-1-g7c22