Advertisement
Kitomas

kit_sdl2_kmixerDevice.c as of 2023-8-24

Aug 24th, 2023
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. #include "../include/kit_sdl2/kit_kmixer.h"
  2. #include "../_private/include/_kit_privmacro.h"
  3. #include "../_private/include/_kit_kmixerPrivate.h"
  4.  
  5.  
  6.  
  7.  
  8. void _kit_kmixerDeviceCallback(void* userdata, Uint8* _stream, int len){
  9.   //kit_kmixerDevice* device=userdata;
  10.   //_kit_kmixerVoiceBufferF stream={ .v=(void*)_stream };
  11. }
  12.  
  13.  
  14.  
  15.  
  16. int kit_kmixerDeviceClose(kit_kmixerDevice** device_p){
  17.  
  18.   return 0;
  19. }
  20.  
  21.  
  22. kit_kmixerDevice* kit_kmixerDeviceOpen(const char* deviceName,
  23.                                        int isCapture, int allowedChanges,
  24.                                        int freq, int stereo, Uint16 samples)
  25. {
  26.   kit_kmixerDevice* device=SDL_malloc(sizeof(kit_kmixerDevice));
  27.   _IF_GOTO_ERROR(device==NULL,;);
  28.   memset(device, 0, sizeof(kit_kmixerDevice));
  29.  
  30.   device->_magic.num=0x0076654472786D6B; //="kmxrDev\x00"
  31.  
  32.   SDL_AudioSpec specWant, specHave;
  33.   specWant.freq=freq;
  34.   specWant.format=AUDIO_F32;
  35.   specWant.channels=1+(stereo&1);
  36.   specWant.samples=samples;
  37.   specWant.callback=_kit_kmixerDeviceCallback;
  38.   specWant.userdata=device;
  39.  
  40.   allowedChanges&=!SDL_AUDIO_ALLOW_FORMAT_CHANGE; //samples are always f32 internally
  41.   device->_devID=SDL_OpenAudioDevice(deviceName,isCapture&1, &specWant,&specHave, allowedChanges);
  42.   _IF_GOTO_ERROR(!device->_devID,;);
  43.   _IF_SDLERR(specHave.channels>2,;,"specified audio device returned as neither mono nor stereo");
  44.   device->spec=specHave;
  45.  
  46.   device->_lock=SDL_CreateMutex();
  47.   _IF_GOTO_ERROR(device->_lock==NULL,;);
  48.  
  49.   device->_thread.cond=SDL_CreateCond();
  50.   _IF_GOTO_ERROR(device->_thread.cond,;);
  51.   device->_thread.queue=kit_coreVectorCreate(1,0,0,sizeof(kit_coreThread),*(Uint32*)"cTh\x00",NULL);
  52.   _IF_GOTO_ERROR(device->_thread.queue==NULL,;);
  53.   device->_thread.pool=kit_coreVectorCreate(_kit_kmixerGlobals.threadPoolSize,0,0,
  54.                                             sizeof(kit_coreThread),*(Uint32*)"cTh\x00",NULL);
  55.   _IF_GOTO_ERROR(device->_thread.pool==NULL,;);
  56.  
  57.   device->_voices.raw=kit_coreVectorCreate(1,0,0,sizeof(_kit_kmixerVoice),*(Uint32*)"kmV\x00",NULL);
  58.   _IF_GOTO_ERROR(device->_voices.raw==NULL,;);
  59.   device->_voices.ord=kit_coreVectorCreate(1,1,0,sizeof(_kit_kmixerVoice),*(Uint32*)"kmV\x00",NULL);
  60.   _IF_GOTO_ERROR(device->_voices.ord==NULL,;);
  61.  
  62.   return device;
  63.   _error_:
  64.   if(device!=NULL){
  65.     if(device->_voices.ord!=NULL) kit_coreVectorDestroy(&device->_voices.ord);
  66.     if(device->_voices.raw!=NULL) kit_coreVectorDestroy(&device->_voices.raw);
  67.  
  68.     if(device->_thread.pool!=NULL) kit_coreVectorDestroy(&device->_thread.pool);
  69.     if(device->_thread.queue!=NULL) kit_coreVectorDestroy(&device->_thread.queue);
  70.     if(device->_thread.cond!=NULL) SDL_DestroyCond(device->_thread.cond);
  71.  
  72.     if(device->_lock!=NULL) SDL_DestroyMutex(device->_lock);
  73.     if(device->_devID!=0) SDL_CloseAudioDevice(device->_devID);
  74.  
  75.     SDL_free(device);
  76.   }
  77.   return NULL;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement