예제 #1
0
/// ------------------------------
/// PROCESS
/// ------------------------------
void TimedFader::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames)
{
	float* in1 = inputs[0];
	float* in2 = inputs[1];
	float* out1 = outputs[0];
	float* out2 = outputs[1];

	float max1 = 0;
	float max2 = 0;

	VstTimeInfo* timeInfo = NULL;
	bool audioIsPlaying = true;

	--in1;
	--in2;
	--out1;
	--out2;

	VstInt32 maxCounter = sampleFrames;
	while (--maxCounter >= 0)
	{
		if (fabs(inputs[0][maxCounter]) > max1)
		{
			max1 = fabs(inputs[0][maxCounter]);
		}
		if (fabs(inputs[1][maxCounter]) > max2)
		{
			max2 = fabs(inputs[1][maxCounter]);
		}
	}

	if (max1 < 0.000001 && max2 < 0.000001)
	{
		audioIsPlaying = false;
	}

	// In any state, if no audio is playing, go to Idle
	if (!audioIsPlaying)
	{
		state = STATE_IDLE;
	}

	switch (state)
	{
	case STATE_PLAYING:	
		// Do nothing else, just let audio play
		while (fPlaySecondsRemaining > 0.0 && --sampleFrames >= 0)
		{
			fPlaySecondsRemaining -= fSecondsPerFrame;
			// let audio pass through
			*++out1 = *++in1 * 1.0;
			*++out2 = *++in2 * 1.0;
		}

		if (fPlaySecondsRemaining <= 0.0)
		{
			state = STATE_FADING;
		}
		faderDisplay->setParameter(PARAM_PlaySecondsRemaining, fPlaySecondsRemaining);
		break;
	case STATE_FADING:
		while (fFadeSecondsRemaining > 0.0 && --sampleFrames >= 0)
		{
			currentFaderFrameCount++;
			fFadeSecondsRemaining -= fSecondsPerFrame;
			// Fade based on current fade tap
			currentFaderMultiplier = GetNextFaderStep();
			*++out1 = *++in1 * currentFaderMultiplier;
			*++out2 = *++in2 * currentFaderMultiplier;
		}

		if (fFadeSecondsRemaining <= 0.0)
		{
			// silence out the rest of the sample
			while (--sampleFrames >= 0)
			{
				*++out1 = *++in1 * muteGain;
				*++out2 = *++in2 * muteGain;
			}
			faderDisplay->setParameter(PARAM_FaderLight, 1);
			state = STATE_DONE;
		}
		faderDisplay->setParameter(PARAM_FadeSecondsRemaining, fFadeSecondsRemaining);
		break;
	case STATE_DONE:
		fPlaySecondsRemaining = fPlayDuration;
		fFadeSecondsRemaining = fFadeDuration;
		// make sure the audio stays muted
		// and wait here until all audio stops playing, then move back to Idle
		while (--sampleFrames >= 0)
		{
			*++out1 = *++in1 * muteGain;
			*++out2 = *++in2 * muteGain;
		}
		break;
	case STATE_IDLE:
		// Set these here again in case someone changed the durations while we are in the IDLE state
		currentFaderFrameCount = 1;
		fPlaySecondsRemaining = fPlayDuration;
		fFadeSecondsRemaining = fFadeDuration;
		faderDisplay->setParameter(PARAM_PlaySecondsRemaining, fPlayDuration);
		faderDisplay->setParameter(PARAM_FadeSecondsRemaining, fFadeDuration);
		faderDisplay->setParameter(PARAM_FaderLight, 0);
		/* Wish this worked...
		//timeInfo = getTimeInfo(kVstTransportPlaying | kVstTransportChanged);
		if (timeInfo != NULL && (timeInfo->flags & kVstTransportPlaying) )
		{
		// start playing
		state = STATE_PLAYING;
		} */
		// Instead, messy hack:
		if (fabs(inputs[0][0]) > 0.0000001 && fabs(inputs[1][0]) > 0.0000001)
		{
			// Audio is coming through, start the timer...
			state = STATE_PLAYING;
		}
		break;
	}
}
예제 #2
0
//---------------------------------------------------------------------------
HRESULT VDJ_API TimedFader::OnProcessSamples(float *buffer, int nb)
{
	// ADD YOUR AUDIO TREATMENT CODE HERE USING THE AUDIO BUFFER *buffer OF 2*nb FLOAT SAMPLES (STEREO SIGNAL)
	float out[2], in[2];

	float max1 = 0;
	float max2 = 0;

	bool audioIsPlaying = true;

	// Check the current buffer for any audio, if none, audio is not playing
	for (int i = 0; i<nb; i++)
	{
		in[0] = buffer[2 * i];
		in[1] = buffer[2 * i + 1];

		if (fabs(in[0]) > max1)
		{
			max1 = fabs(in[0]);
		}
		if (fabs(in[1]) > max2)
		{
			max2 = fabs(in[1]);
		}
	}
	if (max1 < 0.000001 && max2 < 0.000001)
	{
		audioIsPlaying = false;
	}

	// In any state, if no audio is playing, go to Idle
	if (!audioIsPlaying)
	{
		state = STATE_IDLE;
	}

	int index = 0;
	switch (state)
	{
	case STATE_PLAYING:
		// Do nothing else, just let audio play
		while (fPlaySecondsRemaining > 0.0 && index < nb)
		{
			/*in[0] = buffer[2 * index];
			in[1] = buffer[2 * index + 1];
			*/	
			fPlaySecondsRemaining -= fSecondsPerFrame;
			// let audio pass through
			/*
			buffer[2 * index] = in[0] * 1.0;
			buffer[2 * index + 1] = in[1] * 1.0;
			*/
			index++;
		}

		if (fPlaySecondsRemaining <= 0.0)
		{
			state = STATE_FADING;
		}
		//faderDisplay->setParameter(PARAM_PlaySecondsRemaining, fPlaySecondsRemaining);
		break;
	case STATE_FADING:
		while (fFadeSecondsRemaining > 0.0 && index < nb)
		{
			currentFaderFrameCount++;
			fFadeSecondsRemaining -= fSecondsPerFrame;
			// Fade based on current fade tap
			currentFaderMultiplier = GetNextFaderStep();			
			in[0] = buffer[2 * index];
			in[1] = buffer[2 * index + 1];
			out[0] = in[0] * currentFaderMultiplier;
			out[1] = in[1] * currentFaderMultiplier;

			buffer[2 * index] = out[0];
			buffer[2 * index + 1] = out[1];
			index++;
		}

		if (fFadeSecondsRemaining <= 0.0)
		{
			// silence out the rest of the sample
			for (int i = 0; i < nb; i++)
			{
				buffer[2 * i] *= muteGain;
				buffer[2 * i + 1] *= muteGain;
			}
			//faderDisplay->setParameter(PARAM_FaderLight, 1);
			state = STATE_DONE;
		}
		//faderDisplay->setParameter(PARAM_FadeSecondsRemaining, fFadeSecondsRemaining);
		break;
	case STATE_DONE:
		fPlaySecondsRemaining = fPlayDuration;
		fFadeSecondsRemaining = fFadeDuration;
		// make sure the audio stays muted
		// and wait here until all audio stops playing, then move back to Idle
		for (int i = 0; i < nb; i++)
		{
			buffer[2 * i] *= muteGain;
			buffer[2 * i + 1] *= muteGain;
		}
		break;
	case STATE_IDLE:
		// Set these here again in case someone changed the durations while we are in the IDLE state
		currentFaderFrameCount = 1;
		fPlaySecondsRemaining = fPlayDuration;
		fFadeSecondsRemaining = fFadeDuration;
		//faderDisplay->setParameter(PARAM_PlaySecondsRemaining, fPlayDuration);
		//faderDisplay->setParameter(PARAM_FadeSecondsRemaining, fFadeDuration);
		//faderDisplay->setParameter(PARAM_FaderLight, 0);

		// Instead, messy hack:
		//if (fabs(buffer[0]) > 0.0000001 && fabs(buffer[1]) > 0.0000001)
		if (audioIsPlaying)
		{
			// Audio is coming through, start the timer...
			state = STATE_PLAYING;
		}
		break;
	}
	UpdateDisplay();
	return S_OK;
}