Procedural Audio

Solved !

It was a bug with settting the Time value after filling the buffer! (Thanks for the heads-up !). So each time it started filling in the PCM data it would be starting with the wrong time value. Using the corrected code line below fixes the issue and it’s playing correctly now in the Sound Cue (play Cue).

This code works now, but is a little sloppy as I was focused on getting the mechanism working properly first. (So clean it up a bit if you want to use it for something real)



int32 USoundWaveProceduralTest::GeneratePCMData(uint8* PCMData, const int32 SamplesNeeded)
{
	float TimeStart = Time;

	Omega = 2.0f * PI * Frequency;	// angular frequency [rad/s]

	for(int i = 0; i < SamplesNeeded; i++)
	{
		Time = TimeStart + i * DeltaTime;
		int32 a = FMath::RoundToInt( 65535.0f * (1.0f + FMath::Sin(Omega*Time)) * 0.5f);
		if(a > 65535)
		{
			a = 65535;
		}
		else if( a < 0)
		{
			a = 0;
		}
		uint16 ua = (uint16)a;
		PCMData[2*i] = ua;			// Low Byte
		PCMData[2*i+1] = ua >> 8;	// High Byte
	}

	//Time = Time + SamplesNeeded * DeltaTime; // INCORRECT LINE
	Time = TimeStart + SamplesNeeded * DeltaTime; // CORRECTED LINE

	int32 BytesProvided = SamplesNeeded * 2;
	return BytesProvided;
}