Refactor audio logic a bit

This commit is contained in:
UnknownShadow200 2025-06-04 19:47:20 +10:00
parent e4fa7264ba
commit d9320945ea
21 changed files with 57 additions and 94 deletions

View File

@ -20,15 +20,15 @@
int Audio_SoundsVolume, Audio_MusicVolume;
const char* const Sound_Names[SOUND_COUNT] = {
"none", "wood", "gravel", "grass", "stone",
"metal", "glass", "cloth", "sand", "snow",
};
const cc_string Sounds_ZipPathMC = String_FromConst("audio/default.zip");
const cc_string Sounds_ZipPathCC = String_FromConst("audio/classicube.zip");
static const cc_string audio_dir = String_FromConst("audio");
struct Sound {
int channels, sampleRate;
struct AudioChunk chunk;
};
/*########################################################################################################################*
*--------------------------------------------------------Sounds-----------------------------------------------------------*
@ -46,14 +46,6 @@ static void Sounds_Start(void) {
void Audio_PlayDigSound(cc_uint8 type) { }
void Audio_PlayStepSound(cc_uint8 type) { }
#else
#define AUDIO_MAX_SOUNDS 10
struct SoundGroup {
int count;
struct Sound sounds[AUDIO_MAX_SOUNDS];
};
struct Soundboard { struct SoundGroup groups[SOUND_COUNT]; };
static struct Soundboard digBoard, stepBoard;
static RNGState sounds_rnd;

View File

@ -78,7 +78,7 @@ cc_result Audio_Play(struct AudioContext* ctx);
/* Returns the number of buffers being played or queued */
/* (e.g. if inUse is 0, no audio buffers are being played or queued) */
cc_result Audio_Poll(struct AudioContext* ctx, int* inUse);
cc_result Audio_Pause(struct AudioContext* ctx); /* Only implemented with OpenSL ES backend */
cc_result Audio_Pause(struct AudioContext* ctx);
/* Outputs more detailed information about errors with audio. */
cc_bool Audio_DescribeError(cc_result res, cc_string* dst);
@ -93,5 +93,27 @@ void Audio_Warn(cc_result res, const char* action);
cc_result AudioPool_Play(struct AudioData* data);
void AudioPool_Close(void);
/*########################################################################################################################*
*---------------------------------------------------------Sounds---------------------------------------------------------*
*#########################################################################################################################*/
enum SoundType {
SOUND_NONE, SOUND_WOOD, SOUND_GRAVEL, SOUND_GRASS,
SOUND_STONE, SOUND_METAL, SOUND_GLASS, SOUND_CLOTH,
SOUND_SAND, SOUND_SNOW, SOUND_COUNT
};
extern const char* const Sound_Names[SOUND_COUNT];
#define AUDIO_MAX_SOUNDS 10
struct Sound {
int channels, sampleRate;
struct AudioChunk chunk;
};
struct SoundGroup {
int count;
struct Sound sounds[AUDIO_MAX_SOUNDS];
};
struct Soundboard { struct SoundGroup groups[SOUND_COUNT]; };
CC_END_HEADER
#endif

View File

@ -10,6 +10,7 @@ struct AudioContext {
int sampleRate;
cc_bool stereo;
};
#define AUDIO_OVERRIDE_ALLOC
#include "_AudioBase.h"
static int channelIDs;

View File

@ -19,6 +19,7 @@ struct AudioContext {
struct AudioBuffer bufs[AUDIO_MAX_BUFFERS];
int count, sampleRate;
};
#define AUDIO_OVERRIDE_ALLOC
#include "_AudioBase.h"
cc_bool AudioBackend_Init(void) {

View File

@ -19,6 +19,7 @@ struct AudioContext {
int channels, sampleRate, volume;
cc_bool makeAvailable;
};
#define AUDIO_OVERRIDE_ALLOC
#include "_AudioBase.h"
cc_bool AudioBackend_Init(void) {

View File

@ -2,6 +2,8 @@
#if CC_AUD_BACKEND == CC_AUD_BACKEND_NULL
struct AudioContext { int count; };
#define AUDIO_OVERRIDE_ALLOC
#include "_AudioBase.h"
cc_bool AudioBackend_Init(void) { return false; }

View File

@ -5,7 +5,6 @@
#include <string.h>
#include <kai.h>
#define AUDIO_COMMON_ALLOC
#include "_AudioBase.h"
struct AudioContext {
@ -187,13 +186,5 @@ cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
*dst = String_FromReadonly("Unknown Error");
return true;
}
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks) {
return AudioBase_AllocChunks(size, chunks, numChunks);
}
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks) {
AudioBase_FreeChunks(chunks, numChunks);
}
#endif

View File

@ -37,8 +37,9 @@ static void (APIENTRY *_alGenSources)(ALsizei n, ALuint* sources);
static void (APIENTRY *_alDeleteSources)(ALsizei n, const ALuint* sources);
static void (APIENTRY *_alGetSourcei)(ALuint source, ALenum param, ALint* value);
static void (APIENTRY *_alSourcef)(ALuint source, ALenum param, float value);
static void (APIENTRY *_alSourcePlay)(ALuint source);
static void (APIENTRY *_alSourceStop)(ALuint source);
static void (APIENTRY *_alSourcePlay) (ALuint source);
static void (APIENTRY *_alSourcePause)(ALuint source);
static void (APIENTRY *_alSourceStop) (ALuint source);
static void (APIENTRY *_alSourceQueueBuffers)(ALuint source, ALsizei nb, const ALuint* buffers);
static void (APIENTRY *_alSourceUnqueueBuffers)(ALuint source, ALsizei nb, ALuint* buffers);
static void (APIENTRY *_alGenBuffers)(ALsizei n, ALuint* buffers);
@ -95,7 +96,8 @@ static cc_bool LoadALFuncs(void) {
DynamicLib_ReqSym(alSourcePlay), DynamicLib_ReqSym(alSourceStop),
DynamicLib_ReqSym(alSourceQueueBuffers), DynamicLib_ReqSym(alSourceUnqueueBuffers),
DynamicLib_ReqSym(alGenBuffers), DynamicLib_ReqSym(alDeleteBuffers),
DynamicLib_ReqSym(alBufferData), DynamicLib_ReqSym(alDistanceModel)
DynamicLib_ReqSym(alBufferData), DynamicLib_ReqSym(alDistanceModel),
DynamicLib_OptSym(alSourcePlay)
};
void* lib;
@ -226,6 +228,13 @@ cc_result Audio_Play(struct AudioContext* ctx) {
return _alGetError();
}
cc_result Audio_Pause(struct AudioContext* ctx) {
if (!_alSourcePause) return ERR_NOT_SUPPORTED;
_alSourcePause(ctx->source);
return _alGetError();
}
cc_result Audio_Poll(struct AudioContext* ctx, int* inUse) {
ALint processed = 0;
ALuint buffer;
@ -270,13 +279,5 @@ cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
if (err) String_AppendConst(dst, err);
return err != NULL;
}
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks) {
return AudioBase_AllocChunks(size, chunks, numChunks);
}
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks) {
AudioBase_FreeChunks(chunks, numChunks);
}
#endif

View File

@ -4,6 +4,7 @@
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include "ExtMath.h"
static SLObjectItf slEngineObject;
static SLEngineItf slEngineEngine;
static SLObjectItf slOutputObject;
@ -18,7 +19,6 @@ struct AudioContext {
SLVolumeItf playerVolume;
};
#define AUDIO_COMMON_ALLOC
#include "_AudioBase.h"
static SLresult (SLAPIENTRY *_slCreateEngine)(SLObjectItf* engine, SLuint32 numOptions, const SLEngineOption* engineOptions,
@ -254,13 +254,5 @@ cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
if (err) String_AppendConst(dst, err);
return err != NULL;
}
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks) {
return AudioBase_AllocChunks(size, chunks, numChunks);
}
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks) {
AudioBase_FreeChunks(chunks, numChunks);
}
#endif

View File

@ -11,6 +11,7 @@ struct AudioContext {
AudioDriverWaveBuf bufs[AUDIO_MAX_BUFFERS];
int channels, sampleRate;
};
#define AUDIO_OVERRIDE_ALLOC
#include "_AudioBase.h"
static int channelIDs;

View File

@ -361,18 +361,10 @@ cc_result Audio_SetFormat(struct AudioContext* ctx, int channels, int sampleRate
return 0;
}
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks) {
return AudioBase_AllocChunks(size, chunks, numChunks);
}
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks) {
AudioBase_FreeChunks(chunks, numChunks);
}
static cc_bool Audio_FastPlay(struct AudioContext* ctx, struct AudioData* data) {
int channels = data->channels;
int sampleRate = Audio_AdjustSampleRate(data->sampleRate, data->rate);
return !ctx->channels || (ctx->channels == channels && ctx->sampleRate == sampleRate);
}
#endif

View File

