mach icon indicating copy to clipboard operation
mach copied to clipboard

sysaudio: runtime alignment error when converting samples on Windows

Open paoda opened this issue 2 years ago • 4 comments

Hello!, when running the sine.zig example on Windows 10 Build 19045, I receive the following output

 ~\AppData\Local\Temp\sine                                                                                                                                                                           | 11:07:40
➜ zig build run
info: Took 6.984ms to initialize the context...
info: Took 23.701ms to refresh the context...
info: Took 88.8us to get the default playback device...
info: Took 8.264ms to create a player...
info: Took 142.9us to start the player...
info: Took 390us to set the volume...
info: player created & entering i/o loop...
( paused = false, volume = 0.8500000238418579 )
> thread 6136 panic: incorrect alignment
C:\Users\paoda\AppData\Local\zig\p\12206c2ca0378bbc54721553e8167c037325e95e2791379fbe0e245a34334679dc90\src\main.zig:346:70: 0x3694a9 in convertTo__anon_5803 (sine.exe.obj)
            f32 => conv.floatToSigned(SrcType, src, i24, @as([*]i24, @ptrCast(@alignCast(dst)))[0..dst_len]),
                                                                     ^
C:\Users\paoda\AppData\Local\Temp\sine\src\main.zig:64:27: 0x361cd2 in writeCallback (sine.exe.obj)
        sysaudio.convertTo(
                          ^
C:\Users\paoda\AppData\Local\zig\p\12206c2ca0378bbc54721553e8167c037325e95e2791379fbe0e245a34334679dc90\src\wasapi.zig:807:31: 0x3c0700 in writeThread (sine.exe.obj)
                player.writeFn(
                              ^
C:\Users\paoda\.local\bin\zigup\zig\0.12.0-dev.2063+804cee3b9\files\lib\std\Thread.zig:411:13: 0x3a85cc in callFn__anon_8675 (sine.exe.obj)
            @call(.auto, f, args);
            ^
C:\Users\paoda\.local\bin\zigup\zig\0.12.0-dev.2063+804cee3b9\files\lib\std\Thread.zig:523:30: 0x36adfe in entryFn (sine.exe.obj)
                return callFn(f, self.fn_args);
                             ^
???:?:?: 0x7ff8f6dd7343 in ??? (KERNEL32.DLL)
???:?:?: 0x7ff8f8c426b0 in ??? (ntdll.dll)
run
└─ run sine failure
error: the following command exited with error code 3:
C:\Users\paoda\AppData\Local\Temp\sine\zig-out\bin\sine.exe
Build Summary: 3/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run sine failure
error: the following build command failed with exit code 1:
C:\Users\paoda\AppData\Local\Temp\sine\zig-cache\o\f168a5c3d0ff462674b014b722c5598d\build.exe C:\Users\paoda\.local\bin\zigup\zig\0.12.0-dev.2063+804cee3b9\files\zig.exe C:\Users\paoda\AppData\Local\Temp\sine C:\Users\paoda\AppData\Local\Temp\sine\zig-cache C:\Users\paoda\AppData\Local\zig --seed 0x406be11d run

Of note is that if I ignore this, and run the build in ReleaseFast I get the following output (VOLUME WARNING!!!!!)

https://github.com/hexops/mach/assets/22038970/62d9c439-1b30-4424-8037-051b0a3e86a4

paoda avatar Jan 28 '24 17:01 paoda

comptime {
    @compileLog(@sizeOf(i24));
    @compileLog(@alignOf(i24));
}
Compile Log Output:
@as(comptime_int, 4)
@as(comptime_int, 4)

this is just wrong. i don't know how we can force i24 to be three bytes

EDIT: here is the issue https://github.com/ziglang/zig/issues/7336#issuecomment-747602260

alichraghi avatar Jan 28 '24 17:01 alichraghi

@alichraghi I wonder if the i24 was inside a packed struct if it would work.

emidoots avatar Apr 13 '24 14:04 emidoots

Didn't work

comptime {
    @compileLog(@sizeOf(packed struct { x: i24 }));
    @compileLog(@alignOf(packed struct { x: i24 }));
}

///

Compile Log Output:
@as(comptime_int, 4)
@as(comptime_int, 4)

alichraghi avatar Apr 13 '24 20:04 alichraghi

a temporary workaround is to set the device's default format to 16bit

ypsvlq avatar Apr 30 '24 13:04 ypsvlq