Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\main.cpp":
- #include <kit/all.hpp>
- using namespace kit;
- #define SDL_MAIN_HANDLED
- #include "./lodepng/lodepng.hpp"
- #include "./kit_sdl2/_kit_common.hpp"
- void _SurfaceSetColorKey(SDL_Surface* surf, bool enable, u32 key,
- const char* funcname)
- {
- if(SDL_SetColorKey(surf, enable, key))
- THROW_ERRORF("%s: \"%s\"", funcname, SDL_GetError());
- }
- void watch(Event& event, void* userdata){
- if(event.type != KEVENT_WIN_SIZE_CHANGED) return;
- if(userdata){
- ((Window*)userdata)->renewSurface();
- }
- kit_LogInfo("size changed (%llu)", time::getTicks());
- }
- int main(/*int argc, char** argv*/){ try { {
- initSubsystems(KINIT_EVENTS|KINIT_AUDIO);
- srand(time::getTicks());
- Window win(nullptr, 640, 480, WINFLAG_RESIZABLE);
- win.renewSurface();
- Surface livereact("live_reaction.png");
- bool enabled = true;
- EventWatch evtWatch(watch, &win);
- bool fullscreen = false;
- bool run = true;
- while(run){
- if(!enabled) win.renewSurface();
- Event evt;
- while(run && pollEvent(&evt))
- switch(evt.type){
- case KEVENT_QUIT: run = false; break;
- case KEVENT_KEY_DOWN: {
- switch(evt.key.vkey){
- case VKEY_F11: win.setFullscreen(2*(fullscreen^=1)); break;
- case VKEY_RETURN: evtWatch.setState((enabled^=1)); break;
- default:;
- win.fillRects((u32)rand());
- }
- //win.warpMouse(-50,-50);
- } break;
- case KEVENT_WIN_RESIZED: {
- } break;
- default:;
- }
- //livereact.blit(win, nullptr, nullptr);
- win.updateSurface();
- SDL_Delay(16);
- }
- }
- _getnumallocs
- quitSubsystems(KINIT_EVERYTHING);
- _getnumallocs
- return 0;
- } catch(const char* errorText){
- #ifdef _DEBUG
- kit_LogError("FATAL EXCEPTION OCCURRED: \"%s\"\n", errorText);
- #else
- showMsgBox(fstr("Error: %-14s", errorText),
- "FATAL EXCEPTION OCCURRED!", MSGBOX_ERROR);
- #endif /* _DEBUG */
- //redundant, since quitSubsystems(KINIT_EVERYTHING)
- //frees all thread errors automatically anyway
- //freeThreadErrors();
- _getnumallocs
- quitSubsystems(KINIT_EVERYTHING);
- _getnumallocs
- return -1;
- }}
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_AudioCallbackWrapper.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- //workaround for having _AudioCallbackWrapper pause the device,
- //without having to call SDL_PauseAudioDevice inside the callback itself
- //(this may potentially cut off playback before the current buffer plays, idk)
- static int _AudioPauseThread(void* data){
- _AudioDeviceOpaque* opq = (_AudioDeviceOpaque*)data;
- SDL_AudioDeviceID devID = opq->info_p->deviceID;
- //wait for the callback to exit before actually pausing
- SDL_LockAudioDevice(devID);
- SDL_PauseAudioDevice(devID, 1);
- opq->fadeOut = false;
- opq->playing = false;
- SDL_UnlockAudioDevice(devID);
- return 0;
- }
- static inline bool startPauseThread(_AudioDeviceOpaque* opq){
- SDL_Thread* pauseThread = SDL_CreateThread(_AudioPauseThread, "kADPause", opq);
- if(pauseThread){
- SDL_DetachThread(pauseThread);
- } else {
- opq->fadeDelay = KIT_U32_MAX;
- opq->fadeOut = false;
- opq->playing = false;
- }
- return pauseThread != nullptr;
- }
- //not inlined
- //(also, stream_len is in total samples, not sample frames)
- static void apply_fade(_AudioDeviceOpaque* opq,
- f32* stream, u32 stream_len)
- {
- f32 fadeDelta = opq->fadeDelta;
- f32 fadeVolume = opq->fadeVolume;
- u32 fadeDelay = opq->fadeDelay;
- u8 numChannels = opq->info_p->numChannels;
- u32 smp = 0; //this index is shared, as the loops can jump to others at will
- //FADING OUT
- if(opq->fadeOut){
- _FadeOut:;
- for(; smp<stream_len; ++smp){
- //if audio device starts fading in mid-fadeout, jump to the fade-in loop
- if(!opq->fadeOut) goto _FadeIn;
- stream[smp] *= fadeVolume;
- //only change fadeVolume every numChannels samples,
- //so that there's 1 fadeVolume state per sample frame
- if(!((smp+1)%numChannels)) fadeVolume -= fadeDelta;
- //to enforce a minimum volume
- if(fadeVolume < 0.0f) fadeVolume = 0.0f;
- }
- //trigger pause thread if fade out is complete
- if(fadeVolume == 0.0f)
- startPauseThread(opq);
- //FADING IN
- } else if(fadeVolume < 1.0f){
- //let stream warm up before fading in (if fadeDelay != 0)
- for(; (fadeDelay)&&(smp<stream_len); ++smp){
- stream[smp] = 0.0f;
- if(!((smp+1)%numChannels)) --fadeDelay;
- }
- _FadeIn:;
- for(; smp<stream_len; ++smp){
- if(opq->fadeOut) goto _FadeOut;
- stream[smp] *= fadeVolume;
- if(!((smp+1)%numChannels)) fadeVolume += fadeDelta;
- if(fadeVolume > 1.0f) fadeVolume = 1.0f;
- }
- }
- //update relevant data in opq
- opq->fadeVolume = fadeVolume;
- opq->fadeDelay = fadeDelay;
- }
- //for multiplying the by inverse of stuff
- #define INV_S8 0.0078125000000000000000000000000f // = 1.0f/0x80
- #define INV_S16 0.0000305175781250000000000000000f // = 1.0f/0x8000
- #define INV_S32 0.0000000004656612873077392578125f // = 1.0f/0x80000000
- #define fmtconvloop for(u32 i=0; i<length; ++i) to
- //used for output devices
- //(length should be in samples, not sample frames)
- static inline void fmt_to_f32(Mono_smp from, f32* to,
- u32 length, u16 from_fmt)
- {
- switch(from_fmt){
- case SMPFMT_U8 : fmtconvloop[i] = ((f32)from.u8_ [i]-0x80 )*INV_S8 ; break;
- case SMPFMT_S8 : fmtconvloop[i] = ((f32)from.s8_ [i] )*INV_S8 ; break;
- case SMPFMT_U16: fmtconvloop[i] = ((f32)from.u16_[i]-0x8000)*INV_S16; break;
- case SMPFMT_S16: fmtconvloop[i] = ((f32)from.s16_[i] )*INV_S16; break;
- case SMPFMT_S32: fmtconvloop[i] = ((f32)from.s32_[i] )*INV_S32; break;
- case SMPFMT_F32: memory::copy(to, from.f32_, length*sizeof(f32)); break;
- //(f32 samples aren't hard-clipped! make sure to keep them -1.0f -> 1.0f)
- }
- }
- //used for input devices
- //(again, length should be in samples, not sample frames)
- static inline void f32_to_fmt(f32* from, Mono_smp to,
- u32 length, u16 to_fmt)
- {
- //(input f32 samples aren't hard-clipped here either!)
- switch(to_fmt){
- case SMPFMT_U8 : fmtconvloop.u8_ [i] = (u8 )(from[i]*0x7F + 0x80 ); break;
- case SMPFMT_S8 : fmtconvloop.s8_ [i] = (s8 )(from[i]*0x7F ); break;
- case SMPFMT_U16: fmtconvloop.u16_[i] = (u16)(from[i]*0x7FFF + 0x8000); break;
- case SMPFMT_S16: fmtconvloop.s16_[i] = (s16)(from[i]*0x7FFF ); break;
- case SMPFMT_S32: fmtconvloop.s32_[i] = (s32)(from[i]*0x7FFFFFFF ); break;
- case SMPFMT_F32: memory::copy(to.f32_, from, length*sizeof(f32));
- }
- }
- void _AudioCallbackWrapper(void* userdata, u8* _stream, int len){
- u64 timeStartTicks = SDL_GetPerformanceCounter();
- u64 timeStartMS = SDL_GetTicks64();
- AudioDevice* device = (AudioDevice*)userdata;
- f32* stream = (f32*)_stream;
- u32 stream_size = len;
- u32 stream_len = len / sizeof(f32); //samples, not sample frames
- _AudioDeviceOpaque* opq = (_AudioDeviceOpaque*)KIT_GET_CLASS_OPAQUE(device);
- AudioDeviceInfo* info_p = opq->info_p;
- info_p->timeStartTicks = timeStartTicks;
- info_p->timeStartMS = timeStartMS;
- //if pause thread failed to start,
- //simply write zeros to stream (if !isInput) and exit
- if(opq->fadeDelay == KIT_U32_MAX){
- if(!info_p->isInput) memory::set(stream, 0, stream_size);
- return;
- }
- s32 callbackReturn = -1; //'abort playback' by default
- if(!info_p->isInput){ //buffer will be written to
- //(memset 0 the _user_ buffer, not the sdl stream)
- if(info_p->zeroBuffer) memory::set(opq->buffer, 0, opq->buffer_size);
- try { //attempt to call user callback
- if(info_p->callback)
- callbackReturn = info_p->callback(opq->buffer, info_p);
- } catch(const char* errortext){
- kit_LogError("IN AUDIO CALLBACK: \"%s\"", errortext);
- freeThreadErrors();
- //set back to 0, since used callback failed
- memory::set(stream, 0, stream_size);
- }
- //copy buffer to stream (X to f32)
- fmt_to_f32(opq->buffer, stream, stream_len, info_p->sampleFormat);
- //apply fade to output stream
- apply_fade(opq, stream, stream_len);
- } else { //buffer will be read from
- //apply fade to input stream
- apply_fade(opq, stream, stream_len);
- //copy stream to buffer (f32 to X)
- f32_to_fmt(stream, opq->buffer, stream_len, info_p->sampleFormat);
- try { //attempt to call user callback
- if(info_p->callback)
- callbackReturn = info_p->callback(opq->buffer, info_p);
- } catch(const char* errortext){
- kit_LogError("IN AUDIO CALLBACK: \"%s\"", errortext);
- freeThreadErrors();
- }
- }
- opq->fadeOut = callbackReturn != 0;
- if(callbackReturn < 0) startPauseThread(opq);
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_AudioData.cpp":
- #include "_kit_common.hpp"
- #define ADATA_IS_INVALID (!_valid && !_constructing)
- namespace kit {
- //
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_AudioData_LoadVorbis.cpp":
- #include "_kit_common.hpp"
- #include "../stb_vorbis/stb_vorbis.hpp"
- namespace kit {
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_AudioDevice.cpp":
- #include "_kit_common.hpp"
- #define INDEX_FUNC "AudioDevice::AudioDevice(index)"
- #define NAME_FUNC "AudioDevice::AudioDevice(name)"
- #define DEVICE_IS_INVALID (!_valid && !_constructing)
- //i don't think i ended up using these lol
- #define CONSTRUCTOR_ERR(_txt) PUSH_ERRORF("%s: " _txt , funcname)
- #define CONSTRUCTOR_ERRSDL PUSH_ERRORF("%s: \"%s\"", funcname, SDL_GetError())
- #define DEV_PTR ((_AudioDeviceOpaque*)_opq)
- namespace kit {
- //in "kit_AudioCallbackWrapper.cpp"
- void _AudioCallbackWrapper(void* userdata, u8* _stream, int len);
- static inline u32 _count_bits(u32 num){
- u32 count = 0;
- while(num > 0){
- if(num&1) ++count;
- num >>= 1;
- }
- return count;
- }
- //not inlined
- bool _validate_smpfmt(u16 sampleFormat, bool allow0 = false){
- switch(sampleFormat){
- case SMPFMT_U8 :
- case SMPFMT_S8 :
- case SMPFMT_U16LSB:
- case SMPFMT_S16LSB:
- //case SMPFMT_U16MSB:
- //case SMPFMT_S16MSB:
- case SMPFMT_S32LSB:
- //case SMPFMT_S32MSB:
- case SMPFMT_F32LSB:
- //case SMPFMT_F32MSB:
- break;
- case 0:
- if(allow0) break;
- else SDL_FALLTHROUGH;
- default:
- return false;
- }
- return true;
- }
- static inline void validate_desired(const AudioDeviceInfo* desired,
- const char* funcname)
- {
- if(desired->sampleRate > KIT_S32_MAX)
- THROW_ERRORF("%s: desired->sampleRate > KIT_S32_MAX", funcname);
- if(_count_bits(desired->sampleFrames) > 1)
- THROW_ERRORF("%s: desired->sampleFrames is not a power of 2", funcname);
- if(!_validate_smpfmt(desired->sampleFormat, true)){
- THROW_ERRORF("%s: desired->sampleFormat of 0x%04X is invalid",
- funcname, desired->sampleFormat);
- }
- if(desired->numChannels > 8)
- THROW_ERRORF("%s: desired->numChannels > 8", funcname);
- if(desired->callback == nullptr)
- THROW_ERRORF("%s: desired->callback = nullptr", funcname);
- }
- static inline s32 get_dev_params(SDL_AudioSpec& want,
- const AudioDeviceInfo* desired,
- AudioDevice* device)
- {
- want.freq = (s32)((desired->sampleRate) ? desired->sampleRate : 48000);
- want.format = AUDIO_F32LSB;
- want.channels = (desired->numChannels) ? desired->numChannels : 2;
- want.samples = (desired->sampleFrames) ? desired->sampleFrames : 4096;
- want.callback = _AudioCallbackWrapper;
- want.userdata = device;
- s32 allowed_changes = 0;
- if(!desired->sampleRate ) allowed_changes |= SDL_AUDIO_ALLOW_FREQUENCY_CHANGE;
- //underlying stream must be f32, so this one should never be set
- //if(!desired->sampleFormat) allowed_changes |= SDL_AUDIO_ALLOW_FORMAT_CHANGE;
- if(!desired->numChannels ) allowed_changes |= SDL_AUDIO_ALLOW_CHANNELS_CHANGE;
- if(!desired->sampleFrames) allowed_changes |= SDL_AUDIO_ALLOW_SAMPLES_CHANGE;
- return allowed_changes;
- }
- static inline AudioDeviceInfo set_dev_info(SDL_AudioDeviceID deviceID,
- SDL_AudioSpec have,
- const AudioDeviceInfo* desired)
- {
- AudioDeviceInfo info;
- //info.timeStartTicks = 0;
- //info.timeStartMS = 0;
- info.deviceID = deviceID;
- info.sampleRate = have.freq;
- info.sampleFrames = have.samples;
- info.sampleFormat = have.format;
- info.sampleFrameSize = KIT_AUDIO_BYTESIZE(have.format)*have.channels;
- info.numChannels = have.channels;
- info.isInput = desired->isInput;
- info.zeroBuffer = desired->zeroBuffer;
- info.callback = desired->callback;
- info.userdata = desired->userdata;
- return info;
- }
- AudioDevice::AudioDevice(s32 index, //-1 to use default device
- const AudioDeviceInfo* desired,
- bool disableFadeDelay)
- {
- //check if desired is nullptr, since ->isInput is necessary to get device name
- if(desired == nullptr) THROW_ERROR(INDEX_FUNC ": desired = nullptr");
- const char* name = nullptr;
- if(index >= 0){
- name = SDL_GetAudioDeviceName(index, desired->isInput);
- if(name == nullptr)
- THROW_ERRORF(INDEX_FUNC ": \"%s\"", SDL_GetError());
- }
- _construct(name, desired, disableFadeDelay);
- }
- void AudioDevice::_construct(const char* name,
- const AudioDeviceInfo* desired,
- bool disableFadeDelay,
- bool indexed)
- {
- _type = KIT_CLASSTYPE_AUDIODEVICE;
- const char* funcname = (indexed) ? INDEX_FUNC : NAME_FUNC;
- if(desired == nullptr) THROW_ERRORF("%s: desired = nullptr", funcname);
- AudioDeviceInfo* info_p = (AudioDeviceInfo*)&info; //info is normally const
- //a few domain checks
- validate_desired(desired, funcname);
- SDL_AudioSpec want, have;
- s32 allowed_changes = get_dev_params(want, desired, this);
- //actually create the device
- SDL_AudioDeviceID deviceID = SDL_OpenAudioDevice(name, desired->isInput,
- &want, &have, allowed_changes);
- if(!deviceID) THROW_ERRORF("%s: \"%s\"", funcname, SDL_GetError());
- if(desired->sampleFormat) have.format = desired->sampleFormat;
- *info_p = set_dev_info(deviceID, have, desired);
- _opq = memory::alloc(sizeof(_AudioDeviceOpaque));
- if(_opq == nullptr){
- SDL_CloseAudioDevice(info.deviceID);
- THROW_ERRORF("%s: failed to allocate memory for opaque struct", funcname);
- }
- memory::set(_opq, 0, sizeof(_AudioDeviceOpaque));
- DEV_PTR->info_p = info_p;
- DEV_PTR->buffer_size = info.sampleFrames * info.sampleFrameSize;
- DEV_PTR->buffer = memory::allocSIMD(DEV_PTR->buffer_size);
- DEV_PTR->fadeDelta = 1.0f / ((f32)info.sampleRate*FADEDELTA_SEC);
- //DEV_PTR->fadeVolume = 0.0f; //(the prior memset makes setting to 0 redundant)
- //DEV_PTR->fadeDelay = 0;
- //DEV_PTR->fadeOut = false;
- DEV_PTR->noFadeDelay = disableFadeDelay;
- //DEV_PTR->playing = false;
- if(DEV_PTR->buffer == nullptr){
- memory::free(&_opq);
- SDL_CloseAudioDevice(info.deviceID);
- THROW_ERRORF("%s: failed to allocate memory for audio buffer", funcname);
- }
- memory::set(DEV_PTR->buffer, 0, DEV_PTR->buffer_size);
- _valid = true;
- _constructing = false;
- }
- AudioDevice::~AudioDevice(){
- if(!_valid) return;
- _valid = false;
- if(info.deviceID != 0) SDL_CloseAudioDevice(info.deviceID);
- if(_opq != nullptr){
- memory::freeSIMD(&DEV_PTR->buffer);
- memory::free(&_opq);
- }
- }
- bool AudioDevice::isPlaying(){
- if(DEVICE_IS_INVALID)
- THROW_ERROR("AudioDevice::isPlaying(): invalid AudioDevice");
- //return SDL_GetAudioDeviceStatus(info.deviceID) == SDL_AUDIO_PLAYING;
- return DEV_PTR->playing;
- }
- bool AudioDevice::isActive(){
- if(DEVICE_IS_INVALID)
- THROW_ERROR("AudioDevice::isActive(): invalid AudioDevice");
- return SDL_GetAudioDeviceStatus(info.deviceID) == SDL_AUDIO_PLAYING;
- //return DEV_PTR->playing;
- }
- void AudioDevice::setCallback(AudioCallback callback){
- if(DEVICE_IS_INVALID)
- THROW_ERROR("AudioDevice::setCallback(): invalid AudioDevice");
- if(callback == nullptr)
- THROW_ERROR("AudioDevice::setCallback(): callback = nullptr");
- SDL_LockAudioDevice(info.deviceID);
- ((AudioDeviceInfo*)&info)->callback = callback;
- SDL_UnlockAudioDevice(info.deviceID);
- }
- void AudioDevice::setUserdata(void* userdata){
- if(DEVICE_IS_INVALID)
- THROW_ERROR("AudioDevice::setUserdata(): invalid AudioDevice");
- SDL_LockAudioDevice(info.deviceID);
- ((AudioDeviceInfo*)&info)->userdata = userdata;
- SDL_UnlockAudioDevice(info.deviceID);
- }
- void AudioDevice::setPlayback(bool playing){
- if(DEVICE_IS_INVALID)
- THROW_ERROR("AudioDevice::setPlayback(): invalid AudioDevice");
- //this should occur when _AudioCallbackWrapper
- //fails to trigger the pause thread
- if(DEV_PTR->fadeDelay == KIT_U32_MAX){
- SDL_LockAudioDevice(info.deviceID);
- SDL_PauseAudioDevice(info.deviceID, true);
- DEV_PTR->fadeVolume = 0.0f;
- DEV_PTR->fadeDelay = 0;
- DEV_PTR->fadeOut = false;
- DEV_PTR->playing = false;
- SDL_UnlockAudioDevice(info.deviceID);
- }
- DEV_PTR->fadeOut = !playing;
- if(playing && !DEV_PTR->playing){
- //the purpose of fadeDelay is to mute for some samples
- //to give the sdl audio device some time to warm up,
- //otherwise artifacts start to occur (for me, anyway)
- if(!DEV_PTR->noFadeDelay) DEV_PTR->fadeDelay = (u32)(info.sampleRate * FADEDELAY_SEC);
- else DEV_PTR->fadeDelay = 0;
- DEV_PTR->fadeVolume = 0.0f;
- DEV_PTR->playing = true;
- SDL_PauseAudioDevice(info.deviceID, false);
- }
- }
- void AudioDevice::setPlaybackAndWait(bool playing){
- if(DEVICE_IS_INVALID)
- THROW_ERROR("AudioDevice::setPlaybackAndWait(): invalid AudioDevice");
- bool wasPlaying = DEV_PTR->playing;
- setPlayback(playing);
- if(playing && !wasPlaying && !DEV_PTR->noFadeDelay){
- time::sleep((u32)( FADETOTAL_SEC*1000 ));
- } else {
- time::sleep((u32)( FADEDELTA_SEC*1000 ));
- }
- }
- void AudioDevice::lock(bool locked){
- if(DEVICE_IS_INVALID)
- THROW_ERROR("AudioDevice::lock(): invalid AudioDevice");
- if(locked) SDL_LockAudioDevice(info.deviceID);
- else SDL_UnlockAudioDevice(info.deviceID);
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_AudioStream.cpp":
- #include "_kit_common.hpp"
- #define STREAM_IS_INVALID (!_valid && !_constructing)
- #define STRM_PTR ((SDL_AudioStream*)_opq)
- namespace kit {
- bool _validate_smpfmt(u16 sampleFormat, bool allow0 = false);
- AudioStream::AudioStream(const AudioDeviceInfo* src_p,
- const AudioDeviceInfo* dst_p)
- {
- _type = KIT_CLASSTYPE_AUDIOSTREAM;
- if(src_p == nullptr) THROW_ERROR("AudioStream::AudioStream(): src_p = nullptr");
- if(dst_p == nullptr) THROW_ERROR("AudioStream::AudioStream(): dst_p = nullptr");
- if(src_p->sampleRate > KIT_S32_MAX)
- THROW_ERROR("AudioStream::AudioStream(): src_p->sampleRate > KIT_S32_MAX");
- if(dst_p->sampleRate > KIT_S32_MAX)
- THROW_ERROR("AudioStream::AudioStream(): dst_p->sampleRate > KIT_S32_MAX");
- if(!_validate_smpfmt(src_p->sampleFormat))
- THROW_ERROR("AudioStream::AudioStream(): src_p->sampleFormat is invalid");
- if(!_validate_smpfmt(dst_p->sampleFormat))
- THROW_ERROR("AudioStream::AudioStream(): dst_p->sampleFormat is invalid");
- if(src_p->numChannels == 0 || src_p->numChannels > 8)
- THROW_ERROR("AudioStream::AudioStream(): src_p->numChannels must be 1 -> 8");
- if(dst_p->numChannels == 0 || dst_p->numChannels > 8)
- THROW_ERROR("AudioStream::AudioStream(): dst_p->numChannels must be 1 -> 8");
- //since src and dst are normally const,
- //they're punned so that they can be set
- AudioDeviceInfo* _src_p = (AudioDeviceInfo*)&src;
- AudioDeviceInfo* _dst_p = (AudioDeviceInfo*)&dst;
- *_src_p = *src_p;
- *_dst_p = *dst_p;
- _src_p->sampleFrameSize = src.numChannels * KIT_AUDIO_BYTESIZE(src.sampleFormat);
- _dst_p->sampleFrameSize = dst.numChannels * KIT_AUDIO_BYTESIZE(dst.sampleFormat);
- _opq = SDL_NewAudioStream(src.sampleFormat, src.numChannels, (s32)src.sampleRate,
- dst.sampleFormat, dst.numChannels, (s32)dst.sampleRate);
- if(_opq == nullptr)
- THROW_ERRORF("AudioStream::AudioStream(): \"%s\"", SDL_GetError());
- _valid = true;
- _constructing = false;
- }
- AudioStream::~AudioStream(){
- if(!_valid) return;
- _valid = false;
- if(_opq != nullptr){
- SDL_FreeAudioStream(STRM_PTR);
- _opq = nullptr;
- }
- }
- u32 AudioStream::getAvailableBytes(){
- if(STREAM_IS_INVALID)
- THROW_ERROR("AudioStream::getNumAvailable(): invalid AudioStream");
- s32 result = SDL_AudioStreamAvailable(STRM_PTR);
- if(result < 0)
- THROW_ERRORF("AudioStream::getNumAvailable(): \"%s\"", SDL_GetError());
- return (u32)result;
- }
- void AudioStream::flush(){
- if(STREAM_IS_INVALID)
- THROW_ERROR("AudioStream::flush(): invalid AudioStream");
- //as of 2024-08-03, the SDL2 wiki does not even tell you what this returns,
- //but most SDL calls with int returns of this type use 0 and -1 for
- //success and failure respectively. so, hopefully my hunch is right :D
- if(SDL_AudioStreamFlush(STRM_PTR) != 0)
- THROW_ERRORF("AudioStream::flush(): \"%s\"", SDL_GetError());
- }
- void AudioStream::clear(){
- if(STREAM_IS_INVALID)
- THROW_ERROR("AudioStream::clear(): invalid AudioStream");
- SDL_AudioStreamClear(STRM_PTR);
- }
- u32 AudioStream::get(void* buffer_dst, u32 buffer_size){
- if(STREAM_IS_INVALID)
- THROW_ERROR("AudioStream::get(): invalid AudioStream");
- if(buffer_dst == nullptr)
- THROW_ERROR("AudioStream::get(): buffer_dst = nullptr");
- if(buffer_size > KIT_S32_MAX)
- THROW_ERROR("AudioStream::get(): buffer_size > KIT_S32_MAX");
- s32 result = SDL_AudioStreamGet(STRM_PTR, buffer_dst, buffer_size);
- if(result < 0)
- THROW_ERRORF("AudioStream::get(): \"%s\"", SDL_GetError());
- return (u32)result;
- }
- void AudioStream::put(void* buffer_src, u32 buffer_size){
- if(STREAM_IS_INVALID)
- THROW_ERROR("AudioStream::put(): invalid AudioStream");
- if(buffer_src == nullptr)
- THROW_ERROR("AudioStream::put(): buffer_src = nullptr");
- if(buffer_size > KIT_S32_MAX)
- THROW_ERROR("AudioStream::put(): buffer_size > KIT_S32_MAX");
- if(SDL_AudioStreamPut(STRM_PTR, buffer_src, buffer_size) < 0)
- THROW_ERRORF("AudioStream::put(): \"%s\"", SDL_GetError());
- }
- }; /* namespace kit */
- /******************************************************************************/
- /******************************************************************************/
- //"ksdl2\src\kit_sdl2\kit_EventWatch.cpp":
- #include "_kit_common.hpp"
- namespace kit {
- //found in "kit_func_convertEvent.cpp"
- bool _convertEvent(SDL_Event& e_sdl, Event& e_kit);
- static int _EventWatchCallbackWrapper(void* EventWatch_ptr, SDL_Event* e_sdl){
- //ignore all poll sentinel events
- if(e_sdl->type == SDL_POLLSENTINEL) return 0;
- //if _disabled is nonzero, exit early
- if(KIT_GET_CLASS_DATA(EventWatch_ptr)&0xff) return 0;
- EventWatchCallback callback = (EventWatchCallback)KIT_GET_CLASS_OPAQUE(EventWatch_ptr);
- void* userdata = KIT_GET_CLASS_OPAQUE2(EventWatch_ptr);
- Event e_kit;
- //only invoke callback if event conversion was successful
- if(_convertEvent(*e_sdl, e_kit) && callback != nullptr){
- try {
- callback(e_kit, userdata);
- } catch(const char* errortext){
- kit_LogError("IN EVENTWATCH CALLBACK: \"%s\"", errortext);
- freeThreadErrors();
- }
- }
- return 0;
- }
- EventWatch::EventWatch(EventWatchCallback callback, void* userdata,
- EventWatchDestructorCallback onDestruction)
- {
- _type = KIT_CLASSTYPE_EVENTWATCH;
- if(callback == nullptr)
- THROW_ERROR("EventWatch::EventWatch(): callback = nullptr");
- _callback = callback;
- _userdata = userdata;
- _onDestruction = onDestruction;
- SDL_AddEventWatch(_EventWatchCallbackWrapper, this);
- _valid = true;
- _constructing = false;
- }
- EventWatch::~EventWatch(){
- if(!_valid) return;
- _valid = false;
- SDL_DelEventWatch(_EventWatchCallbackWrapper, this);
- if(_onDestruction != nullptr)
- _onDestruction(_userdata);
- }
- }; /* namespace kit */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement