-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathosc.c
70 lines (60 loc) · 1.42 KB
/
osc.c
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
/*
* Osc
*
* This code has been extracted from the Csound opcode "oscili".
* It has been modified to work as a Soundpipe module.
*
* Original Author(s): Barry Vercoe, John FFitch, Robin Whittle
* Year: 1991
* Location: OOps/ugens2.c
*
*/
#include <stdlib.h>
#include <math.h>
#include "soundpipe.h"
int sp_osc_create(sp_osc **osc)
{
*osc = malloc(sizeof(sp_osc));
return SP_OK;
}
int sp_osc_destroy(sp_osc **osc)
{
free(*osc);
return SP_NOT_OK;
}
int sp_osc_init(sp_data *sp, sp_osc *osc, sp_ftbl *ft, SPFLOAT iphs)
{
osc->freq = 440.0;
osc->amp = 0.2;
osc->tbl = ft;
osc->iphs = fabs(iphs);
osc->inc = 0;
if (osc->iphs >= 0){
osc->lphs = ((int32_t)(osc->iphs * SP_FT_MAXLEN)) & SP_FT_PHMASK;
}
return SP_OK;
}
int sp_osc_compute(sp_data *sp, sp_osc *osc, SPFLOAT *in, SPFLOAT *out)
{
sp_ftbl *ftp;
SPFLOAT amp, cps, fract, v1, v2, *ft;
int32_t phs, lobits;
int32_t pos;
SPFLOAT sicvt = osc->tbl->sicvt;
ftp = osc->tbl;
lobits = osc->tbl->lobits;
amp = osc->amp;
cps = osc->freq;
phs = osc->lphs;
ft = osc->tbl->tbl;
osc->inc = (int32_t)lrintf(cps * sicvt);
fract = ((phs) & ftp->lomask) * ftp->lodiv;
pos = phs>>lobits;
v1 = *(ft + pos);
v2 = *(ft + ((pos + 1) % ftp->size));
*out = (v1 + (v2 - v1) * fract) * amp;
phs += osc->inc;
phs &= SP_FT_PHMASK;
osc->lphs = phs;
return SP_OK;
}