Fade out at the end of a sound doesn't work

Hi,

I have still problems with the addFadePoint function.

I want to fade in and fade out on a sound. The fade in works, but the fade out is always to early.

I tried different ways, but I got always the same result.

Here is my main code for this

channel.getSystemObject(out system).ERRCHECK();

channel.getCurrentSound(out var currentSound).ERRCHECK();

currentSound.getFormat(out SOUND_TYPE type, out SOUND_FORMAT format, out int channels, out int bits).ERRCHECK();
system.getSoftwareFormat(out int samplerate, out SPEAKERMODE speakermode, out int numrawspeakers).ERRCHECK();

if (samplerate > 0 && bits > 0)
{
    system.lockDSP().ERRCHECK();

    channel.getDSPClock(out ulong dspclock, out ulong parentclock).ERRCHECK();

    channel.setDelay(0, 0, false).ERRCHECK();

    // milliseconds * samplerate / 1000
    var fadeDelay = Convert.ToUInt64(Math.Round(8000 * samplerate / 1000f, MidpointRounding.AwayFromZero));

    // add a fade point at 'now' with zero volume
    channel.addFadePoint(parentclock, 0f).ERRCHECK();
    // add a fade point 8 seconds later at 1 volume
    channel.addFadePoint(parentclock + fadeDelay, 1f).ERRCHECK();

    // PCM = PCM Samples, related to milliseconds * samplerate / 1000.
    currentSound.getLength(out uint length, TIMEUNIT.PCM).ERRCHECK();
    
    currentSound.getLength(out uint lengthMs, TIMEUNIT.MS).ERRCHECK();
    var convertedLength = Convert.ToUInt64(Math.Round(lengthMs * samplerate / 1000f, MidpointRounding.AwayFromZero));

    // using length or convertedLength doesn't work
    // both are to early!

    // add a start fade point 8 seconds before end with full volume
    channel.addFadePoint(convertedLength - fadeDelay, 1f).ERRCHECK();
    // add a fade point at the end of the track
    channel.addFadePoint(convertedLength, 0f).ERRCHECK();
    // add a delayed stop command at the end of the track ('stopchannels = true')
    channel.setDelay(0, convertedLength, true).ERRCHECK();

    system.unlockDSP().ERRCHECK();
}

system.update().ERRCHECK();

So I think that I have a bug in converting the time length. But I don’t know what…