diff options
author | chai <chaifix@163.com> | 2018-10-23 12:23:58 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-23 12:23:58 +0800 |
commit | 40fc27154fe754181934dc7ee31375e6bdfb33fc (patch) | |
tree | 897ad98d759bc308ef66561181ba88b85f2ccd47 /src/libjin/Audio/SDL/SDLAudio.cpp | |
parent | 1480c9445100075c9e1a894eb07c0ef727b509a1 (diff) |
*merge from minimal
Diffstat (limited to 'src/libjin/Audio/SDL/SDLAudio.cpp')
-rw-r--r-- | src/libjin/Audio/SDL/SDLAudio.cpp | 141 |
1 files changed, 0 insertions, 141 deletions
diff --git a/src/libjin/Audio/SDL/SDLAudio.cpp b/src/libjin/Audio/SDL/SDLAudio.cpp deleted file mode 100644 index f7ca70d..0000000 --- a/src/libjin/Audio/SDL/SDLAudio.cpp +++ /dev/null @@ -1,141 +0,0 @@ -#include "../../modules.h" -#if JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO - -#include <iostream> -#include "SDLAudio.h" -#include "SDLSource.h" -#include "../../math/math.h" -#include "../../utils/log.h" - -namespace jin -{ -namespace audio -{ - - using namespace jin::math; - - /* עcallbackƵ̵߳ */ - static void defaultCallback(void *userdata, Uint8 *stream, int size) - { - static SDLAudio* audio = static_cast<SDLAudio*>(userdata); - if (!audio->goOnProcess()) - return; - audio->lock(); - audio->processCommands(); - audio->processSources(stream, size); - audio->processBuffer(stream, size); - audio->unlock(); - } - - onlyonce bool SDLAudio::initSystem(const SettingBase* s) - { -#if JIN_DEBUG - Loghelper::log(Loglevel::LV_INFO, "Init Audio System"); -#endif - - if (SDL_Init(SDL_INIT_AUDIO) < 0) - return false; - SDL_AudioSpec spec; - Setting* setting = (Setting*)s; - if (setting == nullptr) - return false; - - unsigned int samplerate = setting->samplerate; - unsigned int samples = clamp<int>(setting->samples, 1, setting->samplerate); - - spec.freq = samplerate; // ÿsample,õ 11025, 22050, 44100 and 48000 Hz. - spec.format = AUDIO_S16SYS; // signed 16-bit samples in native byte order - spec.channels = SDLAUDIO_CHANNELS; // - spec.samples = samples; // ÿβʱһã=setting->samplerateÿֻ1 - spec.userdata = this; - spec.callback = defaultCallback; - - audioDevice = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0); - if (audioDevice == 0) - return false; - /* start audio */ - SDL_PauseAudioDevice(audioDevice, 0); - return true; - } - - onlyonce void SDLAudio::quitSystem() - { - SDL_CloseAudio(); - } - - void SDLAudio::lock() - { - SDL_LockAudioDevice(audioDevice); - } - - void SDLAudio::unlock() - { - SDL_UnlockAudioDevice(audioDevice); - } - - bool SDLAudio::goOnProcess() - { - if (state == SDLAudio::State::STOP) - { - SDLSourceManager::get()->removeAllSource(); - pause(); - return false; - } - else if (state == SDLAudio::State::PAUSE) - return false; - else - return true; - } - - void SDLAudio::processCommands() - { - SDLSourceManager::get()->processCommands(); - } - - void SDLAudio::processSources(void* buffer, size_t len) - { - SDLSourceManager::get()->processSources(buffer, len); - } - - void SDLAudio::processBuffer(void* buff, size_t len) - { - short* buffer = (short*)buff; - int samples = (len / SDLAUDIO_BYTEDEPTH) >> 1; // ˫ - const char L = 0, R = 1; - for (int i = 0; i < samples; ++i) - { - short* clip = buffer + (i << 1); - clip[L] *= volume; - clip[R] *= volume; - } - } - - void SDLAudio::play() - { - state = State::PLAY; - } - - void SDLAudio::stop() - { - state = State::STOP; - } - - void SDLAudio::pause() - { - state = State::PAUSE; - } - - void SDLAudio::resume() - { - state = State::PLAY; - } - - void SDLAudio::setVolume(float volume) - { - this->volume = clamp(volume, 0.0f, 1.0f); - } - -} -} - -#endif // JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO
\ No newline at end of file |