示例#1
0
//actual stretch algorithm implementation
void SndBuffer::UpdateTempoChangeSoundTouch2()
{

    long targetSamplesReservoir = 48 * SndOutLatencyMS;  //48000*SndOutLatencyMS/1000
    //base aim at buffer filled %
    float baseTargetFullness = (double)targetSamplesReservoir;  ///(double)m_size;//0.05;

    //state vars
    static bool inside_hysteresis;       //=false;
    static int hys_ok_count;             //=0;
    static float dynamicTargetFullness;  //=baseTargetFullness;
    if (gRequestStretcherReset >= STRETCHER_RESET_THRESHOLD) {
        ConLog("______> stretch: Reset.\n");
        inside_hysteresis = false;
        hys_ok_count = 0;
        dynamicTargetFullness = baseTargetFullness;
    }

    int data = _GetApproximateDataInBuffer();
    float bufferFullness = (float)data;  ///(float)m_size;

#ifdef NEWSTRETCHER_USE_DYNAMIC_TUNING
    {  //test current iterations/sec every 0.5s, and change algo params accordingly if different than previous IPS more than 30%
        static long iters = 0;
        static wxDateTime last = wxDateTime::UNow();
        wxDateTime unow = wxDateTime::UNow();
        wxTimeSpan delta = unow.Subtract(last);
        if (delta.GetMilliseconds() > 500) {
            int pot_targetIPS = 1000.0 / delta.GetMilliseconds().ToDouble() * iters;
            if (!IsInRange(pot_targetIPS, int((float)targetIPS / 1.3f), int((float)targetIPS * 1.3f))) {
                if (MsgOverruns())
                    ConLog("Stretcher: setting iters/sec from %d to %d\n", targetIPS, pot_targetIPS);
                targetIPS = pot_targetIPS;
                AVERAGING_WINDOW = GetClamped((int)(50.0f * (float)targetIPS / 750.0f), 3, (int)AVERAGING_BUFFER_SIZE);
            }
            last = unow;
            iters = 0;
        }
        iters++;
    }
#endif

    //Algorithm params: (threshold params (hysteresis), etc)
    const float hys_ok_factor = 1.04f;
    const float hys_bad_factor = 1.2f;
    int hys_min_ok_count = GetClamped((int)(50.0 * (float)targetIPS / 750.0), 2, 100);  //consecutive iterations within hys_ok before going to 1:1 mode
    int compensationDivider = GetClamped((int)(100.0 * (float)targetIPS / 750), 15, 150);

    float tempoAdjust = bufferFullness / dynamicTargetFullness;
    float avgerage = addToAvg(tempoAdjust);
    tempoAdjust = avgerage;

    // Dampen the adjustment to avoid overshoots (this means the average will compensate to the other side).
    // This is different than simply bigger averaging window since bigger window also has bigger "momentum",
    // so it's slower to slow down when it gets close to the equilibrium state and can therefore resonate.
    // The dampening (sqrt was chosen for no very good reason) manages to mostly prevent that.
    tempoAdjust = sqrt(tempoAdjust);

    tempoAdjust = GetClamped(tempoAdjust, 0.05f, 10.0f);

    if (tempoAdjust < 1)
        baseTargetFullness /= sqrt(tempoAdjust);  // slightly increase latency when running slow.

    dynamicTargetFullness += (baseTargetFullness / tempoAdjust - dynamicTargetFullness) / (double)compensationDivider;
    if (IsInRange(tempoAdjust, 0.9f, 1.1f) && IsInRange(dynamicTargetFullness, baseTargetFullness * 0.9f, baseTargetFullness * 1.1f))
        dynamicTargetFullness = baseTargetFullness;

    if (!inside_hysteresis) {
        if (IsInRange(tempoAdjust, 1.0f / hys_ok_factor, hys_ok_factor))
            hys_ok_count++;
        else
            hys_ok_count = 0;

        if (hys_ok_count >= hys_min_ok_count) {
            inside_hysteresis = true;
            if (MsgOverruns())
                ConLog("======> stretch: None (1:1)\n");
        }

    } else if (!IsInRange(tempoAdjust, 1.0f / hys_bad_factor, hys_bad_factor)) {
        if (MsgOverruns())
            ConLog("~~~~~~> stretch: Dynamic\n");
        inside_hysteresis = false;
        hys_ok_count = 0;
    }

    if (inside_hysteresis)
        tempoAdjust = 1.0;

    if (MsgOverruns()) {
        static int iters = 0;
        static wxDateTime last = wxDateTime::UNow();
        wxDateTime unow = wxDateTime::UNow();
        wxTimeSpan delta = unow.Subtract(last);

        if (delta.GetMilliseconds() > 1000) {  //report buffers state and tempo adjust every second
            ConLog("buffers: %4d ms (%3.0f%%), tempo: %f, comp: %2.3f, iters: %d, (N-IPS:%d -> avg:%d, minokc:%d, div:%d) reset:%d\n",
                   (int)(data / 48), (double)(100.0 * bufferFullness / baseTargetFullness), (double)tempoAdjust, (double)(dynamicTargetFullness / baseTargetFullness), iters, (int)targetIPS, AVERAGING_WINDOW, hys_min_ok_count, compensationDivider, gRequestStretcherReset);
            last = unow;
            iters = 0;
        }
        iters++;
    }

    pSoundTouch->setTempo(tempoAdjust);
    if (gRequestStretcherReset >= STRETCHER_RESET_THRESHOLD)
        gRequestStretcherReset = 0;

    return;
}
示例#2
0
//actual stretch algorithm implementation
void SndBuffer::UpdateTempoChangeSoundTouch2()
{
	//base aim at buffer filled %
	float targetFullness=0.1;

	//threshold params (hysteresis)
	static const float hys_ok_factor=1.03;
	static const int hys_min_ok_count=100; //consecutive iterations within hys_ok before going to 1:1 mode
	static const float hys_bad_factor=1.2;

	//state vars
	static bool inside_hysteresis=false;
	static int hys_ok_count=0;

	//some precalculated values
	static const float hys_ok_min=1.0/hys_ok_factor;
	static const float hys_ok_max=hys_ok_factor;
	static const float hys_bad_min=1.0/hys_bad_factor;
	static const float hys_bad_max=hys_bad_factor;

	float bufferFullness=(float)m_data/(float)m_size;
	static float last_bufferFullness=0;
	if(last_bufferFullness != bufferFullness){// only recalculate if buffer changes
		last_bufferFullness = bufferFullness;
		
		float tempoAdjust=bufferFullness/targetFullness;
		float avgerage = addToAvg(tempoAdjust);
		tempoAdjust = avgerage;
		if( tempoAdjust>1.2 ) tempoAdjust=0.2+pow(tempoAdjust-0.2f, 3);//reduce latency for faster speeds only
		tempoAdjust = clamp( tempoAdjust, 0.1f, 10.0f);

		if( !inside_hysteresis )
		{
			if( tempoAdjust == clamp( tempoAdjust, hys_ok_min, hys_ok_max ) )
				hys_ok_count++;
			else
				hys_ok_count=0;

			if( hys_ok_count >= hys_min_ok_count ){
				inside_hysteresis=true;
				if(MsgOverruns()) printf("======> stretch: None (1:1)\n");
			}

		}
		else if( tempoAdjust != clamp( tempoAdjust, hys_bad_min, hys_bad_max ) ){
			if(MsgOverruns()) printf("~~~~~~> stretch: Dynamic\n");
			inside_hysteresis=false;
			hys_ok_count=0;
		}

		if(inside_hysteresis)
			tempoAdjust=1.0;

		if(MsgOverruns()){
			static int iters=0;
			static wxDateTime last=wxDateTime::UNow();
			wxDateTime unow=wxDateTime::UNow();
			wxTimeSpan delta = unow.Subtract(last);

			if(delta.GetMilliseconds()>1000){//report buffers state and tempo adjust every second
				printf("buffers: %f, actual adjust: %f, iterations: %d\n", bufferFullness, tempoAdjust, iters);
				last=unow;
				iters=0;
			}
			iters++;
		}

		pSoundTouch->setTempo(tempoAdjust);
		
		//collect some stats...
		if(tempoAdjust==1.0)
			ts_stats_normalblocks++;
		else
			ts_stats_stretchblocks++;

	}

	return;
}