Skip to content

Commit

Permalink
+ clunk fixes for static linking and audio devices with different aud…
Browse files Browse the repository at this point in the history
…io formats
  • Loading branch information
Thanduriel committed Jul 6, 2020
1 parent d49773b commit 3f80138
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ target_link_libraries (wimp_s PUBLIC epsilon)
find_package(SDL2 QUIET)
if(NOT SDL2_FOUND)
set(SDL2_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/SDL")
find_package(SDL2 REQUIRED)
endif(NOT SDL2_FOUND)
include_directories("dependencies/sdl/include")
add_subdirectory("dependencies/clunk")
#get_target_property(CLUNK_BIN_DIR clunk-static LOCATION)
#message(${CLUNK_BIN_DIR})
target_include_directories(clunk-static PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/dependencies/clunk")
include_directories("dependencies/clunk")
target_link_libraries (wimp_s PUBLIC clunk)
target_link_libraries (wimp_s PUBLIC clunk-static)
target_link_libraries (wimp_s PUBLIC ${SDL2_LIBRARIES})

# JoFileLib
add_subdirectory("dependencies/JoFileLib")
Expand Down
2 changes: 2 additions & 0 deletions dependencies/SDL/SDL2Config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SET(SDL2_INCLUDE_DIRS "include/")
SET(SDL2_LIBRARIES "${SDL2_DIR}/lib/x64/SDL2.lib")
3 changes: 1 addition & 2 deletions src/gameplay/content.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "content.hpp"

#include "elements/audiocomponent.hpp"
#include <clunk/context.h>
#include <clunk/backend/sdl/backend.h>
#include <clunk/source.h>
#include <clunk/wav_file.h>

#include "elements/collisioncomponent.hpp"
#include "elements/audiocomponent.hpp"
#include "utils/logger.hpp"
namespace Game {
std::unordered_map<std::string, BoundingMesh*> Content::boundingMeshes;
Expand Down
6 changes: 3 additions & 3 deletions src/gameplay/elements/audiocomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
#include "utils/assert.hpp"
#include "control/camera.hpp"

#include "utils/audiobackend.hpp"
#include <clunk/context.h>
#include <clunk/backend/sdl/backend.h>
#include <clunk/source.h>
#include <clunk/wav_file.h>

namespace Game {

clunk::sdl::Backend* AudioSystem::s_backend;
clunk::sdl::AudioBackend* AudioSystem::s_backend;
AudioComponent* AudioSystem::s_audioComponent;
float AudioSystem::s_masterVolume;
float AudioSystem::s_fallOffFactor;

void AudioSystem::Initialize(float _volume)
{
static clunk::sdl::Backend backend(44100, 2, 1024);
static clunk::sdl::AudioBackend backend(44100, 2, 1024);
s_backend = &backend;

clunk::Context &context = backend.get_context();
Expand Down
8 changes: 6 additions & 2 deletions src/gameplay/elements/audiocomponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include "gameplay/core/component.hpp"
#include "math/transformation.hpp"

#ifndef CLUNKAPI
#define CLUNKAPI
#endif

namespace clunk {
namespace sdl { class Backend; }
namespace sdl { class AudioBackend; }

class Object;
class Sample;
Expand Down Expand Up @@ -33,7 +37,7 @@ namespace Game {
static void Enable3DSounds();
private:

static clunk::sdl::Backend* s_backend;
static clunk::sdl::AudioBackend* s_backend;
static AudioComponent* s_audioComponent;
static float s_masterVolume;
static float s_fallOffFactor;
Expand Down
3 changes: 2 additions & 1 deletion src/gameplay/elements/singlesound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include "audiocomponent.hpp"
#include "lifetimecomponent.hpp"
#include "gameplay/core/actor.hpp"
#include "clunk/sample.h"

#include <clunk/sample.h>

namespace Game {

Expand Down
143 changes: 143 additions & 0 deletions src/utils/audiobackend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
MIT License
Copyright (c) 2008-2019 Netive Media Group & Vladimir Menshakov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "audiobackend.hpp"
#include <clunk/backend/sdl/sdl_ex.h>
#include <clunk/context.h>
#include <clunk/locker.h>
#include <SDL_rwops.h>
#include <SDL_audio.h>
#include <SDL.h>
#include <stdexcept>

#undef LOG_ERROR
#include "logger.hpp"

namespace clunk { namespace sdl {

AudioBackend::AudioBackend(int sample_rate, const u8 channels, int period_size) {
LOG_LVL1(("Initializing AudioBackend (" + std::to_string(sample_rate)
+ ", " + std::to_string(channels)
+ ", " + std::to_string(period_size) + ")"));
if (!SDL_WasInit(SDL_INIT_AUDIO)) {
if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1)
throw_sdl(("SDL_InitSubSystem"));
}

SDL_AudioSpec src;
memset(&src, 0, sizeof(src));
src.freq = sample_rate;
src.channels = channels;
src.format = AUDIO_S16SYS;
src.samples = period_size;
src.callback = &AudioBackend::callback;
src.userdata = this;

// !CHANGE! force conversion if the system does not use the targeted format
if ( SDL_OpenAudio(&src, nullptr) < 0 )
throw_sdl(("SDL_OpenAudio(%d, %u, %d)", sample_rate, channels, period_size));
// if (_spec.format != AUDIO_S16SYS)
// throw_ex(("SDL_OpenAudio(%d, %u, %d) returned format %d", sample_rate, channels, period_size, _spec.format));
// if (_spec.channels < 2)
// LOG_ERROR(("Could not operate on %d channels", _spec.channels));

LOG_LVL2(("Opened audio device, sample rate: "+ std::to_string(src.freq)
+ ", channels: " + std::to_string(src.channels)
+ ", period: " + std::to_string(src.samples)));
_context.init(convert(src));
}
void AudioBackend::start()
{
LOG_DEBUG(("starting output"));
SDL_PauseAudio(0);
}

void AudioBackend::stop()
{
LOG_DEBUG(("stopping output"));
SDL_PauseAudio(0);
}

AudioBackend::~AudioBackend() {
LOG_LVL2(("shutting down AudioBackend"));
if (!SDL_WasInit(SDL_INIT_AUDIO))
return;

stop();

SDL_CloseAudio();

SDL_QuitSubSystem(SDL_INIT_AUDIO);
}

void AudioBackend::callback(void *userdata, u8 *stream, int len) {
AudioBackend *self = static_cast<AudioBackend *>(userdata);
TRY {
self->_context.process(stream, len);
} CATCH("callback", {})
}


Sample* AudioBackend::load(const std::string &file) {
Uint8 *buf;
Uint32 len;
SDL_AudioSpec spec;
//SDL_AudioSpec * SDLCALL SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, u8 **audio_buf, u32 *audio_len);
if (SDL_LoadWAV(file.c_str(), &spec, &buf, &len) == NULL)
throw_sdl(("SDL_LoadWav"));
clunk::Buffer wav;
wav.set_data(buf, len, true);
Sample *sample = _context.create_sample();
sample->init(wav, convert(spec));
sample->name = file;
return sample;
}

SDL_AudioSpec AudioBackend::convert(const AudioSpec &spec) {
SDL_AudioSpec r = {};
r.channels = spec.channels;
r.freq = spec.sample_rate;
switch(spec.format) {
case AudioSpec::S8: r.format = AUDIO_S8; break;
case AudioSpec::U8: r.format = AUDIO_U8; break;
case AudioSpec::S16: r.format = AUDIO_S16SYS; break;
case AudioSpec::U16: r.format = AUDIO_U16SYS; break;
default: throw std::runtime_error("invalid audio format");
}
return r;
}

AudioSpec AudioBackend::convert(const SDL_AudioSpec &spec) {
AudioSpec::Format format;
switch(spec.format) {
case AUDIO_S8: format = AudioSpec::S8; break;
case AUDIO_U8: format = AudioSpec::U8; break;
case AUDIO_S16SYS: format = AudioSpec::S16; break;
case AUDIO_U16SYS: format = AudioSpec::U16; break;
default: throw std::runtime_error("invalid sdl audio format");
}
return AudioSpec(format, spec.freq, spec.channels);
}

}}
97 changes: 97 additions & 0 deletions src/utils/audiobackend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// This is a copy of backend.h from clunk which works on systems which have
// a different audio format. Changes are marked with !CHANGE!

/*
MIT License
Copyright (c) 2008-2019 Netive Media Group & Vladimir Menshakov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef CLUNK_BACKEND_SDL_H
#define CLUNK_BACKEND_SDL_H

/* clunk - cross-platform 3D audio API built on top SDL library
* Copyright (C) 2007-2014 Netive Media Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

// !CHANGE! since static linking is used we don not want dllimport specifiers.
#ifndef CLUNKAPI
#define CLUNKAPI
#endif

#include <clunk/config.h>
#include <clunk/sample.h>
#include <clunk/context.h>
#include <SDL.h>
#include <SDL_audio.h>


namespace clunk { namespace sdl {

class CLUNKAPI AudioBackend {
Context _context;
public:
/*!
\brief Initializes clunk context.
\param[in] sample_rate sample rate of the audio output
\param[in] channels audio output channels number, supported values 1 or 2 for now.
\param[out] period_size minimal processing unit (bytes). Less period - less latency.
*/
AudioBackend(int sample_rate, const u8 channels, int period_size);
~AudioBackend();

/*!
\brief loads sample from file
*/
Sample *load(const std::string &fname);
void start();
void stop();

///gets context
Context &get_context() { return _context; }

SDL_AudioSpec convert(const AudioSpec &spec);
AudioSpec convert(const SDL_AudioSpec &spec);

private:
static void callback(void *userdata, u8 *stream, int len);
SDL_AudioSpec _spec;
};

}}


#endif

0 comments on commit 3f80138

Please sign in to comment.