make macto compile on mac.make webto compile for web.make run-webto start a web server on port 8000 to visit the program in your browser.make run-macto run on mac.make cleanto wipe the slate.
-
-
Save whistlegraph/1c9083fefc4f9188d5c4 to your computer and use it in GitHub Desktop.
SDL2 Emscripten Audio Test (for iOS)
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 <stdio.h> | |
| #include <math.h> | |
| #include <SDL.h> | |
| #ifdef EMSCRIPTEN | |
| #include <emscripten/emscripten.h> | |
| #endif | |
| #define GAME_SIZE 256 | |
| SDL_Window *window; | |
| SDL_Renderer *renderer; | |
| SDL_Texture *game_view; | |
| SDL_AudioDeviceID dev; | |
| SDL_AudioSpec want, have; | |
| int quit = 0; | |
| /* Generate some audio. */ | |
| #define FREQ 200 | |
| unsigned int audio_position; | |
| int audio_len; | |
| float audio_frequency; | |
| float audio_volume; | |
| void audioCallback(void* userdata, Uint8* stream, int len) { | |
| len /= 2; | |
| int i; | |
| Sint16* buf = (Sint16*)stream; | |
| for(i = 0; i < len; i++) { | |
| buf[i] = audio_volume * sin(2 * M_PI * audio_position * audio_frequency); | |
| audio_position++; | |
| } | |
| audio_len -= len; | |
| return; | |
| } | |
| void loop_game(){ | |
| SDL_Event event; | |
| while(SDL_PollEvent(&event)){ | |
| switch(event.type){ | |
| case SDL_QUIT: | |
| quit = 1; | |
| break; | |
| case SDL_FINGERDOWN: | |
| case SDL_MOUSEBUTTONDOWN: | |
| SDL_Init(SDL_INIT_AUDIO); | |
| SDL_zero(want); | |
| want.freq = 44100; | |
| want.format = AUDIO_S16; | |
| want.channels = 1; | |
| want.samples = 4096; | |
| want.callback = audioCallback; | |
| audio_len = have.freq * 5; | |
| audio_position = 0; | |
| audio_frequency = 1.0 * FREQ / have.freq; | |
| audio_volume = 6000; | |
| dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); | |
| SDL_PauseAudioDevice(dev, 0); | |
| break; | |
| } | |
| } | |
| SDL_SetRenderTarget(renderer, game_view); | |
| SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); | |
| SDL_RenderClear(renderer); | |
| SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE/2); | |
| SDL_RenderDrawLine(renderer, 0, 0, GAME_SIZE, GAME_SIZE); | |
| SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE/2); | |
| SDL_RenderDrawLine(renderer, GAME_SIZE, 0, 0, GAME_SIZE); | |
| SDL_SetRenderTarget(renderer, NULL); | |
| SDL_RenderClear(renderer); | |
| SDL_RenderCopy(renderer, game_view, NULL, NULL); | |
| SDL_RenderPresent(renderer); | |
| } | |
| int main(int argc, const char * argv[]) { | |
| SDL_Init(SDL_INIT_VIDEO); | |
| window = SDL_CreateWindow( | |
| "iOS SDL2 Audio Test", | |
| SDL_WINDOWPOS_UNDEFINED, | |
| SDL_WINDOWPOS_UNDEFINED, | |
| GAME_SIZE, | |
| GAME_SIZE, | |
| SDL_WINDOW_SHOWN); | |
| renderer = SDL_CreateRenderer(window, | |
| -1, | |
| SDL_RENDERER_ACCELERATED | | |
| SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC); | |
| SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); | |
| game_view = SDL_CreateTexture(renderer, | |
| SDL_PIXELFORMAT_RGBA8888, | |
| SDL_TEXTUREACCESS_TARGET, | |
| GAME_SIZE, | |
| GAME_SIZE); | |
| #ifdef EMSCRIPTEN | |
| emscripten_set_main_loop((em_callback_func)loop_game, 0, 1); | |
| #else | |
| while(!quit){ | |
| loop_game(); | |
| } | |
| #endif | |
| SDL_DestroyTexture(game_view); | |
| SDL_DestroyRenderer(renderer); | |
| SDL_DestroyWindow(window); | |
| SDL_Quit(); | |
| return 0; | |
| } |
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
| OBJS = ios-audio.c | |
| OBJ_NAME = "ios-audio" | |
| mac: | |
| mkdir -p builds/native/mac | |
| gcc -Wall -o $(OBJ_NAME) $(OBJS) `sdl2-config --cflags --libs`; | |
| mv $(OBJ_NAME) builds/native/mac | |
| web: | |
| mkdir -p builds/html | |
| emcc $(OBJS) -O2 --memory-init-file 1 -s USE_SDL=2 -o builds/html/index.html --emrun | |
| clean: | |
| rm -rf builds | |
| run-mac: | |
| builds/native/mac/$(OBJ_NAME) | |
| run-web: | |
| emrun --no_browser --port 8000 builds/html/index.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment