Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _KIT_KMIXERDEVICEPRIVATE_H
- #define _KIT_KMIXERDEVICEPRIVATE_H
- #include "../../include/kit_sdl2/kit_kmixer.h"
- #include "_kit_privmacro.h"
- #include "_kit_kmixerVoicePrivate.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define _DEV_MAGIC_NUM (0x0076654472786D6B) //="kmxrDev\x00"
- #define _CHECK_IF_DEVICE_IS_VALID_R(_value) \
- _IF_SDLERR_R(device->_magic.num!=_DEV_MAGIC_NUM, \
- _value,;,"device is invalid")
- //minimum amplitude needed to halt a fade in/out (0.0 -> 1.0)
- #define fadeCutoff 0.0002
- extern int _kit_kmixerDevicePauseThread(void* data);
- extern void _kit_kmixerDeviceCallback(void* userdata, Uint8* _stream, int len);
- //assumes device is already locked
- static inline void _kit_kmixerDeviceStereoFade(kit_kmixerDevice* device,
- _stereo_samples_f32* stream,
- int len)
- {
- //get values from voice 0
- _kit_kmixerVoice* voice0 = device->_voices.raw->data;
- float volL = (voice0->volL >= 0) ? voice0->volL : 1.0f;
- float volR = (voice0->volR >= 0) ? voice0->volR : volL;
- //get values from device itself
- float fadeMultiplier = device->_fadeMultiplier;
- float fadeVolume = device->_fadeVolume;
- Uint32 fadeInDelay = device->_fadeInDelay;
- Uint32 i = 0; //this index is shared, as the loops can jump to others at will
- //FADING OUT
- if(device->_fadeOut){
- _do_fade_out_stereo: //kit_coreLog("stereo fade out");
- for(; i<len; ++i){
- if(!device->_fadeOut){ fadeVolume = 1-fadeVolume; goto _do_fade_in_stereo; }
- stream[i].l *= fadeVolume*volL;
- stream[i].r *= fadeVolume*volR;
- fadeVolume *= fadeMultiplier;
- }
- if(fadeVolume<fadeCutoff){
- SDL_Thread* pauseThread = SDL_CreateThread(_kit_kmixerDevicePauseThread,"_PauseTh", device);
- //setting _fadeInDelay to -1 will make further calls to the device callback
- //to simply memset 0 until kit_kmixerDevicePlay is called again
- if(pauseThread == NULL) device->_fadeInDelay = -1; //0xffffffff
- else SDL_DetachThread(pauseThread); //make sure thread cleans up when finished
- }
- //NORMAL; JUST APPLY VOLUME
- } else if(!fadeInDelay){
- _do_normal_stereo: //kit_coreLog("stereo normal");
- for(; i<len; ++i){
- if(device->_fadeOut){ goto _do_fade_out_stereo; }
- stream[i].l *= volL;
- stream[i].r *= volR;
- }
- //FADING IN
- } else { //let device warm up before fading in
- for(; (fadeInDelay)&&(i<len); ++i){ //write 0s for fadeInDelaySeconds
- stream[i].l=stream[i].r = 0; --fadeInDelay;
- }
- _do_fade_in_stereo: //kit_coreLog("stereo fade in");
- for(; i<len; ++i){
- if(device->_fadeOut){ fadeVolume = 1-fadeVolume; goto _do_fade_out_stereo; }
- else if(fadeVolume < fadeCutoff){ fadeVolume = 1; goto _do_normal_stereo; }
- stream[i].l *= (1-fadeVolume)*volL;
- stream[i].r *= (1-fadeVolume)*volR;
- fadeVolume *= fadeMultiplier;
- }
- }
- //update device struct to any relevant new values
- device->_fadeVolume = fadeVolume; //update fade volume
- device->_fadeInDelay = fadeInDelay; //update fade in delay
- }
- //also assumes device is already locked
- static inline void _kit_kmixerDeviceMonoFade(kit_kmixerDevice* device,
- float* stream, int len)
- {
- //get values from voice 0
- _kit_kmixerVoice* voice0 = device->_voices.raw->data;
- float volL = (voice0->volL >= 0) ? voice0->volL : 1.0f;
- //float volR = (voice0->volR >= 0) ? voice0->volR : volL; (not used for mono)
- //get values from device itself
- float fadeMultiplier = device->_fadeMultiplier;
- float fadeVolume = device->_fadeVolume;
- Uint32 fadeInDelay = device->_fadeInDelay;
- Uint32 i = 0; //this index is shared, as the loops can jump to others at will
- if(device->_fadeOut){
- _do_fade_out_mono: //SDL_Log("fade out mono");
- for(; i<len; ++i){
- if(!device->_fadeOut){ fadeVolume = 1-fadeVolume; goto _do_fade_in_mono; }
- stream[i] *= fadeVolume*volL;
- fadeVolume *= fadeMultiplier;
- }
- if(fadeVolume < fadeCutoff){
- SDL_Thread* pauseThread = SDL_CreateThread(_kit_kmixerDevicePauseThread,"_PauseTh", device);
- if(pauseThread==NULL) device->_fadeInDelay = -1; //0xffffffff
- else SDL_DetachThread(pauseThread); //make sure thread cleans up when finished
- }
- } else if(!fadeInDelay){
- _do_normal_mono: //SDL_Log("normal mono");
- for(; i<len; ++i){
- if(device->_fadeOut){ goto _do_fade_out_mono; }
- stream[i] *= volL;
- }
- } else { //let device warm up before fading in
- for(; (fadeInDelay)&&(i<len); ++i){ //write 0s for fadeInDelaySeconds
- stream[i] = 0; --fadeInDelay;
- }
- _do_fade_in_mono: //SDL_Log("fade in mono");
- for(; i<len; ++i){
- if(device->_fadeOut){ fadeVolume = 1-fadeVolume; goto _do_fade_out_mono; }
- else if(fadeVolume < fadeCutoff){ fadeVolume = 1; goto _do_normal_mono; }
- stream[i] *= (1-fadeVolume)*volL;
- fadeVolume *= fadeMultiplier;
- }
- }
- //update device struct to any relevant new values
- device->_fadeVolume = fadeVolume; //update fade volume
- device->_fadeInDelay = fadeInDelay; //update fade in delay
- }
- static inline SDL_AudioSpec _kit_kmixerDeviceVoiceToAudioSpec(kit_kmixerDevice* device,
- const kit_kmixerVoiceSpec* vspec)
- {
- SDL_AudioSpec aspec;
- SDL_bool success = SDL_FALSE;
- aspec.freq = vspec->freq;
- aspec.format = AUDIO_F32;
- aspec.channels = 1+(vspec->stereo&1);
- aspec.samples = vspec->samples;
- aspec.callback = _kit_kmixerDeviceCallback;
- aspec.userdata = device;
- //assuming 44.1kHz, <=16 sample frames would be well under 1ms lol
- //(even at 8kHz, it would still only be 2ms)
- _IF_SDLERR(aspec.samples<32,;,"samples < 32")
- _IF_SDLERR(count_bits(aspec.samples)>1,;,"samples not a power of 2")
- success = SDL_TRUE;
- _error_:
- if(!success) aspec.format = 0; //used to check for failure inside DeviceOpen
- return aspec;
- }
- static inline int _kit_kmixerDeviceFillVoice0(kit_kmixerDevice* device,
- kit_kmixerVoiceSpec* vspec)
- {
- _kit_kmixerVoice* raw = device->_voices.raw->data;
- SDL_bool stereo = vspec->stereo&1;
- Uint32 bufferSize = (sizeof(float)*vspec->samples)<<stereo;
- raw->lock=SDL_CreateMutex();
- if(raw->lock==NULL) return -1; //_IF_GOTO_ERROR isn't really necessary here
- raw->inputs = NULL; //created when a voice gets added
- raw->inputRefs = NULL; //also created when a voice gets added
- raw->output = NULL; //unused for voice 0
- //(voice 0's bufferInput is copied to device callback's _stream)
- raw->bufferInput.v = SDL_malloc(bufferSize);
- raw->bufferUser.v = NULL; //unused for voice 0
- raw->bufferConvert.v = NULL; //unused for voice 0
- raw->bufferOutput.v = NULL; //unused for voice 0
- _IF_SDLERR_R(raw->bufferInput.v==NULL,-1,;,"!voice 0 bufferInput")
- //copy voice spec info
- raw->spec = *vspec;
- //(these two members are exceptions)
- raw->spec._size = bufferSize;
- raw->spec.format = AUDIO_F32;
- raw->chainStage = 0;
- raw->index = 0;
- raw->volL = 1.0f;
- raw->volR = (stereo) ? 1.0f : -1.0f;
- raw->applyVolume = SDL_TRUE;
- raw->stereoOutput = stereo;
- //copy reference of voice 0 to ord
- _kit_kmixerVoice* voice0_reference = &raw[0];
- Uint32 newIndex = kit_coreVectorAppend(&device->_voices.ord, &voice0_reference, 0,0);
- _IF_SDLERR_R(newIndex==U32_MAX,-1,;,"!append")
- return 0;
- }
- #ifdef __cplusplus
- }
- #endif
- #endif /* _KIT_KMIXERDEVICEPRIVATE_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement