feat(libSceAudioIn): capture from the host's recording device #243

Merged
adriadam10 merged 2 commits from upstream-pr/243 into main 2026-10-04 06:55:16 +00:00
adriadam10 commented 2026-10-01 18:10:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-01T18:10:54Z.


What

Implements four libSceAudioIn exports with real capture through SDL (already bundled; libSceAudioOut uses it for playback).

  • sceAudioInOpen validates type (0/1), index, grain (128/256), rate (16000/48000) and format (1 / 2 = S16 mono / stereo, 0x11 / 0x12 = float mono / stereo; 0x10 throws, see below), opens the default SDL recording device in that exact format and returns a handle. 8 ports, then PORT_FULL.
  • sceAudioInInput returns one grain: it waits until the device has queued a full grain (up to four grains), copies it and pads any shortfall with silence. If more than four grains are queued, the oldest are dropped, so latency stays bounded when the title reads late. A null buffer flushes the queue.
  • sceAudioInClose releases the port and closes its host device. Input and Close on a port that is still capturing return BUSY.
  • sceAudioInGetSilentState returns 0 with a recording device, and DEVICE_NONE (1) without one. Without a device a port still opens and delivers silence paced at the grain rate, as a console with no microphone would; titles that need the microphone (blowing into the DualSense, voice) get the real input when the host has one.
  • SDL's symbols are kept out of the library's dynamic table (--exclude-libs,ALL, as for FFmpeg).

Sources (behaviour and constants only, no code copied):

  • Types 0/1, grains 128/256, rates 16000/48000 and formats 1/2/0x11/0x12 come from the PS5 SDK bindings in SharpProspero (Interop/Audio/AudioIn.cs). KytyPS5's current src/libs/audio.cpp (OpenPort) uses the same validation.
  • shadPS4 (audioin.h) has S16 mono = 0, S16 stereo = 2 and accepts type 5. That is the PS4 library, so it is not followed here: 0 is refused as on the PS5 sources.
  • KytyPS5 also maps 0x10 to float mono, but SharpProspero doesn't list it, so 0x10 throws NotImplemented instead of guessing.
  • Error codes (including BUSY 0x8026010A) match KytyPS5 and shadPS4.

With SDL linked in, nid_patcher overflowed this library's .dynstr by a few bytes; #242 (merged) fixed the patcher.

Tested

Linux (CachyOS, GCC). New guest_audio_in test (core/libs/tests/GuestAudioIn.cpp), run in two processes so each picks its SDL driver:

  • capture: SDL's disk driver reads a known S16 stereo recording from a file as the microphone; four grains come back sample-exact, then silence at end of file; silent state 0.
  • no_device: no audio driver; silent state 1, five grains of silence paced at 16 ms, buffer bounds respected, PORT_FULL after 8 ports, Close frees a port (a second close and input on it are INVALID_HANDLE, and the next open reuses it), plus all validation errors, including format 0.

It fails on each of three injected bugs (capture ignored, no pacing without a device, silent state ignoring the device). Full suite passes. Windows only through CI. Not tested with a physical microphone or a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4707665292 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/243). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-01T18:10:54Z. --- ### What Implements four `libSceAudioIn` exports with real capture through SDL (already bundled; `libSceAudioOut` uses it for playback). - `sceAudioInOpen` validates type (0/1), index, grain (128/256), rate (16000/48000) and format (1 / 2 = S16 mono / stereo, 0x11 / 0x12 = float mono / stereo; 0x10 throws, see below), opens the default SDL recording device in that exact format and returns a handle. 8 ports, then `PORT_FULL`. - `sceAudioInInput` returns one grain: it waits until the device has queued a full grain (up to four grains), copies it and pads any shortfall with silence. If more than four grains are queued, the oldest are dropped, so latency stays bounded when the title reads late. A null buffer flushes the queue. - `sceAudioInClose` releases the port and closes its host device. `Input` and `Close` on a port that is still capturing return `BUSY`. - `sceAudioInGetSilentState` returns 0 with a recording device, and `DEVICE_NONE` (1) without one. Without a device a port still opens and delivers silence paced at the grain rate, as a console with no microphone would; titles that need the microphone (blowing into the DualSense, voice) get the real input when the host has one. - SDL's symbols are kept out of the library's dynamic table (`--exclude-libs,ALL`, as for FFmpeg). Sources (behaviour and constants only, no code copied): - Types 0/1, grains 128/256, rates 16000/48000 and formats 1/2/0x11/0x12 come from the PS5 SDK bindings in SharpProspero (`Interop/Audio/AudioIn.cs`). KytyPS5's current `src/libs/audio.cpp` (`OpenPort`) uses the same validation. - shadPS4 (`audioin.h`) has S16 mono = 0, S16 stereo = 2 and accepts type 5. That is the PS4 library, so it is not followed here: 0 is refused as on the PS5 sources. - KytyPS5 also maps 0x10 to float mono, but SharpProspero doesn't list it, so 0x10 throws `NotImplemented` instead of guessing. - Error codes (including `BUSY` 0x8026010A) match KytyPS5 and shadPS4. With SDL linked in, `nid_patcher` overflowed this library's `.dynstr` by a few bytes; #242 (merged) fixed the patcher. ### Tested Linux (CachyOS, GCC). New `guest_audio_in` test (`core/libs/tests/GuestAudioIn.cpp`), run in two processes so each picks its SDL driver: - `capture`: SDL's `disk` driver reads a known S16 stereo recording from a file as the microphone; four grains come back sample-exact, then silence at end of file; silent state 0. - `no_device`: no audio driver; silent state 1, five grains of silence paced at 16 ms, buffer bounds respected, `PORT_FULL` after 8 ports, `Close` frees a port (a second close and input on it are `INVALID_HANDLE`, and the next open reuses it), plus all validation errors, including format 0. It fails on each of three injected bugs (capture ignored, no pacing without a device, silent state ignoring the device). Full suite passes. Windows only through CI. Not tested with a physical microphone or a title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-01 18:11:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-01T18:11:19Z.


Progress report (cd61f15 → 7e5ce94)

📈 System libraries: 77.35% (+0.1%, +3 implemented functions)

✅ 3 implemented
Library Name
libSceAudioIn sceAudioInClose
libSceAudioIn sceAudioInGetSilentState
libSceAudioIn sceAudioInInput
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5937572912 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/243#issuecomment-5937572912). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-01T18:11:19Z. --- <!-- progress-report --> ### Progress report (cd61f15 → 7e5ce94) 📈 **System libraries**: 77.35% (+0.1%, +3 implemented functions) <details> <summary>✅ 3 implemented</summary> | Library | Name | | - | - | | libSceAudioIn | `sceAudioInClose` | | libSceAudioIn | `sceAudioInGetSilentState` | | libSceAudioIn | `sceAudioInInput` | </details>
oneandonlydean commented 2026-10-04 05:39:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:39:18Z.


Thanks for taking this on. A few things don't match the references I could check. The format codes: shadPS4 (audioin.h, OrbisAudioInParamFormat) and Kyty (Audio.cpp, AudioInOpen) both have S16 mono as 0 and S16 stereo as 2; this branch rejects 0 and maps 1 to mono, and the float codes 0x10 to 0x12 have no source I could find. shadPS4 also accepts type 5 (voice recognition) and other grain sizes, while Kyty doesn't validate size or rate at all, so the "follows KytyPS5" line in the description doesn't hold for the validation. And sceAudioInClose still throws while Open now takes a port and an SDL device, so a title that closes its port aborts; could Close be implemented in the same PR? With the codes fixed (or the unverified ones throwing) and Close in, I'd be glad to look again. #242 has landed in the meantime, so a rebase would leave only your commit here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5976999131 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/243#issuecomment-5976999131). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:39:18Z. --- Thanks for taking this on. A few things don't match the references I could check. The format codes: shadPS4 (`audioin.h`, `OrbisAudioInParamFormat`) and Kyty (`Audio.cpp`, AudioInOpen) both have S16 mono as 0 and S16 stereo as 2; this branch rejects 0 and maps 1 to mono, and the float codes 0x10 to 0x12 have no source I could find. shadPS4 also accepts type 5 (voice recognition) and other grain sizes, while Kyty doesn't validate size or rate at all, so the "follows KytyPS5" line in the description doesn't hold for the validation. And `sceAudioInClose` still throws while Open now takes a port and an SDL device, so a title that closes its port aborts; could Close be implemented in the same PR? With the codes fixed (or the unverified ones throwing) and Close in, I'd be glad to look again. #242 has landed in the meantime, so a rebase would leave only your commit here.
adriadam10 commented 2026-10-04 06:39:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:39:58Z.


Thanks for checking the references. I dug into it, and I think the difference is which Kyty you looked at:

  • Format codes: the PS5 SDK bindings in SharpProspero (src/SharpProspero/Interop/Audio/AudioIn.cs) have S16Mono = 1, S16Stereo = 2, FloatMono = 0x11, FloatStereo = 0x12, and types VoiceChat = 0 and General = 1. Current KytyPS5 (src/libs/audio.cpp, OpenPort) has the same codes and the same type, grain and rate checks. The old InoriRus/Kyty and shadPS4 (audioin.h) are PS4, where mono is 0 and type 5 exists. So I kept the PS5 codes and now refuse 0 explicitly in the test.
  • 0x10: only KytyPS5 maps it (to float mono), and SharpProspero doesn't list it, so it now throws NotImplemented.
  • sceAudioInClose: implemented. It releases the port and closes its SDL device, and Input/Close on a port that is mid-capture return BUSY (0x8026010A), as in KytyPS5. The test covers close, a second close, input on a closed handle, and reuse of the freed port.
  • Rebased onto main with #242 in, so only this PR's commits are left. The description now lists the sources and the disagreement with shadPS4.

guest_audio_in_capture and guest_audio_in_no_device pass on Linux.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977388779 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/243#issuecomment-5977388779). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:39:58Z. --- Thanks for checking the references. I dug into it, and I think the difference is which Kyty you looked at: - **Format codes:** the PS5 SDK bindings in SharpProspero (`src/SharpProspero/Interop/Audio/AudioIn.cs`) have S16Mono = 1, S16Stereo = 2, FloatMono = 0x11, FloatStereo = 0x12, and types VoiceChat = 0 and General = 1. Current KytyPS5 (`src/libs/audio.cpp`, `OpenPort`) has the same codes and the same type, grain and rate checks. The old InoriRus/Kyty and shadPS4 (`audioin.h`) are PS4, where mono is 0 and type 5 exists. So I kept the PS5 codes and now refuse 0 explicitly in the test. - **0x10:** only KytyPS5 maps it (to float mono), and SharpProspero doesn't list it, so it now throws `NotImplemented`. - **`sceAudioInClose`:** implemented. It releases the port and closes its SDL device, and Input/Close on a port that is mid-capture return `BUSY` (0x8026010A), as in KytyPS5. The test covers close, a second close, input on a closed handle, and reuse of the freed port. - Rebased onto main with #242 in, so only this PR's commits are left. The description now lists the sources and the disagreement with shadPS4. `guest_audio_in_capture` and `guest_audio_in_no_device` pass on Linux.
oneandonlydean commented 2026-10-04 06:55:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T06:55:13Z.


You're right, and thanks for digging into it: those were PS4 references. SharpProspero's PS5 bindings and current KytyPS5's OpenPort both use 1/2/0x11/0x12 with types 0 and 1, and your validation matches KytyPS5's synchronous open. Close and the 0x10 throw look good, and it builds and passes here on current main. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977487756 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/243#issuecomment-5977487756). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T06:55:13Z. --- You're right, and thanks for digging into it: those were PS4 references. SharpProspero's PS5 bindings and current KytyPS5's `OpenPort` both use 1/2/0x11/0x12 with types 0 and 1, and your validation matches KytyPS5's synchronous open. `Close` and the 0x10 throw look good, and it builds and passes here on current main. Merging.
oneandonlydean commented 2026-10-04 07:43:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T07:43:50Z.


Heads-up: guest_audio_in_no_device crashes intermittently on Windows. It first hit the Windows job of an unrelated PR (#489), and I reproduced it on current main with four fork CI runs: 1 crash in 11 attempts on Windows, 0 on Linux, always Exit code 0xc0000409, which is what the UCRT reports for abort or std::terminate. So the no-device path sometimes ends in an uncaught exception or an abort on Windows; maybe the SDL device open or close racing the capture thread? Could you take a look?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977805585 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/243#issuecomment-5977805585). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T07:43:50Z. --- Heads-up: `guest_audio_in_no_device` crashes intermittently on Windows. It first hit the Windows job of an unrelated PR (#489), and I reproduced it on current main with four fork CI runs: 1 crash in 11 attempts on Windows, 0 on Linux, always `Exit code 0xc0000409`, which is what the UCRT reports for `abort` or `std::terminate`. So the no-device path sometimes ends in an uncaught exception or an abort on Windows; maybe the SDL device open or close racing the capture thread? Could you take a look?
adriadam10 commented 2026-10-04 15:28:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T15:28:11Z.


Thanks for tracking it down. Fix in #532. The abort is the test's elapsed >= 64 ms check: on the CI toolchain, sleep_until on a steady clock makes a single sleep_for, and winpthreads nanosleep truncates it to whole milliseconds, so the no-device sceAudioInInput could return up to ~1 ms before its deadline. It now waits until the clock reaches the deadline.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5981599620 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/243#issuecomment-5981599620). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T15:28:11Z. --- Thanks for tracking it down. Fix in #532. The abort is the test's `elapsed >= 64 ms` check: on the CI toolchain, `sleep_until` on a steady clock makes a single `sleep_for`, and winpthreads `nanosleep` truncates it to whole milliseconds, so the no-device `sceAudioInInput` could return up to ~1 ms before its deadline. It now waits until the clock reaches the deadline.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!243
No description provided.