Skip to content

Commit

Permalink
Merge d4ea9d3 into c1601e8
Browse files Browse the repository at this point in the history
  • Loading branch information
gesellkammer committed Feb 22, 2019
2 parents c1601e8 + d4ea9d3 commit c48b8ec
Showing 1 changed file with 154 additions and 33 deletions.
187 changes: 154 additions & 33 deletions Opcodes/emugens/emugens.c
Expand Up @@ -22,8 +22,8 @@
02110-1301 USA
*/

#include <csdl.h>
// #include "/usr/local/include/csound/csdl.h"
// #include <csdl.h>
#include "/usr/local/include/csound/csdl.h"

#define SAMPLE_ACCURATE \
uint32_t n, nsmps = CS_KSMPS; \
Expand Down Expand Up @@ -319,9 +319,14 @@ typedef struct {
int rnd;
} PITCHCONV;

static inline MYFLT
mtof_func(MYFLT midi, MYFLT a4) {
return POWER(FL(2.0), (midi - FL(69.0)) / FL(12.0)) * a4;
}

static int32_t mtof(CSOUND *csound, PITCHCONV *p) {
IGN(csound);
*p->r = POWER(FL(2.0), (*p->k - FL(69.0)) / FL(12.0)) * p->freqA4;
*p->r = mtof_func(*p->k, p->freqA4);
return OK;
}

