View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide

commented version of a demand rate ugen function (C++)



void Dseries_next(Dseries *unit, int inNumSamples)
{
	if (inNumSamples) { // inNumSamples == 0 -> reset from demand ugen
		if (unit->m_repeats < 0) {	// unit->m_repeats < 0 -> unit recieved a reset last time
			float x = DEMANDINPUT(0);
			unit->m_repeats = sc_isnan(x) ? 0 : (int32)floor(x + 0.5f); // test for NaN:
								// if it is a NaN, this will end (repeats = 0)
								// if not, repeats input (x) is increased by 0.5 (make float compat)
			unit->m_value = DEMANDINPUT(1);
			unit->m_step = DEMANDINPUT(2);
		}
		if (unit->m_repeatCount >= unit->m_repeats) { // -> count exceeds repeats
			OUT0(0) = NAN;	// signal end by putting out a NaN.
							// users will know by this that they use their last input ad infinitum
			return;
		}
		OUT0(0) = unit->m_value;
		unit->m_value += unit->m_step; // increase value by step
		unit->m_repeatCount++;
	} else {
		unit->m_repeats = -1; // trigger the reset
		unit->m_repeatCount = 0; // set count to 0 again
	}
}


void Drand_next(Drand *unit, int inNumSamples)
{
	if (inNumSamples) { // inNumSamples == 0 -> reset from demand ugen
		
		if (unit->m_repeats < 0) { // unit->m_repeats < 0 -> unit recieved a reset last time
			float x = DEMANDINPUT(0);
			unit->m_repeats = sc_isnan(x) ? 0 : (int32)floor(x + 0.5f); // see above
		}
		while (true) {
			if (unit->m_repeatCount >= unit->m_repeats) {  // -> count exceeds repeats
				OUT0(0) = NAN; // signal end by putting out a NaN.
							// users will know by this that they use their last input ad infinitum
				return;
			}
			
			// m_index is the current index into the array
			
			if (ISDEMANDINPUT(unit->m_index)) { // check if demand ugen or normal ugen
				if (unit->m_needToResetChild) { // e.g. when arriving new at the index
					unit->m_needToResetChild = false;
					RESETINPUT(unit->m_index); // reset the unit at current index
				}
				float x = DEMANDINPUT(unit->m_index); // get next value at index
				if (sc_isnan(x)) {	// input unit ended
					// generate new index
					unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
					// increase repeat count
					unit->m_repeatCount++;
					unit->m_needToResetChild = true; // don't forget to reset the input next time
				} else {
					OUT0(0) = x;	// if not ended, output x
					return;
				}
			} else { // if normal ugen
				OUT0(0) = IN0(unit->m_index); // get first slot of ugen output
				// act as if ugen ended.
				unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1;
				unit->m_repeatCount++;
				unit->m_needToResetChild = true;
				return;
			}
		}
	} else { // reset part:
		unit->m_repeats = -1;
		unit->m_repeatCount = 0;
		unit->m_needToResetChild = true;
		// generate rand index
		unit->m_index = unit->mParent->mRGen->irand(unit->mNumInputs - 1) + 1; 	}
}




Links to this Page