test/loopwave.c
changeset 10933 a9ce2bbd76e1
parent 10932 98b94b9e3e8c
child 11020 3b993dbe4cf9
equal deleted inserted replaced
10932:98b94b9e3e8c 10933:a9ce2bbd76e1
    49 }
    49 }
    50 
    50 
    51 static void
    51 static void
    52 close_audio()
    52 close_audio()
    53 {
    53 {
    54 	if (device != 0) {
    54     if (device != 0) {
    55 		SDL_CloseAudioDevice(device);
    55         SDL_CloseAudioDevice(device);
    56 		device = 0;
    56         device = 0;
    57 	}
    57     }
    58 }
    58 }
    59 
    59 
    60 static void
    60 static void
    61 open_audio()
    61 open_audio()
    62 {
    62 {
    63 	/* Initialize fillerup() variables */
    63     /* Initialize fillerup() variables */
    64 	device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0);
    64     device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0);
    65 	if (!device) {
    65     if (!device) {
    66 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
    66         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
    67 		SDL_FreeWAV(wave.sound);
    67         SDL_FreeWAV(wave.sound);
    68 		quit(2);
    68         quit(2);
    69 	}
    69     }
    70 
    70 
    71 
    71 
    72 	/* Let the audio run */
    72     /* Let the audio run */
    73 	SDL_PauseAudioDevice(device, SDL_FALSE);
    73     SDL_PauseAudioDevice(device, SDL_FALSE);
    74 }
    74 }
    75 
    75 
    76 static void reopen_audio()
    76 static void reopen_audio()
    77 {
    77 {
    78 	close_audio();
    78     close_audio();
    79 	open_audio();
    79     open_audio();
    80 }
    80 }
    81 
    81 
    82 
    82 
    83 void SDLCALL
    83 void SDLCALL
    84 fillerup(void *unused, Uint8 * stream, int len)
    84 fillerup(void *unused, Uint8 * stream, int len)
   149 
   149 
   150     /* Show the list of available drivers */
   150     /* Show the list of available drivers */
   151     SDL_Log("Available audio drivers:");
   151     SDL_Log("Available audio drivers:");
   152     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   152     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   153         SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   153         SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   154 	}
   154     }
   155 
   155 
   156 	SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   156     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   157 
   157 
   158 	open_audio();
   158     open_audio();
   159 
   159 
   160 	SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
   160     SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
   161 
   161 
   162 #ifdef __EMSCRIPTEN__
   162 #ifdef __EMSCRIPTEN__
   163     emscripten_set_main_loop(loop, 0, 1);
   163     emscripten_set_main_loop(loop, 0, 1);
   164 #else
   164 #else
   165 	while (!done) {
   165     while (!done) {
   166 		SDL_Event event;
   166         SDL_Event event;
   167 
   167 
   168 		while (SDL_PollEvent(&event) > 0) {
   168         while (SDL_PollEvent(&event) > 0) {
   169 			if (event.type == SDL_QUIT) {
   169             if (event.type == SDL_QUIT) {
   170 				done = 1;
   170                 done = 1;
   171 			}
   171             }
   172 			if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
   172             if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
   173 				(event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
   173                 (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
   174 				reopen_audio();
   174                 reopen_audio();
   175 			}
   175             }
   176 		}
   176         }
   177 		SDL_Delay(100);
   177         SDL_Delay(100);
   178 	}
   178     }
   179 #endif
   179 #endif
   180 
   180 
   181     /* Clean up on signal */
   181     /* Clean up on signal */
   182 	close_audio();
   182     close_audio();
   183     SDL_FreeWAV(wave.sound);
   183     SDL_FreeWAV(wave.sound);
   184     SDL_Quit();
   184     SDL_Quit();
   185     return (0);
   185     return (0);
   186 }
   186 }
   187 
   187