SDL_mixer: Add Opus support to CMake builds

From 3251942578399ee6206ad59cf7113a702948c760 Mon Sep 17 00:00:00 2001
From: Cameron Cawley <[EMAIL REDACTED]>
Date: Mon, 9 May 2022 23:02:36 +0100
Subject: [PATCH] Add Opus support to CMake builds

---
 .github/workflows/main.yml |  1 +
 CMakeLists.txt             | 13 ++++++++++++-
 src/codecs/music_opus.c    |  5 +++++
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 5bf76da8..d7b5e3b8 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -74,6 +74,7 @@ jobs:
           -DCMAKE_VERBOSE_MAKEFILE=ON \
           -DSUPPORT_FLAC=ON \
           -DSUPPORT_OGG=ON \
+          -DSUPPORT_OPUS=ON \
           -DSUPPORT_MP3_MPG123=ON \
           -DSUPPORT_MOD_MODPLUG=ON \
           ${{ matrix.platform.cmake }}
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f433dd18..cdc86196 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,7 @@ set(FULL_VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}")
 option(SUPPORT_WAV "Support loading WAVE music" ON)
 option(SUPPORT_FLAC "Support loading FLAC music with libFLAC" OFF)
 option(SUPPORT_OGG "Support loading OGG Vorbis music via libvorbis" OFF)
+option(SUPPORT_OPUS "Support loading OGG Opus music via libopusfile" OFF)
 option(SUPPORT_MP3_MPG123 "Support loading MP3 music via MPG123" OFF)
 option(SUPPORT_MOD_MODPLUG "Support loading MOD music via modplug" OFF)
 option(SUPPORT_MID_TIMIDITY "Support loading MIDI music via TiMidity" ON)
@@ -125,7 +126,7 @@ if (SUPPORT_WAV)
     target_compile_definitions(SDL2_mixer PRIVATE -DMUSIC_WAV)
 endif()
 
-if (SUPPORT_OGG OR SUPPORT_FLAC)
+if (SUPPORT_OGG OR SUPPORT_FLAC OR SUPPORT_OPUS)
     add_subdirectory(external/ogg)
 endif()
 
@@ -143,6 +144,16 @@ if (SUPPORT_OGG)
     target_link_libraries(SDL2_mixer PRIVATE vorbisfile vorbis ogg)
 endif()
 
+if (SUPPORT_OPUS)
+    target_compile_definitions(SDL2_mixer PRIVATE -DMUSIC_OPUS -DOPUSFILE_HEADER=<opusfile.h>)
+    set(OP_DISABLE_HTTP ON CACHE BOOL "Disable HTTP support")
+    set(OP_DISABLE_EXAMPLES ON CACHE BOOL "Do not build example applications")
+    set(OP_DISABLE_DOCS ON CACHE BOOL "Do not build API documentation")
+    add_subdirectory(external/opus)
+    add_subdirectory(external/opusfile)
+    target_link_libraries(SDL2_mixer PRIVATE opusfile)
+endif()
+
 if (SUPPORT_MP3_MPG123)
     target_compile_definitions(SDL2_mixer PRIVATE -DMUSIC_MP3_MPG123)
     add_subdirectory(external/mpg123)
diff --git a/src/codecs/music_opus.c b/src/codecs/music_opus.c
index 1a79e9c8..7b57dd1b 100644
--- a/src/codecs/music_opus.c
+++ b/src/codecs/music_opus.c
@@ -28,7 +28,12 @@
 #include "music_opus.h"
 #include "utils.h"
 
+#ifdef OPUSFILE_HEADER
+#include OPUSFILE_HEADER
+#else
 #include <opus/opusfile.h>
+#endif
+
 
 typedef struct {
     int loaded;