t_int *lfnoise_perform(t_int *w){ t_lfnoise *x = (t_lfnoise *) w[1]; t_float *out = (t_float *) w[3]; int remain = (int) w[4]; t_float freq = x->m_connected ? (*(t_float *)(w[2])) : x->m_freq; float level = x->m_level; float slope = x->m_slope; float curve = x->m_curve; int32 counter = x->m_counter; if (x->ob.z_disabled) return w + 5; RGET do { if (counter<=0) { float value = x->m_nextvalue; x->m_nextvalue = frand2(s1,s2,s3); level = x->m_nextmidpt; x->m_nextmidpt = (x->m_nextvalue + value) * 0.5; counter = (int32)(x->m_sr / sc_max(freq, 0.001f)); counter = sc_max(2, counter); float fseglen = (float)counter; curve = 2.f * (x->m_nextmidpt - level - fseglen * slope) / (fseglen * fseglen + fseglen); } int nsmps = sc_min(remain, counter); remain -= nsmps; counter -= nsmps; while (nsmps--) { *out++ = level; slope += curve; level += slope; } } while (remain); x->m_level = level; x->m_slope = slope; x->m_curve = curve; x->m_counter= counter; RPUT return w + 5; }
size_t AllocPool::LargestFreeChunk() { int word = 0; for (int i=3; i>=0; --i) { if (mBinBlocks[i]) { word = i; break; } } int binBits = (int)mBinBlocks[word]; int bitPosition = NUMBITS(binBits) - 1; int index = (word << 5) + bitPosition; AllocChunkPtr bin = mBins + index; //postbuf("** %p %p %p %p\n", mBinBlocks[0], mBinBlocks[1], mBinBlocks[2], mBinBlocks[3]); //postbuf("%d %d %d %p %p %p\n", word, bitPosition, index, binBits, bin->Prev(), bin->Next()); AllocChunkPtr candidate; size_t maxsize = 0; for (candidate = bin->Prev(); candidate != bin; candidate = candidate->Prev()) { size_t candidate_size = candidate->Size(); maxsize = sc_max(maxsize, candidate_size); //postbuf(" %d %d\n", maxsize, candidate_size); } /*for (int i=0; i<kNumAllocBins; ++i) { bin = mBins + i; if (bin->Prev() != bin) { postbuf("* %d %d\n", i, bin->Prev()->Size()); } }*/ return maxsize; }
sc_arraytex_t * sc_new_arraytex(size_t width, size_t height, int flags) { int i; sc_arraytex_t *rv = sc_xalloc(sc_arraytex_t); /* because we do not support resizing of textures we have to ensure that what we create here is a power of two */ assert(sc_is_power_of_two(width)); assert(sc_is_power_of_two(height)); rv->width = width; rv->height = height; rv->slices = 0; rv->texture = NULL; /* XXX: nearest neighbour mipmap generation appears broken on windows. this might be caused by wrong mipmap levels, a memory corruption in the algorithm or unexpected pitch. */ #if SC_PLATFORM != SC_PLATFORM_WINDOWS if (flags & SC_ARRAYTEX_MIPMAPS && flags & SC_ARRAYTEX_NEAREST) rv->mipmap_levels = sc_intlog2(sc_max(width, height)) + 1; else rv->mipmap_levels = 1; #else rv->mipmap_levels = 1; #endif rv->buffers = sc_xmalloc(sizeof(sc_strbuf_t *) * rv->mipmap_levels); for (i = 0; i < rv->mipmap_levels; i++) rv->buffers[i] = sc_new_strbuf(); rv->textures = sc_new_list(); rv->flags = flags; return rv; }
void MatchingP_Ctor(MatchingP* unit) { SETCALC(MatchingP_next); // [trigger, residual, activ0, activ1,...] = MatchingP.ar(dict, in, dictsize, ntofind, hop=1, method=0) CTOR_GET_BUF // initialize the unit generator state variables. unit->m_dictsize = IN0(2); if(unit->m_dictsize != buf->channels){ printf("ERROR: (unit->m_dictsize != bufChannels)\n"); SETCALC(ClearUnitOutputs); return; } unit->m_hopspls = static_cast<int>(sc_max(0.f, sc_min(1.f, IN0(4))) * buf->frames); unit->m_shuntspls = buf->frames - unit->m_hopspls; const int ntofind = (const int)IN0(3); // UNUSED: unit->mMethod = IN0(5); unit->m_audiowritepos = unit->m_hopspls; unit->m_audioplaybackpos = 0; // audiobuf size is bufFrames + hopspls -- playback happens in first bufFrames, input is written in last hopspls, analysis is in last bufFrames unit->m_audiobuf = (float* )RTAlloc(unit->mWorld, sizeof(float) * (buf->frames + unit->m_hopspls)); Clear(buf->frames + unit->m_hopspls, unit->m_audiobuf); // "activations" will contain [index0, activ0, index1, activ1, ... ] unit->m_activations = (float* )RTAlloc(unit->mWorld, sizeof(float) * 2 * ntofind); // calculate one sample of output. unit->m_fbufnum = -9.9e9; // set it to something that will force the buffer info to be updated when _next runs MatchingP_next(unit, 1); }
void Logistic_ar::m_signal(int n, t_sample *const *in, t_sample *const *out) { t_sample *nout = *out; if(m_sr == m_paramf) { /* it might be possible to implement this without the branch */ double y1 = m_y1; double paramf = m_paramf; for (int i = 0; i!= n;++i) { (*(nout)++)= y1 = paramf * y1 * (1.0 - y1); } m_y1 = y1; } else { double y1 = m_y1; double paramf = m_paramf; int counter = m_counter; int remain = n; do { if (counter<=0) { counter = (int)(m_sr / sc_max(m_freq, .001f)); counter = sc_max(1, counter); y1 = paramf * y1 * (1.0 - y1); } int nsmps = sc_min(remain, counter); remain -= nsmps; counter -= nsmps; for (int i = 0; i!= nsmps;++i) { (*(nout)++)=y1; } } while(remain); m_y1 = y1; m_counter = counter; } }
void DoBufferColoring(World *inWorld, GraphDef *inGraphDef) { // count consumers of outputs for (uint32 j=0; j<inGraphDef->mNumUnitSpecs; ++j) { UnitSpec *unitSpec = inGraphDef->mUnitSpecs + j; for (uint32 i=0; i<unitSpec->mNumInputs; ++i) { InputSpec *inputSpec = unitSpec->mInputSpec + i; if (inputSpec->mFromUnitIndex >= 0) { UnitSpec *outUnit = inGraphDef->mUnitSpecs + inputSpec->mFromUnitIndex; OutputSpec *outputSpec = outUnit->mOutputSpec + inputSpec->mFromOutputIndex; outputSpec->mNumConsumers ++; } } } // buffer coloring { BufColorAllocator bufColor; int32 wireIndexCtr = inGraphDef->mNumConstants; // mNumConstants is a uint32, but limited to int32 in OSC for (uint32 j=0; j<inGraphDef->mNumUnitSpecs; ++j) { UnitSpec *unitSpec = inGraphDef->mUnitSpecs + j; if (unitSpec->mUnitDef->mFlags & kUnitDef_CantAliasInputsToOutputs) { // set wire index, alloc outputs AllocOutputBuffers(unitSpec, bufColor, wireIndexCtr); // set wire index, release inputs ReleaseInputBuffers(inGraphDef, unitSpec, bufColor); } else { // set wire index, release inputs ReleaseInputBuffers(inGraphDef, unitSpec, bufColor); // set wire index, alloc outputs AllocOutputBuffers(unitSpec, bufColor, wireIndexCtr); } } inGraphDef->mNumWireBufs = bufColor.NumBufs(); if (inWorld->mRunning) { // cannot reallocate interconnect buffers while running audio. if (inGraphDef->mNumWireBufs > inWorld->hw->mMaxWireBufs) { throw std::runtime_error("exceeded number of interconnect buffers."); } } else { inWorld->hw->mMaxWireBufs = sc_max(inWorld->hw->mMaxWireBufs, inGraphDef->mNumWireBufs); } } // multiply buf indices by buf length for proper offset int bufLength = inWorld->mBufLength; for (uint32 j=0; j<inGraphDef->mNumUnitSpecs; ++j) { UnitSpec *unitSpec = inGraphDef->mUnitSpecs + j; for (uint32 i=0; i<unitSpec->mNumOutputs; ++i) { OutputSpec *outputSpec = unitSpec->mOutputSpec + i; if (outputSpec->mCalcRate == calc_FullRate) { outputSpec->mBufferIndex *= bufLength; } } } }
void Dneuromodule_Ctor(Dneuromodule *unit) { SETCALC(Dneuromodule_next); unit->m_size = sc_max((int) IN0(0), 0); Neuromodule_initInputs(unit, unit->m_size); Dneuromodule_reset(unit, 0); }
void Squiz_next(Squiz *unit, int inNumSamples) { float *in = ZIN(0); float *out = ZOUT(0); float *buf = unit->m_buf; int buflen = unit->m_buflen; float ratio = sc_min(sc_max(ZIN0(1), 1.0f), (float)buflen); // pitch ratio; also === the sample-by-sample readback increment int zcperchunk = (int)ZIN0(2); int writepos = unit->m_writepos; float prevval = unit->m_prevval; // Used for checking for positivegoing zero crossings float readpos = unit->m_readpos; // Where in the buffer we're reading back from. Float value for accuracy, cast to uninterpolated int position though. int zcsofar = unit->m_zcsofar; float curval, outval; int readposi; for (int i=0; i < inNumSamples; ++i) { // First we read from the buffer readposi = (int)readpos; if(readposi >= buflen){ outval = 0.f; }else{ outval = buf[readposi]; // postincrement the play-head position readpos += ratio; } // Next we write to the buffer curval = ZXP(in); writepos++; // If positive-going zero-crossing (or if buffer full), this is a new segment. //Print("zcsofar: %i\n", zcsofar); if((writepos==buflen) || (prevval<0.f && curval>=0.f && (++zcsofar >= zcperchunk))){ writepos = 0; readpos = 0.f; zcsofar = 0; } buf[writepos] = curval; //Print("prevval: %g, curval: %g, on: %i, drop: %i, outof: %i, mode: %i\n", prevval, curval, on, drop, outof, mode); // Now output the thing we read ZXP(out) = outval; prevval = curval; } // Store state unit->m_writepos = writepos; unit->m_readpos = readpos; unit->m_prevval = prevval; unit->m_zcsofar = zcsofar; }
void LFNoise1_kr::m_set(float f) { dt = sc_max(1/f, .001f); counter = (dt/tick); float nextlevel = rgen.frand2(); m_slope = (nextlevel - m_level) / counter; m_timer.Reset(); m_timer.Delay(tick); }
void LFNoise1_ar::m_signal(int n, t_sample *const *in, t_sample *const *out) { t_sample *nout = *out; float level = m_level; int32 counter = m_counter; float slope = m_slope; RGET; int remain = n; do { if (counter<=0) { counter = (int)(m_sr / sc_max(m_freq, .001f)); counter = sc_max(1, counter); float nextlevel = frand2(s1,s2,s3); slope = (nextlevel - level) / counter; } int nsmps = sc_min(remain, counter); remain -= nsmps; counter -= nsmps; for (int i = 0; i!= nsmps;++i) { (*(nout)++)=level; level+=slope; } } while(remain); m_level = level; m_counter = counter; m_slope = slope; RPUT; }
void FFT_Ctor(FFT *unit) { int winType = sc_clip((int)ZIN0(3), -1, 1); // wintype may be used by the base ctor unit->m_wintype = winType; if(!FFTBase_Ctor(unit, 5)){ SETCALC(FFT_ClearUnitOutputs); // These zeroes are to prevent the dtor freeing things that don't exist: unit->m_inbuf = 0; unit->m_scfft = 0; return; } int audiosize = unit->m_audiosize * sizeof(float); int hopsize = (int)(sc_max(sc_min(ZIN0(2), 1.f), 0.f) * unit->m_audiosize); if (hopsize < unit->mWorld->mFullRate.mBufLength) { Print("FFT_Ctor: hopsize smaller than SC's block size (%i) - automatically corrected.\n", hopsize, unit->mWorld->mFullRate.mBufLength); hopsize = unit->mWorld->mFullRate.mBufLength; } else if (((int)(hopsize / unit->mWorld->mFullRate.mBufLength)) * unit->mWorld->mFullRate.mBufLength != hopsize) { Print("FFT_Ctor: hopsize (%i) not an exact multiple of SC's block size (%i) - automatically corrected.\n", hopsize, unit->mWorld->mFullRate.mBufLength); hopsize = ((int)(hopsize / unit->mWorld->mFullRate.mBufLength)) * unit->mWorld->mFullRate.mBufLength; } unit->m_hopsize = hopsize; unit->m_shuntsize = unit->m_audiosize - hopsize; unit->m_inbuf = (float*)RTAlloc(unit->mWorld, audiosize); SCWorld_Allocator alloc(ft, unit->mWorld); unit->m_scfft = scfft_create(unit->m_fullbufsize, unit->m_audiosize, (SCFFT_WindowFunction)unit->m_wintype, unit->m_inbuf, unit->m_fftsndbuf->data, kForward, alloc); if (!unit->m_scfft) { SETCALC(*ClearUnitOutputs); return; } memset(unit->m_inbuf, 0, audiosize); //Print("FFT_Ctor: hopsize %i, shuntsize %i, bufsize %i, wintype %i, \n", // unit->m_hopsize, unit->m_shuntsize, unit->m_bufsize, unit->m_wintype); if (INRATE(1) == calc_FullRate) { unit->m_numSamples = unit->mWorld->mFullRate.mBufLength; } else { unit->m_numSamples = 1; } SETCALC(FFT_next); }
void GlitchRHPF_next(GlitchRHPF* unit, int inNumSamples) { //printf("GlitchRHPFs_next\n"); float *out = ZOUT(0); float *in = ZIN(0); float freq = ZIN0(1); float reson = ZIN0(2); float y0; float y1 = unit->m_y1; float y2 = unit->m_y2; float a0 = unit->m_a0; float b1 = unit->m_b1; float b2 = unit->m_b2; if (freq != unit->m_freq || reson != unit->m_reson) { float qres = sc_max(0.001f, reson); float pfreq = freq * unit->mRate->mRadiansPerSample; float D = tan(pfreq * qres * 0.5f); float C = ((1.f-D)/(1.f+D)); float cosf = cos(pfreq); float next_b1 = (1.f + C) * cosf; float next_b2 = -C; float next_a0 = (1.f + C + next_b1) * .25f; //post("%g %g %g %g %g %g %g %g %g %g\n", *freq, pfreq, qres, D, C, cosf, next_b1, next_b2, next_a0, y1, y2); float a0_slope = (next_a0 - a0) * unit->mRate->mFilterSlope; float b1_slope = (next_b1 - b1) * unit->mRate->mFilterSlope; float b2_slope = (next_b2 - b2) * unit->mRate->mFilterSlope; LOOP(unit->mRate->mFilterLoops, y0 = a0 * ZXP(in) + b1 * y1 + b2 * y2; ZXP(out) = y0 - 2.f * y1 + y2; y2 = a0 * ZXP(in) + b1 * y0 + b2 * y1; ZXP(out) = y2 - 2.f * y0 + y1; y1 = a0 * ZXP(in) + b1 * y2 + b2 * y0; ZXP(out) = y1 - 2.f * y2 + y0; a0 += a0_slope; b1 += b1_slope; b2 += b2_slope; );
void calcFilterCoefficients(const double newFreq, double & a, double & a2, double & a_inv, double & b, double & b2, double & c, double & g, double & g0) { double fc = sc_max(10, newFreq) * sampleDur(); fc = sc_min(fc, 0.25); a = M_PI * fc; // PI is Nyquist frequency // a = 2 * tan(0.5*a); // dewarping, not required with 2x oversampling a_inv = 1/a; a2 = a*a; b = 2*a + 1; b2 = b*b; c = 1 / (2*a2*a2 - 4*a2*b2 + b2*b2); g0 = 2*a2*a2*c; g = g0 * bh; }
void TextVU_next_kk(TextVU *unit, int inNumSamples){ float in = IN0(1); float trig = IN0(0); float maxinepoch = unit->m_maxinepoch; size_t maxever = unit->m_maxever; size_t width = unit->m_width; float* cutoffs = unit->m_cutoffs; char* vustring = unit->m_vustring; if(IN0(3)){ maxinepoch = 0.f; maxever = 0; } maxinepoch = sc_max(maxinepoch, in); if((unit->m_trig <= 0.f) && (trig > 0.f)){ if(unit->m_mayprint){ // convert the input value to a meter position, using the precalced thresholds size_t index = 0; for(size_t i=0; i<width; ++i){ if(maxinepoch < cutoffs[i]){ if(maxever==i){ vustring[i] = '|'; }else{ vustring[i] = '-'; } }else{ index = i; // this will get pushed up until it stops at the peak vustring[i] = 'X'; } } // note the best ever if(index > maxever){ maxever = index; } Print("%s: %s\n", unit->m_id_string, vustring); maxinepoch = 0.f; } } unit->m_trig = trig; unit->m_maxever = maxever; }
void FFT_Ctor(FFT *unit) { unit->m_wintype = (int)ZIN0(3); // wintype may be used by the base ctor if(!FFTBase_Ctor(unit, 5)){ SETCALC(FFT_ClearUnitOutputs); // These zeroes are to prevent the dtor freeing things that don't exist: unit->m_inbuf = 0; unit->m_transformbuf = 0; unit->m_scfft = 0; return; } int fullbufsize = unit->m_fullbufsize * sizeof(float); int audiosize = unit->m_audiosize * sizeof(float); int hopsize = (int)(sc_max(sc_min(ZIN0(2), 1.f), 0.f) * unit->m_audiosize); if (((int)(hopsize / unit->mWorld->mFullRate.mBufLength)) * unit->mWorld->mFullRate.mBufLength != hopsize) { Print("FFT_Ctor: hopsize (%i) not an exact multiple of SC's block size (%i) - automatically corrected.\n", hopsize, unit->mWorld->mFullRate.mBufLength); hopsize = ((int)(hopsize / unit->mWorld->mFullRate.mBufLength)) * unit->mWorld->mFullRate.mBufLength; } unit->m_hopsize = hopsize; unit->m_shuntsize = unit->m_audiosize - hopsize; unit->m_inbuf = (float*)RTAlloc(unit->mWorld, audiosize); unit->m_transformbuf = (float*)RTAlloc(unit->mWorld, scfft_trbufsize(unit->m_fullbufsize)); unit->m_scfft = (scfft*)RTAlloc(unit->mWorld, sizeof(scfft)); scfft_create(unit->m_scfft, unit->m_fullbufsize, unit->m_audiosize, unit->m_wintype, unit->m_inbuf, unit->m_fftsndbuf->data, unit->m_transformbuf, true); memset(unit->m_inbuf, 0, audiosize); //Print("FFT_Ctor: hopsize %i, shuntsize %i, bufsize %i, wintype %i, \n", // unit->m_hopsize, unit->m_shuntsize, unit->m_bufsize, unit->m_wintype); if (INRATE(1) == calc_FullRate) { unit->m_numSamples = unit->mWorld->mFullRate.mBufLength; } else { unit->m_numSamples = 1; } SETCALC(FFT_next); }
void RedPhasor_next_kk(RedPhasor *unit, int inNumSamples) { float *out= ZOUT(0); float in= ZIN0(0); float rate= sc_max(0, ZIN0(1)); double start= ZIN0(2); double end= ZIN0(3); float loop= ZIN0(4); float previn= unit->m_previn; double level= unit->mLevel; if((previn<=0.f)&&(in>0.f)) { level= start; } if(loop<=0.f) { //kk off if(end<start) { LOOP(inNumSamples, ZXP(out)= level; level-= rate; level= sc_clip(level, end, start); ); } else {
//calculation function once FFT data ready void Loudness_dofft(Loudness *unit, uint32 ibufnum) { World *world = unit->mWorld; //if (ibufnum >= world->mNumSndBufs) ibufnum = 0; SndBuf *buf; // = world->mSndBufs + ibufnum; //int numbins = buf->samples - 2 >> 1; //support LocalBuf if (ibufnum >= world->mNumSndBufs) { int localBufNum = ibufnum - world->mNumSndBufs; Graph *parent = unit->mParent; if(localBufNum <= parent->localBufNum) { buf = parent->mLocalSndBufs + localBufNum; } else { buf = world->mSndBufs; } } else { buf = world->mSndBufs + ibufnum; } LOCK_SNDBUF(buf); float * data= buf->data; float loudsum=0.0; float smask= ZIN0(1); float tmask= ZIN0(2); for (int k=0; k<unit->m_numbands; ++k){ int bandstart=eqlbandbins[k]; //int bandend=eqlbandbins[k+1]; int bandsize= eqlbandsizes[k]; int bandend= bandstart+bandsize; float bsum=0.0; float real, imag, power; int index; float lastpower=0.0; for (int h=bandstart; h<bandend;++h) { index = 2*h; real= data[index]; imag= data[index+1]; power = (real*real) + (imag*imag); //would involve spectral masking here power = sc_max(lastpower*smask,power); //sideways spectral masking with leaky integration lastpower= power; //psychophysical sensation; within critical band, sum using a p metric, (sum m^p)^(1/p) //compresses the gain //power of three combination //bsum= bsum+(power*power*power); //won't sum up power very well //if(power>bsum) bsum=power; bsum= bsum+power; } //store recips of bandsizes? //why average? surely just take max or sum is better! //bsum= bsum/bandsize; //into dB, avoid log of 0 //float db= 10*log10((bsum*10000000)+0.001); //float db= 10*log10((bsum*32382)+0.001); //empricially derived 32382*2.348 float db= 10*log10((bsum*76032.936f)+0.001f); //correct multipler until you get loudness output of 1! //correcting for power of three combination //bsum=bsum+0.001; //4.8810017610244 = log10(76032.936) //float db= 10*((0.33334*log10(bsum)) + 4.8810017610244); //correct multipler until you get loudness output of 1! //printf("bsum %f db %f \n",bsum,db); //convert via contour if(db<contours[k][0]) db=0; else if (db>contours[k][10]) db=phons[10]; else { float prop=0.0; int j; for (j=1; j<11; ++j) { if(db<contours[k][j]) { prop= (db-contours[k][j-1])/(contours[k][j]-contours[k][j-1]); break; } if(j==10) prop=1.0; } db= (1.f-prop)*phons[j-1]+ prop*phons[j]; //printf("prop %f db %f j %d\n",prop,db,j); } //spectralmasking, 6dB drop per frame? //try also with just take db unit->m_ERBbands[k] = sc_max(db, (unit->m_ERBbands[k]) - tmask); //printf("db %f erbband %f \n",db, unit->m_ERBbands[k]); //must sum as intensities, not dbs once corrected, pow used to be other way around //loudsum+= ((pow(10, 0.1*unit->m_ERBbands[k])-0.001)*0.0000308813538386); // loudsum+= ((pow(10, 0.1*unit->m_ERBbands[k])-0.001)); //multiplier not needed since invert below; can trust no overflow? } //total excitation, correct back to dB scale in phons //float phontotal= 10*log10((loudsum*32382)+0.001); float phontotal= 10*log10((loudsum)+0.001); //didn't use divisor above, so no need to restore here //unit->m_phontotal= phontotal; //now to sones: /* from Praat: Excitation.c Sones = 2 ** ((Phones - 40) / 10) */ unit->m_sones= pow (2.f, (phontotal - 40) / 10); //printf("phontotal %f sones %f \n",phontotal, unit->m_sones); //about 5 times per second //if((unit->m_triggerid) && ((unit->m_frame%2==0))) SendTrigger(&unit->mParent->mNode, unit->m_triggerid, bestkey); }
void mod_on_help_unsigned(small_type &us, int unb, int und, sc_digit *ud, int /* vnb */, int vnd, const sc_digit *vd) { #define COPY_DIGITS copy_digits_unsigned { // Body of mod_on_help int old_und = und; und = vec_skip_leading_zeros(und, ud); vnd = vec_skip_leading_zeros(vnd, vd); int cmp_res = vec_cmp(und, ud, vnd, vd); // u < v => u % v = u - case 4 if (cmp_res < 0) return; // u = v => u % v = 0 - case 3 if (cmp_res == 0) { us = SC_ZERO; vec_zero(old_und, ud); return; } // else if u > v - case 5 sc_digit vd0 = (*vd); if ((vnd == 1) && (vd0 == 1)) { us = SC_ZERO; vec_zero(old_und, ud); return; } // One extra digit for d is allocated to simplify vec_div_*(). int nd = sc_max(und, vnd) + 1; #ifdef SC_MAX_NBITS sc_digit d[MAX_NDIGITS + 1]; #else sc_digit *d = new sc_digit[nd]; #endif vec_zero(nd, d); if ((vnd == 1) && (und == 1)) d[0] = (*ud) % vd0; if ((vnd == 1) && (vd0 < HALF_DIGIT_RADIX)) d[0] = vec_rem_small(und, ud, vd0); else vec_rem_large(und, ud, vnd, vd, d); us = check_for_zero(us, nd - 1, d); if (us == SC_ZERO) vec_zero(old_und, ud); else COPY_DIGITS(us, unb, old_und, ud, sc_min(unb, vnd), nd - 1, d); #ifndef SC_MAX_NBITS delete [] d; #endif } #undef COPY_DIGITS }
void LotkaVolterra_next(LotkaVolterra *unit, int inNumSamples) { float *xout = ZOUT(0); float *yout = ZOUT(1); float freq = ZIN0(0); double a = ZIN0(1); double b = ZIN0(2); double c = ZIN0(3); double d = ZIN0(4); double h = ZIN0(5); double x0 = ZIN0(6); double y0 = ZIN0(7); double xn = unit->xn; double yn = unit->yn; float counter = unit->counter; double xnm1 = unit->xnm1; double ynm1 = unit->ynm1; double frac = unit->frac; float samplesPerCycle; double slope; if(freq < unit->mRate->mSampleRate){ samplesPerCycle = unit->mRate->mSampleRate / sc_max(freq, 0.001f); slope = 1.f / samplesPerCycle; } else { samplesPerCycle = 1.f; slope = 1.f; } // reset if start values change if((unit->x0 != x0) || (unit->y0 != y0)){ xnm1 = xn; ynm1 = yn; unit->x0 = xn = x0; unit->y0 = yn = y0; } double dx = xn - xnm1; double dy = yn - ynm1; for (int i=0; i<inNumSamples; ++i) { if(counter >= samplesPerCycle){ counter -= samplesPerCycle; frac = 0.f; xnm1 = xn; ynm1 = yn; double k1x, k2x, k3x, k4x, k1y, k2y, k3y, k4y, kxHalf, kyHalf; // 4th order Runge-Kutta approximate solution for differential equations k1x = h * (xnm1 * (a - b * ynm1)); k1y = h * (ynm1 * (c * xnm1 - d)); kxHalf = k1x * 0.5; kyHalf = k1y * 0.5; k2x = h * ((xnm1 + kxHalf) * (a - b * (ynm1 + kyHalf))); k2y = h * ((ynm1 + kyHalf) * (c * (xnm1 + kxHalf) - d)); kxHalf = k2x * 0.5; kyHalf = k2y * 0.5; k3x = h * ((xnm1 + kxHalf) * (a - b * (ynm1 + kyHalf))); k3y = h * ((ynm1 + kyHalf) * (c * (xnm1 + kxHalf) - d)); k4x = h * ((xnm1 + k3x) * (a - b * (ynm1 + k3y))); k4y = h * ((ynm1 + k3y) * (c * (xnm1 + k3x) - d)); xn = xn + (k1x + 2.0*(k2x + k3x) + k4x) * ONESIXTH; yn = yn + (k1y + 2.0*(k2y + k3y) + k4y) * ONESIXTH; dx = xn - xnm1; dy = yn - ynm1; } counter++; ZXP(xout) = (xnm1 + dx * frac) * 0.5f; ZXP(yout) = (ynm1 + dy * frac) * 0.5f; frac += slope; } unit->xn = xn; unit->yn = yn; unit->counter = counter; unit->xnm1 = xnm1; unit->ynm1 = ynm1; unit->frac = frac; }
void* SC_TcpClientPort::Run() { OSC_Packet *packet = 0; int32 size; int32 msglen; int cmdfd = mCmdFifo[0]; int sockfd = mSocket; int nfds = sc_max(cmdfd, sockfd) + 1; bool cmdClose = false; pthread_detach(mThread); while (true) { fd_set rfds; FD_ZERO(&rfds); FD_SET(cmdfd, &rfds); FD_SET(sockfd, &rfds); if ((select(nfds, &rfds, 0, 0, 0) == -1) || (cmdClose = FD_ISSET(cmdfd, &rfds))) goto leave; if (!FD_ISSET(sockfd, &rfds)) continue; packet = (OSC_Packet*)malloc(sizeof(OSC_Packet)); if (!packet) goto leave; packet->mData = 0; size = recvall(sockfd, &msglen, sizeof(int32)); if (size < (int32)sizeof(int32)) goto leave; // msglen is in network byte order msglen = ntohl(msglen); packet->mData = (char*)malloc(msglen); if (!packet->mData) goto leave; size = recvall(sockfd, packet->mData, msglen); if (size < msglen) goto leave; memcpy(&packet->mReplyAddr.mSockAddr, &mReplySockAddr, sizeof(mReplySockAddr)); packet->mReplyAddr.mSockAddrLen = sizeof(mReplySockAddr); packet->mReplyAddr.mSocket = sockfd; packet->mReplyAddr.mReplyFunc = tcp_reply_func; packet->mSize = msglen; ProcessOSCPacket(packet); packet = 0; } leave: if (packet) { free(packet->mData); free(packet); } // Only call notify function when not closed explicitly if (!cmdClose && mClientNotifyFunc) { (*mClientNotifyFunc)(mClientData); } delete this; return 0; }
void RosslerL_next(RosslerL *unit, int inNumSamples) { float *xout = ZOUT(0); float *yout = ZOUT(1); float *zout = ZOUT(2); float freq = ZIN0(0); double a = ZIN0(1); double b = ZIN0(2); double c = ZIN0(3); double h = ZIN0(4); double x0 = ZIN0(5); double y0 = ZIN0(6); double z0 = ZIN0(7); double xn = unit->xn; double yn = unit->yn; double zn = unit->zn; float counter = unit->counter; double xnm1 = unit->xnm1; double ynm1 = unit->ynm1; double znm1 = unit->znm1; double frac = unit->frac; float samplesPerCycle; double slope; if(freq < unit->mRate->mSampleRate){ samplesPerCycle = unit->mRate->mSampleRate / sc_max(freq, 0.001f); slope = 1.f / samplesPerCycle; } else { samplesPerCycle = 1.f; slope = 1.f; } if((unit->x0 != x0) || (unit->y0 != y0) || (unit->z0 != z0)){ xnm1 = xn; ynm1 = yn; znm1 = zn; unit->x0 = xn = x0; unit->y0 = yn = y0; unit->z0 = zn = z0; } double dx = xn - xnm1; double dy = yn - ynm1; double dz = zn - znm1; for (int i=0; i<inNumSamples; ++i) { if(counter >= samplesPerCycle){ counter -= samplesPerCycle; frac = 0.f; xnm1 = xn; ynm1 = yn; znm1 = zn; double k1x, k2x, k3x, k4x, k1y, k2y, k3y, k4y, k1z, k2z, k3z, k4z, kxHalf, kyHalf, kzHalf; // 4th order Runge-Kutta approximate solution for differential equations k1x = - (h * (ynm1 + znm1)); k1y = h * (xnm1 + a * ynm1); k1z = h * (b + znm1 * (xnm1 - c)); kxHalf = k1x * 0.5; kyHalf = k1y * 0.5; kzHalf = k1z * 0.5; k2x = - (h * (ynm1 + kyHalf + znm1 + kzHalf)); k2y = h * (xnm1 + kxHalf + a * (ynm1 + kyHalf)); k2z = h * (b + (znm1 + kzHalf) * (xnm1 + kxHalf - c)); kxHalf = k2x * 0.5; kyHalf = k2y * 0.5; kzHalf = k2z * 0.5; k3x = - (h * (ynm1 + kyHalf + znm1 + kzHalf)); k3y = h * (xnm1 + kxHalf + a * (ynm1 + kyHalf)); k3z = h * (b + (znm1 + kzHalf) * (xnm1 + kxHalf - c)); k4x = - (h * (ynm1 + k3y + znm1 + k3z)); k4y = h * (xnm1 + k3x + a * (ynm1 + k3y)); k4z = h * (b + (znm1 + k3z) * (xnm1 + k3x - c)); xn = xn + (k1x + 2.0*(k2x + k3x) + k4x) * ONESIXTH; yn = yn + (k1y + 2.0*(k2y + k3y) + k4y) * ONESIXTH; zn = zn + (k1z + 2.0*(k2z + k3z) + k4z) * ONESIXTH; dx = xn - xnm1; dy = yn - ynm1; dz = zn - znm1; } counter++; ZXP(xout) = (xnm1 + dx * frac) * 0.5f; ZXP(yout) = (ynm1 + dy * frac) * 0.5f; ZXP(zout) = (znm1 + dz * frac) * 1.0f; frac += slope; } unit->xn = xn; unit->yn = yn; unit->zn = zn; unit->counter = counter; unit->xnm1 = xnm1; unit->ynm1 = ynm1; unit->znm1 = znm1; unit->frac = frac; }
void ArneodoCoulletTresser_next(ArneodoCoulletTresser *unit, int inNumSamples) { float *xout = ZOUT(0); float *yout = ZOUT(1); float *zout = ZOUT(2); float freq = ZIN0(0); double alpha = ZIN0(1); double h = ZIN0(2); double x0 = ZIN0(3); double y0 = ZIN0(4); double z0 = ZIN0(5); double xn = unit->xn; double yn = unit->yn; double zn = unit->zn; float counter = unit->counter; double xnm1 = unit->xnm1; double ynm1 = unit->ynm1; double znm1 = unit->znm1; double frac = unit->frac; float samplesPerCycle; double slope; if(freq < unit->mRate->mSampleRate){ samplesPerCycle = unit->mRate->mSampleRate / sc_max(freq, 0.001f); slope = 1.f / samplesPerCycle; } else { samplesPerCycle = 1.f; slope = 1.f; } // reset if start values change if((unit->x0 != x0) || (unit->y0 != y0) || (unit->z0 != z0)){ xnm1 = xn; ynm1 = yn; znm1 = zn; unit->x0 = xn = x0; unit->y0 = yn = y0; unit->z0 = zn = z0; } double dx = xn - xnm1; double dy = yn - ynm1; double dz = zn - znm1; for (int i=0; i<inNumSamples; ++i) { if(counter >= samplesPerCycle){ counter -= samplesPerCycle; frac = 0.f; xnm1 = xn; ynm1 = yn; znm1 = zn; double k1x, k2x, k3x, k4x, k1y, k2y, k3y, k4y, k1z, k2z, k3z, k4z, kxHalf, kyHalf, kzHalf; // 4th order Runge-Kutta approximate solution for differential equations k1x = h * (xnm1 * (1.1 - xnm1 / 2 - ynm1 / 2 - znm1 / 10)); k1y = h * (ynm1 * (-0.5 + xnm1 / 2 + ynm1 / 10 - znm1 / 10)); k1z = h * (znm1 * (alpha + 0.2 - alpha * xnm1 - ynm1 / 10 - znm1 / 10)); kxHalf = k1x * 0.5; kyHalf = k1y * 0.5; kzHalf = k1z * 0.5; k2x = h * ((xnm1 + kxHalf) * (1.1 - (xnm1 + kxHalf) / 2 - (ynm1 + kyHalf) / 2 - (znm1 + kzHalf) / 10)); k2y = h * ((ynm1 + kyHalf) * (-0.5 + (xnm1 + kxHalf) / 2 + (ynm1 + kyHalf) / 10 - (znm1 + kzHalf) / 10)); k2z = h * ((znm1 + kzHalf) * (alpha + 0.2 - alpha * (xnm1 + kxHalf) - (ynm1 + kyHalf) / 10 - (znm1 + kzHalf) / 10)); kxHalf = k2x * 0.5; kyHalf = k2y * 0.5; kzHalf = k2z * 0.5; k3x = h * ((xnm1 + kxHalf) * (1.1 - (xnm1 + kxHalf) / 2 - (ynm1 + kyHalf) / 2 - (znm1 + kzHalf) / 10)); k3y = h * ((ynm1 + kyHalf) * (-0.5 + (xnm1 + kxHalf) / 2 + (ynm1 + kyHalf) / 10 - (znm1 + kzHalf) / 10)); k3z = h * ((znm1 + kzHalf) * (alpha + 0.2 - alpha * (xnm1 + kxHalf) - (ynm1 + kyHalf) / 10 - (znm1 + kzHalf) / 10)); k4x = h * ((xnm1 + k3x) * (1.1 - (xnm1 + k3x) / 2 - (ynm1 + k3y) / 2 - (znm1 + k3z) / 10)); k4y = h * ((ynm1 + k3y) * (-0.5 + (xnm1 + k3x) / 2 + (ynm1 + k3y) / 10 - (znm1 + k3z) / 10)); k4z = h * ((znm1 + k3z) * (alpha + 0.2 - alpha * (xnm1 + k3x) - (ynm1 + k3y) / 10 - (znm1 + k3z) / 10)); xn = xn + (k1x + 2.0*(k2x + k3x) + k4x) * ONESIXTH; yn = yn + (k1y + 2.0*(k2y + k3y) + k4y) * ONESIXTH; zn = zn + (k1z + 2.0*(k2z + k3z) + k4z) * ONESIXTH; dx = xn - xnm1; dy = yn - ynm1; dz = zn - znm1; } counter++; ZXP(xout) = (xnm1 + dx * frac) * 0.5f; ZXP(yout) = (ynm1 + dy * frac) * 0.5f; ZXP(zout) = (znm1 + dz * frac) * 1.0f; frac += slope; } unit->xn = xn; unit->yn = yn; unit->zn = zn; unit->counter = counter; unit->xnm1 = xnm1; unit->ynm1 = ynm1; unit->znm1 = znm1; unit->frac = frac; }
void Dfsm_next(Dfsm *unit, int inNumSamples) { int current_state_offset, state_offset; int choice; int index_index; int next_index; float outval; // // printf("\n\n\n ------- \n"); ////////////////////// reset ///////////////////////// // current_state is the internal state, including exit / enty pair. // i.e. first rule is pair [exit, entrance] if (!inNumSamples) { Dfsm_reset(unit); // printf("resetting\n"); } // get some member state state_offset = unit->m_state_offset; current_state_offset = unit->m_current_state_offset; ////////////////////// embedding mode ///////////////////////// if(unit->m_count > 0.f) { outval = DEMANDINPUT_A(current_state_offset, inNumSamples); // this will have to switch behaviour, depending on slotRepeats if(sc_isnan(outval)) { if(unit->m_end) { // exit state last value. end. OUT0(0) = NAN; unit->m_end = 0; unit->m_count = 0.f; // // printf("output: NAN to end stream\n"); return; } else { // other state last value // // printf("(1) resetting input %d\n", current_state_offset); RESETINPUT(current_state_offset); }; } else { // embed current value of current state // // printf("outval: %f\n", outval); OUT0(0) = outval; unit->m_count --; return; } } ////////////////////// init count ///////////////////////// // get new count unit->m_count = DEMANDINPUT_A(0, inNumSamples) - 1.f; // offset: first value is embedded below. if(sc_isnan(unit->m_count)) { // terminate and reset RESETINPUT(0); OUT0(0) = NAN; unit->m_end = 0; unit->m_count = 0.f; // // printf("output: NAN to end stream\n"); return; }; ////////////////////// finding next state ///////////////////////// if(unit->m_current_state >= unit->m_num_states) { unit->m_current_state_offset = state_offset; outval = DEMANDINPUT_A(unit->m_current_state_offset, inNumSamples); // get first state, which is the packed termination state OUT0(0) = outval; // // printf("going to exit state (1): %d end\n", unit->m_current_state); // // printf("output: %f\n", outval); unit->m_end = 1; return; } // how many nextstates ? float size = (float) unit->m_nextstate_sizes[unit->m_current_state]; // get random value and generate random offset (0..size) float rand = DEMANDINPUT_A(1, inNumSamples); choice = (int) sc_max(0.f, rand * size - 0.5f); // look up the nextstate index index_index = unit->m_nextstate_indices[unit->m_current_state] + choice; // we'll need to limit this /0..1/ // get the next state index from the input, add one for offset: first rule is pair [exit, entrance] next_index = IN0(index_index) + 1; unit->m_current_state= next_index; current_state_offset = state_offset + next_index; // exit if(next_index >= unit->m_num_states) { current_state_offset = state_offset; // get first state, which is the packed termination state // // printf("going to exit state (2): %d end\n", next_index); unit->m_end = 1; } // get first value outval = DEMANDINPUT_A(current_state_offset, inNumSamples); if(sc_isnan(outval)) { // // printf("(1) resetting input %d\n", current_state_offset); if(unit->m_end) { outval = NAN; } else { RESETINPUT(current_state_offset); outval = DEMANDINPUT_A(current_state_offset, inNumSamples); } } OUT0(0) = outval; // set member state unit->m_current_state_offset = current_state_offset; // printf("indexindex: %d choice: %d, previndex: %d nextindex: %d outval index: %d\n", index_index, choice, unit->m_current_state, next_index, state_offset + next_index); // // printf("outval: %f\n", outval); }
void PyrGC::Collect(int32 inNumToScan) { mNumToScan = sc_max(mNumToScan, inNumToScan); Collect(); // collect space }
//calculation function once FFT data ready void KeyTrack_calculatekey(KeyTrack *unit, uint32 ibufnum) { World *world = unit->mWorld; SndBuf *buf; if (ibufnum >= world->mNumSndBufs) { int localBufNum = ibufnum - world->mNumSndBufs; Graph *parent = unit->mParent; if(localBufNum <= parent->localBufNum) { buf = parent->mLocalSndBufs + localBufNum; } else { buf = world->mSndBufs; if(unit->mWorld->mVerbosity > -1){ Print("KeyTrack error: Buffer number overrun: %i\n", ibufnum); } } } else { buf = world->mSndBufs + ibufnum; } LOCK_SNDBUF(buf); int numbins = buf->samples - 2 >> 1; //assumed in this representation SCComplexBuf *p = ToComplexApx(buf); const float * data= buf->data; //memcpy(unit->m_FFTBuf, data, NOVER2); //to hold powers float * fftbuf= unit->m_FFTBuf; //get powers for bins //don't need to calculate past half Nyquist, because no indices involved of harmonics above 10000 Hz or so (see index data at top of file) for (int i=0; i<NOVER2; i+=2) { //i>>1 is i/2 fftbuf[i>>1] = ((data[i] * data[i]) + (data[i+1] * data[i+1])); } float * chroma= unit->m_chroma; float sum; int indexbase, index; //experimental; added leaky integration on each note; also, only add to sum if harmonic, ie not a transient float * weights = unit->m_weights; int * bins = unit->m_bins; float chromaleak= ZIN0(2); //zero for new round (should add leaky integrator here! for (int i=0;i<12;++i) chroma[i] *= chromaleak; for (int i=0;i<60;++i) { int chromaindex = (i+9)%12; //starts at A1 up to G#6 sum=0.0; indexbase= 12*i; //6 partials, 2 of each //transient sum, setting up last values too float phasesum=0.0; for(int j=0;j<12;++j) { //12 if 144 data points index=indexbase+j; //experimental transient detection code, not reliable //int binindex= unit->m_bins[index]-1; //SCPolar binnow= p->bin[binindex].ToPolarApx(); //float phaseadvance= (binindex+1)*(TWOPI*0.5); //k * (512/44100) * (44100/1024) //convert bin number to frequency //float power= binnow.mag * binnow.mag; //(p->bin[binindex].real)*(p->bin[binindex].real) + (p->bin[binindex].imag)*(p->bin[binindex].imag); //(p->bin[binindex].mag); //power *= power; //int phaseindex= indexbase+j; //float phasenow= binnow.phase; //0.0; //(p->bin[binindex].phase); //float prevphase = fmod(unit->m_prevphase[index]+phaseadvance,TWOPI); //float a,b,tmp; //a=phasenow; b=prevphase; //b=phasenow; a=prevphase; //if(b<a) {b= b+TWOPI;} //float phasechange = sc_min(b-a,a+TWOPI-b); //more complicated, need mod 2pi and to know lower and upper //phasesum+= phasechange; //unit->m_prevphase[index]= phasenow; //((p->bin[index-1].mag) * (p->bin[index-1].mag)) //printf("comparison %f %f \n",fftbuf[g_bins2[index]], power); //sum+= (unit->m_weights[index])* power; sum+= (weights[index])* (fftbuf[bins[index]]); } //transient test here too? //if(phasesum>(5*PI)){sum=0.0;} //if((i>5) && (i<15)) //printf("test phasesum %f \n", phasesum); //unit->m_leaknote[i] = (0.8*unit->m_leaknote[i]) + sum; chroma[chromaindex]+= sum; //unit->m_leaknote[i]; //sum; } float* key = unit->m_key; //major for (int i=0;i<12;++i) { sum=0.0; for (int j=0;j<7;++j) { indexbase=g_major[j]; index=(i+indexbase)%12; //sum+=(chroma[index]*g_kkmajor[indexbase]); sum+=(chroma[index]*g_diatonicmajor[indexbase]); } key[i]=sum; //10*log10(sum+1); } //minor for (int i=0;i<12;++i) { sum=0.0; for (int j=0;j<7;++j) { indexbase=g_minor[j]; index=(i+indexbase)%12; //sum+=(chroma[index]*g_kkminor[indexbase]); sum+=(chroma[index]*g_diatonicminor[indexbase]); } key[12+i]=sum; } float keyleak= ZIN0(1); //fade parameter to 0.01 for histogram in seconds, convert to FFT frames //keyleak in seconds, convert to drop time in FFT hop frames (FRAMEPERIOD) keyleak= sc_max(0.001f,keyleak/unit->m_frameperiod); //FRAMEPERIOD; //now number of frames, actual leak param is decay exponent to reach 0.01 in x seconds, ie 0.01 = leakparam ** (x/ffthopsize) //0.01 is -40dB keyleak= pow(0.01f,(1.f/keyleak)); float * histogram= unit->m_histogram; int bestkey=0; float bestscore=0.0; for (int i=0;i<24;++i) { histogram[i]= (keyleak*histogram[i])+key[i]; if(histogram[i]>bestscore) { bestscore=histogram[i]; bestkey=i; } //printf("%f ",histogram[i]); } //should find secondbest and only swap if win by a margin //printf(" best %d \n\n",bestkey); //what is winning currently? find max in histogram unit->m_currentKey=bestkey; //about 5 times per second //if((unit->m_triggerid) && ((unit->m_frame%2==0))) SendTrigger(&unit->mParent->mNode, unit->m_triggerid, bestkey); }
void div_on_help_unsigned(small_type &us, int unb, int und, sc_digit *ud, int vnb, int vnd, const sc_digit *vd) { #define CONVERT_SM_to_2C_to_SM convert_unsigned_SM_to_2C_to_SM #define COPY_DIGITS copy_digits_unsigned { // Body of div_on_help int old_und = und; und = vec_skip_leading_zeros(und, ud); vnd = vec_skip_leading_zeros(vnd, vd); int cmp_res = vec_cmp(und, ud, vnd, vd); if (cmp_res < 0) { // u < v => u / v = 0 - case 4 us = SC_ZERO; vec_zero(old_und, ud); return; } sc_digit vd0 = (*vd); if ((cmp_res > 0) && (vnd == 1) && (vd0 == 1)) { us = CONVERT_SM_to_2C_to_SM(us, unb, old_und, ud); return; } // One extra digit for d is allocated to simplify vec_div_*(). int nd = sc_max(und, vnd) + 1; #ifdef SC_MAX_NBITS sc_digit d[MAX_NDIGITS + 1]; #else sc_digit *d = new sc_digit[nd]; #endif vec_zero(nd, d); // u = v => u / v = 1 - case 3 if (cmp_res == 0) d[0] = 1; else if ((vnd == 1) && (und == 1)) d[0] = (*ud) / vd0; else if ((vnd == 1) && (vd0 < HALF_DIGIT_RADIX)) vec_div_small(und, ud, vd0, d); else vec_div_large(und, ud, vnd, vd, d); COPY_DIGITS(us, unb, old_und, ud, sc_max(unb, vnb), nd - 1, d); #ifndef SC_MAX_NBITS delete [] d; #endif } #undef COPY_DIGITS #undef CONVERT_SM_to_2C_to_SM }