#include #include #include #include #include void FOSDLAudioCallback(void* userdata, Uint8* buffer, int len); int main(int argc, char* argv[]) { int screen_width = 128; int screen_height = 64; int i; int num_audio_dev; int audio_dev_id; int quit; SDL_Window *window = NULL; SDL_Surface *screen = NULL; SDL_AudioSpec spec_desired; SDL_AudioSpec spec_received; SDL_AudioDeviceID audiodev; SDL_Event event; if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init(): %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); /* Handling audio devices */ num_audio_dev = SDL_GetNumAudioDevices(false); for (i = 0; i < num_audio_dev; ++i) { SDL_Log("Audio device %d: %s", i, SDL_GetAudioDeviceName(i, 0)); } fprintf(stdout, "Which audio device to open? "); scanf("%d", &audio_dev_id); fprintf(stdout, "\nDevice %d : %s choosen\n", audio_dev_id, SDL_GetAudioDeviceName(audio_dev_id, 0)); SDL_zero(spec_desired); spec_desired.freq = 22050; spec_desired.format = AUDIO_S16LSB; spec_desired.channels = 1; spec_desired.samples = 8192; spec_desired.callback = FOSDLAudioCallback; spec_desired.userdata = NULL; audiodev = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(audio_dev_id, 0), 0, &spec_desired, &spec_received, SDL_AUDIO_ALLOW_FORMAT_CHANGE); if(!audiodev) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenAudioDevice(): %s\n", SDL_GetError()); exit(1); } if(spec_desired.format != spec_received.format) { SDL_Log("Format requested not available\n"); } fflush(stdout); fflush(stderr); window = SDL_CreateWindow("Soundz!", 0, 0, screen_width, screen_height, SDL_WINDOW_SHOWN); if (!window) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow: %s\n", SDL_GetError()); exit(1); } SDL_PauseAudioDevice(audiodev, 0); screen = SDL_GetWindowSurface(window); quit = 0; while(!quit) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: quit = 1 ; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: quit = 1; break; } } } //surface->pixels } SDL_Log("Terminating normally.\n"); SDL_CloseAudioDevice(audiodev); return 0; } void FOSDLAudioCallback(void* userdata, Uint8* buffer, int len) { fprintf(stdout, "%d\n", len); fflush(stdout); for (size_t index = 0; index < len; index++) { buffer[index] = rand() % 256; } }