From fa912b616b12eaaab785b0c26e6e28a1804dad57 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Wed, 6 May 2020 17:24:03 +0530 Subject: [PATCH] Remove SDL_sound dependency --- CMakeLists.txt | 10 ++++++---- src/aldatasource.h | 7 +++++++ src/alstream.cpp | 2 ++ src/filesystem.cpp | 2 -- src/main.cpp | 8 ++++++++ src/sdlsoundsource.cpp | 4 ++++ src/soundemitter.cpp | 17 ++++++++++++++--- src/vorbissource.cpp | 18 +++++++++++++++++- 8 files changed, 58 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 75f1810..852f2de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -380,7 +380,6 @@ endif() link_directories( ../libsigc++-2.10.3/sigc++/.libs - ../SDL_sound/.libs ../pixman-0.40.0/pixman/.libs ../physfs-3.0.2 ../ruby @@ -388,6 +387,7 @@ link_directories( ../mruby/build/x86_64-pc-linux-gnu/lib libs + #../SDL_sound/.libs ${SDL2_LIBRARY_DIRS} ) @@ -406,11 +406,12 @@ target_include_directories(${PROJECT_NAME} PRIVATE src windows ../libsigc++-2.10.3 - ../SDL_sound ../pixman-0.40.0/pixman ../physfs-3.0.2/src ../mruby/include - #.. + + + #../SDL_sound ${SDL2_INCLUDE_DIRS} #${VORBISFILE_INCLUDE_DIRS} @@ -421,11 +422,12 @@ target_include_directories(${PROJECT_NAME} PRIVATE target_link_libraries(${PROJECT_NAME} #ruby-static.a - sigc-2.0.a physfs.a SDL_sound.a pixman-1.a + sigc-2.0.a physfs.a pixman-1.a #sigc-2.0.x86.a physfs.x86.a SDL_sound.x86.a pixman-1.x86.a mruby.a + #SDL_sound.a #ruby-static.x86.a #crypt #dl diff --git a/src/aldatasource.h b/src/aldatasource.h index 6db63db..fee02e8 100644 --- a/src/aldatasource.h +++ b/src/aldatasource.h @@ -40,6 +40,11 @@ struct ALDataSource * to provided AL buffer */ virtual Status fillBuffer(AL::Buffer::ID alBuffer) = 0; + /* Read everything into the provided buffer */ + virtual int fillBufferFull(AL::Buffer::ID alBuffer) { + return 0; + }; + virtual int sampleRate() = 0; /* If the source doesn't support seeking, it will @@ -53,10 +58,12 @@ struct ALDataSource virtual bool setPitch(float value) = 0; }; +#ifndef __EMSCRIPTEN__ ALDataSource *createSDLSource(SDL_RWops &ops, const char *extension, uint32_t maxBufSize, bool looped); +#endif ALDataSource *createVorbisSource(SDL_RWops &ops, bool looped); diff --git a/src/alstream.cpp b/src/alstream.cpp index 9dcca49..9d4ddad 100644 --- a/src/alstream.cpp +++ b/src/alstream.cpp @@ -244,7 +244,9 @@ struct ALStreamOpenHandler : FileSystem::OpenHandler } } +#ifndef __EMSCRIPTEN__ source = createSDLSource(*srcOps, ext, STREAM_BUF_SIZE, looped); +#endif } catch (const Exception &e) { diff --git a/src/filesystem.cpp b/src/filesystem.cpp index af0b2ba..ce88b6c 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -31,8 +31,6 @@ #include -#include - #include #include #include diff --git a/src/main.cpp b/src/main.cpp index 917df14..59cde9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,7 +24,10 @@ #include #include #include + +#ifndef __EMSCRIPTEN__ #include +#endif #include #include @@ -272,6 +275,7 @@ int main(int argc, char *argv[]) return 0; } +#ifndef __EMSCRIPTEN__ if (Sound_Init() == 0) { showInitError(std::string("Error initializing SDL_sound: ") + Sound_GetError()); @@ -281,6 +285,7 @@ int main(int argc, char *argv[]) return 0; } +#endif SDL_Window *win; Uint32 winFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS; @@ -385,7 +390,10 @@ int main(int argc, char *argv[]) alcCloseDevice(alcDev); SDL_DestroyWindow(win); +#ifndef __EMSCRIPTEN__ Sound_Quit(); +#endif + TTF_Quit(); IMG_Quit(); SDL_Quit(); diff --git a/src/sdlsoundsource.cpp b/src/sdlsoundsource.cpp index cffdcd6..3d4e98a 100644 --- a/src/sdlsoundsource.cpp +++ b/src/sdlsoundsource.cpp @@ -22,6 +22,8 @@ #include "aldatasource.h" #include "exception.h" +#ifndef __EMSCRIPTEN__ + #include struct SDLSoundSource : ALDataSource @@ -129,3 +131,5 @@ ALDataSource *createSDLSource(SDL_RWops &ops, { return new SDLSoundSource(ops, extension, maxBufSize, looped); } + +#endif diff --git a/src/soundemitter.cpp b/src/soundemitter.cpp index fb6795a..828ac04 100644 --- a/src/soundemitter.cpp +++ b/src/soundemitter.cpp @@ -28,10 +28,11 @@ #include "util.h" #include "debugwriter.h" +#ifndef __EMSCRIPTEN__ #include - -#ifdef __EMSCRIPTEN__ +#else #include "emscripten.hpp" +#include "aldatasource.h" #endif #define SE_CACHE_MEM (10*1024*1024) // 10 MB @@ -198,6 +199,11 @@ struct SoundOpenHandler : FileSystem::OpenHandler bool tryRead(SDL_RWops &ops, const char *ext, const char *fullPath) { +#ifdef __EMSCRIPTEN__ + ALDataSource *source = createVorbisSource(ops, false); + buffer = new SoundBuffer; + buffer->bytes = source->fillBufferFull(buffer->alBuffer); +#else Sound_Sample *sample = Sound_NewSample(&ops, ext, 0, STREAM_BUF_SIZE); if (!sample) @@ -221,7 +227,7 @@ struct SoundOpenHandler : FileSystem::OpenHandler buffer->bytes, sample->actual.rate); Sound_FreeSample(sample); - +#endif return true; } }; @@ -253,8 +259,13 @@ SoundBuffer *SoundEmitter::allocateBuffer(const std::string &filename) if (!buffer) { char buf[512]; +#ifdef __EMSCRIPTEN__ + snprintf(buf, sizeof(buf), "Unable to decode with vorbisfile: %s", + filename.c_str()); +#else snprintf(buf, sizeof(buf), "Unable to decode sound: %s: %s", filename.c_str(), Sound_GetError()); +#endif Debug() << buf; return 0; diff --git a/src/vorbissource.cpp b/src/vorbissource.cpp index 7716a69..3e3a432 100644 --- a/src/vorbissource.cpp +++ b/src/vorbissource.cpp @@ -78,6 +78,9 @@ struct VorbisSource : ALDataSource std::vector sampleBuf; + int bufUsed = 0; + bool readFull = false; + VorbisSource(SDL_RWops &ops, bool looped) : src(ops), @@ -178,7 +181,7 @@ struct VorbisSource : ALDataSource { void *bufPtr = sampleBuf.data(); int availBuf = sampleBuf.size(); - int bufUsed = 0; + bufUsed = 0; int canRead = availBuf; @@ -259,6 +262,12 @@ struct VorbisSource : ALDataSource } canRead -= res; + + /* Double size if we want to read everything */ + if (readFull && !(canRead > 16)) { + canRead += sampleBuf.size(); + sampleBuf.resize(sampleBuf.size() * 2); + } } if (retStatus != ALDataSource::Error) @@ -268,6 +277,13 @@ struct VorbisSource : ALDataSource return retStatus; } + int fillBufferFull(AL::Buffer::ID alBuffer) { + readFull = true; + fillBuffer(alBuffer); + readFull = false; + return bufUsed*sizeof(int16_t); + } + uint32_t loopStartFrames() { if (loop.valid)