Remove SDL_sound dependency
This commit is contained in:
		
							parent
							
								
									bf1602e49f
								
							
						
					
					
						commit
						fa912b616b
					
				
					 8 changed files with 58 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -244,7 +244,9 @@ struct ALStreamOpenHandler : FileSystem::OpenHandler
 | 
			
		|||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
#ifndef __EMSCRIPTEN__
 | 
			
		||||
			source = createSDLSource(*srcOps, ext, STREAM_BUF_SIZE, looped);
 | 
			
		||||
#endif
 | 
			
		||||
		}
 | 
			
		||||
		catch (const Exception &e)
 | 
			
		||||
		{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -31,8 +31,6 @@
 | 
			
		|||
 | 
			
		||||
#include <physfs.h>
 | 
			
		||||
 | 
			
		||||
#include <SDL_sound.h>
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,7 +24,10 @@
 | 
			
		|||
#include <SDL.h>
 | 
			
		||||
#include <SDL_image.h>
 | 
			
		||||
#include <SDL_ttf.h>
 | 
			
		||||
 | 
			
		||||
#ifndef __EMSCRIPTEN__
 | 
			
		||||
#include <SDL_sound.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,6 +22,8 @@
 | 
			
		|||
#include "aldatasource.h"
 | 
			
		||||
#include "exception.h"
 | 
			
		||||
 | 
			
		||||
#ifndef __EMSCRIPTEN__
 | 
			
		||||
 | 
			
		||||
#include <SDL_sound.h>
 | 
			
		||||
 | 
			
		||||
struct SDLSoundSource : ALDataSource
 | 
			
		||||
| 
						 | 
				
			
			@ -129,3 +131,5 @@ ALDataSource *createSDLSource(SDL_RWops &ops,
 | 
			
		|||
{
 | 
			
		||||
	return new SDLSoundSource(ops, extension, maxBufSize, looped);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,10 +28,11 @@
 | 
			
		|||
#include "util.h"
 | 
			
		||||
#include "debugwriter.h"
 | 
			
		||||
 | 
			
		||||
#ifndef __EMSCRIPTEN__
 | 
			
		||||
#include <SDL_sound.h>
 | 
			
		||||
 | 
			
		||||
#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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -78,6 +78,9 @@ struct VorbisSource : ALDataSource
 | 
			
		|||
 | 
			
		||||
	std::vector<int16_t> 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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue