Advertisement
Kitomas

kit_sdl2_acodec.c as of 2023-9-28

Sep 29th, 2023
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include "../include/kit_sdl2/kit_acodec.h"
  2. #include "../_private/include/_kit_privmacro.h"
  3.  
  4.  
  5.  
  6.  
  7. #if defined(_KIT_ACODEC_DEBUG) || defined(_KIT_ALL_DEBUG)
  8. const SDL_bool kit_acodecIsDebug=SDL_TRUE;
  9. #else
  10. const SDL_bool kit_acodecIsDebug=SDL_FALSE;
  11. #endif
  12.  
  13.  
  14.  
  15.  
  16. enum _kit_acodecMagicNums {
  17.   mag_kPCM = 0x4D43506B, //="kPCM" (.kpm)
  18.   mag_kFDM = 0x4D44466B, //="kFDM" (.kfd)
  19.   mag_RIFF = 0x46464952, //="RIFF" (.wav)
  20. };
  21.  
  22.  
  23.  
  24.  
  25. kit_acodecPCM* kit_acodecLoadAudio(const char* filePath, SDL_AudioFormat format,
  26.                                    Uint32 sampleRate, SDL_bool linearInterpolation)
  27. {
  28.   kit_acodecPCM* pcmIn = NULL;
  29.   kit_acodecPCM* pcmOut = NULL;
  30.   SDL_bool success = SDL_FALSE;
  31.   _IF_SDLERR(filePath==NULL,;,"!filePath")
  32.   linearInterpolation &= 1;
  33.  
  34.  
  35.   size_t fileSize = kit_coreFileSize(filePath);
  36.   _IF_SDLERR(fileSize<4,;,"fileSize<4")
  37.  
  38.   Uint32 magic;
  39.   FILE* file = fopen(filePath, "wb");
  40.   _IF_SDLERR(file==NULL,;,"!fopen")
  41.   size_t bytesRead = fread(&magic,1,sizeof(Uint32),file);
  42.   _IF_SDLERR(bytesRead<4,fclose(file),"bytesRead<4")
  43.   _IF_SDLERR(fclose(file),;,"!fclose")
  44.  
  45.  
  46.   switch(magic){
  47.   case mag_kPCM: pcmIn = kit_acodecPCMRead(filePath); break;
  48.   case mag_kFDM: pcmIn = kit_acodecFDMRead(filePath); break;
  49.   case mag_RIFF: pcmIn = kit_acodecWAVRead(filePath); break;
  50.   default: _IS_SDLERR(;,"unknown format 0x%04X \"%.4s\"",magic,(char*)&magic) }
  51.   _IF_GOTO_ERROR(pcmIn==NULL,;)
  52.  
  53.  
  54.   //convert data type only if new format > 0
  55.   if(format > 0){
  56.     pcmOut = kit_acodecPCMConvertFormat(pcmIn, format);
  57.     kit_acodecPCMDestroy(&pcmIn);
  58.     _IF_GOTO_ERROR(pcmOut==NULL,;)
  59.   }
  60.  
  61.  
  62.   //resample only if sampleRate is > 0
  63.   if(sampleRate > 0){
  64.     //(swap output with input only if data type was previously converted)
  65.     if(format > 0) pcmIn = pcmOut;
  66.     pcmOut = kit_acodecPCMResample(pcmIn, sampleRate, linearInterpolation);
  67.     kit_acodecPCMDestroy(&pcmIn);
  68.     _IF_GOTO_ERROR(pcmOut==NULL,;)
  69.   }
  70.  
  71.  
  72.   success = SDL_TRUE;
  73.   _error_:
  74.   if(!success){
  75.     kit_acodecPCMDestroy(&pcmIn);
  76.     kit_acodecPCMDestroy(&pcmOut);
  77.   }
  78.   return pcmOut;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement