Skip to content

Commit fe81948

Browse files
committed
new metro2 opcode like metro but with swing
1 parent ece2de3 commit fe81948

File tree

1 file changed

+74
-4
lines changed

1 file changed

+74
-4
lines changed

Opcodes/metro.c

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
metro.c:
33
4-
Copyright (C) 2000 Gabriel Maldonado
4+
Copyright (C) 2000 Gabriel Maldonado, (C) 2019 Gleb Rogozinsky
55
66
This file is part of Csound.
77
@@ -31,6 +31,15 @@ typedef struct {
3131
int32_t flag;
3232
} METRO;
3333

34+
// METRO2 ADDED BY GLEB ROGOZINSKY Oct 2019
35+
typedef struct {
36+
OPDS h;
37+
MYFLT *sr, *xcps, *kswng, *iamp, *iphs;
38+
double amp2, curphs, curphs2, swng_init;
39+
int32_t flag, flag2;
40+
} METRO2;
41+
//
42+
3443
typedef struct {
3544
OPDS h;
3645
MYFLT *trig, *ndx, *maxtics, *ifn, *outargs[VARGMAX];
@@ -64,8 +73,8 @@ static int32_t metro_set(CSOUND *csound, METRO *p)
6473

6574
static int32_t metro(CSOUND *csound, METRO *p)
6675
{
67-
IGN(csound);
6876
double phs= p->curphs;
77+
IGN(csound);
6978
if (phs == 0.0 && p->flag) {
7079
*p->sr = FL(1.0);
7180
p->flag = 0;
@@ -81,6 +90,67 @@ static int32_t metro(CSOUND *csound, METRO *p)
8190
return OK;
8291
}
8392

93+
/* GLEB ROGOZINSKY Oct 2019
94+
Opcode metro2 in addition to 'classic' metro opcode,
95+
allows swinging with possibiliy of setting its own amplitude value
96+
*/
97+
static int32_t metro2_set(CSOUND *csound, METRO2 *p)
98+
{
99+
double phs = *p->iphs;
100+
double swng = *p->kswng;
101+
int32 longphs;
102+
p->amp2 = *p->iamp;
103+
104+
if (phs >= 0.0) {
105+
if (UNLIKELY((longphs = (int32)phs)))
106+
csound->Warning(csound, Str("metro2:init phase truncation"));
107+
p->curphs = (MYFLT)phs - (MYFLT)longphs;
108+
p->curphs2 = (MYFLT)phs - (MYFLT)longphs + 1.0 - (MYFLT)swng;
109+
}
110+
p->flag = 1;
111+
p->flag2 = 1;
112+
p->swng_init = (MYFLT)swng;
113+
return OK;
114+
}
115+
116+
static int32_t metro2(CSOUND *csound, METRO2 *p)
117+
{
118+
double phs= p->curphs;
119+
double phs2= p->curphs2;
120+
double phs2_init = p->swng_init;
121+
double amp2= p->amp2;
122+
double swng= *p->kswng;
123+
IGN(csound);
124+
// MAIN TICK
125+
if (phs == 0.0 && p->flag) {
126+
*p->sr = FL(1.0);
127+
p->flag = 0;
128+
}
129+
else if ((phs += *p->xcps * CS_ONEDKR * 0.5) >= 1.0 ) {
130+
*p->sr = FL(1.0);
131+
phs -= 1.0;
132+
p->flag = 0;
133+
}
134+
else
135+
*p->sr = FL(0.0);
136+
p->curphs = phs;
137+
138+
// SWINGING TICK
139+
if (phs2 == 0.0 && p->flag2) {
140+
*p->sr = FL(amp2);
141+
p->flag2 = 0;
142+
}
143+
else if ((phs2 += *p->xcps * CS_ONEDKR * 0.5) >= (1.0 + swng - phs2_init) ) {
144+
*p->sr = FL(amp2);
145+
phs2 -= 1.0;
146+
p->flag2 = 0;
147+
}
148+
p->curphs2 = phs2;
149+
150+
return OK;
151+
}
152+
//
153+
84154
static int32_t split_trig_set(CSOUND *csound, SPLIT_TRIG *p)
85155
{
86156

@@ -255,15 +325,15 @@ static int32_t timeseq(CSOUND *csound, TIMEDSEQ *p)
255325

256326
static OENTRY localops[] = {
257327
{ "metro", S(METRO), 0, 3, "k", "ko", (SUBR)metro_set, (SUBR)metro },
328+
{ "metro2", S(METRO2), 0, 3, "k", "kkpo", (SUBR)metro2_set, (SUBR)metro2 },
258329
{ "splitrig", S(SPLIT_TRIG), 0, 3, "", "kkiiz",
259330
(SUBR)split_trig_set, (SUBR)split_trig },
260331
{ "timedseq",S(TIMEDSEQ), TR, 3, "k", "kiz", (SUBR)timeseq_set, (SUBR)timeseq }
261-
};
332+
};O
262333

263334
int32_t metro_init_(CSOUND *csound)
264335
{
265336
return csound->AppendOpcodes(csound, &(localops[0]),
266337
(int32_t
267338
) (sizeof(localops) / sizeof(OENTRY)));
268339
}
269-

0 commit comments

Comments
 (0)