Files
ADOFAIRenderer/Util/Audio/AudioSyncCalculator.cs
T
2026-10-11 16:45:18 +09:00

35 lines
910 B
C#

using System;
namespace ADOFAIRenderer.Util.Audio;
internal class AudioSyncCalculator
{
private readonly long _sampleRate;
private readonly (long Numerator, long Denominator) _fpsRational;
// ReSharper disable once NotAccessedField.Local
private long _currentSample;
private long _remainder;
public AudioSyncCalculator(long sampleRate, double fps)
{
_sampleRate = sampleRate;
_fpsRational = RationalUtil.Double2MaxPrecisionInt(fps);
}
public void Reset()
{
_currentSample = 0;
_remainder = 0;
}
public int GetNextFrameSamples()
{
var numerator = _sampleRate * _fpsRational.Denominator + _remainder;
var samplesNeeded = numerator / _fpsRational.Numerator;
_remainder = numerator % _fpsRational.Numerator;
_currentSample += samplesNeeded;
return (int)samplesNeeded;
}
}