Advertisement
Kitomas

kmixerAsync Demo

Oct 27th, 2023
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. #define SDL_MAIN_HANDLED
  2. #include <kit_sdl2/kit_all.h>
  3.  
  4.  
  5. int main(int argc, char** argv){
  6.   //initialize kmixer and core, which kmixer relies on
  7.   if(kit_coreInit(0)<0) return 1;
  8.   if(kit_kmixerInit(-0.5f)<0) return 2; //use only half the cpu cores present
  9.  
  10.   //set up device specification, which optionally doubles as voice 1's spec
  11.   kit_kmixerVoiceSpec specO,spec={0};
  12.   spec.callback = NULL;
  13.   spec.freq     = 44100;
  14.   spec.samples  = 4096;
  15.   spec.stereo   = SDL_TRUE;
  16.   spec.format   = 0; //AUDIO_F32;
  17.   kit_kmixerDevice* device = kit_kmixerDeviceOpen(NULL,0,&spec,&specO);
  18.   if(device==NULL) return 3;
  19.  
  20.   //todo: SOLVE THE PROBLEM WITH ADDING TRACKS
  21.   Uint32 asyncID = kit_kmixerAsyncAdd(device, SDL_TRUE, SDL_TRUE, 0, 3);
  22.   if(!asyncID){ SDL_Log("error=\"%s\"",SDL_GetError()); return 5; }
  23.  
  24.  
  25.   kit_acodecPCM* bgm = kit_acodecWAVRead("dat/44100_f32_stereo_lotus.wav");
  26.   if(!bgm){ SDL_Log("bgm error=\"%s\"",SDL_GetError()); return 4; }
  27.   bgm->loopCount = 1;
  28.  
  29.   kit_acodecPCM* choir = kit_acodecWAVRead("dat/44100_f32_stereo_choir.wav");
  30.   if(!choir){ SDL_Log("choir error=\"%s\"",SDL_GetError()); return 4; }
  31.  
  32.   kit_acodecPCM* jingle = kit_acodecWAVRead("dat/44100_f32_stereo_jingle.wav");
  33.   if(!jingle){ SDL_Log("jingle error=\"%s\"",SDL_GetError()); return 4; }
  34.  
  35.   kit_acodecPCM* mboxA3 = kit_acodecWAVRead("dat/44100_f32_stereo_mboxA3.wav");
  36.   if(!mboxA3){ SDL_Log("mboxA3 error=\"%s\"",SDL_GetError()); return 4; }
  37.  
  38.  
  39.  
  40.   kit_kmixerDeviceUnpauseAndWait(device);
  41.   Uint32 trackNum;
  42.  
  43.  
  44.   SDL_Log("play bgm (with 1 loop)");
  45.   Uint32 bgmTrack = kit_kmixerAsyncPlayPVS(device, asyncID,  bgm,
  46.                                            0.0f, 0.33f,0.33f, 0.8);
  47.   SDL_Delay(2000);
  48.  
  49.   SDL_Log("pan left");
  50.   kit_kmixerAsyncPlayP(device, asyncID, choir, -0.5f);
  51.   SDL_Delay(2000);
  52.  
  53.   SDL_Log("pan right");
  54.   kit_kmixerAsyncPlayP(device, asyncID, choir, 0.5f);
  55.   SDL_Delay(2500);
  56.  
  57.   SDL_Log("speed up");
  58.   trackNum = kit_kmixerAsyncPlayV(device, asyncID,  jingle, 0.6f,0.6f);
  59.   kit_kmixerAsyncSetTrackDeltaS2(device, asyncID, trackNum, 1);
  60.   while(kit_kmixerAsyncGetTrackPlayState(device,asyncID,trackNum)) SDL_Delay(100);
  61.  
  62.   SDL_Log("slow down");
  63.   trackNum = kit_kmixerAsyncPlayV(device, asyncID,  jingle,  0.6f,0.6f);
  64.   kit_kmixerAsyncSetTrackDeltaS2(device, asyncID, trackNum, -1);
  65.   while(kit_kmixerAsyncGetTrackPlayState(device,asyncID,trackNum)) SDL_Delay(100);
  66.   SDL_Delay(1000);
  67.  
  68.   for(int i=1; i<=4; ++i){
  69.     SDL_Log("music box note at: A%i",i+2);
  70.     trackNum = kit_kmixerAsyncPlayPVS(device, asyncID, mboxA3, 0.0f, 0.7f,0.7f, (double)i);
  71.     SDL_Delay(750);
  72.     kit_kmixerAsyncStopTrack(device, asyncID, trackNum);
  73.   }
  74.  
  75.   kit_kmixerAsyncSetTrackDeltaS2(device, asyncID, bgmTrack, -2*0.8);
  76.   while(kit_kmixerAsyncGetActiveTracks(device,asyncID)) SDL_Delay(100);
  77.  
  78.  
  79.   kit_kmixerDevicePauseAndWait(device);
  80.  
  81.  
  82.   kit_acodecPCMDestroy(&mboxA3);
  83.   kit_acodecPCMDestroy(&jingle);
  84.   kit_acodecPCMDestroy(&choir);
  85.   kit_acodecPCMDestroy(&bgm);
  86.   kit_kmixerDeviceClose(&device);
  87.  
  88.   kit_kmixerQuit();
  89.   kit_coreQuit();
  90.   kit_coreLog("(If this is a nonzero value, that means there's a memory leak!)");
  91.   SDL_Quit(); //kit_coreQuit includes SDL_Quit, but the above log itself seems to use heap
  92.   kit_coreLog("# of allocations = %i",SDL_GetNumAllocations());
  93.   return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement