OSStatus Csound_Render(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { csdata *cdata = (csdata *) inRefCon; CSOUND *csound = cdata->csound; int onchnls = cdata->onchnls; MYFLT *outputBuffer = cdata->outputBuffer; int j,k; AudioUnitSampleType *buffer; int n = inNumberFrames*onchnls; IGN(ioActionFlags); IGN(inTimeStamp); IGN(inBusNumber); n = csound->ReadCircularBuffer(csound,cdata->outcb,outputBuffer,n); for (k = 0; k < onchnls; k++) { buffer = (AudioUnitSampleType *) ioData->mBuffers[k].mData; for(j=0; (unsigned int) j < inNumberFrames; j++){ buffer[j] = (AudioUnitSampleType) outputBuffer[j*onchnls+k] ; outputBuffer[j*onchnls+k] = FL(0.0); } } return 0; }
OSStatus Csound_Input(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { csdata *cdata = (csdata *) inRefCon; CSOUND *csound = cdata->csound; int inchnls = cdata->inchnls; MYFLT *inputBuffer = cdata->inputBuffer; int j,k; AudioUnitSampleType *buffer; int n = inNumberFrames*inchnls; int l; IGN(ioData); AudioUnitRender(cdata->inunit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, cdata->inputdata); for (k = 0; k < inchnls; k++){ buffer = (AudioUnitSampleType *) cdata->inputdata->mBuffers[k].mData; for(j=0; (unsigned int) j < inNumberFrames; j++){ inputBuffer[j*inchnls+k] = buffer[j]; } } l = csound->WriteCircularBuffer(csound, cdata->incb,inputBuffer,n); return 0; }
int tableir_kontrol(CSOUND *csound, TABL *p) { int ndx, len = p->len; int mask = p->ftp->lenmask; MYFLT tmp, frac; MYFLT x1, x2; IGN(csound); tmp = (*p->ndx + *p->offset)*p->mul; ndx = MYFLOOR(tmp); frac = tmp - ndx; if (p->iwrap) { if (p->np2) { while(ndx >= len) ndx -= len; while(ndx < 0) ndx += len; } else ndx &= mask; } else { if (UNLIKELY(ndx >= len)) ndx = len - 1; else if (UNLIKELY(ndx < 0)) ndx = 0; } x1 = p->ftp->ftable[ndx]; x2 = p->ftp->ftable[ndx+1]; *p->sig = x1 + (x2 - x1)*frac; return OK; }
static int32_t tabmorph(CSOUND *csound, TABMORPH *p) { IGN(csound); MYFLT /* index, index_frac, */ tabndx1, tabndx2, tabndx1frac, tabndx2frac; MYFLT tab1val1,tab1val2, tab2val1, tab2val2, interpoint, val1, val2; int64_t index_int; int32_t tabndx1int, tabndx2int; index_int = (int32_t) *p->xindex % p->length; tabndx1 = *p->xtabndx1; tabndx1int = (int32_t) tabndx1; tabndx1frac = tabndx1 - tabndx1int; tabndx1int %= p->numOfTabs; tab1val1 = (p->table[tabndx1int ])[index_int]; tab1val2 = (p->table[tabndx1int+1])[index_int]; val1 = tab1val1 * (1-tabndx1frac) + tab1val2 * tabndx1frac; tabndx2 = *p->xtabndx2; tabndx2int = (int32_t) tabndx2; tabndx2frac = tabndx2 - tabndx2int; tabndx2int %= p->numOfTabs; tab2val1 = (p->table[tabndx2int ])[index_int]; tab2val2 = (p->table[tabndx2int+1])[index_int]; val2 = tab2val1 * (1-tabndx2frac) + tab2val2 * tabndx2frac; interpoint = *p->xinterpoint; interpoint = (interpoint < 0 ? 0 : (interpoint > 1.0 ? 1.0 : interpoint)); /* interpoint -= (int32_t) interpoint; to limit to zero to 1 range */ *p->out = val1 * (1 - interpoint) + val2 * interpoint; return OK; }
int csoundReadCircularBuffer(CSOUND *csound, void *p, void *out, int items) { IGN(csound); if (p == NULL) return 0; { int remaining; int itemsread, numelem = ((circular_buffer *)p)->numelem; int elemsize = ((circular_buffer *)p)->elemsize; int i=0, rp = ((circular_buffer *)p)->rp; char *buffer = ((circular_buffer *)p)->buffer; if ((remaining = checkspace(p, 0)) == 0) { return 0; } itemsread = items > remaining ? remaining : items; for (i=0; i < itemsread; i++){ memcpy((char *) out + (i * elemsize), &(buffer[elemsize * rp++]), elemsize); if (rp == numelem) { rp = 0; } } #if defined(MSVC) InterlockedExchange(&((circular_buffer *)p)->rp, rp); #elif defined(HAVE_ATOMIC_BUILTIN) __sync_lock_test_and_set(&((circular_buffer *)p)->rp,rp); #else ((circular_buffer *)p)->rp = rp; #endif return itemsread; } }
int32_t fareylen (CSOUND *csound, FAREYLEN *p) { IGN(csound); int32_t n = (int32_t) *p->kn; *p->kr = (MYFLT) FareyLength (n); return OK; }
static int32_t MinAbs_arate(CSOUND *csound, MINMAX *p) { IGN(csound); int32_t i; uint32_t offset = p->h.insdshead->ksmps_offset; uint32_t early = p->h.insdshead->ksmps_no_end; uint32_t n, nsmps = CS_KSMPS; int32_t nargs = ((int32_t) p->INOCOUNT) - 1; MYFLT *out = p->xout; MYFLT *in1 = p->xin1; MYFLT **in2 = p->xin2toN; MYFLT min, temp; if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT)); if (UNLIKELY(early)) { nsmps -= early; memset(&out[nsmps], '\0', early*sizeof(MYFLT)); } for (n=offset; n<nsmps; n++) { min = FABS(in1[n]); for (i = 0; i < nargs; ++i) { temp = FABS(in2[i][n]); if (UNLIKELY(temp < min)) min = temp; } out[n] = min; } return OK; }
int csoundReadCircularBuffer(CSOUND *csound, void *p, void *out, int items) { if (p == NULL) return 0; { int remaining; int itemsread, numelem = ((circular_buffer *)p)->numelem; int elemsize = ((circular_buffer *)p)->elemsize; int i=0, rp = ((circular_buffer *)p)->rp; char *buffer = ((circular_buffer *)p)->buffer; IGN(csound); if ((remaining = checkspace(p, 0)) == 0) { return 0; } itemsread = items > remaining ? remaining : items; for (i=0; i < itemsread; i++){ memcpy((char *) out + (i * elemsize), &(buffer[elemsize * rp++]), elemsize); if (rp == numelem) { rp = 0; } } ((circular_buffer *)p)->rp = rp; return itemsread; } }
int csoundWriteCircularBuffer(CSOUND *csound, void *p, const void *in, int items) { IGN(csound); if (p == NULL) return 0; int remaining; int itemswrite, numelem = ((circular_buffer *)p)->numelem; int elemsize = ((circular_buffer *)p)->elemsize; int i=0, wp = ((circular_buffer *)p)->wp; char *buffer = ((circular_buffer *)p)->buffer; if ((remaining = checkspace(p, 1)) == 0) { return 0; } itemswrite = items > remaining ? remaining : items; for(i=0; i < itemswrite; i++){ memcpy(&(buffer[elemsize * wp++]), ((char *) in) + (i * elemsize), elemsize); if(wp == numelem) wp = 0; } #if defined(MSVC) InterlockedExchange(&((circular_buffer *)p)->wp, wp); #elif defined(HAVE_ATOMIC_BUILTIN) __sync_lock_test_and_set(&((circular_buffer *)p)->wp,wp); #else ((circular_buffer *)p)->wp = wp; #endif return itemswrite; }
CS_VARIABLE* createString(void* cs, void* p) { CSOUND* csound = (CSOUND*)cs; CS_VARIABLE* var = csound->Calloc(csound, sizeof (CS_VARIABLE)); IGN(p); var->memBlockSize = CS_FLOAT_ALIGN(sizeof(STRINGDAT)); return var; }
CS_VARIABLE* createBool(void* cs, void* p) { CSOUND* csound = (CSOUND*)cs; CS_VARIABLE* var = csound->Calloc(csound, sizeof (CS_VARIABLE)); IGN(p); var->memBlockSize = sizeof (MYFLT); var->initializeVariableMemory = &varInitMemory; return var; }
int32_t serialFlush(CSOUND *csound, SERIALFLUSH *p) { IGN(csound); #ifndef WIN32 tcflush(*p->port, TCIFLUSH); // who knows if this works... #endif return OK; }
CS_VARIABLE* createFsig(void* cs, void* p) { CSOUND* csound = (CSOUND*)cs; CS_VARIABLE* var = csound->Calloc(csound, sizeof (CS_VARIABLE)); IGN(p); var->memBlockSize = CS_FLOAT_ALIGN(sizeof(PVSDAT)); var->initializeVariableMemory = &varInitMemory; return var; }
static int32_t locsend(CSOUND *csound, LOCSEND *p) { /* MYFLT *r1, *r2, *r3=NULL, *r4=NULL; */ /* MYFLT *rrev1, *rrev2, *rrev3=NULL, *rrev4=NULL; */ IGN(csound); LOCSIG *q = p->locsig; uint32_t offset = p->h.insdshead->ksmps_offset; uint32_t early = p->h.insdshead->ksmps_no_end; uint32_t n, nsmps = CS_KSMPS; /* r1 = p->r1; */ /* r2 = p->r2; */ /* rrev1 = q->rrev1; */ /* rrev2 = q->rrev2; */ /* if (p->OUTOCOUNT == 4) { */ /* r3 = p->r3; */ /* r4 = p->r4; */ /* rrev3 = q->rrev3; */ /* rrev4 = q->rrev4; */ /* } */ /* for (n=0;n<nsmps; n++) { */ /* r1[n] = rrev1[n]; */ /* r2[n] = rrev2[n]; */ /* if (p->OUTOCOUNT == 4) { */ /* r3[n] = rrev3[n]; */ /* r4[n] = rrev4[n]; */ /* } */ /* } */ /* Quicker form is: */ if (UNLIKELY(offset)) { memset(p->r1, '\0', offset*sizeof(MYFLT)); memset(p->r2, '\0', offset*sizeof(MYFLT)); if (p->OUTOCOUNT == 4) { memset(p->r3, '\0', offset*sizeof(MYFLT)); memset(p->r4, '\0', offset*sizeof(MYFLT)); } } if (UNLIKELY(early)) { nsmps -= early; memset(&p->r1[nsmps], '\0', early*sizeof(MYFLT)); memset(&p->r2[nsmps], '\0', early*sizeof(MYFLT)); if (p->OUTOCOUNT == 4) { memset(&p->r3[nsmps], '\0', early*sizeof(MYFLT)); memset(&p->r4[nsmps], '\0', early*sizeof(MYFLT)); } } n = (nsmps-offset)*sizeof(MYFLT); memcpy(p->r1+offset, q->rrev1, n); memcpy(p->r2+offset, q->rrev2, n); if (p->OUTOCOUNT == 4) { memcpy(p->r3+offset, q->rrev3, n); memcpy(p->r4+offset, q->rrev4, n); } return OK; }
int32_t tablefilterset(CSOUND *csound, TABFILT *p) { IGN(csound); p->pdft = 0; p->psft = 0; *p->ftype = 1; *p->threshold = 7; return OK; }
static int32_t lowpr_set(CSOUND *csound, LOWPR *p) { IGN(csound); if (*p->istor==FL(0.0)) p->ynm1 = p->ynm2 = 0.0; p->okf = 0.0; p->okr = 0.0; p->k = 0.0; return OK; }
static int32_t lowpr_w_sep(CSOUND *csound, LOWPR_SEP *p) { IGN(csound); MYFLT b, k; MYFLT *ar, *asig, yn,*ynm1, *ynm2 ; MYFLT coef1, coef2; MYFLT kfcobase = *p->kfco; MYFLT sep = (*p->sep / p->loop); uint32_t offset = p->h.insdshead->ksmps_offset; uint32_t early = p->h.insdshead->ksmps_no_end; uint32_t n, nsmps = CS_KSMPS; int32_t j; MYFLT kres = *p->kres; MYFLT kfco; ynm1 = p->ynm1; ynm2 = p->ynm2; asig = p->asig; if (UNLIKELY(offset)) memset(p->ar, '\0', offset*sizeof(MYFLT)); if (UNLIKELY(early)) { nsmps -= early; memset(&p->ar[nsmps], '\0', early*sizeof(MYFLT)); } ar = p->ar; for (j=0; j< p->loop; j++) { MYFLT lynm1 = ynm1[j]; MYFLT lynm2 = ynm2[j]; /* linfco=log((double) kfco)*ONEtoLOG2 ; linfco = linfco + (sep / p->loop)*j; kfco = (MYFLT) pow(2.0,linfco); */ kfco = kfcobase * (FL(1.0) + (sep * j)); b = FL(10.0) / ( kres * (MYFLT)sqrt((double)kfco)) - FL(1.0); k = FL(1000.0) / kfco; coef1 = (b+FL(2.0) *k); coef2 = FL(1.0)/(FL(1.0) + b + k); for (n=offset;n<nsmps; n++) { /* This can be speeded up avoiding indirection */ ar[n] = yn = (coef1 * lynm1 - k * lynm2 + asig[n]) * coef2; lynm2 = lynm1; lynm1 = yn; } ynm1[j] = lynm1; ynm2[j] = lynm2; asig= p->ar; } return OK; }
int32_t pvsinfo(CSOUND *csound, PVSINFO *p) { IGN(csound); #ifdef _DEBUG /* init stage opcode : this should always be a proper fsig */ assert(p->fsrc->frame.auxp != NULL); #endif *p->ioverlap = (MYFLT) p->fsrc->overlap; *p->inumbins = (MYFLT) (p->fsrc->N / 2) + 1; *p->iwinsize = (MYFLT) p->fsrc->winsize; *p->iformat = (MYFLT) p->fsrc->format; return OK; }
static int32_t tabmorphi(CSOUND *csound, TABMORPH *p) /* interpolation */ { IGN(csound); MYFLT index, index_frac, tabndx1, tabndx2, tabndx1frac, tabndx2frac; MYFLT tab1val1a,tab1val2a, tab2val1a, tab2val2a, interpoint, val1, val2; MYFLT val1a, val2a, val1b, val2b, tab1val1b,tab1val2b, tab2val1b, tab2val2b; int64_t index_int; int32_t tabndx1int, tabndx2int; index = *p->xindex; index_int = (int32_t) index; index_frac = index - index_int; index_int %= p->length; tabndx1 = *p->xtabndx1; tabndx1int = (int32_t) tabndx1; tabndx1frac = tabndx1 - tabndx1int; tabndx1int %= p->numOfTabs; tab1val1a = (p->table[tabndx1int ])[index_int]; tab1val2a = (p->table[tabndx1int+1])[index_int]; val1a = tab1val1a * (1-tabndx1frac) + tab1val2a * tabndx1frac; tab1val1b = (p->table[tabndx1int ])[index_int+1]; tab1val2b = (p->table[tabndx1int+1])[index_int+1]; val1b = tab1val1b * (1-tabndx1frac) + tab1val2b * tabndx1frac; val1 = val1a + (val1b-val1a) * index_frac; /*--------------*/ tabndx2 = *p->xtabndx2; tabndx2int = (int32_t) tabndx2; tabndx2frac = tabndx2 - tabndx2int; tabndx2int %= p->numOfTabs; tab2val1a = (p->table[tabndx2int ])[index_int]; tab2val2a = (p->table[tabndx2int+1])[index_int]; val2a = tab2val1a * (1-tabndx2frac) + tab2val2a * tabndx2frac; tab2val1b = (p->table[tabndx2int ])[index_int+1]; tab2val2b = (p->table[tabndx2int+1])[index_int+1]; val2b = tab2val1b * (1-tabndx2frac) + tab2val2b * tabndx2frac; val2 = val2a + (val2b-val2a) * index_frac; interpoint = *p->xinterpoint; interpoint -= (int32_t) interpoint; /* to limit to zero to 1 range */ *p->out = val1 * (1 - interpoint) + val2 * interpoint; return OK; }
static int32_t call_system(CSOUND *csound, SYSTEM *p) { IGN(csound); if ((int32_t)*p->nowait!=0) { if ((*p->res = fork())) return OK; else { if (UNLIKELY(system((char*)p->commandLine->data)<0)) exit(1); exit(0); } } else { *p->res = (MYFLT)system((char*)p->commandLine->data); return OK; } }
int32_t serialEnd(CSOUND *csound, SERIALEND *p) { IGN(csound); #ifdef WN32 SERIAL_GLOBALS *q; q = (SERIAL_GLOBALS*) csound->QueryGlobalVariable(csound, "serialGlobals_"); if (UNLIKELY(q = NULL)) return csound->PerfError(csound, Str("Nothing to close")); CloseHandle((HANDLE)q->handles[(int32_t)p->port]); q->handles[(int32_t)*p->port] = NULL; #else close((int32_t)*p->port); #endif return OK; }
static int32_t lowprx(CSOUND *csound, LOWPRX *p) { IGN(csound); MYFLT b, k = p->k; MYFLT *ar, *asig, yn,*ynm1, *ynm2 ; MYFLT coef1 = p->coef1, coef2 = p->coef2; MYFLT *kfco = p->kfco, *kres = p->kres; uint32_t offset = p->h.insdshead->ksmps_offset; uint32_t early = p->h.insdshead->ksmps_no_end; uint32_t n, nsmps = CS_KSMPS; int32_t j; int32_t asgf = IS_ASIG_ARG(p->kfco), asgr = IS_ASIG_ARG(p->kres); ynm1 = p->ynm1; ynm2 = p->ynm2; asig = p->asig; if (UNLIKELY(offset)) memset(p->ar, '\0', offset*sizeof(MYFLT)); if (UNLIKELY(early)) { nsmps -= early; memset(&p->ar[nsmps], '\0', early*sizeof(MYFLT)); } for (j=0; j< p->loop; j++) { ar = p->ar; for (n=offset;n<nsmps;n++) { MYFLT fco = (asgf ? kfco[n] : *kfco); MYFLT res = (asgr ? kres[n] : *kres); if (p->okf != fco || p->okr != res) { /* Only if changed */ b = FL(10.0) / (res * SQRT(fco)) - FL(1.0); k = FL(1000.0) / fco; coef1 = (b+FL(2.0) * k); coef2 = FL(1.0)/(FL(1.0) + b + k); p->okf = fco; p->okr = res; /* remember to save recalculation */ } ar[n] = yn = (coef1 * ynm1[j] - k * ynm2[j] + asig[n]) * coef2; ynm2[j] = ynm1[j]; ynm1[j] = yn; } asig= p->ar; } p->k = k; p->coef1 = coef1; p->coef2 = coef2; return OK; }
int tabler_kontrol(CSOUND *csound, TABL *p) { int ndx, len = p->len; int mask = p->ftp->lenmask; IGN(csound); ndx = MYFLOOR((*p->ndx + *p->offset)*p->mul); if (p->iwrap) { if (p->np2) { while(ndx >= len) ndx -= len; while(ndx < 0) ndx += len; } else ndx &= mask; } else { if (UNLIKELY(ndx >= len)) ndx = len - 1; else if (UNLIKELY(ndx < 0)) ndx = 0; } *p->sig = p->ftp->ftable[ndx]; return OK; }
int32_t serialWrite(CSOUND *csound, SERIALWRITE *p) { IGN(csound); #ifdef WIN32 HANDLE port = get_port(csound, (int32_t)*p->port); if (UNLIKELY(port==NULL)) return NOTOK; #endif { unsigned char b = *p->toWrite; #ifndef WIN32 if (UNLIKELY(write((int32_t)*p->port, &b, 1)<0)) return NOTOK; #else int32_t nbytes; WriteFile(port, &b, 1, (PDWORD)&nbytes, NULL); #endif } return OK; }
int table3r_kontrol(CSOUND *csound, TABL *p) { int ndx, len = p->len; int mask = p->ftp->lenmask; MYFLT tmp, frac; MYFLT x0, x1, x2, x3; MYFLT *func =p->ftp->ftable; MYFLT fracub, fracsq, temp1; IGN(csound); tmp = (*p->ndx + *p->offset)*p->mul; ndx = MYFLOOR(tmp); frac = tmp - ndx; if (p->iwrap) { if (p->np2) { while(ndx >= len) ndx -= len; while(ndx < 0) ndx += len; } else ndx &= mask; } else { if (UNLIKELY(ndx >= len)) ndx = len - 1; else if (UNLIKELY(ndx < 0)) ndx = 0; } if (UNLIKELY(ndx<1 || ndx==len-1 || len <4)) { x1 = func[ndx]; x2 = func[ndx+1]; *p->sig = x1 + (x2 - x1)*frac; } else { x0 = func[ndx-1]; x1 = func[ndx]; x2 = func[ndx+1]; x3 = func[ndx+2]; fracsq = frac*frac; fracub = fracsq*x0; temp1 = x3+FL(3.0)*x1; *p->sig = x1 + FL(0.5)*fracub + frac*(x2 - fracub/FL(6.0) - temp1/FL(6.0) - x0/FL(3.0)) + frac*fracsq*(temp1/FL(6.0) - FL(0.5)*x2) + fracsq*(FL(0.5)*x2 - x1); } return OK; }
CS_VARIABLE* createAsig(void* cs, void* p) { int ksmps; CSOUND* csound = (CSOUND*)cs; IGN(p); //FIXME - this needs to take into account local ksmps, once //context work is complete // if (instr != NULL) { // OPDS* p = (OPDS*)instr; // ksmps = CS_KSMPS; // } else { ksmps = csound->ksmps; // } CS_VARIABLE* var = csound->Calloc(csound, sizeof (CS_VARIABLE)); var->memBlockSize = ksmps * sizeof (MYFLT); var->updateMemBlockSize = &updateAsigMemBlock; var->initializeVariableMemory = &varInitMemory; return var; }
void csoundFlushCircularBuffer(CSOUND *csound, void *p) { if (p == NULL) return; { int remaining; int itemsread, numelem = ((circular_buffer *)p)->numelem; int i=0, rp = ((circular_buffer *)p)->rp; //MYFLT *buffer = ((circular_buffer *)p)->buffer; IGN(csound); if ((remaining = checkspace(p, 0)) == 0) { return; } itemsread = numelem > remaining ? remaining: numelem; for (i=0; i < itemsread; i++){ rp++; if(rp == numelem) rp = 0; } ((circular_buffer *)p)->rp = rp; } }
static int32_t MinAbs_krate(CSOUND *csound, MINMAX *p) { IGN(csound); int32_t i; int32_t nargs = ((int32_t) p->INOCOUNT) - 1; MYFLT *out = p->xout; MYFLT *in1 = p->xin1; MYFLT **in2 = p->xin2toN; MYFLT min, temp; min = FABS(*in1); for (i = 0; i < nargs; ++i) { temp = FABS(in2[i][0]); if (UNLIKELY(temp < min)) min = temp; } *out = min; return OK; }
int32_t serialWrite_S(CSOUND *csound, SERIALWRITE *p) { IGN(csound); #ifdef WIN32 HANDLE port = get_port(csound, (int32_t)*p->port); if (UNLIKELY(port==NULL)) return NOTOK; #endif #ifndef WIN32 if (UNLIKELY(write((int32_t)*p->port, ((STRINGDAT*)p->toWrite)->data, ((STRINGDAT*)p->toWrite)->size))!= ((STRINGDAT*)p->toWrite)->size) /* Does Windows write behave correctly? */ return NOTOK; #else int32_t nbytes; WriteFile(port,p->toWrite, strlen((char *)p->toWrite), (PDWORD)&nbytes, NULL); #endif return OK; }
int32_t serialRead(CSOUND *csound, SERIALREAD *p) { IGN(csound); unsigned char b = 0; #ifdef WIN32 size_t bytes; HANDLE port = get_port(csound, (int32_t)*p->port); if (UNLIKELY(port==NULL)) return NOTOK; ReadFile(port, &b, 1, (PDWORD)&bytes, NULL); #else ssize_t bytes; bytes = read((int32_t)*p->port, &b, 1); #endif if (bytes > 0) *p->rChar = b; else *p->rChar = -1; return OK; }