@ -3,7 +3,7 @@
#if defined CC_BUILD_WEBAUDIO
struct AudioContext { int contextID, count, rate; void* data; };
#define AUDIO_COMMON_ALLOC
#define AUDIO_OVERRIDE_ALLOC
#include "_AudioBase.h"
extern int interop_InitAudio(void);
@ -69,13 +69,5 @@ cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
String_AppendUtf8(dst, buffer, len);
return len > 0;
}
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks) {
return AudioBase_AllocChunks(size, chunks, numChunks);
}
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks) {
AudioBase_FreeChunks(chunks, numChunks);
}
#endif

View File

@ -6,7 +6,6 @@ static cc_bool ax_inited;
struct AudioContext { int count; };
#define AUDIO_COMMON_ALLOC
#include "_AudioBase.h"
cc_bool AudioBackend_Init(void) {
@ -64,13 +63,5 @@ static cc_bool Audio_FastPlay(struct AudioContext* ctx, struct AudioData* data)
cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
return false;
}
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks) {
return AudioBase_AllocChunks(size, chunks, numChunks);
}
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks) {
AudioBase_FreeChunks(chunks, numChunks);
}
#endif

View File

@ -26,7 +26,6 @@ struct AudioContext {
};
#define AUDIO_COMMON_VOLUME
#define AUDIO_COMMON_ALLOC
#include "_AudioBase.h"
cc_bool AudioBackend_Init(void) { return true; }
@ -159,13 +158,5 @@ cc_bool Audio_DescribeError(cc_result res, cc_string* dst) {
String_AppendConst(dst, buffer);
return true;
}
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks) {
return AudioBase_AllocChunks(size, chunks, numChunks);
}
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks) {
AudioBase_FreeChunks(chunks, numChunks);
}
#endif

View File

@ -8,14 +8,10 @@
#include "Event.h"
#include "Picking.h"
#include "Lighting.h"
#include "Audio.h"
struct _BlockLists Blocks;
const char* const Sound_Names[SOUND_COUNT] = {
"none", "wood", "gravel", "grass", "stone",
"metal", "glass", "cloth", "sand", "snow",
};
/*########################################################################################################################*
*---------------------------------------------------Default properties----------------------------------------------------*
*#########################################################################################################################*/

View File

@ -13,13 +13,6 @@ CC_BEGIN_HEADER
struct IGameComponent;
extern struct IGameComponent Blocks_Component;
enum SoundType {
SOUND_NONE, SOUND_WOOD, SOUND_GRAVEL, SOUND_GRASS,
SOUND_STONE, SOUND_METAL, SOUND_GLASS, SOUND_CLOTH,
SOUND_SAND, SOUND_SNOW, SOUND_COUNT
};
extern const char* const Sound_Names[SOUND_COUNT];
/* Describes how a block is rendered in the world. */
enum DrawType {
DRAW_OPAQUE, /* Completely covers blocks behind (e.g. dirt). */

View File

@ -13,6 +13,7 @@
#include "Logger.h"
#include "Vectors.h"
#include "Chat.h"
#include "Audio.h"
/* Data for a resizable queue, used for liquid physic tick entries. */
struct TickQueue {

View File

@ -17,6 +17,7 @@
#include "TexturePack.h"
#include "Options.h"
#include "Drawer2D.h"
#include "Audio.h"
#define COMMANDS_PREFIX "/client"
#define COMMANDS_PREFIX_SPACE "/client "

View File

@ -16,6 +16,7 @@
#include "Chat.h"
#include "TexturePack.h"
#include "Utils.h"
#include "Audio.h"
#ifdef CC_BUILD_FILESYSTEM
static struct LocationUpdate* spawn_point;

View File

@ -34,6 +34,7 @@
#include "HeldBlockRenderer.h"
#include "Options.h"
#include "Screens.h"
#include "Audio.h"
struct _ProtocolData Protocol;

View File

@ -84,8 +84,8 @@ static cc_bool AudioBase_AdjustSound(struct AudioContext* ctx, int i, struct Aud
}
#endif
#ifdef AUDIO_COMMON_ALLOC
static cc_result AudioBase_AllocChunks(int size, struct AudioChunk* chunks, int numChunks) {
#ifndef AUDIO_OVERRIDE_ALLOC
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks) {
cc_uint8* dst = (cc_uint8*)Mem_TryAlloc(numChunks, size);
int i;
if (!dst) return ERR_OUT_OF_MEMORY;
@ -98,7 +98,7 @@ static cc_result AudioBase_AllocChunks(int size, struct AudioChunk* chunks, int
return 0;
}
static void AudioBase_FreeChunks(struct AudioChunk* chunks, int numChunks) {
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks) {
Mem_Free(chunks[0].data);
}
#endif