aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2014-12-15 01:08:30 +0100
committerraysan5 <raysan5@gmail.com>2014-12-15 01:08:30 +0100
commitcfa60ab7e6313f85594f63d2830fdc2ce19e180e (patch)
tree2d962e95278204622a4b259ea95c9adf41857156 /src/audio.c
parentd3cf316e40b531542b6f55b965f3369d4aca28d6 (diff)
downloadraylib-cfa60ab7e6313f85594f63d2830fdc2ce19e180e.tar.gz
raylib-cfa60ab7e6313f85594f63d2830fdc2ce19e180e.zip
Added support for emscripten and more
Added PLATFORM_WEB support (emscripten-webgl) [audio] Added LoadSoundFromWave() [textures] Added LoadTextureFromImage() to replace CreateTexture() Some TraceLogs edited...
Diffstat (limited to 'src/audio.c')
-rw-r--r--src/audio.c63
1 files changed, 54 insertions, 9 deletions
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)
{