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

@ -165,7 +165,7 @@ ifeq ($(PLATFORM),PLATFORM_RPI)
endif
ifeq ($(PLATFORM),PLATFORM_WEB)
# HTML5 emscripten compiler
# WARNING: To compile to HTML5, code must be redesigned
# WARNING: To compile to HTML5, code must be redesigned
# to use emscripten.h and emscripten_set_main_loop()
CC = emcc
endif
@ -303,12 +303,12 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP)
# Libraries for Debian GNU/Linux desktop compiling
# NOTE: Required packages: libegl1-mesa-dev
LDLIBS = -lraylib -lGL -lm -lpthread -ldl -lrt
# On X11 requires also below libraries
LDLIBS += -lX11
# NOTE: It seems additional libraries are not required any more, latest GLFW just dlopen them
#LDLIBS += -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor
# On Wayland windowing system, additional libraries requires
ifeq ($(USE_WAYLAND_DISPLAY),TRUE)
LDLIBS += -lwayland-client -lwayland-cursor -lwayland-egl -lxkbcommon
@ -435,10 +435,12 @@ EXAMPLES = \
shaders/shaders_texture_waves \
shaders/shaders_julia_set \
shaders/shaders_eratosthenes \
shaders/shaders_basic_lighting \
audio/audio_module_playing \
audio/audio_music_stream \
audio/audio_raw_stream \
audio/audio_sound_loading \
audio/audio_multichannel_sound \
physac/physics_demo \
physac/physics_friction \
physac/physics_movement \

View File

@ -0,0 +1,116 @@
/*
* This example was coded to demonstrate multi channel audio changes added to raylib
*
* This example has been created using raylib (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* This example Copyright (c) 2018 Chris Camacho (codifies) http://bedroomcoders.co.uk/captcha/
*
* THIS EXAMPLE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This example may be freely redistributed.
*/
#include "raylib.h"
#include <stdio.h> // sprintf
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [audio] abuse!");
InitAudioDevice(); // Initialize audio device
Sound fxWav = LoadSound("resources/sound.wav"); // Load WAV audio file
Sound fxOgg = LoadSound("resources/tanatana.ogg"); // Load OGG audio file
//InitPlayBufferPool();
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
int frame = 0;
// Main game loop
// old system still works alongside
SetSoundVolume(fxWav, 0.2); // effects all mutltiplay replay (really annoying sound!!!)
PlaySound(fxOgg);
bool inhibitWav = false;
bool inhibitOgg = false;
int maxFrame = 60;
int numberPlaying = 0;
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
frame++;
if (IsKeyDown(KEY_ENTER)) { inhibitWav=true; } else { inhibitWav=false; }
if (IsKeyDown(KEY_SPACE)) { inhibitOgg=true; } else { inhibitOgg=false; }
// deliberatly hammer the play pool to see what dropping old
// pool entries sounds like....
if (frame % 5==0) {
if (!inhibitWav) PlaySoundEx(fxWav);
}
if (frame==maxFrame) {
if (!inhibitOgg) PlaySoundEx(fxOgg);
frame=0;
maxFrame = GetRandomValue(6,12);
}
numberPlaying = ConcurrentPlayChannels();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
char msg[1024];
DrawText("multichannel sound abuse!", 200, 180, 20, LIGHTGRAY);
DrawText("Space to inhibit new ogg triggering", 200, 200, 20, LIGHTGRAY);
DrawText("Enter to inhibit new wav triggering", 200, 220, 20, LIGHTGRAY);
sprintf(msg,"concurrently playing %i", numberPlaying);
DrawText(msg, 200, 280, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
// you MUST stop the buffer pool before unloading
// any Sounds it might have used...
StopPlayBufferPool();
UnloadSound(fxWav); // Unload sound data
UnloadSound(fxOgg); // Unload sound data
CloseAudioDevice(); // Close audio device
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

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