aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaulshc <[email protected]>2018-05-20 17:23:03 +0200
committerGitHub <[email protected]>2018-05-20 17:23:03 +0200
commitd3a81f4f28484b246084a5ec5cac5619adcfa819 (patch)
tree9b32b8f2815b390f2da3ebe6cc3af0c67b109046
parentdd51ba396babac675846f14d9159e32c1c864cb8 (diff)
EFX: Frequency Shifter implementation
Add frequency shifter effect using discrete Hilbert transform. Only mono signal processing by now (LEFT_DIRECTION).
-rw-r--r--Alc/ALc.c6
-rw-r--r--Alc/effects/fshifter.c323
-rw-r--r--CMakeLists.txt1
-rw-r--r--OpenAL32/Include/alAuxEffectSlot.h1
-rw-r--r--OpenAL32/Include/alEffect.h10
-rw-r--r--OpenAL32/alAuxEffectSlot.c1
-rw-r--r--OpenAL32/alEffect.c7
7 files changed, 347 insertions, 2 deletions
diff --git a/Alc/ALc.c b/Alc/ALc.c
index 597cc890..1d001a55 100644
--- a/Alc/ALc.c
+++ b/Alc/ALc.c
@@ -550,8 +550,8 @@ static const struct {
DECL(AL_EFFECT_ECHO),
DECL(AL_EFFECT_FLANGER),
DECL(AL_EFFECT_PITCH_SHIFTER),
-#if 0
DECL(AL_EFFECT_FREQUENCY_SHIFTER),
+#if 0
DECL(AL_EFFECT_VOCAL_MORPHER),
#endif
DECL(AL_EFFECT_RING_MODULATOR),
@@ -632,6 +632,10 @@ static const struct {
DECL(AL_FLANGER_FEEDBACK),
DECL(AL_FLANGER_DELAY),
+ DECL(AL_FREQUENCY_SHIFTER_FREQUENCY),
+ DECL(AL_FREQUENCY_SHIFTER_LEFT_DIRECTION),
+ DECL(AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION),
+
DECL(AL_RING_MODULATOR_FREQUENCY),
DECL(AL_RING_MODULATOR_HIGHPASS_CUTOFF),
DECL(AL_RING_MODULATOR_WAVEFORM),
diff --git a/Alc/effects/fshifter.c b/Alc/effects/fshifter.c
new file mode 100644
index 00000000..2d1b64fe
--- /dev/null
+++ b/Alc/effects/fshifter.c
@@ -0,0 +1,323 @@
+/**
+ * OpenAL cross platform audio library
+ * Copyright (C) 2018 by Raul Herraiz.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * Or go to http://www.gnu.org/copyleft/lgpl.html
+ */
+
+#include "config.h"
+
+#include <math.h>
+#include <stdlib.h>
+
+#include "alMain.h"
+#include "alAuxEffectSlot.h"
+#include "alError.h"
+#include "alu.h"
+#include "filters/defs.h"
+
+#include "alcomplex.h"
+
+#define HIL_SIZE 1024
+#define OVERSAMP (1<<2)
+
+#define HIL_STEP (HIL_SIZE / OVERSAMP)
+#define FIFO_LATENCY (HIL_STEP * (OVERSAMP-1))
+
+
+typedef struct ALfshifterState {
+ DERIVE_FROM_TYPE(ALeffectState);
+
+ /* Effect parameters */
+ ALsizei count;
+ ALdouble frac_freq;
+ ALdouble inc;
+ ALdouble ld_sign;
+
+ /*Effects buffers*/
+ ALfloat InFIFO[HIL_SIZE];
+ ALcomplex OutFIFO[HIL_SIZE];
+ ALcomplex OutputAccum[2*HIL_SIZE];
+ ALcomplex Analytic[HIL_SIZE];
+ ALcomplex Outdata[BUFFERSIZE];
+
+ alignas(16) ALfloat BufferOut[BUFFERSIZE];
+
+ /* Effect gains for each output channel */
+ ALfloat CurrentGains[MAX_OUTPUT_CHANNELS];
+ ALfloat TargetGains[MAX_OUTPUT_CHANNELS];
+
+} ALfshifterState;
+
+static ALvoid ALfshifterState_Destruct(ALfshifterState *state);
+static ALboolean ALfshifterState_deviceUpdate(ALfshifterState *state, ALCdevice *device);
+static ALvoid ALfshifterState_update(ALfshifterState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props);
+static ALvoid ALfshifterState_process(ALfshifterState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
+DECLARE_DEFAULT_ALLOCATORS(ALfshifterState)
+
+DEFINE_ALEFFECTSTATE_VTABLE(ALfshifterState);
+
+/* Define a Hann window, used to filter the HIL input and output. */
+alignas(16) static ALdouble HannWindow[HIL_SIZE];
+
+static void InitHannWindow(void)
+{
+ ALsizei i;
+
+ /* Create lookup table of the Hann window for the desired size, i.e. HIL_SIZE */
+ for(i = 0;i < HIL_SIZE>>1;i++)
+ {
+ ALdouble val = sin(M_PI * (ALdouble)i / (ALdouble)(HIL_SIZE-1));
+ HannWindow[i] = HannWindow[HIL_SIZE-1-i] = val * val;
+ }
+}
+
+static alonce_flag HannInitOnce = AL_ONCE_FLAG_INIT;
+
+static void ALfshifterState_Construct(ALfshifterState *state)
+{
+ ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
+ SET_VTABLE2(ALfshifterState, ALeffectState, state);
+
+ alcall_once(&HannInitOnce, InitHannWindow);
+}
+
+static ALvoid ALfshifterState_Destruct(ALfshifterState *state)
+{
+ ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
+}
+
+static ALboolean ALfshifterState_deviceUpdate(ALfshifterState *state, ALCdevice *device)
+{
+ /* (Re-)initializing parameters and clear the buffers. */
+ state->count = FIFO_LATENCY;
+ state->frac_freq = 0.0;
+ state->inc = 0.0;
+ state->ld_sign = -1.0;
+
+ memset(state->InFIFO , 0, HIL_SIZE*sizeof(ALfloat));
+ memset(state->OutFIFO , 0, HIL_SIZE*sizeof(ALcomplex));
+ memset(state->OutputAccum, 0, 2*HIL_SIZE*sizeof(ALcomplex));
+ memset(state->Analytic , 0, HIL_SIZE*sizeof(ALcomplex));
+
+ memset(state->CurrentGains, 0, sizeof(state->CurrentGains));
+ memset(state->TargetGains, 0, sizeof(state->TargetGains));
+
+ return AL_TRUE;
+}
+
+static ALvoid ALfshifterState_update(ALfshifterState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
+{
+ const ALCdevice *device = context->Device;
+ ALfloat coeffs[MAX_AMBI_COEFFS];
+
+ state->frac_freq = props->Fshifter.Frequency/device->Frequency;
+
+ switch (props->Fshifter.Left_direction)
+ {
+ case AL_FREQUENCY_SHIFTER_DIRECTION_DOWN:
+ state->ld_sign = -1.0;
+ break;
+
+ case AL_FREQUENCY_SHIFTER_DIRECTION_UP:
+ state->ld_sign = 1.0;
+ break;
+
+ case AL_FREQUENCY_SHIFTER_DIRECTION_OFF:
+ state->frac_freq = 0.0;
+ break;
+ }
+
+ CalcAngleCoeffs(0.0f, 0.0f, 0.0f, coeffs);
+ ComputeDryPanGains(&device->Dry, coeffs, slot->Params.Gain, state->TargetGains);
+}
+
+static ALvoid ALfshifterState_process(ALfshifterState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
+{
+ ALsizei i,k;
+ ALfloat *restrict BufferOut = state->BufferOut;
+
+ for (i = 0; i < SamplesToDo; i++){
+
+ /* Fill FIFO buffer with samples data */
+ state->InFIFO[state->count] = SamplesIn[0][i];
+ state->Outdata[i] = state->OutFIFO[state->count-FIFO_LATENCY];
+
+ state->count++;
+
+ /* Check whether FIFO buffer is filled */
+ if (state->count >= HIL_SIZE )
+ {
+ state->count = FIFO_LATENCY;
+
+ /* Real signal windowing and store in Analytic buffer */
+ for (k = 0; k < HIL_SIZE;k++)
+ {
+ state->Analytic[k].Real = state->InFIFO[k] * HannWindow[k];
+ state->Analytic[k].Imag = 0.0f;
+ }
+ /*Processing signal by Discrete Hilbert Transform (analytical signal)*/
+ hilbert(HIL_SIZE, state->Analytic);
+
+ /* Windowing and add to output accumulator */
+ for(k=0; k < HIL_SIZE; k++)
+ {
+ state->OutputAccum[k].Real += 2.0f*HannWindow[k]*state->Analytic[k].Real / OVERSAMP;
+ state->OutputAccum[k].Imag += 2.0f*HannWindow[k]*state->Analytic[k].Imag / OVERSAMP;
+ }
+ for (k = 0; k < HIL_STEP; k++)
+ {
+ state->OutFIFO[k] = state->OutputAccum[k];
+ }
+
+ /* shift accumulator */
+ memmove(state->OutputAccum, state->OutputAccum + HIL_STEP, HIL_SIZE*sizeof(ALcomplex));
+
+ /* move input FIFO */
+ for (k = 0; k < FIFO_LATENCY; k++) state->InFIFO[k] = state->InFIFO[k + HIL_STEP];
+ }
+ }
+ /*Process frequency shifter using the analytic signal obtained*/
+ for ( k = 0; k < SamplesToDo; k++, state->inc += state->frac_freq )
+ {
+ ALdouble phase;
+
+ if( state->inc >= 1.0 ) state->inc -= 1.0;
+
+ phase = (2.0*M_PI*state->inc);
+
+ BufferOut[k] = (ALfloat)(state->Outdata[k].Real*cos(phase) +
+ state->ld_sign*state->Outdata[k].Imag*sin(phase));
+ }
+
+ /* Now, mix the processed sound data to the output. */
+ MixSamples(BufferOut, NumChannels, SamplesOut, state->CurrentGains, state->TargetGains,
+ maxi(SamplesToDo, 512), 0, SamplesToDo);
+
+}
+
+typedef struct FshifterStateFactory {
+ DERIVE_FROM_TYPE(EffectStateFactory);
+} FshifterStateFactory;
+
+static ALeffectState *FshifterStateFactory_create(FshifterStateFactory *UNUSED(factory))
+{
+ ALfshifterState *state;
+
+ NEW_OBJ0(state, ALfshifterState)();
+ if(!state) return NULL;
+
+ return STATIC_CAST(ALeffectState, state);
+}
+
+DEFINE_EFFECTSTATEFACTORY_VTABLE(FshifterStateFactory);
+
+EffectStateFactory *FshifterStateFactory_getFactory(void)
+{
+ static FshifterStateFactory FshifterFactory = { { GET_VTABLE2(FshifterStateFactory, EffectStateFactory) } };
+
+ return STATIC_CAST(EffectStateFactory, &FshifterFactory);
+}
+
+void ALfshifter_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
+{
+ ALeffectProps *props = &effect->Props;
+ switch(param)
+ {
+ case AL_FREQUENCY_SHIFTER_FREQUENCY:
+ if(!(val >= AL_FREQUENCY_SHIFTER_MIN_FREQUENCY && val <= AL_FREQUENCY_SHIFTER_MAX_FREQUENCY))
+ SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter frequency out of range");
+ props->Fshifter.Frequency = (ALfloat) val;
+ break;
+
+ default:
+ alSetError(context, AL_INVALID_ENUM, "Invalid frequency shifter float property 0x%04x", param);
+ }
+}
+
+void ALfshifter_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
+{
+ ALfshifter_setParamf(effect, context, param, vals[0]);
+}
+
+void ALfshifter_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
+{
+ ALeffectProps *props = &effect->Props;
+ switch(param)
+ {
+ case AL_FREQUENCY_SHIFTER_LEFT_DIRECTION:
+ if(!(val >= AL_FREQUENCY_SHIFTER_MIN_LEFT_DIRECTION && val <= AL_FREQUENCY_SHIFTER_MAX_LEFT_DIRECTION))
+ SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter left direction out of range");
+ props->Fshifter.Left_direction = val;
+ break;
+
+ case AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION:
+ if(!(val >= AL_FREQUENCY_SHIFTER_MIN_RIGHT_DIRECTION && val <= AL_FREQUENCY_SHIFTER_MAX_RIGHT_DIRECTION))
+ SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter right direction out of range");
+ props->Fshifter.Right_direction = val;
+ break;
+
+ default:
+ alSetError(context, AL_INVALID_ENUM, "Invalid frequency shifter integer property 0x%04x", param);
+ }
+}
+void ALfshifter_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
+{
+ ALfshifter_setParami(effect, context, param, vals[0]);
+}
+
+void ALfshifter_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
+{
+ const ALeffectProps *props = &effect->Props;
+ switch(param)
+ {
+ case AL_FREQUENCY_SHIFTER_LEFT_DIRECTION:
+ *val = (ALint)props->Fshifter.Left_direction;
+ break;
+ case AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION:
+ *val = (ALint)props->Fshifter.Right_direction;
+ break;
+ default:
+ alSetError(context, AL_INVALID_ENUM, "Invalid frequency shifter integer property 0x%04x", param);
+ }
+}
+void ALfshifter_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
+{
+ ALfshifter_getParami(effect, context, param, vals);
+}
+
+void ALfshifter_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
+{
+
+ const ALeffectProps *props = &effect->Props;
+ switch(param)
+ {
+ case AL_FREQUENCY_SHIFTER_FREQUENCY:
+ *val = (ALfloat)props->Fshifter.Frequency;
+ break;
+
+ default:
+ alSetError(context, AL_INVALID_ENUM, "Invalid frequency shifter float property 0x%04x", param);
+ }
+
+}
+
+void ALfshifter_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
+{
+ ALfshifter_getParamf(effect, context, param, vals);
+}
+
+DEFINE_ALEFFECT_VTABLE(ALfshifter);
diff --git a/CMakeLists.txt b/CMakeLists.txt
index cd3ecbed..a6024bf4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -761,6 +761,7 @@ SET(ALC_OBJS
Alc/effects/distortion.c
Alc/effects/echo.c
Alc/effects/equalizer.c
+ Alc/effects/fshifter.c
Alc/effects/modulator.c
Alc/effects/null.c
Alc/effects/pshifter.c
diff --git a/OpenAL32/Include/alAuxEffectSlot.h b/OpenAL32/Include/alAuxEffectSlot.h
index bb9aef59..c1eae443 100644
--- a/OpenAL32/Include/alAuxEffectSlot.h
+++ b/OpenAL32/Include/alAuxEffectSlot.h
@@ -166,6 +166,7 @@ EffectStateFactory *DistortionStateFactory_getFactory(void);
EffectStateFactory *EchoStateFactory_getFactory(void);
EffectStateFactory *EqualizerStateFactory_getFactory(void);
EffectStateFactory *FlangerStateFactory_getFactory(void);
+EffectStateFactory *FshifterStateFactory_getFactory(void);
EffectStateFactory *ModulatorStateFactory_getFactory(void);
EffectStateFactory *PshifterStateFactory_getFactory(void);
diff --git a/OpenAL32/Include/alEffect.h b/OpenAL32/Include/alEffect.h
index 50b64ee1..206c495b 100644
--- a/OpenAL32/Include/alEffect.h
+++ b/OpenAL32/Include/alEffect.h
@@ -18,6 +18,7 @@ enum {
ECHO_EFFECT,
EQUALIZER_EFFECT,
FLANGER_EFFECT,
+ FSHIFTER_EFFECT,
MODULATOR_EFFECT,
PSHIFTER_EFFECT,
DEDICATED_EFFECT,
@@ -33,7 +34,7 @@ struct EffectList {
int type;
ALenum val;
};
-#define EFFECTLIST_SIZE 12
+#define EFFECTLIST_SIZE 13
extern const struct EffectList EffectList[EFFECTLIST_SIZE];
@@ -65,6 +66,7 @@ extern const struct ALeffectVtable ALdistortion_vtable;
extern const struct ALeffectVtable ALecho_vtable;
extern const struct ALeffectVtable ALequalizer_vtable;
extern const struct ALeffectVtable ALflanger_vtable;
+extern const struct ALeffectVtable ALfshifter_vtable;
extern const struct ALeffectVtable ALmodulator_vtable;
extern const struct ALeffectVtable ALnull_vtable;
extern const struct ALeffectVtable ALpshifter_vtable;
@@ -145,6 +147,12 @@ typedef union ALeffectProps {
ALfloat HighGain;
} Equalizer;
+ struct {
+ ALfloat Frequency;
+ ALint Left_direction;
+ ALint Right_direction;
+ } Fshifter;
+
struct {
ALfloat Frequency;
ALfloat HighPassCutoff;
diff --git a/OpenAL32/alAuxEffectSlot.c b/OpenAL32/alAuxEffectSlot.c
index d04fc4a7..6f6ef163 100644
--- a/OpenAL32/alAuxEffectSlot.c
+++ b/OpenAL32/alAuxEffectSlot.c
@@ -54,6 +54,7 @@ static const struct {
{ AL_EFFECT_ECHO, EchoStateFactory_getFactory },
{ AL_EFFECT_EQUALIZER, EqualizerStateFactory_getFactory },
{ AL_EFFECT_FLANGER, FlangerStateFactory_getFactory },
+ { AL_EFFECT_FREQUENCY_SHIFTER, FshifterStateFactory_getFactory },
{ AL_EFFECT_RING_MODULATOR, ModulatorStateFactory_getFactory },
{ AL_EFFECT_PITCH_SHIFTER, PshifterStateFactory_getFactory},
{ AL_EFFECT_DEDICATED_DIALOGUE, DedicatedStateFactory_getFactory },
diff --git a/OpenAL32/alEffect.c b/OpenAL32/alEffect.c
index e7dc6ace..a69bf70c 100644
--- a/OpenAL32/alEffect.c
+++ b/OpenAL32/alEffect.c
@@ -44,6 +44,7 @@ const struct EffectList EffectList[EFFECTLIST_SIZE] = {
{ "echo", ECHO_EFFECT, AL_EFFECT_ECHO },
{ "equalizer", EQUALIZER_EFFECT, AL_EFFECT_EQUALIZER },
{ "flanger", FLANGER_EFFECT, AL_EFFECT_FLANGER },
+ { "fshifter", FSHIFTER_EFFECT, AL_EFFECT_FREQUENCY_SHIFTER },
{ "modulator", MODULATOR_EFFECT, AL_EFFECT_RING_MODULATOR },
{ "pshifter", PSHIFTER_EFFECT, AL_EFFECT_PITCH_SHIFTER },
{ "dedicated", DEDICATED_EFFECT, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT },
@@ -584,6 +585,12 @@ static void InitEffectParams(ALeffect *effect, ALenum type)
effect->Props.Chorus.Delay = AL_FLANGER_DEFAULT_DELAY;
effect->vtab = &ALflanger_vtable;
break;
+ case AL_EFFECT_FREQUENCY_SHIFTER:
+ effect->Props.Fshifter.Frequency = AL_FREQUENCY_SHIFTER_DEFAULT_FREQUENCY;
+ effect->Props.Fshifter.Left_direction = AL_FREQUENCY_SHIFTER_DEFAULT_LEFT_DIRECTION;
+ effect->Props.Fshifter.Right_direction = AL_FREQUENCY_SHIFTER_DEFAULT_RIGHT_DIRECTION;
+ effect->vtab = &ALfshifter_vtable;
+ break;
case AL_EFFECT_RING_MODULATOR:
effect->Props.Modulator.Frequency = AL_RING_MODULATOR_DEFAULT_FREQUENCY;
effect->Props.Modulator.HighPassCutoff = AL_RING_MODULATOR_DEFAULT_HIGHPASS_CUTOFF;