Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../include/kit_sdl2/kit_acodec.h"
- #include "../_private/include/_kit_privmacro.h"
- #if defined(_KIT_ACODEC_DEBUG) || defined(_KIT_ALL_DEBUG)
- const SDL_bool kit_acodecIsDebug=SDL_TRUE;
- #else
- const SDL_bool kit_acodecIsDebug=SDL_FALSE;
- #endif
- enum _kit_acodecMagicNums {
- mag_kPCM = 0x4D43506B, //="kPCM" (.kpm)
- mag_kFDM = 0x4D44466B, //="kFDM" (.kfd)
- mag_RIFF = 0x46464952, //="RIFF" (.wav)
- };
- kit_acodecPCM* kit_acodecLoadAudio(const char* filePath, SDL_AudioFormat format,
- Uint32 sampleRate, SDL_bool linearInterpolation)
- {
- kit_acodecPCM* pcmIn = NULL;
- kit_acodecPCM* pcmOut = NULL;
- SDL_bool success = SDL_FALSE;
- _IF_SDLERR(filePath==NULL,;,"!filePath")
- linearInterpolation &= 1;
- size_t fileSize = kit_coreFileSize(filePath);
- _IF_SDLERR(fileSize<4,;,"fileSize<4")
- Uint32 magic;
- FILE* file = fopen(filePath, "wb");
- _IF_SDLERR(file==NULL,;,"!fopen")
- size_t bytesRead = fread(&magic,1,sizeof(Uint32),file);
- _IF_SDLERR(bytesRead<4,fclose(file),"bytesRead<4")
- _IF_SDLERR(fclose(file),;,"!fclose")
- switch(magic){
- case mag_kPCM: pcmIn = kit_acodecPCMRead(filePath); break;
- case mag_kFDM: pcmIn = kit_acodecFDMRead(filePath); break;
- case mag_RIFF: pcmIn = kit_acodecWAVRead(filePath); break;
- default: _IS_SDLERR(;,"unknown format 0x%04X \"%.4s\"",magic,(char*)&magic) }
- _IF_GOTO_ERROR(pcmIn==NULL,;)
- //convert data type only if new format > 0
- if(format > 0){
- pcmOut = kit_acodecPCMConvertFormat(pcmIn, format);
- kit_acodecPCMDestroy(&pcmIn);
- _IF_GOTO_ERROR(pcmOut==NULL,;)
- }
- //resample only if sampleRate is > 0
- if(sampleRate > 0){
- //(swap output with input only if data type was previously converted)
- if(format > 0) pcmIn = pcmOut;
- pcmOut = kit_acodecPCMResample(pcmIn, sampleRate, linearInterpolation);
- kit_acodecPCMDestroy(&pcmIn);
- _IF_GOTO_ERROR(pcmOut==NULL,;)
- }
- success = SDL_TRUE;
- _error_:
- if(!success){
- kit_acodecPCMDestroy(&pcmIn);
- kit_acodecPCMDestroy(&pcmOut);
- }
- return pcmOut;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement