Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this program plays a sine wave at 440Hz for a second
- #define SDL_MAIN_HANDLED
- #include <kit_sdl2/kit_kmixer.h>
- //#define PI2 (basically M_PI*2, which is already defined in macroconst.h)
- #define sample_rate 44100
- #define volume 0.5
- #define hertz 440
- float position = 0;
- void sin_callback(void* userdata, void* _stream, int size, SDL_bool hasInput){
- float* destination = _stream;
- int len = size/sizeof(float);
- for(int i=0; i<len; ++i){
- destination[i] = SDL_sinf(position * PI2*hertz) * volume;
- position += 1.0f/sample_rate;
- }
- }
- int main(int argc, char** argv){
- //initialize kmixer and core, which kmixer relies on
- if(kit_coreInit(0)<0) return 1;
- if(kit_kmixerInit(-2)<0) return 2; //use only half the cpu cores present
- //set up device specification, which optionally doubles as voice 1's spec
- kit_kmixerVoiceSpec specO,spec={0};
- spec.callback = sin_callback;
- spec.freq = sample_rate;
- spec.samples = 1024;
- spec.stereo = SDL_FALSE;
- spec.format = AUDIO_F32;
- kit_kmixerDevice* device=kit_kmixerDeviceOpen(NULL,0,&spec,&specO);
- if(device==NULL) return 3;
- kit_kmixerDeviceUnpauseAndWait(device);
- SDL_Delay(1000); //let it play for 1 second
- kit_kmixerDevicePauseAndWait(device);
- kit_kmixerDeviceClose(&device);
- kit_kmixerQuit();
- kit_coreQuit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement