Created
May 26, 2012 19:59
-
-
Save remram44/2795115 to your computer and use it in GitHub Desktop.
Very simple synthetizer with SDL_audio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <SDL/SDL.h> | |
| #include <stdio.h> | |
| #include <math.h> | |
| #define NUM_NOTES 37 | |
| float notes[NUM_NOTES] = {0}; | |
| float freqs[NUM_NOTES] = {0}; | |
| int volume = 32; | |
| void audio_callback(void *udata, Uint8 *stream, int len) | |
| { | |
| static size_t i = 0; | |
| size_t j = i; | |
| float freq = 200 + 0.001*i; | |
| for(; i < j + len; i++) | |
| { | |
| int n; | |
| stream[i-j] = 0; | |
| for(n = 0; n < NUM_NOTES; n++) | |
| { | |
| stream[i-j] += notes[n]*sin(2.0*M_PI*i * freqs[n] / 44100.0); | |
| } | |
| } | |
| } | |
| void interactive_mode(void); | |
| int main(int argc, char **argv) | |
| { | |
| SDL_AudioSpec audiospec; | |
| SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER); | |
| audiospec.freq = 44100; | |
| audiospec.format = AUDIO_S16SYS; | |
| audiospec.channels = 2; | |
| audiospec.samples = 1024; | |
| audiospec.callback = audio_callback; | |
| audiospec.userdata = NULL; | |
| if(SDL_OpenAudio(&audiospec, NULL) < 0) | |
| { | |
| fprintf(stderr, "Impossible d'ouvrir l'audio\n"); | |
| return 1; | |
| } | |
| { | |
| int n; | |
| float f = powf(2, -1.0/12.0); | |
| float p = 1046.5f; | |
| for(n = NUM_NOTES - 1; n >= 0; n--) | |
| { | |
| freqs[n] = p; | |
| p *= f; | |
| } | |
| } | |
| SDL_PauseAudio(0); | |
| SDL_SetVideoMode(200, 100, 32, 0); | |
| interactive_mode(); | |
| SDL_CloseAudio(); | |
| SDL_Quit(); | |
| return 0; | |
| } | |
| void interactive_mode(void) | |
| { | |
| int quit = 0; | |
| SDL_Event event; | |
| while(!quit) | |
| { | |
| while(SDL_PollEvent(&event)) | |
| { | |
| if(event.type == SDL_KEYDOWN) | |
| { | |
| switch(event.key.keysym.sym) | |
| { | |
| case SDLK_ESCAPE: | |
| quit = 1; | |
| break; | |
| case SDLK_a: | |
| notes[0] = volume; | |
| break; | |
| case SDLK_w: | |
| notes[1] = volume; | |
| break; | |
| case SDLK_s: | |
| notes[2] = volume; | |
| break; | |
| case SDLK_e: | |
| notes[3] = volume; | |
| break; | |
| case SDLK_d: | |
| notes[4] = volume; | |
| break; | |
| case SDLK_f: | |
| notes[5] = volume; | |
| break; | |
| case SDLK_t: | |
| notes[6] = volume; | |
| break; | |
| case SDLK_g: | |
| notes[7] = volume; | |
| break; | |
| case SDLK_y: | |
| notes[8] = volume; | |
| break; | |
| case SDLK_h: | |
| notes[9] = volume; | |
| break; | |
| case SDLK_u: | |
| notes[10] = volume; | |
| break; | |
| case SDLK_j: | |
| notes[11] = volume; | |
| break; | |
| case SDLK_k: | |
| notes[12] = volume; | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| else if(event.type == SDL_QUIT) | |
| quit = 1; | |
| } | |
| if(!quit) | |
| { | |
| int i = 0; | |
| SDL_Delay(10); | |
| for(i = 0; i < NUM_NOTES; i++) | |
| notes[i] /= 1.1; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment