Basic audio with stb_vorbis decoding and openal

This commit is contained in:
Doyle Thai 2016-07-25 22:10:50 +10:00
parent 002d6524f1
commit 6b2e759257
4 changed files with 5454 additions and 25 deletions

5
.gitignore vendored
View File

@ -4,7 +4,10 @@
# Custom # Custom
*.swp *.swp
*.opendb *.opendb
data/textures/SrcAssets/ data/textures/
data/audio/
dengine_assets.7z
todo.txt
# User-specific files # User-specific files
*.suo *.suo

5397
extern/stb-master/include/STB/stb_vorbis.c vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,11 +2,15 @@
#include <OpenAL/al.h> #include <OpenAL/al.h>
#include <OpenAL/alc.h> #include <OpenAL/alc.h>
#define _CRT_SECURE_NO_WARNINGS
#include <STB/stb_vorbis.c>
#include "Dengine/AssetManager.h" #include "Dengine/AssetManager.h"
#include "Dengine/Common.h" #include "Dengine/Common.h"
#include "Dengine/Debug.h" #include "Dengine/Debug.h"
#include "Dengine/Math.h" #include "Dengine/Math.h"
#include "Dengine/OpenGL.h" #include "Dengine/OpenGL.h"
#include "Dengine/Platform.h"
#include "WorldTraveller/WorldTraveller.h" #include "WorldTraveller/WorldTraveller.h"
@ -124,6 +128,21 @@ int main()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK); glCullFace(GL_BACK);
/*
*******************
* INITIALISE GAME
*******************
*/
GameState worldTraveller = {0};
worldTraveller_gameInit(&worldTraveller,
V2i(frameBufferWidth, frameBufferHeight));
#ifdef DENGINE_DEBUG
debug_init(&worldTraveller.arena, V2i(windowWidth, windowHeight),
worldTraveller.assetManager.font);
#endif
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller));
/* /*
******************* *******************
* INITIALISE AUDIO * INITIALISE AUDIO
@ -132,7 +151,6 @@ int main()
alGetError(); alGetError();
// TODO(doyle): Read this http://www.gamedev.net/page/resources/_/technical/game-programming/basic-openal-sound-manager-for-your-project-r3791 // TODO(doyle): Read this http://www.gamedev.net/page/resources/_/technical/game-programming/basic-openal-sound-manager-for-your-project-r3791
ALCdevice *deviceAL = alcOpenDevice(NULL); ALCdevice *deviceAL = alcOpenDevice(NULL);
if (!deviceAL) if (!deviceAL)
{ {
printf("alcOpenDevice() failed: Failed to init OpenAL device.\n"); printf("alcOpenDevice() failed: Failed to init OpenAL device.\n");
@ -148,32 +166,47 @@ int main()
} }
AL_CHECK_ERROR(); AL_CHECK_ERROR();
#define NUM_BUFFERS 3 /* Open audio file */
#define BUFFER_SIZE 4096 PlatformFileRead fileRead = {0};
ALuint audioBufferIds[NUM_BUFFERS]; platform_readFileToBuffer(&worldTraveller.arena,
alGenBuffers(NUM_BUFFERS, audioBufferIds); "data/audio/Yuki Kajiura - Swordland.ogg",
&fileRead);
i32 channels, sampleRate, numSamples;
ALshort *vorbisData = NULL;
numSamples = stb_vorbis_decode_memory(fileRead.buffer, fileRead.size,
&channels, &sampleRate, &vorbisData);
platform_closeFileRead(&worldTraveller.arena, &fileRead);
/* Number of concurrent audio files */
ALuint audioSourceId;
alGenSources(1, &audioSourceId);
AL_CHECK_ERROR(); AL_CHECK_ERROR();
ALuint audioSourcesIds[NUM_BUFFERS]; /* Audio data buffers */
alGenBuffers(NUM_BUFFERS, audioSourcesIds); ALuint audioBufferId;
alGenBuffers(1, &audioBufferId);
AL_CHECK_ERROR(); AL_CHECK_ERROR();
alBufferData(audioBufferId, AL_FORMAT_STEREO16, vorbisData,
numSamples * channels * sizeof(i16), sampleRate);
alSourceQueueBuffers(audioSourceId, 1, &audioBufferId);
alSourcePlay(audioSourceId);
#if 0
ALuint audioFormat = AL_FORMAT_MONO16;
if (vorbisInfo.channels == 2) audioFormat = AL_FORMAT_STEREO16;
i32 audioState;
alGetSourcei(audioSourceIds[0], AL_SOURCE_STATE, &audioState);
#endif
/* /*
******************* *******************
* INITIALISE GAME * GAME LOOP
******************* *******************
*/ */
GameState worldTraveller = {0};
worldTraveller_gameInit(&worldTraveller,
V2i(frameBufferWidth, frameBufferHeight));
#ifdef DENGINE_DEBUG
debug_init(&worldTraveller.arena, V2i(windowWidth, windowHeight),
worldTraveller.assetManager.font);
#endif
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller));
f32 startTime = CAST(f32)(glfwGetTime()); f32 startTime = CAST(f32)(glfwGetTime());
f32 secondsElapsed = 0.0f; // Time between current frame and last frame f32 secondsElapsed = 0.0f; // Time between current frame and last frame
@ -188,11 +221,6 @@ int main()
glfwSwapInterval(1); glfwSwapInterval(1);
#endif #endif
/*
*******************
* GAME LOOP
*******************
*/
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
/* Check and call events */ /* Check and call events */

View File

@ -7,6 +7,7 @@ typedef uint8_t u8;
typedef uint32_t u32; typedef uint32_t u32;
typedef uint64_t u64; typedef uint64_t u64;
typedef int16_t i16;
typedef int32_t i32; typedef int32_t i32;
typedef i32 b32; typedef i32 b32;