Exemple #1
0
// Get position in seconds
float Controller::runningTime()
{
	return runningFrames() / Engine::mixer()->processingSampleRate();
}
// 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 );
}