Example #1
0
void LfoController::updateValueBuffer()
{
	m_phaseOffset = m_phaseModel.value() / 360.0;	
	float * values = m_valueBuffer.values();	
	float phase = m_currentPhase + m_phaseOffset;

	// roll phase up until we're in sync with period counter
	m_bufferLastUpdated++; 
	if( m_bufferLastUpdated < s_periods )
	{
		int diff = s_periods - m_bufferLastUpdated;
		phase += static_cast<float>( engine::framesPerTick() * diff ) / m_duration;
		m_bufferLastUpdated += diff;
	}


	for( int i = 0; i < m_valueBuffer.length(); i++ )
	{		
		const float currentSample = m_sampleFunction != NULL 
			? m_sampleFunction( phase )
			: m_userDefSampleBuffer->userWaveSample( phase );
			
		values[i] = qBound( 0.0f, m_baseModel.value() + ( m_amountModel.value() * currentSample / 2.0f ), 1.0f );

		phase += 1.0 / m_duration;
	}
	
	m_currentPhase = absFraction( phase - m_phaseOffset );
}
Example #2
0
// This code took forever to get right. It can
// definately be optimized.
// The code should probably be integrated with the oscillator class. But I
// don't know how to use oscillator because it is so confusing
float LfoController::value( int _offset )
{
	int frame = runningFrames() + _offset + m_phaseCorrection;

	//If the song is playing, sync the value with the time of the song.
	if( engine::getSong()->isPlaying() || engine::getSong()->isExporting() )
	{
		// The new duration in frames
		// (Samples/Second) / (periods/second) = (Samples/cycle)
		float newDurationF =
				engine::mixer()->processingSampleRate() *
				m_speedModel.value();

		switch(m_multiplierModel.value() )
		{
			case 1:
				newDurationF /= 100.0;
				break;

			case 2:
				newDurationF *= 100.0;
				break;

			default:
				break;
		}

		m_phaseOffset = qRound(
			m_phaseModel.value() * newDurationF / 360.0 );


		int newDuration = static_cast<int>( newDurationF );
		m_phaseCorrection = static_cast<int>(engine::getSong()->getTicks()*engine::framesPerTick())%newDuration
								+ m_phaseOffset;

		// re-run the first calculation again
		frame = m_phaseCorrection + _offset;
	}

	// speedModel  0..1   fast..slow  0ms..20000ms
	// duration m_duration
	//

	//  frames / (20seconds of frames)
	float sampleFrame = float( frame+m_phaseOffset ) / 
		(engine::mixer()->processingSampleRate() *  m_speedModel.value() );

	switch(m_multiplierModel.value() )
	{
		case 1:
			sampleFrame *= 100.0;
			break;

		case 2:
			sampleFrame /= 100.0;
			break;

		default:
			break;
	}

	// 44100 frames/sec
	return m_baseModel.value() + ( m_amountModel.value() * 
				( m_sampleFunction != NULL ?
					m_sampleFunction(sampleFrame):
					m_userDefSampleBuffer->userWaveSample(sampleFrame) )
			/ 2.0f );
}