-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ clunk fixes for static linking and audio devices with different aud…
…io formats
- Loading branch information
1 parent
d49773b
commit 3f80138
Showing
8 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|