Advertisement
Kitomas

kmixer_demo.c

Oct 12th, 2023
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. //this program plays a sine wave at 440Hz for a second
  2. #define SDL_MAIN_HANDLED
  3. #include <kit_sdl2/kit_kmixer.h>
  4.  
  5. //#define PI2 (basically M_PI*2, which is already defined in macroconst.h)
  6.  
  7. #define sample_rate 44100
  8. #define volume 0.5
  9. #define hertz 440
  10.  
  11. float position = 0;
  12. void sin_callback(void* userdata, void* _stream, int size, SDL_bool hasInput){
  13.   float* destination = _stream;
  14.   int len = size/sizeof(float);
  15.  
  16.   for(int i=0; i<len; ++i){
  17.     destination[i] = SDL_sinf(position * PI2*hertz) * volume;
  18.     position += 1.0f/sample_rate;
  19.   }
  20. }
  21.  
  22. int main(int argc, char** argv){
  23.   //initialize kmixer and core, which kmixer relies on
  24.   if(kit_coreInit(0)<0) return 1;
  25.   if(kit_kmixerInit(-2)<0) return 2; //use only half the cpu cores present
  26.  
  27.   //set up device specification, which optionally doubles as voice 1's spec
  28.   kit_kmixerVoiceSpec specO,spec={0};
  29.   spec.callback = sin_callback;
  30.   spec.freq     = sample_rate;
  31.   spec.samples  = 1024;
  32.   spec.stereo   = SDL_FALSE;
  33.   spec.format   = AUDIO_F32;
  34.   kit_kmixerDevice* device=kit_kmixerDeviceOpen(NULL,0,&spec,&specO);
  35.   if(device==NULL) return 3;
  36.  
  37.  
  38.   kit_kmixerDeviceUnpauseAndWait(device);
  39.   SDL_Delay(1000); //let it play for 1 second
  40.   kit_kmixerDevicePauseAndWait(device);
  41.  
  42.  
  43.   kit_kmixerDeviceClose(&device);
  44.  
  45.   kit_kmixerQuit();
  46.   kit_coreQuit();
  47.  
  48.   return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement