OVR: Audio Support and SDL upgrade from depricate audio handling
authorAndreas Eversberg <jolly@eversberg.eu>
Sun, 15 Apr 2018 10:15:27 +0000 (12:15 +0200)
committerAndreas Eversberg <jolly@eversberg.eu>
Sun, 22 Apr 2018 10:04:04 +0000 (12:04 +0200)
src/libsdl/sdl.c
src/libsdl/sdl.h
src/mercenary/main.c

index d7e96ce..d0eeb78 100644 (file)
@@ -30,6 +30,8 @@
 
 static int sdl_initialized = 0;
 static int audio_initialized = 0;
+static const char *device_string = NULL;
+static SDL_AudioDeviceID audio_devid = 0;
 static SDL_Window *gl_window = NULL;
 static SDL_GLContext gl_context = NULL;
 static void (*keyboard_sdl)(int down, enum keycode keycode) = NULL;
@@ -42,10 +44,10 @@ static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int
 
        SDL_memset(stream, 0, len);
        audio_sdl(audio_data, len / sizeof(float) / 2);
-       SDL_MixAudio(stream, (Uint8 *)audio_data, len, SDL_MIX_MAXVOLUME);
+       SDL_MixAudioFormat(stream, (Uint8 *)audio_data, AUDIO_F32, len, SDL_MIX_MAXVOLUME / 2.0);
 }
 
-int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), void (*resize_window)(int width, int height), int multisampling, int vbl_sync)
+int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), void (*resize_window)(int width, int height), int multisampling, int vbl_sync, int rift)
 {
        int rc;
 
@@ -133,6 +135,19 @@ int init_sdl(const char *progname, int width, int height, int sound_samplerate,
        if (multisampling)
                glEnable(GL_MULTISAMPLE);
 
+       if (rift) {
+               int i, count;
+               count = SDL_GetNumAudioDevices(0);
+               const char *string;
+               for(i = 0; i < count; i++) {
+                       string = SDL_GetAudioDeviceName(i, 0);
+                       if (strstr(string, "Rift"))
+                               device_string = string;
+               }
+               if (!device_string)
+                       print_error("Oculus Rift headset not found, falling back to default audio device.\n");
+       }
+
        /* open audio */
        SDL_AudioSpec want, have;
        SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */
@@ -141,27 +156,25 @@ int init_sdl(const char *progname, int width, int height, int sound_samplerate,
        want.channels = 2;
        want.samples = sound_chunk; /* must be a power of two */
        want.callback = audio_cb;
-       rc = SDL_OpenAudio(&want, &have);
-       if (rc < 0) {
+       audio_devid = SDL_OpenAudioDevice(device_string, 0, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
+       if (audio_devid <= 0) {
                print_error("Failed to open audio! (No speaker connected?)\n");
+               rc = -EIO;
                goto error;
        } else if (have.format != want.format) {
                print_error("Failed to open audio with desired audio format\n");
-               SDL_CloseAudio();
                rc = -EIO;
                goto error;
        } else if (have.freq != want.freq) {
                print_error("Failed to open audio with desired sample rate\n");
-               SDL_CloseAudio();
                rc = -EIO;
                goto error;
        } else if (have.channels != want.channels) {
                print_error("Failed to open audio with desired channels (got %d)\n", have.channels);
-               SDL_CloseAudio();
                rc = -EIO;
                goto error;
        } else {
-               SDL_PauseAudio(0);
+               SDL_PauseAudioDevice(audio_devid, 0);
                audio_initialized = 1;
        }
 
@@ -333,10 +346,13 @@ void exit_sdl(void)
 
        /* exit SDL library */
        if (audio_initialized) {
-               SDL_PauseAudio(1);
-               SDL_CloseAudio();
+               SDL_PauseAudioDevice(audio_devid, 1);
                audio_initialized = 0;
        }
+       if (audio_devid > 0) {
+               SDL_CloseAudioDevice(audio_devid);
+               audio_devid = 0;
+       }
        if (sdl_initialized) {
                SDL_Quit();
                sdl_initialized = 0;
index e731599..c37caa7 100644 (file)
@@ -82,7 +82,7 @@ enum keycode {
        KEYCODE_INSERT,
 };
 
-int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), void (*resize_window)(int width, int height), int multisampling, int vbl_sync);
+int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), void (*resize_window)(int width, int height), int multisampling, int vbl_sync, int rift);
 int event_sdl(void);
 void swap_sdl(void);
 void exit_sdl(void);
index edd88cc..aea09f1 100644 (file)
@@ -1004,14 +1004,16 @@ int main(int argc, char *argv[])
        /* init SDL and OpenGL */
 #ifdef HAVE_OVR
        int vbl_sync = 0;
+       int rift = 1;
        window_width = SCREEN_WIDTH;
        window_height = SCREEN_HEIGHT;
 #else
        int vbl_sync = 1;
+       int rift = 0;
        window_width = (config_debug_opengl) ? SCREEN_WIDTH / 3 * 2 : SCREEN_WIDTH;
        window_height = (config_debug_opengl) ? SCREEN_HEIGHT / 3 * 4 : SCREEN_HEIGHT;
 #endif
-       rc = init_sdl(argv[0], window_width, window_height, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, audio_sdl, resize_window, config_multisampling, vbl_sync);
+       rc = init_sdl(argv[0], window_width, window_height, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, audio_sdl, resize_window, config_multisampling, vbl_sync, rift);
        if (rc < 0)
                goto done;
 #ifdef HAVE_OVR