Advertisement
Kitomas

_kit_kmixerVoicePrivate.h as of 2023-10-12

Oct 12th, 2023
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. #ifndef _KIT_KMIXERVOICEPRIVATE_H
  2. #define _KIT_KMIXERVOICEPRIVATE_H
  3.  
  4.  
  5. #include "../../include/kit_sdl2/kit_core.h"
  6. #include "_kit_kmixerPrivate.h"
  7.  
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11.  
  12.  
  13.  
  14.  
  15. typedef struct _kit_kmixerVoice _kit_kmixerVoice;
  16. struct _kit_kmixerVoice { //128B
  17.   //(lock is compared with NULL to check if voice is valid)
  18.   SDL_mutex*           lock; //to make sure a voice doesn't get deleted while it's processing
  19.   kit_coreVector*    inputs; //list of input voice indexes, if any (can be NULL)
  20.   kit_kmixerDevice*  device; //reference to the voice's device
  21.   Uint32             output; //index of output voice
  22.  
  23.   Uint32          timeStamp; //result of SDL_GetTicks() at the time of voice creation
  24.  
  25.   kit_acodecPCMSamples   bufferInput; //input buffer (aka the mixing stage's output)
  26.   kit_acodecPCMSamples    bufferUser; //buffer to be filled in or modified by the user
  27.   kit_acodecPCMSamples bufferConvert; //sometimes used for buffer conversion (always stereo size)
  28.   kit_acodecPCMSamples  bufferOutput; //final output buffer (aka a mixing stage input)
  29.  
  30.   kit_kmixerVoiceSpec  spec; //contains info for bufferUser, given by the user
  31.  
  32.   Uint32         chainStage; //the voice's current position in the processing chain
  33.   Uint32              index; //index of this specific voice in the device's voice list
  34.  
  35.   //volL can actually be <0, but it will be MAX()'d to 0 anyway
  36.    //if volR <0, volR would then be set to whatever volL is after the MAX()
  37.   float                volL; //left ear volume (or total volume if mono); 0.0 -> 1.0
  38.   float                volR; //right ear volume (ignored if mono); 0.0 -> 1.0
  39.  
  40.   SDL_bool     stereoOutput; //output is mono if SDL_FALSE, stereo if SDL_TRUE
  41. };
  42.  
  43.  
  44.  
  45. extern int _kit_kmixerVoiceProc(void* data);
  46.  
  47.  
  48. extern int _kit_kmixerVoiceMix(void* data);
  49.  
  50.  
  51. extern int _kit_kmixerVoiceRebuildOrd(kit_kmixerDevice* device);
  52.  
  53.  
  54.  
  55.  
  56. static inline int _kit_kmixerVoiceAddFillSpec(kit_kmixerDevice* device,
  57.                                               kit_kmixerVoiceSpec* vspec)
  58. {
  59.   _kit_kmixerVoice* voice0 = device->_raw->data;
  60.  
  61.  
  62.   vspec->freq    = voice0->spec.freq;
  63.   vspec->samples = voice0->spec.samples;
  64.   vspec->_size   = voice0->spec.samples<<vspec->stereo;
  65.  
  66.   switch(vspec->format){
  67.   case AUDIO_F32:                    SDL_FALLTHROUGH;
  68.   case AUDIO_S32: vspec->_size *= 2; SDL_FALLTHROUGH;
  69.   case AUDIO_S16: vspec->_size *= 2; SDL_FALLTHROUGH;
  70.   case AUDIO_U8 : break;
  71.   default: _IS_SDLERR(;,"spec's format is invalid"); }
  72.  
  73.  
  74.   /*!err*/ return  0;
  75.   _error_: return -1;
  76. }
  77.  
  78.  
  79.  
  80. static inline int _kit_kmixerVoiceAddFillVoice(kit_kmixerVoiceSpec* vspec,
  81.                                                _kit_kmixerVoice* voiceO,
  82.                                                _kit_kmixerVoice* voiceI)
  83. {
  84.   kit_coreMemset(voiceI, 0, sizeof(_kit_kmixerVoice));
  85.  
  86.   Uint32 stereo_f32_size   = sizeof(float)*vspec->samples*2;
  87.   Uint32 bufferUser_size   = vspec->_size;
  88.   Uint32 bufferOutput_size = (sizeof(float)*vspec->samples)<<voiceO->spec.stereo;
  89.  
  90.  
  91.   voiceI->lock = SDL_CreateMutex();
  92.   _IF_GOTO_ERROR(voiceI->lock==NULL,;)
  93.  
  94.   voiceI->inputs    = NULL; //created when the first input is added
  95.   //voiceI->device  = <handled inside VoiceAdd>
  96.   voiceI->output    = voiceO->index;
  97.  
  98.   voiceI->timeStamp = SDL_GetTicks();
  99.  
  100.   voiceI->bufferInput.data   = SDL_malloc(stereo_f32_size);
  101.   voiceI->bufferUser.data    = SDL_malloc(bufferUser_size);
  102.   voiceI->bufferConvert.data = SDL_malloc(stereo_f32_size);
  103.   voiceI->bufferOutput.data  = SDL_malloc(bufferOutput_size);
  104.  
  105.   voiceI->spec = *vspec;
  106.  
  107.   //voiceI->chainStage = <handled inside VoiceAddInput>
  108.   //voiceI->index      = <handled inside VoiceAdd>
  109.  
  110.   voiceI->volL = 1.0f;
  111.   voiceI->volR = (voiceI->spec.stereo) ? 1.0f : -1.0f;
  112.  
  113.   voiceI->stereoOutput = voiceO->spec.stereo;
  114.  
  115.  
  116.   /*!err*/ return  0;
  117.   _error_: return -1;
  118. }
  119.  
  120.  
  121.  
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125.  
  126. #endif /* _KIT_KMIXERVOICEPRIVATE_H */
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement