hexops / mach

zig game engine & graphics toolkit
https://machengine.org
Other
3.01k stars 145 forks source link

sysaudio: opus play.zig example crashes after a few seconds of playing #1160

Open TemariVirus opened 5 months ago

TemariVirus commented 5 months ago

Expected behaviour: running zig build run-example plays time.opus on loop without error Actual behaviour: after a few seconds of playing normally, it crashes with this error:

thread 22228 panic: integer part of floating point value out of bounds
C:\Users\user\AppData\Local\zig\p\12203f0d4afc83bae8571434ff48a459732c15c9542df13e0a97baedc73212d355a1\src\conv.zig:338:47: 0xd81dfc in floatToSignedSIMD__anon_11417 (example-play.exe.obj)
        dst[i..][0..vec_size].* = @as(VecDst, @intFromFloat(src_vec * max));
                                              ^
C:\Users\user\AppData\Local\zig\p\12203f0d4afc83bae8571434ff48a459732c15c9542df13e0a97baedc73212d355a1\src\conv.zig:292:26: 0xd65f47 in floatToSigned__anon_10458 (example-play.exe.obj)
        floatToSignedSIMD(SrcType, src, DestType, dst);
                         ^
C:\Users\user\AppData\Local\zig\p\12203f0d4afc83bae8571434ff48a459732c15c9542df13e0a97baedc73212d355a1\src\main.zig:339:38: 0xd1e830 in convertTo__anon_7577 (example-play.exe.obj)
            f32 => conv.floatToSigned(SrcType, src, i16, @as([*]i16, @ptrCast(@alignCast(dst)))[0..dst_len]),
                                     ^
C:\Users\user\repos\mach-opus\examples\play.zig:62:23: 0xd151e3 in writeCallback (example-play.exe.obj)
    sysaudio.convertTo(
                      ^
C:\Users\user\AppData\Local\zig\p\12203f0d4afc83bae8571434ff48a459732c15c9542df13e0a97baedc73212d355a1\src\wasapi.zig:807:31: 0xd840cd in writeThread (example-play.exe.obj)
                player.writeFn(
                              ^
C:\Program Files\Zig\lib\std\Thread.zig:411:13: 0xd67b2c in callFn__anon_10487 (example-play.exe.obj)
            @call(.auto, f, args);
            ^
C:\Program Files\Zig\lib\std\Thread.zig:523:30: 0xd2072f in entryFn (example-play.exe.obj)
                return callFn(f, self.fn_args);
                             ^
???:?:?: 0x7ffddd83257c in ??? (KERNEL32.DLL)
???:?:?: 0x7ffddeaaaa57 in ??? (ntdll.dll)

The error seems to be caused by samples 605716 to 605725 of the decoded sound file: { 1.00861, 0.03097, 1.01480, 0.02588, 1.01671, 0.02050, 1.01438, 0.01490, 1.00786 }

Some of the values are >= 1, causing an overflow when converting from f32 to i16. To make sure that this isn't an issue with time.opus, I tried to play another file and encountered the same error (both files play fine on Window's Media Player).

I'm running Windows 11 with Zig 2024.1.0-mach

slimsag commented 4 months ago

Sorry for missing this.

I have a hunch it could be fixed already in sysaudio, but the example is not yet updated to use the latest version of sysaudio.

I suspect once https://github.com/hexops/mach/issues/1170 is closed that this will work.

I'll ping you once you can test again if that's alright :)