Esempio n. 1
0
void simple_envelope::process(unsigned int buf_size, sample &in, sample &CV, bool smooth)
{
  // a bit of a crap filter to smooth clicks
  static float SMOOTH = 0.999;
  static float ONEMINUS_SMOOTH = 1-SMOOTH;
  float one_over_decay=1/m_decay;
  float temp=0;

  if (m_t==-1000)
    {
      in.zero();
      CV.zero();
      m_current=0;
      return;
    }

  for (unsigned int n=0; n<buf_size; n++)
    {
      // if we are in the delay (before really being triggered)
      if (m_t<0)
	{
	  in[n]*=m_current;
	  CV[n]=m_current;
	}
      else // in the envelope
	{
	  // if we are in the envelope...
	  if (m_t<m_decay)
	    {
	      // in the decay
	      temp=(1-m_t*one_over_decay)*m_volume;
	      if (!feq(temp,m_current,0.01) && smooth)
		{
		  // only filter if necc
		  temp=(temp*ONEMINUS_SMOOTH+m_current*SMOOTH);
		}
	      in[n]*=temp;
	      CV[n]=temp;
	      m_current=temp;
	    }
	  else
	    {
	      in[n]*=0;
	      CV[n]=0;
	      m_current=0;

	      // we've run off the end
	      m_t=-1000;
	    }
	}

      m_t+=m_sample_time;
    }
}
Esempio n. 2
0
void envelope::process(unsigned int buf_size, sample &CV, bool smooth)
{
  if (m_attack==0 && m_decay==0 && m_release==0)
    {
      return;
    }

  smooth=true;

  // a bit of a crap filter to smooth clicks
  static float SMOOTH = 0.98;
  static float ONEMINUS_SMOOTH = 1-SMOOTH;

  float temp=0;
  bool freeze=false;
  float nt;

  if (m_t==-1000)
    {
      CV.zero();
      m_current=0;
      return;
    }


  for (unsigned int n=0; n<buf_size; n++)
    {

      // if we are in the delay (before really being triggered)
      if (m_t<0)
	{
	  float temp=0;
	  if (!feq(temp,m_current,0.01) && smooth)
	    {
	      // only filter if necc
	      temp=(temp*ONEMINUS_SMOOTH+m_current*SMOOTH);
	    }
	  CV[n]=temp;
	  m_current=temp;
	  m_t+=m_sample_time;
	}
      else // in the envelope
	{
	  // if we are in the envelope...
	  if (m_t>=0 && m_t<m_attack+m_decay+m_release)
	    {
	      // find out what part of the envelope we are in
	      // in the attack
	      if (m_t<m_attack)
		{
		  // get normalised position to
		  // get the volume between 0 and 1
		  temp=m_t/m_attack;
		}
	      else
		// in the decay
		if (m_t<m_attack+m_decay)
		  {
		    // normalised position in m_attack->m_decay range
		    nt=(m_t-m_attack)/m_decay;

		    // volume between 1 and m_sustain
		    temp=(1-nt)+(m_sustain*nt);
		  }
		else // in the release
		  {
		    // normalised position in m_decay->m_release range
		    nt=(m_t-(m_attack+m_decay))/m_release;

		    // volume between m_sustain and 0
		    temp=m_sustain*(1-nt);

		    if (m_release<0.2f)
		      {
			temp=m_sustain;
		      }

		    //if (m_trigger) freeze=true;
		  }

	      temp*=m_volume;

	      if (!feq(temp,m_current,0.01) && smooth)
		{
		  // only filter if necc
		  temp=(temp*ONEMINUS_SMOOTH+m_current*SMOOTH);
		}
	      CV[n]=temp;
	      m_current=temp;

	      if (!freeze) m_t+=m_sample_time;
	    }
	  else
	    {
	      if (!feq(temp,m_current,0.01) && smooth)
		{
		  temp=m_current*SMOOTH;
		}

	      CV[n]=temp;
	      m_current=temp;

	      // if we've run off the end
	      if (m_t>m_attack+m_decay+m_release)
		{
		  m_t=-1000;
		}
	    }
	}
    }
}