From cfa60ab7e6313f85594f63d2830fdc2ce19e180e Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 15 Dec 2014 01:08:30 +0100 Subject: Added support for emscripten and more Added PLATFORM_WEB support (emscripten-webgl) [audio] Added LoadSoundFromWave() [textures] Added LoadTextureFromImage() to replace CreateTexture() Some TraceLogs edited... --- src/audio.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 9 deletions(-) (limited to 'src/audio.c') diff --git a/src/audio.c b/src/audio.c index fd482534..d6d16360 100644 --- a/src/audio.c +++ b/src/audio.c @@ -69,15 +69,6 @@ typedef struct Music { } Music; -// Wave file data -typedef struct Wave { - void *data; // Buffer data pointer - unsigned int dataSize; // Data size in bytes - unsigned int sampleRate; - short bitsPerSample; - short channels; -} Wave; - //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- @@ -219,6 +210,60 @@ Sound LoadSound(char *fileName) return sound; } +// Load sound from wave data +Sound LoadSoundFromWave(Wave wave) +{ + Sound sound; + + if (wave.data != NULL) + { + ALenum format = 0; + // The OpenAL format is worked out by looking at the number of channels and the bits per sample + if (wave.channels == 1) + { + if (wave.bitsPerSample == 8 ) format = AL_FORMAT_MONO8; + else if (wave.bitsPerSample == 16) format = AL_FORMAT_MONO16; + } + else if (wave.channels == 2) + { + if (wave.bitsPerSample == 8 ) format = AL_FORMAT_STEREO8; + else if (wave.bitsPerSample == 16) format = AL_FORMAT_STEREO16; + } + + // Create an audio source + ALuint source; + alGenSources(1, &source); // Generate pointer to audio source + + alSourcef(source, AL_PITCH, 1); + alSourcef(source, AL_GAIN, 1); + alSource3f(source, AL_POSITION, 0, 0, 0); + alSource3f(source, AL_VELOCITY, 0, 0, 0); + alSourcei(source, AL_LOOPING, AL_FALSE); + + // Convert loaded data to OpenAL buffer + //---------------------------------------- + ALuint buffer; + alGenBuffers(1, &buffer); // Generate pointer to buffer + + // Upload sound data to buffer + alBufferData(buffer, format, wave.data, wave.dataSize, wave.sampleRate); + + // Attach sound buffer to source + alSourcei(source, AL_BUFFER, buffer); + + // Unallocate WAV data + UnloadWave(wave); + + TraceLog(INFO, "[Wave] Sound file loaded successfully"); + TraceLog(INFO, "[Wave] Sample rate: %i - Channels: %i", wave.sampleRate, wave.channels); + + sound.source = source; + sound.buffer = buffer; + } + + return sound; +} + // Load sound to memory from rRES file (raylib Resource) Sound LoadSoundFromRES(const char *rresName, int resId) { -- cgit v1.2.3 From 905b6ec53df01a4f660c12c08c32e2cc301f7ad6 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 31 Dec 2014 18:03:32 +0100 Subject: Added full support for HTML5 (emscripten) Corrected some bugs on the way... Automatically convert textures to POT on RPI and WEB --- src/audio.c | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'src/audio.c') diff --git a/src/audio.c b/src/audio.c index d6d16360..40c24895 100644 --- a/src/audio.c +++ b/src/audio.c @@ -45,9 +45,14 @@ // Defines and Macros //---------------------------------------------------------------------------------- #define MUSIC_STREAM_BUFFERS 2 -#define MUSIC_BUFFER_SIZE 4096*2 // PCM data buffer (short) - 16Kb - // NOTE: Reduced to avoid frame-stalls on RPI -//#define MUSIC_BUFFER_SIZE 4096*8 // PCM data buffer (short) - 64Kb + +#if defined(PLATFORM_RPI) + // NOTE: On RPI should be lower to avoid frame-stalls + #define MUSIC_BUFFER_SIZE 4096*2 // PCM data buffer (short) - 16Kb (RPI) +#else + // NOTE: On HTML5 (emscripten) this is allocated on heap, by default it's only 16MB!...just take care... + #define MUSIC_BUFFER_SIZE 4096*8 // PCM data buffer (short) - 64Kb +#endif //---------------------------------------------------------------------------------- // Types and Structures Definition @@ -97,7 +102,7 @@ void InitAudioDevice(void) // Open and initialize a device with default settings ALCdevice *device = alcOpenDevice(NULL); - if(!device) TraceLog(ERROR, "Could not open audio device"); + if(!device) TraceLog(ERROR, "Audio device could not be opened"); ALCcontext *context = alcCreateContext(device, NULL); @@ -196,13 +201,12 @@ Sound LoadSound(char *fileName) // Attach sound buffer to source alSourcei(source, AL_BUFFER, buffer); + + TraceLog(INFO, "[%s] Sound file loaded successfully (SampleRate: %i, BitRate: %i, Channels: %i)", fileName, wave.sampleRate, wave.bitsPerSample, wave.channels); // Unallocate WAV data UnloadWave(wave); - TraceLog(INFO, "[%s] Sound file loaded successfully", fileName); - TraceLog(INFO, "[%s] Sample rate: %i - Channels: %i", fileName, wave.sampleRate, wave.channels); - sound.source = source; sound.buffer = buffer; } @@ -254,8 +258,7 @@ Sound LoadSoundFromWave(Wave wave) // Unallocate WAV data UnloadWave(wave); - TraceLog(INFO, "[Wave] Sound file loaded successfully"); - TraceLog(INFO, "[Wave] Sample rate: %i - Channels: %i", wave.sampleRate, wave.channels); + TraceLog(INFO, "[Wave] Sound file loaded successfully (SampleRate: %i, BitRate: %i, Channels: %i)", wave.sampleRate, wave.bitsPerSample, wave.channels); sound.source = source; sound.buffer = buffer; @@ -280,7 +283,10 @@ Sound LoadSoundFromRES(const char *rresName, int resId) FILE *rresFile = fopen(rresName, "rb"); - if (!rresFile) TraceLog(WARNING, "[%s] Could not open raylib resource file", rresName); + if (rresFile == NULL) + { + TraceLog(WARNING, "[%s] rRES raylib resource file could not be opened", rresName); + } else { // Read rres file (basic file check - id) @@ -372,12 +378,12 @@ Sound LoadSoundFromRES(const char *rresName, int resId) // Attach sound buffer to source alSourcei(source, AL_BUFFER, buffer); + + TraceLog(INFO, "[%s] Sound loaded successfully from resource (SampleRate: %i, BitRate: %i, Channels: %i)", rresName, wave.sampleRate, wave.bitsPerSample, wave.channels); // Unallocate WAV data UnloadWave(wave); - TraceLog(INFO, "[%s] Sound loaded successfully from resource, sample rate: %i", rresName, (int)sampleRate); - sound.source = source; sound.buffer = buffer; } @@ -492,7 +498,10 @@ void PlayMusicStream(char *fileName) // Open audio stream currentMusic.stream = stb_vorbis_open_filename(fileName, NULL, NULL); - if (currentMusic.stream == NULL) TraceLog(WARNING, "[%s] Could not open ogg audio file", fileName); + if (currentMusic.stream == NULL) + { + TraceLog(WARNING, "[%s] OGG audio file could not be opened", fileName); + } else { // Get file info @@ -582,11 +591,13 @@ void ResumeMusicStream(void) // Check if music is playing bool MusicIsPlaying(void) { - ALenum state; + bool playing = false; + ALint state; alGetSourcei(currentMusic.source, AL_SOURCE_STATE, &state); + if (state == AL_PLAYING) playing = true; - return (state == AL_PLAYING); + return playing; } // Set volume for music @@ -757,9 +768,9 @@ static Wave LoadWAV(const char *fileName) wavFile = fopen(fileName, "rb"); - if (!wavFile) + if (wavFile == NULL) { - TraceLog(WARNING, "[%s] Could not open WAV file", fileName); + TraceLog(WARNING, "[%s] WAV file could not be opened", fileName); } else { @@ -811,7 +822,7 @@ static Wave LoadWAV(const char *fileName) wave.channels = waveFormat.numChannels; wave.bitsPerSample = waveFormat.bitsPerSample; - TraceLog(INFO, "[%s] Wave file loaded successfully", fileName); + TraceLog(INFO, "[%s] WAV file loaded successfully (SampleRate: %i, BitRate: %i, Channels: %i)", fileName, wave.sampleRate, wave.bitsPerSample, wave.channels); } } } @@ -860,6 +871,8 @@ static Wave LoadOGG(char *fileName) int samplesObtained = stb_vorbis_get_samples_short_interleaved(oggFile, info.channels, wave.data, totalSamplesLength); TraceLog(DEBUG, "[%s] Samples obtained: %i", fileName, samplesObtained); + + TraceLog(INFO, "[%s] OGG file loaded successfully (SampleRate: %i, BitRate: %i, Channels: %i)", fileName, wave.sampleRate, wave.bitsPerSample, wave.channels); stb_vorbis_close(oggFile); -- cgit v1.2.3