Browse Source

move invert and shift into common function

master
Alexandros Theodotou 3 years ago
parent
commit
1e50259cda
Signed by: alex
GPG Key ID: 022EAE42313D70F3
  1. 51
      src/zlfo.c
  2. 53
      src/zlfo_math.h
  3. 60
      src/zlfo_ui.c

51
src/zlfo.c

@ -536,52 +536,11 @@ run ( @@ -536,52 +536,11 @@ run (
/* invert horizontally */
long shifted_current_sample =
self->common.current_sample;
if (*self->hinvert >= 0.01f)
{
shifted_current_sample =
self->common.period_size -
self->common.current_sample;
if (shifted_current_sample ==
self->common.period_size)
shifted_current_sample = 0;
}
/* shift */
if (*self->shift >= 0.5f)
{
/* add the samples to shift from 0 to
* (half the period width) */
shifted_current_sample +=
(long)
/* shift ratio */
(((*self->shift - 0.5f) * 2.f) *
/* half a period */
(self->common.period_size / 2.f));
/* readjust */
shifted_current_sample =
shifted_current_sample %
self->common.period_size;
}
else
{
/* subtract the samples to shift between
* 0 and (half the period width) */
shifted_current_sample -=
(long)
/* shift ratio */
(((0.5f - *self->shift) * 2.f) *
/* half a period */
(self->common.period_size / 2.f));
/* readjust */
while (shifted_current_sample < 0)
{
shifted_current_sample +=
self->common.period_size;
}
}
invert_and_shift_xval (
self->common.current_sample,
self->common.period_size,
*self->hinvert >= 0.01f,
*self->shift);
if (IS_STEP_MODE (self))
{

53
src/zlfo_math.h

@ -369,4 +369,57 @@ recalc_vars ( @@ -369,4 +369,57 @@ recalc_vars (
}
}
static inline long
invert_and_shift_xval (
long base,
long max_val,
int hinvert,
float shift)
{
long ret = base;
/* invert horizontally */
if (hinvert)
{
ret = max_val - ret;
while (ret >= max_val)
ret -= max_val;
}
/* shift */
if (shift >= 0.5f)
{
/* add the samples to shift from 0 to
* (half the period width) */
ret +=
(long)
/* shift ratio */
(((shift - 0.5f) * 2.f) *
/* half a period */
(max_val / 2.f));
/* readjust */
ret = ret % max_val;
}
else
{
/* subtract the samples to shift between
* 0 and (half the period width) */
ret -=
(long)
/* shift ratio */
(((0.5f - shift) * 2.f) *
/* half a period */
(max_val / 2.f));
/* readjust */
while (ret < 0)
{
ret += max_val;
}
}
return ret;
}
#endif

60
src/zlfo_ui.c

@ -1365,45 +1365,14 @@ draw_graph ( @@ -1365,45 +1365,14 @@ draw_graph (
while (idouble < GRID_WIDTH - 0.01)
{
/* from 0 to GRID_WIDTH */
double xval = (double) i;
long xvall = (long) i;
double xvald = (double) i;
/* invert horizontally */
if (self->hinvert)
{
xval = (double) (GRID_WIDTH - i);
}
/* shift */
if (self->shift >= 0.5f)
{
xval +=
/* shift ratio */
(((double) self->shift - 0.50) *
2.0) *
/* half the period */
(GRID_WIDTH / 2.0);
/* adjust */
while (xval >= GRID_WIDTH)
{
xval -= GRID_WIDTH;
}
}
else
{
xval -=
/* shift ratio */
((0.50 - (double) self->shift) *
2.0) *
/* half a period */
(GRID_WIDTH / 2.0);
/* adjust */
while (xval < 0.0)
{
xval += GRID_WIDTH;
}
}
xvall =
invert_and_shift_xval (
i, GRID_WIDTH, self->hinvert,
self->shift);
xvald = (double) xvall;
#define DRAW_VAL(val) \
/* invert vertically */ \
@ -1455,10 +1424,12 @@ draw_graph ( @@ -1455,10 +1424,12 @@ draw_graph (
prev_draw_##val = draw_val
/* calculate sine */
double sine = (double) self->sine_cache[(int) xval];
double sine =
(double) self->sine_cache[xvall];
/* calculate saw */
double saw = (double) self->saw_cache[(int) xval];
double saw =
(double) self->saw_cache[xvall];
/* triangle can be calculated based on the
* saw */
@ -1473,14 +1444,13 @@ draw_graph ( @@ -1473,14 +1444,13 @@ draw_graph (
/* square too */
double square = saw < 0.0 ? -1.0 : 1.0;
/* get prev and next nodes to calculate
* custom */
double ratio = xvald / GRID_WIDTH;
int prev_idx =
get_prev_idx (
self, node_indices, xval / GRID_WIDTH);
self, node_indices, ratio);
int next_idx =
get_next_idx (
self, node_indices, xval / GRID_WIDTH);
self, node_indices, ratio);
/* calculate custom */
double custom =
@ -1497,7 +1467,7 @@ draw_graph ( @@ -1497,7 +1467,7 @@ draw_graph (
next_idx < 0 ?
self->nodes[0][2] :
self->nodes[next_idx][2],
(float) xval, GRID_WIDTH);
(float) xvald, GRID_WIDTH);
/* adjust for -1 to 1 */
custom = custom * 2 - 1;

Loading…
Cancel
Save