Exemplo n.º 1
0
/* Return a value within a range close to a preferred value.
   tightness values:
      0     adheres to the extreme furthest from mid
      1     even distribution
      2+    hugs mid more and more with increasing values of tight
      no negative allowed
*/
static double
prob(double low, double mid, double high, double tight)
{
   int repeat;
   double lowrange, hirange, range, num, tightrand;

   if (tight < 0.0)
      tight = 0.0;

   lowrange = mid - low;
   hirange = high - mid;
   range = MAX(lowrange, hirange);

   repeat = 0;
   do {
      tightrand = pow((rrand() + 1.0) * 0.5, tight);

      if (rrand() > 0.0)
         num = mid + (tightrand * range);
      else
         num = mid - (tightrand * range);

      if (num < low || num > high)
         repeat++;
      else
         repeat = 0;
   } while (repeat > 0);

   return num;
}
Exemplo n.º 2
0
int
main(int argc, char *argv[])
{
	RTcmix *rrr;
	int i;
	double start, dur, pchval;
	double pitches[14] = {50.0, 66.667, 75.0, 100.0, 114.0, 133.333,
		150.0, 177.777, 200.0, 228.0, 266.666, 300.0, 355.555, 400.0};
	float sleepness;

	rrr = new RTcmix(44100, 2, 4096);
	rrr->printOn();
	sleep(1); // give the thread time to initialized

	// set up the instrument
	rrr->cmd("load", 1, "WAVETABLE");
	rrr->cmd("makegen", 8, 1.0, 7.0, 1000.0, 0.0, 500.0, 1.0, 500.0, 0.0);
	rrr->cmd("makegen", 10,
		2.0, 10.0, 1000.0, 1.0, 0.5, 0.25, 0.125, 0.06, 0.03, 0.015);


	tsrand(); /* seeds the random generators with time-of-day */

	while(1) {
		for (i = 0; i < 4; i++) {
			if (brrand() < 0.5) {
				start = 1.0;
				dur = brrand() * 20.0 + 5.0;
				pchval = pitches[(int)(brrand() * 14.0)];
				rrr->cmd("WAVETABLE", 5,
					start, dur, 5000.0, pchval, 0.0);

				start += brrand();
				dur += brrand();
				pchval += rrand() * (pchval * 0.0069);
				rrr->cmd("WAVETABLE", 5,
					start, dur, 5000.0, pchval, 1.0);

				start += brrand();
				dur += brrand();
				pchval += rrand() * (pchval * 0.0069);
				rrr->cmd("WAVETABLE", 5,
					start, dur, 5000.0, pchval, 0.2);

				start += brrand();
				dur += brrand();
				pchval += rrand() * (pchval * 0.0069);
				rrr->cmd("WAVETABLE", 5,
					start, dur, 5000.0, pchval, 0.8);
			}
		}
		sleepness = brrand() * 10.0 + 4.0;
		sleep((int)sleepness);
	}
}
Exemplo n.º 3
0
void timer(unsigned long ts) {
  int n;
  char *ch, buf[512];

  if (nexttalk <= ts) {
    n = config_getcnt("fortune.so", "channels");
    if (n > 0 && randomline(buf, sizeof(buf)) != NULL) {
      ch = config_getn("fortune.so", "channels", rrand(n));
      irc_privmsg(ch, "%s", buf);
    }
    nexttalk = ts + min_delay + rrand(max_delay - min_delay);
  }
}
Exemplo n.º 4
0
int reply(info_t * in) {
	int e, m;

	if (in->cmd == cmd_privmsg && !tail_cmd(&in->tail,"alignment")) {
		e = rrand(3);
		m = rrand(3);
		if ((e == 1) && (m == 1))
			e = 3;
		irc_privmsg(to_sender(in), "Your alignment is %s %s.", 
			ethic[e], moral[m]);
	}
	return 0;
}
Exemplo n.º 5
0
int NOISE :: run()
{
   for (int i = 0; i < framesToRun(); i++) {
      if (--branch <= 0) {
         double p[nargs];
         update(p, nargs);
         amp = p[2];
         if (amparray)
            amp *= tablei(currentFrame(), amparray, amptabs);
         pctleft = nargs > 3 ? p[3] : 0.5;         // default is .5
         branch = skip;
      }

      float out[2];
      out[0] = rrand() * amp;

      if (outputChannels() == 2) {
         out[1] = out[0] * (1.0 - pctleft);
         out[0] *= pctleft;
      }

      rtaddout(out);
      increment();
   }

   return framesToRun();
}
Exemplo n.º 6
0
char *randomline(char *buf, int maxlen) {
  FILE *f;
  int lines, i;

  f = fopen("fortune.txt", "r");
  if (!f)
    return NULL;
        
  for (lines = 0;;) {
    if (!fgets(buf, maxlen, f))
      break;
    ++lines;
  }

  if (!lines)
    return NULL;

  fseek(f, 0, SEEK_SET);
  lines = rrand(lines);
  for (i = 0; i < lines; i++) {
    if (!fgets(buf, maxlen, f))
      break;
  }
  return buf;
}
Exemplo n.º 7
0
void scene_ChooseEnemyCommand(int index)
{
	int dice;

	//KAWA	IMPORTANT fix here -- instead of having the opponent use Struggle,
	//the game would hang. In the memory viewer, you could see the opponent's
	//data flicker between encryption states as the dice kept being rolled and
	//usage denied.
	if(!battle_CanShowFightMenu(index))
	{
		battle_AutoChooseMove(index);
		return;
	}

	while(1)
	{
		dice = rrand(4);
		if(!battle_IsMoveSensible(index,dice)) continue;
		if(battle_CanChooseMove(index,dice,0))
		{
			battle_RegisterMove(index, dice);
			return;
		}
	}
}
int IINOISE::run()
{
	for (int i = 0; i < framesToRun(); i++)  {
		if (--branch <= 0) {
			if (fastUpdate) {
				if (amptable)
					amp = ampmult * tablei(currentFrame(), amptable, amptabs);
			}
			else
				doupdate();
			branch = getSkip();
		}

		float sig = rrand();

		float out[2];
		out[0] = 0.0f;
		for (int j = 0; j < nresons; j++)
			out[0] += resons[j]->next(sig) * resonamp[j];

		out[0] *= amp;
		if (outputChannels() == 2) {
			out[1] = out[0] * (1.0f - pan);
			out[0] *= pan;
		}

		rtaddout(out);
		increment();
	}
	return framesToRun();
}
Exemplo n.º 9
0
int BROWN::run()
{
	for (int i = 0; i < framesToRun(); i++) {
		if (--_branch <= 0) {
			doupdate();
			_branch = getSkip();
		}
		float out[2];

		while (true) {
			float r = rrand();
			_brown += r;
			if (_brown <- 8.0 || _brown > 8.0)
				_brown -= r;
			else
				break;
		}

		out[0] = _brown * 0.125 * _amp;

		if (outputChannels() == 2) {
			out[1] = out[0] * (1.0 - _pan);
			out[0] *= _pan;
		}

		rtaddout(out);
		increment();
	}

	return framesToRun();
}
Exemplo n.º 10
0
int reply(info_t * in) {
	char *p;
	int i;

	if (in->cmd == cmd_privmsg && !tail_cmd(&in->tail,"fut")) {
		i = config_getcnt("fut.so", "insult");
		p = tail_getp(&in->tail);
		if(p) {
			irc_privmsg(to_sender(in), "%s: %s", p, 
				i?config_getn("fut.so", "insult", rrand(i)):"Fut");
		} else {
			irc_privmsg(to_sender(in), "%s",
				i?config_getn("fut.so", "insult", rrand(i)):"Fut");
		}
	}

	return 0;
}
double STGRANR::prob(double low,double mid,double high,double tight)  
        // Returns a value within a range close to a preferred value 
	
                    // tightness: 0 max away from mid
                     //               1 even distribution
                      //              2+amount closeness to mid
                      //              no negative allowed 
{
	int repeat;
	double range, num, sign;

	range = (high-mid) > (mid-low) ? high-mid : mid-low;
	do {
	  	if (rrand() > 0.)  
			sign = 1.; 
		else  sign = -1.;
	  	num = mid + sign*(pow((rrand()+1.)*.5,tight)*range);
	} while(num < low || num > high);
	return(num);
}
Exemplo n.º 12
0
int main(int argc, char *argv[])
{
    const cl_uint numBodies = 10;
    float *m     = cl_malloc_array(float, numBodies);
    float *t     = cl_malloc_array(float, numBodies);
    cl_float4 *a = cl_malloc_array(cl_float4, numBodies);
    cl_float4 *v = cl_malloc_array(cl_float4, numBodies);
    cl_float4 *p = cl_malloc_array(cl_float4, numBodies);

#ifdef CL_BUILD_RUNTIME
    cl_uint type = clGetTypeFromString(CL_USER_DEVICE_TYPE);
    cl_uint count = CL_USER_DEVICE_COUNT;
    cl_environment_t *pEnv = clCreateEnvironment(KDIR"kernel_nbody.cl",type,count,notify,CL_ARGS);
#else
    cl_environment_t *pEnv = clCreateEnvironmentFromBins(&gKernelBins, notify, CL_ARGS);
#endif
    if (pEnv)
    {
        cl_uint i = 0, j = 0;
        cl_uint numIterations = (argc > 1?atoi(argv[1]):10);
        for (i = 0; i < numBodies; i++)
        {
            m[i] = frand() * ipow(10,rrand(4,27)); // masses should be 10^4 - 10^27 ("Earth heavy")
            frand4(a[i], 1, 3);
            frand4(v[i], 1, 2);
            frand4(p[i], 4, 8);
            t[i] = 0.001f; // 1 millisecond.
        }
        i = 0;
        for (j = 0; j < numIterations; j++)
        {
            nbodies(pEnv, m, a, v, p, t, numBodies);
#if defined(DARWIN)
            printf("[%6u] p={%lf,%lf,%lf} v={%lf,%lf,%lf} a={%lf,%lf,%lf}\n", i,
                    p[i][0], p[i][1], p[i][2],
                    v[i][0], v[i][1], v[i][2],
                    a[i][0], a[i][1], a[i][2]);
#else
            printf("[%6u] p={%lf,%lf,%lf} v={%lf,%lf,%lf} a={%lf,%lf,%lf}\n", i,
                    p[i].s[0], p[i].s[1], p[i].s[2],
                    v[i].s[0], v[i].s[1], v[i].s[2],
                    a[i].s[0], a[i].s[1], a[i].s[2]);
#endif     
        }
        clDeleteEnvironment(pEnv);
        cl_free(t);
        cl_free(m);
        cl_free(v);
        cl_free(a);
        cl_free(p);
    }
    return 0;
}
Exemplo n.º 13
0
int reply(info_t * in) {
   time_t now = time(NULL);
   int i, j;
   
   if (helloinfo.last + helloinfo.interval > now)
      return 0;

   if (in->cmd == cmd_privmsg) {
      in->tail = skip_nick(in->tail, in->me);
      for (i = 0; i < helloinfo.ntrigger; i++) {
         if (!regex(in->tail, helloinfo.trigger[i])) {
            if(rrand(helloinfo.odds) > 0)
               return 1;
            irc_privmsg(to_sender(in), "%s",
               helloinfo.reply[rrand(helloinfo.nreply)]);
            helloinfo.last = now;
         }
      }
   }
   return 0;
}
Exemplo n.º 14
0
void frand4(cl_float4 f, cl_int l, cl_int h)
{
#if 0 //defined(DARWIN)
    f[0] = frand() * ipow(10, rrand(l,h));
    f[1] = frand() * ipow(10, rrand(l,h));
    f[2] = frand() * ipow(10, rrand(l,h));
    f[3] = frand() * ipow(10, rrand(l,h));
#else
    f.s[0] = frand() * ipow(10, rrand(l,h));
    f.s[1] = frand() * ipow(10, rrand(l,h));
    f.s[2] = frand() * ipow(10, rrand(l,h));
    f.s[3] = frand() * ipow(10, rrand(l,h));
#endif
}
Exemplo n.º 15
0
void shuffle(void *obj, size_t nmemb, size_t size)
{
  //Fisher–Yates shuffle algorithm
  void *temp = malloc(size);
  size_t n = nmemb;
  while ( n > 1 ) {
    size_t k = rrand(n--);
    memcpy(temp, BYTE(obj) + n*size, size);
    memcpy(BYTE(obj) + n*size, BYTE(obj) + k*size, size);
    memcpy(BYTE(obj) + k*size, temp, size);
  }
  free(temp);
}
Exemplo n.º 16
0
int reply(info_t * in) {
   time_t now = time(NULL);
   int i, nword[WORD_CNT];
   
   if (talkinfo.last + talkinfo.interval > now)
      return 0;

   if (in->cmd == cmd_privmsg) {
	   if (!tail_cmd(&in->tail, "sercz")) {
		   for(i = 0; i < WORD_CNT; i++) {
			   nword[i] = rrand(talkinfo.nword[i]);
		   }
		   irc_privmsg(to_sender(in), "%s %s",
   			   talkinfo.word[0][nword[0]], talkinfo.word[1][nword[1]]);
		   irc_privmsg(to_sender(in), "%s %d %s %s",
			   talkinfo.word[2][nword[2]], rrand(talkinfo.max - 2) + 2,
			   talkinfo.word[3][nword[3]], talkinfo.word[4][nword[4]]);
		   irc_privmsg(to_sender(in), "und dabei mein%s %s",
			   talkinfo.word[5][nword[5]], talkinfo.word[6][nword[6]]);				
	   }
   }
   return 0;
}
Exemplo n.º 17
0
int init(void) {
  char *p;

  p = config_get("fortune.so", "mindelay");
  if (p) {
    min_delay = strtoul(p, NULL, 10);
    if (min_delay == ULONG_MAX && errno == ERANGE) {
      min_delay = MIN_DELAY_DEF;
      printc("Illegal minimum delay. Use default %lu\n",
        MIN_DELAY_DEF);
    }
  } else {
    min_delay = MIN_DELAY_DEF;
    printc("No minimum delay. Use default %lu\n", MIN_DELAY_DEF);
  }

  p = config_get("fortune.so", "maxdelay");
  if (p) {
    max_delay = strtoul(p, NULL, 10);
    if (max_delay == ULONG_MAX && errno == ERANGE) {
      max_delay = MAX_DELAY_DEF;
      printc("Illegal maximum delay. Use default %lu\n",
        MAX_DELAY_DEF);
    }
  } else {
    max_delay = MAX_DELAY_DEF;
    printc("No maximum delay. Use default %lu\n", MAX_DELAY_DEF);
  }
 
  if (max_delay < min_delay)
    max_delay = min_delay;

  nexttalk = time(NULL) + min_delay + rrand(max_delay - min_delay);

  p = config_get("fortune.so", "reqdelay");
  if (p) {
    req_delay = strtoul (p, NULL, 10);
    if (req_delay == ULONG_MAX && errno == ERANGE) {
      req_delay = REQ_DELAY_DEF;
      printc("Illegal request delay. Use default %lu\n",
        REQ_DELAY_DEF);
    }
  } else {
    req_delay = REQ_DELAY_DEF;
    printc("No request delay. Use default %lu\n", REQ_DELAY_DEF);
  }
  lasttime = 0;

  return 0;
}
Exemplo n.º 18
0
void initiate(double x[], double y[], double nx_old[],
              double ny_old[], double nx_new[], double ny_new[],
              double sumnx[], double sumny[], double r[],
              double nx_temp[], double ny_temp[], double v[],
              double complex Ax[], double complex Ay[],
              double complex A1[], double complex Wk[],
              double complex Ax_complex[], double complex Ay_complex[],
              double complex A1_complex[],
              double complex Wk_complex[]) {
  
  for (int i = 0; i < N; i++) {
    double phi = rrand() * 2 * M_PI;
    nx_old[i] = cos(phi);
    ny_old[i] = sin(phi);
    x[i] = rrand() * L;
    y[i] = rrand() * L;
    nx_new[i] = nx_old[i];
    ny_new[i] = ny_old[i];
    sumnx[i] = 0;
    sumny[i] = 0;
    r[i] = 0;
    nx_temp[i] = 0;
    ny_temp[i] = 0;
  }
  for (int i = 0; i < T; i++) {
    v[i] = 0;
    A1[i] = 0;
    Ax[i] = 0;
    Ay[i] = 0;
    Wk[i] = 0;
    A1_complex[i] = 0;
    Ax_complex[i] = 0;
    Ay_complex[i] = 0;
    Wk_complex[i] = 0;
  }
}
Exemplo n.º 19
0
int LSFLUTE::run()
{
	for (int i = 0; i < framesToRun(); i++) {
		if (olength1 < length1) olength1++;
		if (olength1 > length1) olength1--;
		if (olength2 < length2) olength2++;
		if (olength2 > length2) olength2--;
		if (--branch <= 0) {
			aamp = tablei(currentFrame(), amparr, amptabs);
			oamp = tablei(currentFrame(), oamparr, oamptabs);
			branch = skip;
		}

		float sig = (rrand() * namp * aamp) + aamp;
		float del1sig = mdelget(del1ptr,olength1,dl1ptr);
		sig = sig + (del1sig * -0.35);
#ifdef MAXMSP
		delput(sig,del2ptr,dl2ptr);
#else
		mdelput(sig,del2ptr,dl2ptr);
#endif

		sig = mdelget(del2ptr,olength2,dl2ptr);
		sig = (sig * sig * sig) - sig;
		sig = (0.4 * sig) + (0.9 * del1sig);

		float out[2];
		out[0] = sig * amp * oamp;
		sig = (dampcoef * sig) + ((1.0 - dampcoef) * oldsig);
		oldsig = sig;
#ifdef MAXMSP
		delput(sig,del1ptr,dl1ptr);
#else
		mdelput(sig,del1ptr,dl1ptr);
#endif

		if (outputChannels() == 2) {
			out[1] = (1.0 - spread) * out[0];
			out[0] *= spread;
		}

		rtaddout(out);
		increment();
	}
	return framesToRun();
}
Exemplo n.º 20
0
int CLAR::run()
{
	for (int i = 0; i < framesToRun(); i++) {
		if (--branch <= 0) {
			if (amparr) {
#ifdef MAXMSP
				aamp = rtcmix_table(currentFrame(), amparr, amptabs);
#else
				aamp = table(currentFrame(), amparr, amptabs);
#endif
			}
			if (oamparr)
				oamp = tablei(currentFrame(), oamparr, oamptabs);
			branch = skip;
		}

		float sig = (rrand() * namp * aamp) + aamp;
		float del1sig = mdelget(del1,length1,dl1);
		float del2sig = mdelget(del2,length2,dl2);
		if (del1sig > 1.0) del1sig = 1.0;
		if (del1sig < -1.0) del1sig = -1.0;
		if (del2sig > 1.0) del2sig = 1.0;
		if (del2sig < -1.0) del2sig = -1.0;
		sig = sig + 0.9 * ((d2gain * del2sig) + ((0.9-d2gain) * del1sig));
		float csig = -0.5 * sig + aamp;
		float ssig = sig * sig;
		sig = (0.3 * ssig) + (-0.8 * (sig * ssig));
		sig = sig + csig;
		sig = (0.7 * sig) + (0.3 * oldsig);
		oldsig = sig;
		delput(sig,del2,dl2);
		delput(sig,del1,dl1);

		float out[2];
		out[0] = sig * amp * oamp;
		if (outputChannels() == 2) {
			out[1] = (1.0 - spread) * out[0];
			out[0] *= spread;
		}

		rtaddout(out);
		increment();
	}
	return framesToRun();
}
Exemplo n.º 21
0
int CRACKLE::init(double p[], int n_args)
{
	nargs = n_args;

	const float outskip = p[0];
	const float dur = p[1];

	if (rtsetoutput(outskip, dur, this) == -1)
		return DONT_SCHEDULE;

	if (outputChannels() > 2)
		return die("CRACKLE", "Use mono or stereo output only.");

	x0 = rrand() * 0.1;
	x1 = 0;
	x2 = 0;
	x3 = 0;

	return nSamps();
}
Exemplo n.º 22
0
static void fill_parameters(struct header_s *h)
{
        rrand_seed(time(NULL), time(NULL) + 2);
        
        h->index_id = rrand();
        h->iblock = iblock;
        h->segment_size = segment_size;

        char *basename = pparm_common_name(dex_names[FW]);
        char *name;
        int fd;

        asprintf(&name, "%s.%u.nfo", basename, iblock);
        if ((fd = open(name, O_RDONLY, 0)) == -1)
                dub_sysdie("Couldn't open file %s", name);

        fio_read(fd, h->fw_layers, sizeof(h->fw_layers));
        close(fd);

        free(name);
        free(basename);
}
Exemplo n.º 23
0
int SFLUTE::run()
{
	for (int i = 0; i < framesToRun(); i++) {
		if (--branch <= 0) {
			aamp = tablei(currentFrame(), amparr, amptabs);
			oamp = tablei(currentFrame(), oamparr, oamptabs);
			branch = skip;
		}

		float sig = (rrand() * namp * aamp) + aamp;
		float del1sig = mdelget(del1,length1,dl1);
		sig = sig + (del1sig * -0.35);
// BGG mm -- delput works fine
//		mdelput(sig,del2,dl2);
		delput(sig,del2,dl2);

		sig = mdelget(del2,length2,dl2);
		sig = (sig * sig * sig) - sig;
		sig = (0.4 * sig) + (0.9 * del1sig);

		float out[2];
		out[0] = sig * amp * oamp;
		sig = (dampcoef * sig) + ((1.0 - dampcoef) * oldsig);
		oldsig = sig;
// BGG mm -- delput works fine
//		mdelput(sig,del1,dl1);
		delput(sig,del1,dl1);

		if (outputChannels() == 2) {
			out[1] = (1.0 - spread) * out[0];
			out[0] *= spread;
		}

		rtaddout(out);
		increment();
	}
	return framesToRun();
}
Exemplo n.º 24
0
int PINK::run()
{
	for (int i = 0; i < framesToRun(); i++) {
		if (--_branch <= 0) {
			doupdate();
			_branch = getSkip();
		}

		/*
			This is an approximation to a -10dB/decade filter using a weighted sum 
			of first order filters. It is accurate to within +/-0.05dB above 9.2Hz 
			(44100Hz sampling rate). Unity gain is at Nyquist, but can be adjusted 
			by scaling the numbers at the end of each line.  -Paul Kellet
		*/
		const double white = rrand() * _amp;
		_b0 = _b0 * 0.99886 + white * 0.0555179;
		_b1 = _b1 * 0.99332 + white * 0.0750759;
		_b2 = _b2 * 0.96900 + white * 0.1538520;
		_b3 = _b3 * 0.86650 + white * 0.3104856;
		_b4 = _b4 * 0.55000 + white * 0.5329522;
		_b5 = _b5 * -0.7616 - white * 0.0168980;
		double pink = _b0 + _b1 + _b2 + _b3 + _b4 + _b5 + _b6 + white * 0.5362;
		_b6 = white * 0.115926;

		float out[2];
		out[0] = pink * 0.15;

		if (outputChannels() == 2) {
			out[1] = out[0] * (1.0 - _pan);
			out[0] *= _pan;
		}
		rtaddout(out);
		increment();
	}
	return framesToRun();
}
Exemplo n.º 25
0
int VSTART1::run()
{
    for (int i = 0; i < framesToRun(); i++) {
        if (--branch1 <= 0) {
            vsi = (( (rrand()+1.0)/2.0) * vsidiff) + vsibot;
            branch1 = (int)((float)vlen/vsi);
        }
        if (--branch2 <= 0) {
            if (amptable)
                aamp = tablei(currentFrame(), amptable, amptabs) * amp;
            float vamp = tablei(currentFrame(), eloc, tab) * vdepth;
            float freqch = oscili(vamp,vsi,vloc,vlen,&vphase);
            sset(SR, freq+freqch, tf0, tfN, strumq1);
            branch2 = reset;
            vphase += (float)branch2 * vsi;
            while (vphase >= (float) vlen)
                vphase -= (float) vlen;
        }

        float a = strum(d, strumq1);
        float b = dist(dgain*a);
        d = fbgain*delay(b, dq);

        float out[2];
        out[0] = (cleanlevel*a + distlevel*b) * aamp;

        if (outputChannels() == 2) { /* split stereo files between the channels */
            out[1] = (1.0 - spread) * out[0];
            out[0] *= spread;
        }

        rtaddout(out);
        increment();
    }
    return framesToRun();
}
int STGRANR::run()
{
        long i,attacksamps,j,thechunksamp,waitsamps,left,ngrains,rsamps;
        float *outp;
	float loc, ampfac, interval;
        double    frac;
	const int frameCount = framesToRun();
        
	if (in == NULL)        /* first time, so allocate it */
		in = new float [RTBUFSAMPS * inputChannels()];

//        if ( (durhi*(float)SR) > frameCount())
//		rtcmix_advise("STGRANR", "Grain duration larger than buffer.");
        
    outp = outbuf;               /* point to inst private out buffer */

	// figure out how many grains are in this chunk 
	ngrains = (int)((float)frameCount/(rate*(float)SR));	
        
	if ( ngrains  < 1 ) {
                if ( grainoverlap ) 
                    ngrains = 1;
                else {
					if ( (rrand()*.5+.5) < ((float)frameCount/(rate*(float)SR)) )
                        ngrains = 1;
                    else 
                         ngrains = 0;
                }
        }
        
        thechunksamp = 0;
        if ( ngrains ) {  
             for (i = 0; i < ngrains; i++) {
				attacksamps = frameCount/ngrains; // no grainoverlap yet
                transp = (float)prob(transplo, transpmid, transphi, transpti);
                interval = octpch(transp);
                increment = (double) cpsoct(10.0 + interval) / cpsoct(10.0);
                gdur = (float)prob(durlo,durmid,durhi,durti);
                grainsamps = (long)(gdur*(float)SR);    
                if ( grainsamps > attacksamps) {
                    overlapsample = grainsamps - attacksamps; // where to start in next attacksamps in envelop
                    grainoverlap = 1;
                    grainsamps = attacksamps;
                }
		ratevar = (float)prob(ratevarlo, ratevarmid, ratevarhi, ratevarti);
                waitsamps = (long)(ratevar*(float)(attacksamps - grainsamps));
		spread = (float)prob(loclo,locmid,lochi,locti);
		tableset(SR, gdur, grlen, tabg); 
		for ( j = 0; j < attacksamps; j++ ) {
			if (--branch < 0) { 
				aamp = tablei(currentFrame(), amptable, tabs) * amp;
				branch = skip;
			}
                        while (get_frame) {
                            if (inframe >= attacksamps) {
                                rtgetin(in, this, attacksamps * inputChannels());
                                inframe = 0;
                            }
                            oldersig[0] = oldsig[0];
                            oldsig[0] = newsig[0];
                            newsig[0] = in[(inframe * inputChannels())/* + inchan*/];
                            if ( inputChannels() == 2 ) {
                                oldersig[1] = oldsig[1];
                                oldsig[1] = newsig[1];
                                newsig[1] = in[(inframe * inputChannels())+1];
                            }
                            
                            inframe++;
                            incount++;
                    
                            if (counter - (double) incount < 0.5)
                                get_frame = 0;
                        }
                        if(( j < grainsamps + waitsamps) && ( j > waitsamps )) { 
                            ampfac = tablei(j-waitsamps,grenvtable,tabg);
                            frac = (counter - (double) incount) + 2.0;
                            outp[0] = interp(oldersig[0], oldsig[0], newsig[0], frac) * ampfac;
                            if (inputChannels() == 2 && outputChannels() == 2)  // split stereo files between the channels 
                                outp[1] = interp(oldersig[1], oldsig[1], newsig[1], frac) * ampfac;
                        }
                        else 
                            outp[0] = 0.0;
                        if (outputChannels() == 2 ) {  // split stereo files between the channels 
                            outp[1] = (1.0 - spread) * outp[0];
                            outp[0] *= spread;
                        }
						Instrument::increment(); // sample of whole note
                        thechunksamp++; // sample within chunk
                        outp += outputChannels();  
                        counter += increment;         /* keeps track of interp pointer */
                        if (counter - (double) incount >= -0.5)
                            get_frame = 1;
		}
                totalgrains++;
            }
        }
        else { // ngrains = 0
			for ( j = 0; j < frameCount; j++ ) {
                outp[0] = 0.0;
                if (outputChannels() == 2) outp[1] = 0.0;
                outp += outputChannels();
				Instrument::increment(); // sample of whole note
                thechunksamp++; // sample within chunk
           }
        }
 //       rtcmix_advise("STGRANR", "totalgrains: %ld\n",totalgrains);
        return thechunksamp;        
}
Exemplo n.º 27
0
double
sgran(float p[], int n_args)
{
	long n,bgrainsamps,bgraindist,bgrainslide;
	long i,nsamps,gstt_var,count;
	long egrainsamps,egraindist,egrainslide;
	long grainsamps,grainslide,graindistdiff;
	float si=0.0,phase,val=0.0,amp,out[2],chb,freq;
	float tabs[2],tab[2],tab2[2],tab3[2],tab4[2],tab5[2];
	double *array,*wave,*envel,*rate_shape,*dur_shape,*loc_shape,*freq_shape;
	float gdist_inc;
	double gstt_per,lo,mid,hi,ti,slodiff,smiddiff,shidiff,stidiff;
	double dlodiff,dmiddiff,dhidiff,dtidiff;
	double llodiff,lmiddiff,lhidiff,ltidiff;
	double flodiff,fmiddiff,fhidiff,ftidiff;
	int len,j,z,chans,randflag=0;

#ifdef EMBEDDED
	int outrepos();
#endif
	float rrand();
	void srrand();
	double prob();

	if (p[37] > 0)
		srrand(p[37]);
   else
		srrand(3);

	nsamps = setnote(p[0],p[1],1); /* set file position */

	array = floc(1);             /* used to be setline  -JGG */
	if (array) {
		int amplen = fsize(1);
		tableset(SR(), p[1], amplen, tabs);
	}
	else
		rtcmix_advise("sgran", "Setting phrase curve to all 1's.");

	wave = floc(6); /* finds starting loc. of waveform */
	if (wave == NULL)
		die("sgran", "You haven't made the oscillator waveform (table 6).");
	len = fsize(6); /* length of playing waveform function */

	envel = floc(8);  /* NOTE: used to be floc(1), now stolen by setline  -JGG */
	if (envel == NULL)
		die("sgran", "You haven't made the grain envelope (table 8).");
	/* NOTE: fsize(8) called in loop below */

	bgrainsamps = grainsamps = p[14] * SR();
	bgraindist = p[3] * SR();
	bgrainslide = grainslide = bgraindist - bgrainsamps;

	egrainsamps = p[18] * SR();
	egraindist = p[4] * SR();
	egrainslide = egraindist - egrainsamps;

	graindistdiff = egraindist - bgraindist;

	rate_shape = floc(2);
	if (rate_shape == NULL)
		die("sgran", "You haven't made the grain density function (table 2).");
	tableset(SR(), p[1]-p[4],fsize(2),tab2);

	dur_shape = floc(3);
	if (dur_shape == NULL)
		die("sgran", "You haven't made the grain duration function (table 3).");
	tableset(SR(), p[1]-p[4],fsize(3),tab3);

	loc_shape = floc(4);
	if (loc_shape == NULL)
		die("sgran", "You haven't made the grain location function (table 4).");
	tableset(SR(), p[1]-p[4],fsize(4),tab4);

	freq_shape = floc(5);
	if (freq_shape == NULL)
		die("sgran", "You haven't made the grain frequency function (table 5).");
	tableset(SR(), p[1]-p[4],fsize(5),tab5);

	slodiff = (double)(p[9]-p[5])/nsamps; /* get stt zero/one differences */
	smiddiff = (double)(p[10]-p[6])/nsamps;
	shidiff = (double)(p[11]-p[7])/nsamps;
	stidiff = (double)(p[12]-p[8])/nsamps;

	dlodiff = (double)(p[17]-p[13]); /*get dur zero/one differences */
	dmiddiff = (double)(p[18]-p[14]);
	dhidiff = (double)(p[19]-p[15]);
	dtidiff = (double)(p[20]-p[16]);

	llodiff = (double)(p[25]-p[21]); /*get loc zero/one differences */
	lmiddiff = (double)(p[26]-p[22]);
	lhidiff = (double)(p[27]-p[23]);
	ltidiff = (double)(p[28]-p[24]);
	chb = p[21];

	if (p[29] < 0) 		/* if negative, noise is the input */
		randflag = 1;

	flodiff = (double)(p[33]-p[29]); /*freq zero/one differences */
	fmiddiff = (double)(p[34]-p[30]);
	fhidiff = (double)(p[35]-p[31]);
	ftidiff = (double)(p[36]-p[32]);

	z = 2;

	chans = sfchans(&sfdesc[1]); /* get file number of channels */
	amp = p[2];
	gstt_var = 0;
	count = 0;

	j = 0; /* "branch once a cycle" loop for amp envelope */

	for(i = 0; i < nsamps; i++) {
		count++;
		phase = 0;
		tableset(SR(), grainsamps/SR(),fsize(8),tab);
		if(!randflag) {
			lo = p[29] + flodiff*tablei(i,freq_shape,tab5);
			mid = p[30] + fmiddiff*tablei(i,freq_shape,tab5);
			hi = p[31] + fhidiff*tablei(i,freq_shape,tab5);
			ti = p[32] + ftidiff*tablei(i,freq_shape,tab5);
			lo = (lo > mid) ? mid : lo;
			hi = (hi < mid) ? mid : hi;
			ti = (ti < 0) ? 0 : ti; 
			freq = prob(lo, mid, hi, ti);
			si = freq * (float)len/SR(); 
		}

/*
	fprintf(stderr,"i: %ld, grainsamps: %ld, grainslide: %ld\n",i,grainsamps,grainslide);
*/

		for (n = 0; n < grainsamps; n++) {
			while (!j--) {   /* branch in here when j reaches 0 */
				float tmp = 1.0;
				if (array)
					tmp = tablei(i,array,tabs);
				val = amp * tablei(n,envel,tab) * tmp;
				j = ((grainsamps-n) > z) ? z : (grainsamps-n);
			}
			if(randflag)
				out[0] = rrand()*val;
			else
				out[0] =  oscili(val,si,wave,len,&phase);

			if (chans > 1) { /* stereo */
				out[1] = (1.0 - chb) * out[0];
				out[0] *= chb;
			}
			ADDOUT(out,1);
		}

		if((i+grainslide+gstt_var+grainsamps) < 0) {
			outrepos((grainslide),1);
			i += grainsamps;
			i += grainslide;
		}	
		else {
			outrepos((grainslide+gstt_var),1);
			i += grainsamps;
			i += grainslide;
			i += gstt_var;
		}

		lo = p[13] + dlodiff*tablei(i,dur_shape,tab3);
		mid = p[14] + dmiddiff*tablei(i,dur_shape,tab3);
		hi = p[15] + dhidiff*tablei(i,dur_shape,tab3);
		ti = p[16] + dtidiff*tablei(i,dur_shape,tab3);
		lo = (lo > mid) ? mid : lo;
		hi = (hi < mid) ? mid : hi;
		ti = (ti < 0) ? 0 : ti; 
		grainsamps = (long)(prob(lo, mid, hi, ti)*SR());


		/*	get percentage to vary next stt of grain */
		lo = p[5] + slodiff*i;
		mid = p[6] + smiddiff*i;
		hi = p[7] + shidiff*i;
		ti = p[8] + stidiff*i;
		lo = (lo > mid) ? mid : lo;
		hi = (hi < mid) ? mid : hi;
		ti = (ti < 0) ? 0 : ti; 
		gstt_per = prob(lo, mid, hi, ti);
		gstt_var = (long)(gstt_per*(grainsamps+grainslide)); 

/* calculate grainslide */
		gdist_inc = tablei(i,rate_shape,tab2);
		grainslide = (float)bgraindist + (float)graindistdiff*gdist_inc - grainsamps;

		lo = p[21] + llodiff*tablei(i,loc_shape,tab4);
		mid = p[22] + lmiddiff*tablei(i,loc_shape,tab4);
		hi = p[23] + lhidiff*tablei(i,loc_shape,tab4);
		ti = p[24] + ltidiff*tablei(i,loc_shape,tab4);
		lo = (lo > mid) ? mid : lo;
		hi = (hi < mid) ? mid : hi;
		ti = (ti < 0) ? 0 : ti; 
		chb = prob(lo, mid, hi, ti);
	}
	printf("\n%ld grains\n",count);
	endnote(1);

	return(0.0);
}
Exemplo n.º 28
0
// Generate a random number between -m/2 and m/2
double randm(double m, double strength) {
  double random_number2 = rrand();
  random_number2 -= 0.5;
  random_number2 *= strength * m;
  return random_number2;
}
Exemplo n.º 29
0
int VSTART1::init(double p[], int n_args)
{
// p0 = start; p1 = dur; p2 = pitch (oct.pc); p3 = fundamental decay time
// p4 = nyquist decay time; p5 = distortion gain; p6 = feedback gain
// p7 = feedback pitch (oct.pc); p8 = clean signal level
// p9 = distortion signal level; p10 = amp; p11 = squish
// p12 = low vibrato freq range; p13 = hi vibrato freq range
// p14 = vibrato freq depth (expressed in cps); p15 = random seed value
// p16 = pitch update (default 200/sec)
// p17 = stereo spread [optional]
// p18 = flag for deleting pluck arrays (used by FRET, BEND, etc.) [optional]
// assumes makegen 1 is the amplitude envelope, makegen 2 is the vibrato
// function, and makegen 3 is the vibrato amplitude envelope

    if (rtsetoutput(p[0], p[1], this) == -1)
        return DONT_SCHEDULE;

    strumq1 = new StrumQueue;
    strumq1->ref();
    curstrumq[0] = strumq1;
    freq = cpspch(p[2]);
    tf0 = p[3];
    tfN = p[4];
    sset(SR, freq, tf0, tfN, strumq1);
    randfill(1.0, (int)p[11], strumq1);

    dq = new DelayQueue;
    dq->ref();
    curdelayq = dq;
    delayset(SR, cpspch(p[7]), dq);
    delayclean(dq);

    amp = p[10];
    amptable = floc(1);
    if (amptable) {
        int amplen = fsize(1);
        tableset(SR, p[1], amplen, amptabs);
    }
    else {
        rtcmix_advise("VSTART1", "Setting phrase curve to all 1's.");
        aamp = amp;
    }

    vloc = floc(2);
    if (vloc == NULL)
        return die("VSTART1", "You need to store a vibrato function in gen num. 2.");
    vlen = fsize(2);

    vsibot = p[12] * (float)vlen/SR;
    vsidiff = vsibot - (p[13] * (float)vlen/SR);
    srrand((int)p[15]);
    vsi = ((rrand()+1.0)/2.0) * vsidiff;
    vsi += vsibot;
    vphase = 0.0;

    eloc = floc(3);
    if (eloc == NULL)
        return die("VSTART1", "You need to store a vibrato amp. envelope in gen num. 3.");
    int elen = fsize(3);
    tableset(SR, p[1], elen, tab);

    dgain = p[5];
    fbgain = p[6]/dgain;
    cleanlevel = p[8];
    distlevel = p[9];
    vdepth = p[14];
    reset = (int)p[16];
    if (reset == 0) reset = 200;
    spread = p[17];
    deleteflag = (int)p[18];

    d = 0.0;

    return nSamps();
}
Exemplo n.º 30
0
void TitleScreen(int skip)
{
	int i, f = 0;
	int t = 0, buildstep = 0,buildt = 0;

	int particleX[MAXTITLEPARTICLES];
	int particleY[MAXTITLEPARTICLES];
	int particleS[MAXTITLEPARTICLES];
	int particleT[MAXTITLEPARTICLES];

	ClearOamBak();
	REG_BG3CNT |= 1 << BG_CBB_SHIFT;

	DmaArrayCopy(titlebackPal, MEM_PAL_BG + 0x0000);
	DmaArrayCopy(titlebackTiles, MEM_VRAM + 0x4000);
	DmaArrayCopy(titlebackMap, MEM_VRAM + 0xf000);

	DmaArrayCopy(titlemonTiles, MEM_VRAM + 0x0000);
	DmaArrayCopy(titlemonMap, MEM_VRAM + 0xe800);

	for(i = 0; i < MAXTITLEPARTICLES; i++)
	{
		particleX[i] = rrand(32) * 8;
		particleY[i] = 170;
		particleS[i] = rrand(3) + 1;
		particleT[i] = rrand(2);
	}

	REG_BG0HOFS = 0;
	REG_BG0VOFS = -2;
	REG_BG1HOFS = 0;
	REG_BG1VOFS = -32;
	REG_BG2HOFS = 0;
	REG_BG2VOFS = 0;
	REG_BG3HOFS = 0;
	REG_BG3VOFS = 0;

	DmaArrayCopy(titlelogoTiles, MEM_VRAM + 0x8000);
	DmaArrayCopy(titlelogoPal, MEM_PAL_BG + 0x0020);
	for(i=0;i<320;i++) BG0MAP[i] = titlelogoMap[i] | 0x1000;

	DmaArrayCopy(titleflavorTiles, MEM_VRAM_OBJ + 0x0000);
	DmaArrayCopy(titleflavorPal, MEM_PAL_OBJ + 0x0000);

	DmaArrayCopy(titletextsTiles, MEM_VRAM_OBJ + 0x0800);

	DmaArrayCopy(titleparticlesTiles, MEM_VRAM_OBJ + 0x0c00);
	DmaArrayCopy(titleparticlesPal, MEM_PAL_OBJ + 0x0020);

	OamBak[0].Size = 3;
	OamBak[0].Shape = 1;
	OamBak[0].HPos = 63;
	OamBak[0].VPos = 55;
	OamBak[0].CharNo = 0;
	OamBak[0].ObjMode = 1;
	OamBak[1].Size = 3;
	OamBak[1].Shape = 1;
	OamBak[1].HPos = 63+64;
	OamBak[1].VPos = 55;
	OamBak[1].CharNo = 32;
	OamBak[1].ObjMode = 1;

	for(i=0; i<7; i++)
	{
		OamBak[2+i].Size = 1;
		OamBak[2+i].Shape = 1;
		OamBak[2+i].Pltt = 0;
		OamBak[2+i].CharNo = 64 + (i<<2);
	}
	for(i=0; i<7; i++)
	{
		OamBak[2+i].HPos = 8 + (i<<5);
		OamBak[2+i].VPos = 152;
	}
	for(i=0; i<2; i++)
	{
		OamBak[6+i].VPos = 152;
		OamBak[6+i].HPos = 169 + (i<<5);
	}

	REG_DISPCNT =  DCNT_MODE0 | DCNT_BG2 | DCNT_BG3;
	REG_BLDCNT = BLD_WHITE | BLD_BG3;

	if(skip)
	{
		REG_DISPCNT =  DCNT_MODE0 | DCNT_OBJ | DCNT_BG0 | DCNT_BG1 | DCNT_BG2 | DCNT_BG3 | DCNT_OBJ_1D;
		REG_BLDCNT = 0;
		buildstep = 3;
		REG_BG1VOFS = 0;
	}

	sndPlaySound(80,SOUND_NORESTART);

	while(1)
	{
		DoVBlank();
		KeyRead();
		if(Trg & A_BUTTON)
			break;
		if(Trg & START_BUTTON)
			break;
		if(buildstep == 0)
		{
			REG_BLDY = 16 - (buildt>>2);
			buildt++;
			if(buildt==64)
			{
				REG_DISPCNT |= DCNT_BG1;
				buildstep++;
				buildt=0;
			}
		}
		else if(buildstep == 1)