Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "_sfx.hpp"
- //workaround for having _sfx_callback pause the device,
- //without having to call SDL_PauseAudioDevice inside the callback itself
- int _sfx_pauseThread(void* data){
- sfx_class* sfx = (sfx_class*)data;
- if(sfx->isClosing()) return 0;
- sfx->lockDevice(true);
- //note: might cause problems if sfx is dangling for whatever reason
- try {
- sfx->lock(true);
- } catch(...){
- sfx->lockDevice(false);
- sfx->_getFadeInDelay() = 0xffffffff; //this should work, right?
- return -1;
- }
- //make sure current buffer finishes playing
- float bufferLengthSeconds = (float)sfx->getBufferLength() / sfx->getSampleRate();
- SDL_Delay(bufferLengthSeconds*1000 + 10); //+10ms just to be sure
- SDL_PauseAudioDevice(sfx->getDeviceID(),1);
- sfx->_setPlaying(false);
- sfx->lockDevice(false);
- sfx->lock(false);
- return 0;
- }
- void _sfx_callback(void* userdata, Uint8* _stream, int size){
- sfx_class* sfx = (sfx_class*)userdata;
- sfx_f32s* stream = (sfx_f32s*)_stream;
- int len = size/sizeof(sfx_f32s);
- size_t numTracks = sfx->getNumTracks();
- std::vector<sfx_track>& tracks = sfx->_getTracks();
- Uint64 timeStampEnd = sfx->getTimeStampEnd();
- sfx->_setTimeStampStart();
- SDL_memset(stream,0,size); //stream must be filled no matter what
- if(sfx->isClosing()) goto _skip_everything_; //if device is closing, exit early
- //if previous attempt to pause sfx failed, exit early
- try {
- sfx->lock(true);
- } catch(...){
- sfx->_getFadeInDelay() = 0xffffffff; //should work
- goto _skip_everything_;
- }
- if(sfx->_getFadeInDelay() == 0xffffffff) goto _unlock_device_;
- /**/
- for(size_t ti=0; ti<numTracks; ++ti){
- if(tracks[ti].pcm != nullptr){
- _sfx_mixTrack(tracks[ti],
- timeStampEnd,
- stream, len);
- }
- }
- /**/
- //apply linear fade to reduce popping when pausing and unpausing the device
- _sfx_globalFade(sfx, stream, len);
- _unlock_device_ : try { sfx->lock(false); } catch(...){} //just in case
- _skip_everything_: sfx->_setTimeStampEnd();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement