Kode / Kha

Ultra-portable, high performance, open source multimedia framework.
http://kha.tech
zlib License
1.49k stars 174 forks source link

HL/hxcpp: audio2.Audio.samplesPerSecond is 0 #1360

Closed MoritzBrueckner closed 3 years ago

MoritzBrueckner commented 3 years ago

Describe the bug When using hxcpp on Windows, kha.audio2.Audio.samplesPerSecond is 0 after System.start() run successfully:

System.start({title: "Test", width: 1024, height: 768}, function(_) {
    trace(kha.audio2.Audio.samplesPerSecond);
    // Output: 0
}

This is independent of whether DirectSound or WASAPI is used.

On HL/C for example, the sample rate gets correctly set by a call to kore_init_audio(), on hxcpp I could not find an equivalent call.

Execution Environment:


A few other related problems that I want to mention here (I will open issues for them eventually but I first have to do some more tests):

MoritzBrueckner commented 3 years ago

Unfortunately I now experienced the same issue with hl and I don't know why it didn't happen yesterday. But the good news is that I found a possible solution for hl (not yet for hxcpp).

The call to kore_init_audio() currently looks like this on hl:

https://github.com/Kode/Kha/blob/96528518c62ba4279cfac793d0a9a319d1892624/Backends/Kinc-HL/kha/SystemImpl.hx#L42

The last parameter is implicitly converted to a hl.Ref<Int>, but apparently the reference in the generated c code doesn't point to the integer holding the sample rate, but to a copy of that integer. I'm not sure if this is an issue with hl or expected behaviour, but there is a simple workaround in Kha:

final samplesRef: hl.Ref<Int> = kha.audio2.Audio.samplesPerSecond;
kore_init_audio(kha.audio2.Audio._callCallback, kha.audio2.Audio._readSample, samplesRef);
kha.audio2.Audio.samplesPerSecond = samplesRef.get();

@ RobDangerous Would this fix in Kha be ok or is it actually a bug in hl that needs to be fixed?


Edit: I also found the issue with hxcpp, the samplerate is assigned in run_kinc(), however that is called after the System.start() callback has run:

https://github.com/Kode/Kha/blob/96528518c62ba4279cfac793d0a9a319d1892624/Backends/Kinc-hxcpp/kha/SystemImpl.hx#L173-L175

RobDangerous commented 3 years ago

The fix is fine by me but I have no idea whether it's an HL-bug - maybe ask Nic about it.

MoritzBrueckner commented 3 years ago

Yeah it seems to be a known Haxe or HL bug: https://github.com/HaxeFoundation/haxe/issues/9803. There doesn't seem to be much activity on this, so for now I would propose to use the workaround from above: https://github.com/Kode/Kha/pull/1361.

As for the hxcpp issue: should there be a similar function like kore_init_audio for Kinc-hxcpp? I guess it wouldn't work well to swap run_kinc() and calling the callback...

RobDangerous commented 3 years ago

Yes, something similar was needed.