playSound returns ERR_INVALID_POSITION for user created stream.

Hi,

I’m trying to replicate user_created_sound low level cpp sample in Unity, but getting error from %subj%.
I have

...
FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
exinfo.numchannels = 2;
exinfo.defaultfrequency = 44100;
exinfo.decodebuffersize = 44100;
exinfo.length = (uint)( exinfo.defaultfrequency * exinfo.numchannels * 2 * 5 ); // 2 == sizeof(short) or Int16 or two bytes per sample in channel
exinfo.format = FMOD.SOUND_FORMAT.PCM16;
exinfo.pcmreadcallback = this.pcmreadcallback;
exinfo.pcmsetposcallback = this.pcmsetposcallback;

result = system.createSound( "", FMOD.MODE.OPENUSER | FMOD.MODE.CREATESTREAM, ref exinfo, out sound);
// here result == FMOD.RESULT.OK

result = system.playSound(sound, null, false, out channel);
// here result == FMOD.RESULT.ERR_INVALID_POSITION
...

Both callbacks defined as specified by respective delegate get called from/by createSound with proper parameters… , but playSound always fails with seeking error ( no callbacks, obviously, are called afterwards ); is this an overlook in the c# wrapper / unity plugin, or am I doing something very stupid ?

Thanks!

See http://www.fmod.org/documentation/#content/generated/engine_new_unity/script_example_timeline.html for example of using C# callbacks and avoiding GC issues. Please update with the content of the callbacks if you’re still having issues.

Thanks, but still the same result / the callbacks are (for testing) empty, just returning immediately:

[AOT.MonoPInvokeCallback(typeof(FMOD.SOUND_PCMREADCALLBACK))]
FMOD.RESULT PCMReadCallback(System.IntPtr soundraw, System.IntPtr data, uint datalen) {
return FMOD.RESULT.OK;
}

[AOT.MonoPInvokeCallback(typeof(FMOD.SOUND_PCMSETPOSCALLBACK))]
FMOD.RESULT PCMSetPosCallback(System.IntPtr soundraw, int subsound, uint position, FMOD.TIMEUNIT postype)
{
return FMOD.RESULT.OK;
}

FMOD.SOUND_PCMREADCALLBACK pcmreadcallback;
FMOD.SOUND_PCMSETPOSCALLBACK pcmsetposcallback;

void Start() {
this.pcmreadcallback = new FMOD.SOUND_PCMREADCALLBACK(PCMReadCallback);
this.pcmsetposcallback = new FMOD.SOUND_PCMSETPOSCALLBACK(PCMSetPosCallback);

FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
...
}

both of them are called by createSound, but playSound still returns ERR_INVALID_POSITION

( empty read callback just for testing, when it has its body:

float t1 = 0, t2 = 0; // time
float v1 = 0, v2 = 0; // velocity

[AOT.MonoPInvokeCallback(typeof(FMOD.SOUND_PCMREADCALLBACK))]
FMOD.RESULT PCMReadCallback(System.IntPtr soundraw, System.IntPtr data, uint datalen)
{
for (int count = 0; count < (datalen >> 2); count++) // >>2 = 16bit stereo (4 bytes per sample)
{
System.Runtime.InteropServices.Marshal.WriteInt16(data, 0, (short)(Mathf.Sin(t1) * 32767f));
System.Runtime.InteropServices.Marshal.WriteInt16(data, 1, (short)(Mathf.Sin(t2) * 32767f));

t1 += 0.01f + v1;
t2 += 0.0142f + v2;
v1 += (float)(Mathf.Sin(t1) * 0.002f);
v2 += (float)(Mathf.Sin(t2) * 0.002f);
}

return FMOD.RESULT.OK;
}

the result is still the same )

( sorry about the formatting, apparently it isn’t recognized in the comment / )

There’s a bug in the C# API wrapper. The fix will be released in 1.08.11.

You can see the fix here:

Thanks for the info and link !