// trigger_seam.cpp — see trigger_seam.h.

#include "core/instrument/map/trigger_seam.h"

#include <algorithm>

namespace reasampler::instrument::map {

std::int64_t triggerPlayLength(double lengthFraction,
                               std::int64_t frameCount,
                               std::int64_t startFrame) {
    const std::int64_t postStart = (std::max)(std::int64_t{0}, frameCount - startFrame);
    if (postStart <= 0 || lengthFraction <= 0.0) return 0;
    return static_cast<std::int64_t>(lengthFraction * static_cast<double>(postStart) + 0.5);
}

double framesToFadeFraction(std::int64_t fadeFrames, std::int64_t playLength) {
    if (playLength <= 0) return 0.0;
    return static_cast<double>(fadeFrames) / static_cast<double>(playLength);
}

std::int64_t fadeFractionToFrames(double fadeFraction, std::int64_t playLength) {
    if (playLength <= 0) return 0;
    return static_cast<std::int64_t>(fadeFraction * static_cast<double>(playLength) + 0.5);
}

}  // namespace reasampler::instrument::map
