summaryrefslogtreecommitdiffstats
path: root/src/osc_tri.c
blob: f0c9951a57cb6557b4bf2da648ec8bf0eefb6e69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "osc.h"

osc_t OSC_tri = {
  .name = "w_triangle",
  .start = 0,
  .len = 4,
};

static float
tri(int index)
{
  if (index == 0) return 0.0f;
  else if (index == 1) return 1.0f;
  else if (index == 2) return 0.0f;
  else if (index == 3) return -1.0f;
  //else return 0.0f;
}

float
osc_tri(float offset)
{
  return osc_interpolate(offset,
                         tri((int)offset),
                         tri(osc_next_index(&OSC_tri, offset)));
}

float
osc_tri_next(float f, float offset)
{
  return osc_next_offset(&OSC_tri, f, offset);
}


static float
tri_sample(osc_t * osc, float offset)
{
  return osc_interpolate(offset,
                         tri((int)offset),
                         tri(osc_next_index(osc, offset)));
}

static float
tri_next(osc_t * osc, float f, float offset)
{
  return osc_next_offset(osc, f, offset);
}

static const struct osc_ops osc_operations = {
  .sample = tri_sample,
  .next = tri_next,
};

osc_t *
make_tri(const char * name)
{
  osc_t * osc = (osc_t *)malloc(sizeof(osc_t));

  int len = strlen(name);
  strncpy(osc->name, name, 16);
  osc->data = NULL;
  osc->len = 2;
  osc->start = 0;
  osc->type = WAVE;
  osc->ops = &osc_operations;

  return osc;
}

void
free_tri(osc_t * osc)
{
  free(osc);
}