Expand All @@ -344,7 +349,7 @@ static int32_t ftom(CSOUND *csound, PITCHCONV *p) {
static int32_t
ftom_init(CSOUND *csound, PITCHCONV *p) {
p->freqA4 = csound->GetA4(csound);
p->rnd = (int)*p->irnd;
p->rnd = (int)(*p->irnd);
ftom(csound, p);
return OK;
}
Expand All @@ -359,6 +364,80 @@ static int32_t pchtom(CSOUND *csound, PITCHCONV *p) {
}


typedef struct {
OPDS h;
ARRAYDAT *outarr, *inarr;
MYFLT *irnd;
MYFLT freqA4;
int rnd;
int skip;
} PITCHCONV_ARR;


static int32_t
ftom_arr(CSOUND *csound, PITCHCONV_ARR *p) {
MYFLT x, *indata, *outdata;
uint32_t i;
if(p->skip) {
p->skip = 0;
return OK;
}
MYFLT a4 = p->freqA4;
IGN(csound);
indata = p->inarr->data;
outdata = p->outarr->data;
for(i=0; i < p->inarr->sizes[0]; i++) {
x = indata[i];
outdata[i] = FL(12.0) * LOG2(x / a4) + FL(69.0);
}
if(UNLIKELY(p->rnd)) {
for(i=0; i < p->inarr->sizes[0]; i++) {
outdata[i] = (MYFLT)MYFLT2LRND(outdata[i]);
}
}
return OK;
}

static int32_t
ftom_arr_init(CSOUND *csound, PITCHCONV_ARR *p) {
p->freqA4 = csound->GetA4(csound);
p->rnd = (int)*p->irnd;
tabensure(csound, p->outarr, p->inarr->sizes[0]);
p->skip = 0;
ftom_arr(csound, p);
p->skip = 1;
return OK;
}

static int32_t
mtof_arr(CSOUND *csound, PITCHCONV_ARR *p) {
MYFLT x, *indata, *outdata;
uint32_t i;
if(p->skip) {
p->skip = 0;
return OK;
}
MYFLT a4 = p->freqA4;
IGN(csound);
indata = p->inarr->data;
outdata = p->outarr->data;
for(i=0; i < p->inarr->sizes[0]; i++) {
x = indata[i];
outdata[i] = POWER(FL(2.0), (x - FL(69.0)) / FL(12.0)) * a4;
}
return OK;
}

static int32_t
mtof_arr_init(CSOUND *csound, PITCHCONV_ARR *p) {
p->freqA4 = csound->GetA4(csound);
tabensure(csound, p->outarr, p->inarr->sizes[0]);
p->skip = 0;
mtof_arr(csound, p);
p->skip = 1;
return OK;
}

/*
bpf --> break point function with linear interpolation
Expand Down Expand Up @@ -700,22 +779,18 @@ typedef struct {

int32_t _pcs[] = {9, 11, 0, 2, 4, 5, 7};

static int32_t
ntom(CSOUND *csound, NTOM *p) {
/*
formats accepted: 8D+ (equals to +50 cents), 4C#, 8A-31 7Bb+30
- no lowercase
- octave is necessary and comes always first
- no negative octaves, no octaves higher than 9
*/
char *n = (char *) p->notename->data;
#define FAIL -999

static MYFLT ntomfunc(CSOUND *csound, char *note) {
char *n = note;
uint32_t notelen = strlen(note);
int32_t octave = n[0] - '0';
int32_t pcidx = n[1] - 'A';
if (pcidx < 0 || pcidx >= 7) {
csound->Message(csound,
Str("expecting a char between A and G, but got %c\n"),
n[1]);
return NOTOK;
return FAIL;
}
int32_t pc = _pcs[pcidx];
int32_t cents = 0;
Expand All @@ -729,7 +804,7 @@ ntom(CSOUND *csound, NTOM *p) {
} else {
cursor = 2;
}
int32_t rest = p->notename->size - 1 - cursor;
int32_t rest = notelen - 1 - cursor;
if (rest > 0) {
int32_t sign = n[cursor] == '+' ? 1 : -1;
if (rest == 1) {
Expand All @@ -739,12 +814,27 @@ ntom(CSOUND *csound, NTOM *p) {
} else if (rest == 3) {
cents = 10 * (n[cursor + 1] - '0') + (n[cursor + 2] - '0');
} else {
csound->Message(csound,"%s", Str("format not understood\n"));
return NOTOK;
csound->Message(csound,Str("format not understood, note: %s, notelen: %d\n"), n, notelen);
return FAIL;
}
cents *= sign;
}
*p->r = ((octave + 1) * 12 + pc) + cents / FL(100.0);
return ((octave + 1) * 12 + pc) + cents / FL(100.0);
}


static int32_t
ntom(CSOUND *csound, NTOM *p) {
/*
formats accepted: 8D+ (equals to +50 cents), 4C#, 8A-31 7Bb+30
- no lowercase
- octave is necessary and comes always first
- no negative octaves, no octaves higher than 9
*/
MYFLT midi = ntomfunc(csound, p->notename->data);
if(midi == FAIL)
return NOTOK;
*p->r = midi;
return OK;
}

Expand Down Expand Up @@ -831,6 +921,26 @@ mton(CSOUND *csound, MTON *p) {
return OK;
}

/*
ntof -- notename to frequency
kfreq ntof Snotename
kfreq = ntof("4A") -> 440 (depending on the value of a4 variable)
*/

static int32_t
ntof(CSOUND *csound, NTOM *p) {
MYFLT midi = ntomfunc(csound, p->notename->data);
if(midi == FAIL)
return NOTOK;
MYFLT a4 = csound->GetA4(csound);
*p->r = mtof_func(midi, a4);
return OK;
}


/*
Expand Down Expand Up @@ -1690,21 +1800,23 @@ array_and(CSOUND *csound, BINOP_AAA *p) {
/*
Input types:
* a, k, s, i, w, f,
* o (optional i-rate, default to 0), O optional krate=0
* p (opt, default to 1), P optional krate=1
* q (opt, 10),
* v(opt, 0.5),
* j(opt, ?1), J optional krate=-1
* h(opt, 127),
* y (multiple inputs, a-type),
* z (multiple inputs, k-type),
* Z (multiple inputs, alternating k- and a-types),
* m (multiple inputs, i-type),
* M (multiple inputs, any type)
* n (multiple inputs, odd number of inputs, i-type).
* . anytype
* ? optional
- a, k, s, i, w, f,
- o (optional i-rate, default to 0), O optional krate=0
- p (opt, default to 1), P optional krate=1
- q (opt, 10),
- v(opt, 0.5),
- j(opt, ?1), J optional krate=-1
- h(opt, 127),
- y (multiple inputs, a-type),
- z (multiple inputs, k-type),
- Z (multiple inputs, alternating k- and a-types),
- m (multiple inputs, i-type),
- M (multiple inputs, any type)
- n (multiple inputs, odd number of inputs, i-type).
- . anytype
- ? optional
- * any type, any number of inputs
*/

#define S(x) sizeof(x)
Expand All @@ -1728,9 +1840,14 @@ static OENTRY localops[] = {

{ "mtof", S(PITCHCONV), 0, 3, "k", "k", (SUBR)mtof_init, (SUBR)mtof },
{ "mtof", S(PITCHCONV), 0, 1, "i", "i", (SUBR)mtof_init },
{ "mtof", S(PITCHCONV_ARR), 0, 3, "k[]", "k[]", (SUBR)mtof_arr_init, (SUBR)mtof_arr },
{ "mtof", S(PITCHCONV_ARR), 0, 1, "i[]", "i[]", (SUBR)mtof_arr_init },

{ "ftom", S(PITCHCONV), 0, 3, "k", "ko", (SUBR)ftom_init, (SUBR)ftom},
{ "ftom", S(PITCHCONV), 0, 1, "i", "io", (SUBR)ftom_init},
{ "ftom", S(PITCHCONV_ARR), 0, 3, "k[]", "k[]o", (SUBR)ftom_arr_init, (SUBR)ftom_arr},
{ "ftom", S(PITCHCONV_ARR), 0, 1, "i[]", "i[]o", (SUBR)ftom_arr_init, (SUBR)ftom_arr},


{ "pchtom", S(PITCHCONV), 0, 1, "i", "i", (SUBR)pchtom },
{ "pchtom", S(PITCHCONV), 0, 2, "k", "k", NULL, (SUBR)pchtom },
Expand Down Expand Up @@ -1761,6 +1878,10 @@ static OENTRY localops[] = {
{ "mton", S(MTON), 0, 3, "S", "k", (SUBR)mton, (SUBR)mton },
{ "mton", S(MTON), 0, 1, "S", "i", (SUBR)mton },

{ "ntof", S(NTOM), 0, 3, "k", "S", (SUBR)ntof, (SUBR)ntof },
{ "ntof", S(NTOM), 0, 1, "i", "S", (SUBR)ntof },


{ "cmp", S(Cmp), 0, 3, "a", "aSa", (SUBR)cmp_init, (SUBR)cmp_aa,},
{ "cmp", S(Cmp), 0, 3, "a", "aSk", (SUBR)cmp_init, (SUBR)cmp_ak },
{ "cmp", S(Cmp_array1), 0, 3, "k[]", "k[]Sk",
Expand Down

0 comments on commit c48b8ec

Please sign in to comment.