From 53373a43b8984aea6a7e2107b264d208c00a5f53 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 16 Nov 2018 20:32:19 -0800 Subject: [PATCH] Convert ALu.c to C++ Required changes to bsincgen to generate C++-friendly structures. --- Alc/{ALu.c => alu.cpp} | 31 ++++++++++-------- Alc/filters/defs.h | 8 +++++ Alc/mixer/defs.h | 8 +++++ Alc/mixvoice.c | 2 +- CMakeLists.txt | 2 +- OpenAL32/Include/alu.h | 6 ++-- native-tools/bsincgen.c | 72 ++++++++++++++++++++--------------------- 7 files changed, 75 insertions(+), 54 deletions(-) rename Alc/{ALu.c => alu.cpp} (98%) diff --git a/Alc/ALu.c b/Alc/alu.cpp similarity index 98% rename from Alc/ALu.c rename to Alc/alu.cpp index a9b5a009..df857b80 100644 --- a/Alc/ALu.c +++ b/Alc/alu.cpp @@ -82,7 +82,7 @@ static HrtfDirectMixerFunc MixDirectHrtf = MixDirectHrtf_C; void DeinitVoice(ALvoice *voice) { - al_free(ATOMIC_EXCHANGE_PTR_SEQ(&voice->Update, NULL)); + al_free(ATOMIC_EXCHANGE_PTR_SEQ(&voice->Update, static_cast(nullptr))); } @@ -322,7 +322,8 @@ static bool CalcContextParams(ALCcontext *Context) ALlistener *Listener = Context->Listener; struct ALcontextProps *props; - props = ATOMIC_EXCHANGE_PTR(&Context->Update, NULL, almemory_order_acq_rel); + props = static_cast(ATOMIC_EXCHANGE_PTR(&Context->Update, + static_cast(nullptr), almemory_order_acq_rel)); if(!props) return false; Listener->Params.MetersPerUnit = props->MetersPerUnit; @@ -347,7 +348,8 @@ static bool CalcListenerParams(ALCcontext *Context) struct ALlistenerProps *props; aluVector vel; - props = ATOMIC_EXCHANGE_PTR(&Listener->Update, NULL, almemory_order_acq_rel); + props = static_cast(ATOMIC_EXCHANGE_PTR(&Listener->Update, + static_cast(nullptr), almemory_order_acq_rel)); if(!props) return false; /* AT then UP */ @@ -390,7 +392,8 @@ static bool CalcEffectSlotParams(ALeffectslot *slot, ALCcontext *context, bool f struct ALeffectslotProps *props; ALeffectState *state; - props = ATOMIC_EXCHANGE_PTR(&slot->Update, NULL, almemory_order_acq_rel); + props = static_cast(ATOMIC_EXCHANGE_PTR(&slot->Update, + static_cast(nullptr), almemory_order_acq_rel)); if(!props && !force) return false; if(props) @@ -1459,7 +1462,8 @@ static void CalcSourceParams(ALvoice *voice, ALCcontext *context, bool force) ALbufferlistitem *BufferListItem; struct ALvoiceProps *props; - props = ATOMIC_EXCHANGE_PTR(&voice->Update, NULL, almemory_order_acq_rel); + props = static_cast(ATOMIC_EXCHANGE_PTR(&voice->Update, + static_cast(nullptr), almemory_order_acq_rel)); if(!props && !force) return; if(props) @@ -1523,8 +1527,8 @@ static void ApplyStablizer(FrontStablizer *Stablizer, ALfloat (*RESTRICT Buffer) int lidx, int ridx, int cidx, ALsizei SamplesToDo, ALsizei NumChannels) { - ALfloat (*RESTRICT lsplit)[BUFFERSIZE] = ASSUME_ALIGNED(Stablizer->LSplit, 16); - ALfloat (*RESTRICT rsplit)[BUFFERSIZE] = ASSUME_ALIGNED(Stablizer->RSplit, 16); + ALfloat (*RESTRICT lsplit)[BUFFERSIZE] = Stablizer->LSplit; + ALfloat (*RESTRICT rsplit)[BUFFERSIZE] = Stablizer->RSplit; ALsizei i; /* Apply an all-pass to all channels, except the front-left and front- @@ -1572,13 +1576,12 @@ static void ApplyDistanceComp(ALfloat (*RESTRICT Samples)[BUFFERSIZE], DistanceC { ALsizei i, c; - Values = ASSUME_ALIGNED(Values, 16); for(c = 0;c < numchans;c++) { - ALfloat *RESTRICT inout = ASSUME_ALIGNED(Samples[c], 16); + ALfloat *RESTRICT inout = Samples[c]; const ALfloat gain = distcomp[c].Gain; const ALsizei base = distcomp[c].Length; - ALfloat *RESTRICT distbuf = ASSUME_ALIGNED(distcomp[c].Buffer, 16); + ALfloat *RESTRICT distbuf = distcomp[c].Buffer; if(base == 0) { @@ -1679,7 +1682,7 @@ static void Write##A(const ALfloat (*RESTRICT InBuffer)[BUFFERSIZE], \ \ for(j = 0;j < numchans;j++) \ { \ - const ALfloat *RESTRICT in = ASSUME_ALIGNED(InBuffer[j], 16); \ + const ALfloat *RESTRICT in = InBuffer[j]; \ T *RESTRICT out = (T*)OutBuffer + Offset*numchans + j; \ \ for(i = 0;i < SamplesToDo;i++) \ @@ -1745,7 +1748,8 @@ void aluMixData(ALCdevice *device, ALvoid *OutBuffer, ALsizei NumSamples) { if(!MixSource(voice, source->id, ctx, SamplesToDo)) { - ATOMIC_STORE(&voice->Source, NULL, almemory_order_relaxed); + ATOMIC_STORE(&voice->Source, static_cast(nullptr), + almemory_order_relaxed); ATOMIC_STORE(&voice->Playing, false, almemory_order_release); SendSourceStoppedEvent(ctx, source->id); } @@ -1862,7 +1866,8 @@ void aluHandleDisconnect(ALCdevice *device, const char *msg, ...) ALvoice *voice = ctx->Voices[i]; ALsource *source; - source = ATOMIC_EXCHANGE_PTR(&voice->Source, NULL, almemory_order_relaxed); + source = static_cast(ATOMIC_EXCHANGE_PTR(&voice->Source, + static_cast(nullptr), almemory_order_relaxed)); if(source && ATOMIC_LOAD(&voice->Playing, almemory_order_relaxed)) { /* If the source's voice was playing, it's now effectively diff --git a/Alc/filters/defs.h b/Alc/filters/defs.h index beb4ab3e..58b7cc0a 100644 --- a/Alc/filters/defs.h +++ b/Alc/filters/defs.h @@ -4,6 +4,10 @@ #include "AL/al.h" #include "math_defs.h" +#ifdef __cplusplus +extern "C" { +#endif + /* Filters implementation is based on the "Cookbook formulae for audio * EQ biquad filter coefficients" by Robert Bristow-Johnson * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt @@ -109,4 +113,8 @@ inline void BiquadFilter_passthru(BiquadFilter *filter, ALsizei numsamples) } } +#ifdef __cplusplus +} // extern "C" +#endif + #endif /* ALC_FILTER_H */ diff --git a/Alc/mixer/defs.h b/Alc/mixer/defs.h index acb8a8c2..46f70982 100644 --- a/Alc/mixer/defs.h +++ b/Alc/mixer/defs.h @@ -6,6 +6,10 @@ #include "alMain.h" #include "alu.h" +#ifdef __cplusplus +extern "C" { +#endif + struct MixGains; struct MixHrtfParams; @@ -116,4 +120,8 @@ const ALfloat *Resample_bsinc_Neon(const InterpState *state, const ALfloat *REST ALsizei frac, ALint increment, ALfloat *RESTRICT dst, ALsizei dstlen); +#ifdef __cplusplus +} // extern "C" +#endif + #endif /* MIXER_DEFS_H */ diff --git a/Alc/mixvoice.c b/Alc/mixvoice.c index ffbcdb25..9a774786 100644 --- a/Alc/mixvoice.c +++ b/Alc/mixvoice.c @@ -261,7 +261,7 @@ static void LoadSamples(ALfloat *RESTRICT dst, const ALvoid *RESTRICT src, ALint static const ALfloat *DoFilters(BiquadFilter *lpfilter, BiquadFilter *hpfilter, ALfloat *RESTRICT dst, const ALfloat *RESTRICT src, - ALsizei numsamples, enum ActiveFilters type) + ALsizei numsamples, int type) { ALsizei i; switch(type) diff --git a/CMakeLists.txt b/CMakeLists.txt index 509cc511..1b925889 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -799,7 +799,7 @@ SET(OPENAL_OBJS ) SET(ALC_OBJS Alc/alc.cpp - Alc/ALu.c + Alc/alu.cpp Alc/alconfig.cpp Alc/alconfig.h Alc/bs2b.c diff --git a/OpenAL32/Include/alu.h b/OpenAL32/Include/alu.h index 03c388b4..2dbd3107 100644 --- a/OpenAL32/Include/alu.h +++ b/OpenAL32/Include/alu.h @@ -136,7 +136,7 @@ inline void aluMatrixfSet(aluMatrixf *matrix, ALfloat m00, ALfloat m01, ALfloat } -enum ActiveFilters { +enum { AF_None = 0, AF_LowPass = 1, AF_HighPass = 2, @@ -285,7 +285,7 @@ typedef struct ALvoice { InterpState ResampleState; struct { - enum ActiveFilters FilterType; + int FilterType; DirectParams Params[MAX_INPUT_CHANNELS]; ALfloat (*Buffer)[BUFFERSIZE]; @@ -294,7 +294,7 @@ typedef struct ALvoice { } Direct; struct { - enum ActiveFilters FilterType; + int FilterType; SendParams Params[MAX_INPUT_CHANNELS]; ALfloat (*Buffer)[BUFFERSIZE]; diff --git a/native-tools/bsincgen.c b/native-tools/bsincgen.c index 72bd8183..4e85135b 100644 --- a/native-tools/bsincgen.c +++ b/native-tools/bsincgen.c @@ -249,6 +249,11 @@ static void BsiGenerateTables(FILE *output, const char *tabname, const double re for(si = 0; si < BSINC_SCALE_COUNT; si++) mt[si] = (mt[si]+3) & ~3; + // Calculate the table size. + i = 0; + for(si = 0; si < BSINC_SCALE_COUNT; si++) + i += 4 * BSINC_PHASE_COUNT * mt[si]; + fprintf(output, "/* This %d%s order filter has a rejection of -%.0fdB, yielding a transition width\n" " * of ~%.3f (normalized frequency). Order increases when downsampling to a\n" @@ -256,12 +261,36 @@ static void BsiGenerateTables(FILE *output, const char *tabname, const double re " * width) suffers to reduce the CPU cost. The bandlimiting will cut all sound\n" " * after downsampling by ~%.2f octaves.\n" " */\n" -"const BSincTable %s = {\n", +"alignas(16) constexpr float %s_tab[%d] = {\n", order, (((order%100)/10) == 1) ? "th" : ((order%10) == 1) ? "st" : ((order%10) == 2) ? "nd" : ((order%10) == 3) ? "rd" : "th", - rejection, width, log2(1.0/scaleBase), tabname); + rejection, width, log2(1.0/scaleBase), tabname, i); + for(si = 0; si < BSINC_SCALE_COUNT; si++) + { + const int m = mt[si]; + const int o = num_points_min - (m / 2); + + for(pi = 0; pi < BSINC_PHASE_COUNT; pi++) + { + fprintf(output, " /* %2d,%2d (%d) */", si, pi, m); + fprintf(output, "\n "); + for(i = 0; i < m; i++) + fprintf(output, " %+14.9ef,", filter[si][pi][o + i]); + fprintf(output, "\n "); + for(i = 0; i < m; i++) + fprintf(output, " %+14.9ef,", scDeltas[si][pi][o + i]); + fprintf(output, "\n "); + for(i = 0; i < m; i++) + fprintf(output, " %+14.9ef,", phDeltas[si][pi][o + i]); + fprintf(output, "\n "); + for(i = 0; i < m; i++) + fprintf(output, " %+14.9ef,", spDeltas[si][pi][o + i]); + fprintf(output, "\n"); + } + } + fprintf(output, "};\nconstexpr BSincTable %s = {\n", tabname); /* The scaleBase is calculated from the Kaiser window transition width. It represents the absolute limit to the filter before it fully cuts @@ -286,37 +315,8 @@ static void BsiGenerateTables(FILE *output, const char *tabname, const double re } fprintf(output, " },\n"); - - // Calculate the table size. - i = 0; - for(si = 0; si < BSINC_SCALE_COUNT; si++) - i += 4 * BSINC_PHASE_COUNT * mt[si]; - - fprintf(output, "\n /* Tab (%d entries) */ {\n", i); - for(si = 0; si < BSINC_SCALE_COUNT; si++) - { - const int m = mt[si]; - const int o = num_points_min - (m / 2); - - for(pi = 0; pi < BSINC_PHASE_COUNT; pi++) - { - fprintf(output, " /* %2d,%2d (%d) */", si, pi, m); - fprintf(output, "\n "); - for(i = 0; i < m; i++) - fprintf(output, " %+14.9ef,", filter[si][pi][o + i]); - fprintf(output, "\n "); - for(i = 0; i < m; i++) - fprintf(output, " %+14.9ef,", scDeltas[si][pi][o + i]); - fprintf(output, "\n "); - for(i = 0; i < m; i++) - fprintf(output, " %+14.9ef,", phDeltas[si][pi][o + i]); - fprintf(output, "\n "); - for(i = 0; i < m; i++) - fprintf(output, " %+14.9ef,", spDeltas[si][pi][o + i]); - fprintf(output, "\n"); - } - } - fprintf(output, " }\n};\n\n"); + fprintf(output, " %s_tab\n", tabname); + fprintf(output, "};\n\n"); } @@ -348,12 +348,12 @@ int main(int argc, char *argv[]) "static_assert(BSINC_SCALE_COUNT == %d, \"Unexpected BSINC_SCALE_COUNT value!\");\n" "static_assert(BSINC_PHASE_COUNT == %d, \"Unexpected BSINC_PHASE_COUNT value!\");\n" "static_assert(FRACTIONONE == %d, \"Unexpected FRACTIONONE value!\");\n\n" -"typedef struct BSincTable {\n" +"struct BSincTable {\n" " const float scaleBase, scaleRange;\n" " const int m[BSINC_SCALE_COUNT];\n" " const int filterOffset[BSINC_SCALE_COUNT];\n" -" alignas(16) const float Tab[];\n" -"} BSincTable;\n\n", BSINC_SCALE_COUNT, BSINC_PHASE_COUNT, FRACTIONONE); +" const float *Tab;\n" +"};\n\n", BSINC_SCALE_COUNT, BSINC_PHASE_COUNT, FRACTIONONE); /* A 23rd order filter with a -60dB drop at nyquist. */ BsiGenerateTables(output, "bsinc24", 60.0, 23); /* An 11th order filter with a -40dB drop at nyquist. */