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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
metro.c:
Copyright (C) 2000 Gabriel Maldonado, (C) 2019 Gleb Rogozinsky
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "stdopcod.h"
#include <math.h>
typedef struct {
OPDS h;
MYFLT *sr, *xcps, *iphs, *kgate;
double curphs;
double gate;
int32_t flag;
} METRO;
// METRO2 ADDED BY GLEB ROGOZINSKY Oct 2019
typedef struct {
OPDS h;
MYFLT *sr, *xcps, *kswng, *iamp, *iphs;
double amp2, curphs, curphs2, swng_init;
int32_t flag, flag2;
} METRO2;
//
typedef struct {
OPDS h;
MYFLT *trig, *ndx, *maxtics, *ifn, *outargs[VARGMAX];
int32_t numouts, currtic, old_ndx;
MYFLT *table;
} SPLIT_TRIG;
typedef struct {
OPDS h;
MYFLT *ktrig, *kphs, *ifn, *args[VARGMAX];
MYFLT endSeq, *table, oldPhs;
int32_t numParm, endIndex, prevIndex, nextIndex ;
MYFLT prevActime, nextActime;
int32_t initFlag;
} TIMEDSEQ;
static int32_t metro_set(CSOUND *csound, METRO *p)
{
double phs = *p->iphs;
int32 longphs;
if (phs >= 0.0) {
if (UNLIKELY((longphs = (int32)phs)))
csound->Warning(csound, Str("metro:init phase truncation"));
p->curphs = (MYFLT)phs - (MYFLT)longphs;
}
p->flag=1;
return OK;
}
static int32_t metro(CSOUND *csound, METRO *p)
{
double phs= p->curphs;
IGN(csound);
if (phs == 0.0 && p->flag) {
*p->sr = FL(1.0);
p->flag = 0;
}
else if ((phs += *p->xcps * CS_ONEDKR) >= 1.0) {
*p->sr = FL(1.0);
phs -= 1.0;
p->flag = 0;
}
else
*p->sr = FL(0.0);
p->curphs = phs;
return OK;
}
/* John ffitch Oct 2021; for beginers */
static int32_t metrobpm(CSOUND *csound, METRO *p)
{
double phs= p->curphs;
IGN(csound);
p->gate = *p->kgate;
if (phs == 0.0 && p->flag) {
*p->sr = FL(1.0);
p->flag = 0;
}
else if ((phs += *p->xcps * CS_ONEDKR/60) >= 1.0) {
*p->sr = FL(1.0);
phs -= 1.0;
p->flag = 0;
}
else if (phs>= p->gate)
*p->sr = FL(0.0);
p->curphs = phs;
return OK;
}
/* GLEB ROGOZINSKY Oct 2019
Opcode metro2 in addition to 'classic' metro opcode,
allows swinging with possibiliy of setting its own amplitude value
*/
static int32_t metro2_set(CSOUND *csound, METRO2 *p)
{
double phs = *p->iphs;
double swng = *p->kswng;
int32 longphs;
p->amp2 = *p->iamp;
if (phs >= 0.0) {
if (UNLIKELY((longphs = (int32)phs)))
csound->Warning(csound, Str("metro2:init phase truncation"));
p->curphs = (MYFLT)phs - (MYFLT)longphs;
p->curphs2 = (MYFLT)phs - (MYFLT)longphs + 1.0 - (MYFLT)swng;
}
p->flag = 1;
p->flag2 = 1;
p->swng_init = (MYFLT)swng;
return OK;
}
static int32_t metro2(CSOUND *csound, METRO2 *p)
{
double phs= p->curphs;
double phs2= p->curphs2;
double phs2_init = p->swng_init;
double amp2= p->amp2;
double swng= *p->kswng;
IGN(csound);
// MAIN TICK
if (phs == 0.0 && p->flag) {
*p->sr = FL(1.0);
p->flag = 0;
}
else if ((phs += *p->xcps * CS_ONEDKR * 0.5) >= 1.0 ) {
*p->sr = FL(1.0);
phs -= 1.0;
p->flag = 0;
}
else
*p->sr = FL(0.0);
p->curphs = phs;
// SWINGING TICK
if (phs2 == 0.0 && p->flag2) {
*p->sr = FL(amp2);
p->flag2 = 0;
}
else if ((phs2 += *p->xcps * CS_ONEDKR * 0.5) >= (1.0 + swng - phs2_init) ) {
*p->sr = FL(amp2);
phs2 -= 1.0;
p->flag2 = 0;
}
p->curphs2 = phs2;
return OK;
}
//
static int32_t split_trig_set(CSOUND *csound, SPLIT_TRIG *p)
{
/* syntax of each table element:
numtics_elem1,
tic1_out1, tic1_out2, ... , tic1_outN,
tic2_out1, tic2_out2, ... , tic2_outN,
tic3_out1, tic3_out2, ... , tic3_outN,
.....
ticN_out1, ticN_out2, ... , ticN_outN,
numtics_elem2,
tic1_out1, tic1_out2, ... , tic1_outN,
tic2_out1, tic2_out2, ... , tic2_outN,
tic3_out1, tic3_out2, ... , tic3_outN,
.....
ticN_out1, ticN_out2, ... , ticN_outN,
*/
FUNC *ftp;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->ifn)) == NULL)) {
return csound->InitError(csound, Str("splitrig: incorrect table number"));
}
p->table = ftp->ftable;
p->numouts = p->INOCOUNT-4;
p->currtic = 0;
return OK;
}
static int32_t split_trig(CSOUND *csound, SPLIT_TRIG *p)
{
IGN(csound);
int32_t j;
int32_t numouts = p->numouts;
MYFLT **outargs = p->outargs;
if (*p->trig) {
int32_t ndx = (int32_t) *p->ndx * (numouts * (int32_t) *p->maxtics + 1);
int32_t numtics = (int32_t) p->table[ndx];
MYFLT *table = &(p->table[ndx+1]);
int32_t kndx = (int32_t) *p->ndx;
int32_t currtic;
if (kndx != p->old_ndx) {
p->currtic = 0;
p->old_ndx = kndx;
}
currtic = p->currtic;
for (j = 0; j < numouts; j++)
*outargs[j] = table[j + currtic * numouts ];
p->currtic = (currtic +1) % numtics;
}
else { // Maybe a memset?
for(j =0; j< numouts; j++)
*outargs[j] = FL(0.0);
}
return OK;
}
static int32_t timeseq_set(CSOUND *csound, TIMEDSEQ *p)
{
FUNC *ftp;
MYFLT *table;
uint32_t j;
if (UNLIKELY((ftp = csound->FTnp2Finde(csound, p->ifn)) == NULL)) return NOTOK;
table = p->table = ftp->ftable;
p->numParm = p->INOCOUNT-2; /* ? */
for (j = 0; j < ftp->flen; j+= p->numParm) {
if (table[j] < 0) {
p->endSeq = table[j+1];
p->endIndex = j/p->numParm;
break;
}
}
p->initFlag = 1;
return OK;
}
static int32_t timeseq(CSOUND *csound, TIMEDSEQ *p)
{
IGN(csound);
MYFLT *table = p->table, minDist = CS_ONEDKR;
MYFLT phs = *p->kphs, endseq = p->endSeq;
int32_t j,k, numParm = p->numParm, endIndex = p->endIndex;
while (phs > endseq)
phs -=endseq;
while (phs < 0 )
phs +=endseq;
if (p->initFlag) {
prev:
for (j=0,k=endIndex; j < endIndex; j++, k--) {
if (table[j*numParm + 1] > phs ) {
p->nextActime = table[j*numParm + 1];
p->nextIndex = j;
p->prevActime = table[(j-1)*numParm + 1];
p->prevIndex = j-1;
break;
}
if (table[k*numParm + 1] < phs ) {
p->nextActime = table[(k+1)*numParm + 1];
p->nextIndex = k+1;
p->prevActime = table[k*numParm + 1];
p->prevIndex = k;
break;
}
}
if (phs == p->prevActime&& p->prevIndex != -1 ) {
*p->ktrig = 1;
for (j=0; j < numParm; j++) {
*p->args[j]=table[p->prevIndex*numParm + j];
}
}
else if (phs == p->nextActime && p->nextIndex != -1 ) {
*p->ktrig = 1;
for (j=0; j < numParm; j++) {
*p->args[j]=table[p->nextIndex*numParm + j];
}
}
/*p->oldPhs = phs; */
p->initFlag=0;
}
else {
if (phs > p->nextActime || phs < p->prevActime) {
for (j=0; j < numParm; j++) {
*p->args[j]=table[p->nextIndex*numParm + j];
}
if (table[p->nextIndex*numParm] != -1) /* if it is not end locator */
/**p->ktrig = 1; */
*p->ktrig = table[p->nextIndex*numParm + 3];
if (phs > p->nextActime) {
if (p->prevIndex > p->nextIndex && p->oldPhs < phs) {
/* there is a phase jump */
*p->ktrig = 0;
goto fine;
}
if (fabs(phs-p->nextActime) > minDist)
goto prev;
p->prevActime = table[p->nextIndex*numParm + 1];
p->prevIndex = p->nextIndex;
p->nextIndex = (p->nextIndex + 1) % endIndex;
p->nextActime = table[p->nextIndex*numParm + 1];
}
else {
if (fabs(phs-p->nextActime) > minDist)
goto prev;
p->nextActime = table[p->prevIndex*numParm + 1]; /*p->nextActime+1; */
p->nextIndex = p->prevIndex;
p->prevIndex = (p->prevIndex - 1);
if (p->prevIndex < 0) {
p->prevIndex += p->endIndex;
}
p->prevActime = table[p->prevIndex*numParm + 1]; /*p->nextActime+1; */
}
}
else
*p->ktrig = 0;
fine:
p->oldPhs = phs;
}
return OK;
}
#define S(x) sizeof(x)
static OENTRY localops[] = {
{ "metro", S(METRO), 0, 3, "k", "ko", (SUBR)metro_set, (SUBR)metro },
{ "metro2", S(METRO2), 0, 3, "k", "kkpo", (SUBR)metro2_set, (SUBR)metro2 },
{ "metrobpm",S(METRO), 0, 3, "k", "koO", (SUBR)metro_set, (SUBR)metrobpm },
{ "splitrig", S(SPLIT_TRIG), 0, 3, "", "kkiiz",
(SUBR)split_trig_set, (SUBR)split_trig },
{ "timedseq",S(TIMEDSEQ), TR, 3, "k", "kiz", (SUBR)timeseq_set, (SUBR)timeseq }
};
int32_t metro_init_(CSOUND *csound)
{
return csound->AppendOpcodes(csound, &(localops[0]),
(int32_t
) (sizeof(localops) / sizeof(OENTRY)));
}
|