add multi channel audio to raudio (#895)

* added multi channel sound replay to raudio
added -fPIC to Makefile for Linux
added simple lighting and audio multi channel to examples Makefile

* not properly reporting audio buffer pool size...
This commit is contained in:
chriscamacho
2019-06-29 09:49:42 +01:00
committed by Ray
parent a4e307ed96
commit 83a4eb0852
6 changed files with 259 additions and 14 deletions

View File

@ -161,7 +161,7 @@ ifeq ($(PLATFORM),PLATFORM_ANDROID)
# Starting at 2019 using ARM64 is mandatory for published apps
ANDROID_ARCH ?= ARM
ANDROID_API_VERSION = 21
# Android required path variables
# NOTE: Android NDK is just required to generate the standalone toolchain,
# in case is not already provided
@ -169,7 +169,7 @@ ifeq ($(PLATFORM),PLATFORM_ANDROID)
# Android standalone toolchain path
ANDROID_TOOLCHAIN = C:/android_toolchain_$(ANDROID_ARCH)_API$(ANDROID_API_VERSION)
ifeq ($(ANDROID_ARCH),ARM)
ANDROID_ARCH_NAME = armeabi-v7a
endif
@ -258,6 +258,10 @@ endif
# -fno-strict-aliasing jar_xm.h does shady stuff (breaks strict aliasing)
CFLAGS += -Wall -std=c99 -D_DEFAULT_SOURCE -Wno-missing-braces -Werror=pointer-arith -fno-strict-aliasing
ifeq ($(PLATFORM_OS),LINUX)
CFLAGS += -fPIC
endif
ifeq ($(RAYLIB_BUILD_MODE),DEBUG)
CFLAGS += -g
endif
@ -528,14 +532,14 @@ models.o : models.c raylib.h rlgl.h raymath.h
# Compile audio module
raudio.o : raudio.c raylib.h
$(CC) -c $< $(CFLAGS) $(INCLUDE_PATHS) -D$(PLATFORM)
# Compile raygui module
# NOTE: raygui header should be distributed with raylib.h
raygui.o : raygui.c raygui.h
@echo #define RAYGUI_IMPLEMENTATION > raygui.c
@echo #include "$(RAYLIB_MODULE_RAYGUI_PATH)/raygui.h" > raygui.c
$(CC) -c $< $(CFLAGS) $(INCLUDE_PATHS) -D$(PLATFORM) -DRAYGUI_IMPLEMENTATION
# Compile physac module
# NOTE: physac header should be distributed with raylib.h
physac.o : physac.c physac.h

View File

@ -178,10 +178,11 @@ typedef enum {
} TraceLogType;
#endif
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
// ...
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
@ -230,7 +231,7 @@ struct rAudioBuffer {
unsigned int bufferSizeInFrames;
rAudioBuffer *next;
rAudioBuffer *prev;
unsigned char buffer[1];
unsigned char *buffer;
};
// HACK: To avoid CoreAudio (macOS) symbol collision
@ -268,6 +269,24 @@ void SetAudioBufferPitch(AudioBuffer *audioBuffer, float pitch);
void TrackAudioBuffer(AudioBuffer *audioBuffer);
void UntrackAudioBuffer(AudioBuffer *audioBuffer);
//----------------------------------------------------------------------------------
// multi channel playback globals
//----------------------------------------------------------------------------------
// number of channels in the pool
#define PLAY_POOL_SIZE 16
// the buffer pool
AudioBuffer* PlayBufferPool[PLAY_POOL_SIZE];
// these are used to determine the oldest playing channel
unsigned long PlayPoolAge = 0;
unsigned long PlayPoolAges[PLAY_POOL_SIZE] = {0};
//----------------------------------------------------------------------------------
// Log callback function
static void OnLog(ma_context *pContext, ma_device *pDevice, ma_uint32 logLevel, const char *message)
{
@ -462,6 +481,15 @@ static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 fr
}
}
// initialise the multichannel buffer pool
static void InitPlayBufferPool()
{
// dummy buffers
for (int i=0; i<PLAY_POOL_SIZE; i++) {
PlayBufferPool[i] = CreateAudioBuffer(DEVICE_FORMAT, DEVICE_CHANNELS, DEVICE_SAMPLE_RATE, 0, AUDIO_BUFFER_USAGE_STATIC);
}
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Audio Device initialization and Closing
//----------------------------------------------------------------------------------
@ -526,9 +554,21 @@ void InitAudioDevice(void)
TraceLog(LOG_INFO, "Audio sample rate: %d -> %d", device.sampleRate, device.playback.internalSampleRate);
TraceLog(LOG_INFO, "Audio buffer size: %d", device.playback.internalBufferSizeInFrames);
InitPlayBufferPool();
TraceLog(LOG_INFO, "Audio multichannel pool size: %i", PLAY_POOL_SIZE);
isAudioInitialized = MA_TRUE;
}
// internal
static void FreePlayBufferPool() {
for (int i = 0; i < PLAY_POOL_SIZE; i++) {
// NB important free only the buffer struct not the attached data...!
RL_FREE(PlayBufferPool[i]);
}
}
// Close the audio device for all contexts
void CloseAudioDevice(void)
{
@ -542,6 +582,8 @@ void CloseAudioDevice(void)
ma_device_uninit(&device);
ma_context_uninit(&context);
FreePlayBufferPool();
TraceLog(LOG_INFO, "Audio device closed successfully");
}
@ -567,7 +609,8 @@ void SetMasterVolume(float volume)
// Create a new audio buffer. Initially filled with silence
AudioBuffer *CreateAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 bufferSizeInFrames, AudioBufferUsage usage)
{
AudioBuffer *audioBuffer = (AudioBuffer *)RL_CALLOC(sizeof(*audioBuffer) + (bufferSizeInFrames*channels*ma_get_bytes_per_sample(format)), 1);
AudioBuffer *audioBuffer = (AudioBuffer *)RL_CALLOC(sizeof(*audioBuffer), 1);
audioBuffer->buffer = RL_CALLOC((bufferSizeInFrames*channels*ma_get_bytes_per_sample(format)), 1);
if (audioBuffer == NULL)
{
TraceLog(LOG_ERROR, "CreateAudioBuffer() : Failed to allocate memory for audio buffer");
@ -623,6 +666,7 @@ void DeleteAudioBuffer(AudioBuffer *audioBuffer)
}
UntrackAudioBuffer(audioBuffer);
RL_FREE(audioBuffer->buffer);
RL_FREE(audioBuffer);
}
@ -966,6 +1010,79 @@ void PlaySound(Sound sound)
PlayAudioBuffer((AudioBuffer *)sound.audioBuffer);
}
// play a sound in the multichannel buffer pool
void PlaySoundEx(Sound s)
{
int found = -1;
unsigned long oldAge = 0;
int oldIndex = -1;
// find the first non playing pool entry
for (int i=0; i<PLAY_POOL_SIZE; i++) {
if (PlayPoolAges[i] > oldAge) {
oldAge = PlayPoolAges[i];
oldIndex = i;
}
if (!IsAudioBufferPlaying(PlayBufferPool[i])) {
found = i;
break;
}
}
// if no none playing pool members can be found choose the oldest
if (found == -1) {
TraceLog(LOG_WARNING,"pool age %i ended a sound early no room in buffer pool",PlayPoolAge);
if (oldIndex == -1) {
// shouldn't be able to get here... but just in case something odd happens!
TraceLog(LOG_ERROR,"sound buffer pool couldn't determine oldest buffer not playing sound");
return;
}
found = oldIndex;
// just in case...
StopAudioBuffer(PlayBufferPool[found]);
}
// experimentally mutex lock doesn't seem to be needed this makes sense
// as PlayBufferPool[found] isn't playing and the only stuff we're copying
// shouldn't be changing...
PlayPoolAges[found] = PlayPoolAge;
PlayPoolAge++;
PlayBufferPool[found]->volume = ((AudioBuffer*)s.audioBuffer)->volume;
PlayBufferPool[found]->pitch = ((AudioBuffer*)s.audioBuffer)->pitch;
PlayBufferPool[found]->looping = ((AudioBuffer*)s.audioBuffer)->looping;
PlayBufferPool[found]->usage = ((AudioBuffer*)s.audioBuffer)->usage;
PlayBufferPool[found]->isSubBufferProcessed[0] = false;
PlayBufferPool[found]->isSubBufferProcessed[1] = false;
PlayBufferPool[found]->bufferSizeInFrames = ((AudioBuffer*)s.audioBuffer)->bufferSizeInFrames;
PlayBufferPool[found]->buffer = ((AudioBuffer*)s.audioBuffer)->buffer;
PlayAudioBuffer(PlayBufferPool[found]);
}
// MUST be called before UnLoadSound is used on any sound played with PlaySoundEx
void StopPlayBufferPool()
{
for (int i = 0; i < PLAY_POOL_SIZE; i++) {
StopAudioBuffer(PlayBufferPool[i]);
}
}
// number of sounds playing in the multichannel buffer pool
int ConcurrentPlayChannels()
{
int n = 0;
for (int i=0; i<PLAY_POOL_SIZE; i++) {
if (IsAudioBufferPlaying(PlayBufferPool[i])) {
n++;
}
}
return n;
}
// Pause a sound
void PauseSound(Sound sound)
{

View File

@ -139,6 +139,9 @@ void UpdateSound(Sound sound, const void *data, int samplesCount);// Update soun
void UnloadWave(Wave wave); // Unload wave data
void UnloadSound(Sound sound); // Unload sound
void PlaySound(Sound sound); // Play a sound
void PlaySoundEx(Sound s); // Play a sound using the multi channel buffer pool
void StopPlayBufferPool(); // MUST be called before UnLoadSound is used on any sound played with PlaySoundEx
int ConcurrentPlayChannels(); // Number of sounds playing in the multichannel buffer pool
void PauseSound(Sound sound); // Pause a sound
void ResumeSound(Sound sound); // Resume a paused sound
void StopSound(Sound sound); // Stop playing a sound
@ -164,7 +167,7 @@ float GetMusicTimeLength(Music music); // Get music tim
float GetMusicTimePlayed(Music music); // Get current music time played (in seconds)
// AudioStream management functions
AudioStream InitAudioStream(unsigned int sampleRate,
AudioStream InitAudioStream(unsigned int sampleRate,
unsigned int sampleSize,
unsigned int channels); // Init audio stream (to stream raw audio pcm data)
void UpdateAudioStream(AudioStream stream, const void *data, int samplesCount); // Update audio stream buffers with data
@ -182,4 +185,4 @@ void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for
}
#endif
#endif // RAUDIO_H
#endif // RAUDIO_H

View File

@ -631,7 +631,7 @@ typedef enum {
GAMEPAD_BUTTON_LEFT_FACE_RIGHT,
GAMEPAD_BUTTON_LEFT_FACE_DOWN,
GAMEPAD_BUTTON_LEFT_FACE_LEFT,
// This is normally a DPAD
GAMEPAD_BUTTON_RIGHT_FACE_UP,
GAMEPAD_BUTTON_RIGHT_FACE_RIGHT,
@ -1327,7 +1327,7 @@ RLAPI void EndScissorMode(void); // End
RLAPI void InitVrSimulator(void); // Init VR simulator for selected device parameters
RLAPI void CloseVrSimulator(void); // Close VR simulator for current device
RLAPI void UpdateVrTracking(Camera *camera); // Update VR tracking (position and orientation) and camera
RLAPI void SetVrConfiguration(VrDeviceInfo info, Shader distortion); // Set stereo rendering configuration parameters
RLAPI void SetVrConfiguration(VrDeviceInfo info, Shader distortion); // Set stereo rendering configuration parameters
RLAPI bool IsVrSimulatorReady(void); // Detect if VR simulator is ready
RLAPI void ToggleVrMode(void); // Enable/Disable VR experience
RLAPI void BeginVrDrawing(void); // Begin VR simulator stereo rendering
@ -1356,6 +1356,9 @@ RLAPI void ExportWaveAsCode(Wave wave, const char *fileName); // Export
// Wave/Sound management functions
RLAPI void PlaySound(Sound sound); // Play a sound
RLAPI void PlaySoundEx(Sound s); // Play a sound using the multi channel buffer pool
RLAPI void StopPlayBufferPool(); // MUST be called before UnLoadSound is used on any sound played with PlaySoundEx
RLAPI int ConcurrentPlayChannels(); // Number of sounds playing in the multichannel buffer pool
RLAPI void PauseSound(Sound sound); // Pause a sound
RLAPI void ResumeSound(Sound sound); // Resume a paused sound
RLAPI void StopSound(Sound sound); // Stop playing a sound