BASS_FX_ReverseCreate position behaves differently for 24bit vs 16bit .wav

Started by Jumprocks,

Jumprocks

Hello :)

I've noticed some odd behavior when testing BASS FX reverse with 24bit wav files. Normally when passing a decode stream to BASS_FX_ReverseCreate and then setting BASS_ATTRIB_REVERSE_DIR to 1, the current position for the reverse stream gets set to 0. For a 24bit .wav, it stays at the end instead.

Here's a minimal reproduction in C# ManagedBass:

using ManagedBass;
using ManagedBass.Fx;

if (!Bass.Init()) throw new Exception($"{Bass.LastError}");

// I tried this on BASS 2.4.18.3 + FX 2.4.12.6
// Also tried BASS 2.4.18.28 + FX 2.4.12.20

Console.WriteLine($"BASS: {Bass.Version}");
Console.WriteLine($"BASS_FX: {BassFx.Version}");

// ffmpeg -f lavfi -i anullsrc=channel_layout=mono:sample_rate=44100 -c:a pcm_s16le -t 10 16bit.wav
check("16bit.wav");
// ffmpeg -f lavfi -i anullsrc=channel_layout=mono:sample_rate=44100 -c:a pcm_s24le -t 10 24bit.wav
check("24bit.wav");

void check(string file)
{
    var source = Bass.CreateStream(file, 0, 0, BassFlags.Decode);
    Console.WriteLine($"checking {file}");

    var reverse = BassFx.ReverseCreate(source, 5f, BassFlags.Default);

    double pos() => Bass.ChannelBytes2Seconds(reverse, Bass.ChannelGetPosition(reverse, PositionFlags.Bytes));

    Console.WriteLine($"  reverse: {pos()}");

    Bass.ChannelSetAttribute(reverse, ChannelAttribute.ReverseDirection, 1);

    Console.WriteLine($"  reverse again: {pos()}");

    Bass.StreamFree(reverse);
    Bass.StreamFree(source);
}

Bass.Free();

// Output on my machine:
// BASS: 2.4.18.28
// BASS_FX: 2.4.12.20
// checking 16bit.wav
//   reverse: 10
//   reverse again: 0
// checking 24bit.wav
//   reverse: 10
//   reverse again: 10

If needed I can convert this to C, but my C skills are quite lacking.
I've been testing on Windows.
The issue seems quite similar to this thread https://www.un4seen.com/forum/?topic=17757.0.

Thanks

Ian @ un4seen

I don't seem to be able to reproduce that here (I'm seeing pos=0 after switching to forwards), so please upload your 24bit.wav test file to have a look at:

   ftp://ftp.un4seen.com/incoming/

Jumprocks

Uploaded 24bit.wav

I also just ran the test script on my Linux system and got the same results:
OS: Ubuntu 26.04.1 LTS
BASS: 2.4.18.3
BASS_FX: 2.4.12.6
checking 16bit.wav
  reverse: 10
  reverse again: 0
checking 24bit.wav
  reverse: 10
  reverse again: 10

Ian @ un4seen

I'm still seeing pos=0 with your test file (using either the release or latest BASS/BASS_FX builds on Windows), so it's a bit of a mystery. I'm using C rather than .Net, but that shouldn't change the result. Check your Bass.ChannelSetAttribute call's result to see whether it's failing - that could explain the position not changing.

You could also try this (before the direction switch):

result = Bass.ChannelSetPosition(source, Bass.ChannelGetPosition(reverse, PositionFlags.Bytes), PositionFlags.Bytes);

That call should fail.

Jumprocks

Set attribute is returning true for both files.

Interestingly, the ChannelSetPosition call you wrote is returning False for the 16bit file (as you predicted), but True for the 24bit file.

var source = Bass.CreateStream("24bit.wav", 0, 0, BassFlags.Decode);
var reverse = BassFx.ReverseCreate(source, 5f, BassFlags.Default);
Console.WriteLine(Bass.ChannelGetPosition(source, PositionFlags.Bytes)); // 0
Console.WriteLine(Bass.ChannelGetPosition(reverse, PositionFlags.Bytes)); // 882000
var res = Bass.ChannelSetPosition(source, Bass.ChannelGetPosition(reverse, PositionFlags.Bytes), PositionFlags.Bytes);
Console.WriteLine(res); // True for the 24bit file, False for 16bit

I decided to try converting it to C (Linux). Getting the same results:
#include <stdio.h>
#include "bass.h"
#include "bass_fx.h"

int main(void)
{
    if (!BASS_Init(-1, 44100, 0, 0, NULL)) {
        return 1;
    }

    HSTREAM source = BASS_StreamCreateFile(BASS_FILE_NAME, "24bit.wav", 0, 0, BASS_STREAM_DECODE);
    HSTREAM reverse = BASS_FX_ReverseCreate(source, 5.0, BASS_FX_FREESOURCE);
    bool res = BASS_ChannelSetPosition(source, BASS_ChannelGetPosition(reverse, BASS_POS_BYTE), BASS_POS_BYTE);
    printf("res: %d\n", res); // 1 for 24bit, 0 for 16bit

    BASS_StreamFree(reverse);
    BASS_Free();
}
C is using BASS 2.4.18.3 + FX 2.4.12.6

Ian @ un4seen

Quote from: JumprocksInterestingly, the ChannelSetPosition call you wrote is returning False for the 16bit file (as you predicted), but True for the 24bit file.

I have an idea what's happening now. There's a floating-point calculation in that call, which will be using an inexact fraction (16/24) in the 24-bit file's case. Depending on the FPU and its precision setting, that may end up getting rounded down, resulting in the call succeeding instead of failing.

Here's an update for you to try:

    www.un4seen.com/stuff/bass.zip

Let me know if the ChannelSetPosition call still succeeds or you still have the problem with switching the reverse stream to forward.

Jumprocks

That fixed it.

ChannelSetPosition now fails for both 16/24bit files and the position after the second reverse is 0 for both files. I've been testing on 64bit systems for all of this.

Thank you!