Example #1
0
int drumsynth::generate(short * buffer,int len)
{
  //MessageBox(NULL, dsfile, "DrumSynth", MB_OK); //////////////////

  //input file

  int i=0;
  //output file 
  wavemode=1;
  wavewords=0;

  for (i=0;i<len;i++)
    buffer[i]=0;
  
  if (tpos>=Length)
    return -1;


  //sloppy programming? wait if DLL in use
  //while(busy==1) Sleep(0); busy=1;
  //while(busy==1) sleep(0); busy=1;

  prepare=1;
  if (prepare==0)
    {
      return -2;
    }

  for (i=0;i<len;i++)
    buffer[i]=0;


  //generate
  //tpos = 0;
  //while(tpos<Length)
  //{
    tplus = tpos + len-1;

    if(NON==1) //noise
    {
      for(t=tpos; t<=tplus; t++)
      {
        if(t < envData[2][NEXTT]) envData[2][ENV] = envData[2][ENV] + envData[2][dENV];
        else UpdateEnv(2, t);
        x[2] = x[1];
        x[1] = x[0];
        x[0] = (randmax2 * (float)rand()) - 1.f; 
        TT = a * x[0] + b * x[1] + c * x[2] + d * TT;
        DF[t - tpos] = TT * g * envData[2][ENV];
      }
      if(t>=envData[2][MDA_MAX]) NON=0;
    }
    else for(j=0; j<len; j++) DF[j]=0.f;
    
    if(TON==1) //tone
    {
      TphiStart = Tphi;
      if(TDroop==1)
      {
        for(t=tpos; t<=tplus; t++)
          phi[t - tpos] = F2 + (ddF * (float)exp(t * TDroopRate));
      }          
      else
      {
        for(t=tpos; t<=tplus; t++)
          phi[t - tpos] = F1 + (t / envData[1][MDA_MAX]) * ddF;
      }  
      for(t=tpos; t<=tplus; t++)
      {
        totmp = t - tpos;
        if(t < envData[1][NEXTT]) 
          envData[1][ENV] = envData[1][ENV] + envData[1][dENV];
        else UpdateEnv(1, t);
        Tphi = Tphi + phi[totmp];
        DF[totmp] += TL * envData[1][ENV] * (float)sin(fmod(Tphi,TwoPi));//overflow?
      }
      if(t>=envData[1][MDA_MAX]) TON=0;
    }
    else for(j=0; j<len; j++) phi[j]=F2; //for overtone sync
    
    if(BON==1) //noise band 1
    {
      for(t=tpos; t<=tplus; t++)
      {
        if(t < envData[5][NEXTT])
          envData[5][ENV] = envData[5][ENV] + envData[5][dENV];
        else UpdateEnv(5, t);
        if((t % BFStep) == 0) BdF = randmax * (float)rand() - 0.5f;
        BPhi = BPhi + BF + BQ * BdF;
        botmp = t - tpos;
        DF[botmp] = DF[botmp] + (float)cos(fmod(BPhi,TwoPi)) * envData[5][ENV] * BL;
      }
      if(t>=envData[5][MDA_MAX]) BON=0;
    }

    if(BON2==1) //noise band 2
    {
      for(t=tpos; t<=tplus; t++)
      {
        if(t < envData[6][NEXTT])
          envData[6][ENV] = envData[6][ENV] + envData[6][dENV];
        else UpdateEnv(6, t);
        if((t % BFStep2) == 0) BdF2 = randmax * (float)rand() - 0.5f;
        BPhi2 = BPhi2 + BF2 + BQ2 * BdF2;
        botmp = t - tpos;
        DF[botmp] = DF[botmp] + (float)cos(fmod(BPhi2,TwoPi)) * envData[6][ENV] * BL2;
      }
      if(t>=envData[6][MDA_MAX]) BON2=0;
    }

    for (t=tpos; t<=tplus; t++)
    {
      if(OON==1) //overtones
      {
        if(t<envData[3][NEXTT])
          envData[3][ENV] = envData[3][ENV] + envData[3][dENV];
        else
        {
          if(t>=envData[3][MDA_MAX]) //wait for OT2
          {
            envData[3][ENV] = 0; 
            envData[3][dENV] = 0; 
            envData[3][NEXTT] = 999999;
          }
          else UpdateEnv(3, t);
        }
        //
        if(t<envData[4][NEXTT])
          envData[4][ENV] = envData[4][ENV] + envData[4][dENV];
        else
        {
          if(t>=envData[4][MDA_MAX]) //wait for OT1
          {
            envData[4][ENV] = 0;
            envData[4][dENV] = 0;
            envData[4][NEXTT] = 999999;
          }
          else UpdateEnv(4, t);
        }
        //
        TphiStart = TphiStart + phi[t - tpos];
        if(OF1Sync==1) Ophi1 = TphiStart * OF1; else Ophi1 = Ophi1 + OF1;
        if(OF2Sync==1) Ophi2 = TphiStart * OF2; else Ophi2 = Ophi2 + OF2;
        Ot=0.0f;
        switch (OMode)
        {
          case 0: //add
            Ot = OBal1 * envData[3][ENV] * waveform(Ophi1, OW1);
            Ot = OL * (Ot + OBal2 * envData[4][ENV] * waveform(Ophi2, OW2));
            break;
        
          case 1: //FM
            Ot = ODrive * envData[4][ENV] * waveform(Ophi2, OW2);
            Ot = OL * envData[3][ENV] * waveform(Ophi1 + Ot, OW1);
            break;
        
          case 2: //RM
            Ot = (1 - ODrive / 8) + (((ODrive / 8) * envData[4][ENV]) * waveform(Ophi2, OW2));
            Ot = OL * envData[3][ENV] * waveform(Ophi1, OW1) * Ot;
            break;

          case 3: //808 Cymbal
            for(j=0; j<6; j++)
            {
              Oc[j][0] += 1.0f;
              
              if(Oc[j][0]>Oc[j][1])
              { 
                Oc[j][0] -= Oc[j][1]; 
                Ot = OL * envData[3][ENV];
              }
            }
            Ocf1 = envData[4][ENV] * OcF;  //filter freq
            Oc0 += Ocf1 * Oc1;
            Oc1 += Ocf1 * (Ot + Oc2 - OcQ * Oc1 - Oc0);  //bpf
            Oc2 = Ot;
            Ot = Oc1;
            break;
        }
      } 
          
      if(MainFilter==1) //filter overtones
      {
        if(t<envData[7][NEXTT])
          envData[7][ENV] = envData[7][ENV] + envData[7][dENV];
        else UpdateEnv(7, t);

        MFtmp = envData[7][ENV];
        if(MFtmp >0.2f)
          MFfb = 1.001f - (float)pow(10.0f, MFtmp - 1);
        else
          MFfb = 0.999f - 0.7824f * MFtmp; 
        
        MFtmp = Ot + MFres * (1.f + (1.f/MFfb)) * (MFin - MFout); 
        MFin = MFfb * (MFin - MFtmp) + MFtmp;
        MFout = MFfb * (MFout - MFin) + MFin;

        DF[t - tpos] = DF[t - tpos] + (MFout - (HighPass * Ot));
      }
      else if(MainFilter==2) //filter all
      {
        if(t<envData[7][NEXTT])
          envData[7][ENV] = envData[7][ENV] + envData[7][dENV];
        else UpdateEnv(7, t);

        MFtmp = envData[7][ENV];
        if(MFtmp >0.2f)
          MFfb = 1.001f - (float)pow(10.0f, MFtmp - 1);
        else
          MFfb = 0.999f - 0.7824f * MFtmp; 
        
        MFtmp = DF[t - tpos] + Ot + MFres * (1.f + (1.f/MFfb)) * (MFin - MFout); 
        MFin = MFfb * (MFin - MFtmp) + MFtmp;
        MFout = MFfb * (MFout - MFin) + MFin;
        
        DF[t - tpos] = MFout - (HighPass * (DF[t - tpos] + Ot));
      }
      else DF[t - tpos] = DF[t - tpos] + Ot; //no filter
    }
    
    if(DiON==1) //bit resolution
    {
      for(j=0; j<len; j++)
        DF[j] = DGain * (int)(DF[j] / DAtten);
      
      for(j=0; j<len; j+=DStep) //downsampling
      {
        DownAve = 0;
        DownStart = j;
        DownEnd = j + DStep - 1;
        for(jj = DownStart; jj<=DownEnd; jj++) 
          DownAve = DownAve + DF[jj];
        DownAve = DownAve / DStep;
        for(jj = DownStart; jj<=DownEnd; jj++) 
          DF[jj] = DownAve;
      }  
    }
    else for(j=0; j<len; j++) DF[j] *= DGain;
    
    for(j = 0; j<len; j++) //clipping + output
    {
      if(DF[j] > clippoint) 
        buffer[wavewords++] = clippoint;
      else
        if(DF[j] < -clippoint)
          buffer[wavewords++] = -clippoint;
        else 
          buffer[wavewords++] = (short)DF[j];
    } 

    if(((100*tpos) / Length) > (progress + 4))
    {
      progress = (100*tpos) / Length;
      sprintf(percent,"%i%%", progress);
      //if(hWnd>0) SetWindowText(hWnd, percent);
      
    }
    tpos = tpos + len;
    //}

  /* if(wavemode==0) */
  /* { */
  /*   fwrite(wave, 2, Length, fp);  //write data */
  /*   fwrite(&WI,  1, 44, fp); //write INFO chunk */
  /*   fwrite(&comment, 1, commentLen, fp); */
  /*   fclose(fp); */
  /* } */
  /* wavemode = 0; //force compatibility!! */
  /* busy = 0; //success!!  */

  return wavewords;
}
Example #2
0
int main()
{
	InitDaemon();

	FILE * pf = fopen("PidStore.txt","w");
	fprintf(pf,"%d\n",getpid());
	fclose(pf);	

	s[0] = (Sensor *)malloc(sizeof(Sensor));
	s[1] = (Sensor *)malloc(sizeof(Sensor));
	s[2] = (Sensor *)malloc(sizeof(Sensor));
	s[3] = (Sensor *)malloc(sizeof(Sensor));
	s[4] = (Sensor *)malloc(sizeof(Sensor));
	s[0]->relaySta = s[1]->relaySta = s[2]->relaySta = s[3]->relaySta = CLOSE;
	ctlSta = AUTO;

	signal(SIG_SET,Action);
	signal(SIG_GET,Action);
	
	int counter = 0;
	
	env = (Env *) malloc(sizeof(Env));
	env->temp = env->humid = env->illumi = \
	env->thres.tempH = env->thres.tempL = env->thres.humidH = env->thres.humidL =\
	env->thres.illumiH = env->thres.illumiL = 0;
	while(1)
	{
		g_Sensor_status * sstatus = (g_Sensor_status *)malloc(sizeof(g_Sensor_status));
		FILE * flog = fopen("AutoMachine.log","ab+");
		fprintf(flog,"%dth loops...\n",counter);
		fclose(flog);
		GetData(sstatus);
		UpdateEnv(sstatus,env);

		counter++;
		/*
		if(counter % 1200 == 0)
		{
			WriteFile("exchange.txt",env);
			OutputEnv();	
		}
		else
		{
			if(counter >= 12000)
			{
				unlink("exchange.txt");
				counter = 0;
			}
		}*/
		if(counter < 180)
		{
			WriteFile("exchange.txt",env);
			OutputRFID("rfid.txt",sstatus);
			OutputEnv();	
		}
		else
		{
				unlink("exchange.txt");
				unlink("AutoMachine.log");
				unlink("env.txt");
				unlink("rfid.txt");
				counter = 0;
		}

		if(ctlSta == AUTO)
			AutoControl(sstatus,env,s);
	
		//SynRelay();
		free(sstatus);
			
		sleep(1);		
	}
	unlink("/web/clientfifo");
	unlink("/web/serverfifo");
	free(env);
	return 0;
}