add 5.1 surround create script

This commit is contained in:
scootz 2026-05-02 14:21:02 +01:00
parent 3081392099
commit 6fd0802239
2 changed files with 440 additions and 59 deletions

130
README.md
View file

@ -1,93 +1,105 @@
# reforger-surround-sound-tool
# reforger-surround
Temporary PipeWire/PulseAudio 5.1 sink helper for testing Arma Reforger surround output on Linux.
The goal is narrow: create a virtual 5.1 output device, make it the launch-time default, then let Arma Reforger decide whether to create a surround stream. Carla, QPWGraph, EQ, convolution, and long-term routing are intentionally manual follow-up steps.
## Getting started
## Requirements
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
- PipeWire with PulseAudio compatibility
- `pactl`
- Arma Reforger running through Steam/Proton or another Linux audio path that appears in PipeWire/Pulse
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Install
## Add your files
Run directly from the repo:
* [Create](https://docs.gitlab.com/user/project/repository/web_editor/#create-a-file) or [upload](https://docs.gitlab.com/user/project/repository/web_editor/#upload-a-file) files
* [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.com/vereto/foss/reforger-surround-sound-tool.git
git branch -M main
git push -uf origin main
```bash
./reforger-surround --help
```
## Integrate with your tools
Or place it somewhere on your `PATH`:
* [Set up project integrations](https://gitlab.com/vereto/foss/reforger-surround-sound-tool/-/settings/integrations)
```bash
install -Dm755 reforger-surround ~/.local/bin/reforger-surround
```
## Collaborate with your team
## Basic Test Workflow
* [Invite team members and collaborators](https://docs.gitlab.com/user/project/members/)
* [Create a new merge request](https://docs.gitlab.com/user/project/merge_requests/creating_merge_requests/)
* [Automatically close issues from merge requests](https://docs.gitlab.com/user/project/issues/managing_issues/#closing-issues-automatically)
* [Enable merge request approvals](https://docs.gitlab.com/user/project/merge_requests/approvals/)
* [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
Create the 5.1 sink and make it the temporary default:
## Test and Deploy
```bash
reforger-surround up
```
Use the built-in continuous integration in GitLab.
Launch Arma Reforger manually while `Reforger Surround 5.1` is the selected/default output device.
* [Get started with GitLab CI/CD](https://docs.gitlab.com/ci/quick_start/)
* [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/user/application_security/sast/)
* [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/topics/autodevops/requirements/)
* [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/user/clusters/agent/)
* [Set up protected environments](https://docs.gitlab.com/ci/environments/protected_environments/)
After Arma has created its audio stream, restore your normal default device:
***
```bash
reforger-surround restore
```
# Editing this README
Check whether Arma negotiated surround:
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
```bash
reforger-surround check
```
## Suggestions for a good README
Clean up the temporary sink:
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
```bash
reforger-surround down
```
## Name
Choose a self-explaining name for your project.
## Commands
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
```bash
reforger-surround create # create the 5.1 sink without changing defaults
reforger-surround up # create the sink, save current default, make 5.1 default
reforger-surround restore # restore the saved previous default, keep 5.1 sink alive
reforger-surround down # restore previous default and unload the 5.1 sink
reforger-surround status # show sink/default/game stream state
reforger-surround check # report whether Arma is 2ch or 6ch
```
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Configuration
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
Defaults are intentionally generic:
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
```bash
REFORGER_SURROUND_SINK_NAME=reforger_surround_51
REFORGER_SURROUND_DESCRIPTION="Reforger Surround 5.1"
REFORGER_SURROUND_APP_NAME="Arma Reforger"
```
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
Override them per command if needed:
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
```bash
REFORGER_SURROUND_APP_NAME="ArmaReforgerSteam.exe" reforger-surround check
```
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Expected Results
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
Good result:
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
```text
Sample Specification: ... 6ch 48000Hz
Channel Map: front-left,front-right,front-center,lfe,rear-left,rear-right
```
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
Stereo-only result:
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
```text
Sample Specification: ... 2ch 48000Hz
Channel Map: front-left,front-right
```
## License
For open source projects, say how it is licensed.
If Arma creates a stereo-only stream, restart the game with the 5.1 sink selected/default before launch. Moving an already-created stereo stream usually does not make the game renegotiate surround.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
## Notes
- `up` temporarily routes normal system audio to the 5.1 sink until `restore` is run.
- `restore` does not unload the 5.1 sink; it only gives normal system audio back to the previous default.
- `down` unloads the temporary sink created by this tool.
- LFE may be silent in Arma Reforger even when the stream is 6ch.

369
reforger-surround Executable file
View file

@ -0,0 +1,369 @@
#!/usr/bin/env bash
set -euo pipefail
sink_name="${REFORGER_SURROUND_SINK_NAME:-reforger_surround_51}"
sink_description="${REFORGER_SURROUND_DESCRIPTION:-Reforger Surround 5.1}"
app_name="${REFORGER_SURROUND_APP_NAME:-Arma Reforger}"
state_dir="${XDG_STATE_HOME:-$HOME/.local/state}/reforger-surround"
module_id_file="${state_dir}/module-id"
previous_default_sink_file="${state_dir}/previous-default-sink"
usage() {
cat <<'EOF'
Usage:
reforger-surround create
reforger-surround up
reforger-surround restore
reforger-surround down
reforger-surround status
reforger-surround check
Commands:
create Create the temporary 5.1 sink without changing the default sink.
up Create the 5.1 sink, save the current default, and make it default.
restore Restore the saved previous default sink, leaving the 5.1 sink alive.
down Restore the saved previous default sink and unload the 5.1 sink.
status Show default sink, virtual sink state, and detected Reforger stream.
check Report whether a running Reforger stream negotiated stereo or surround.
Environment:
REFORGER_SURROUND_SINK_NAME Override sink name. Default: reforger_surround_51
REFORGER_SURROUND_DESCRIPTION Override visible sink name. Default: Reforger Surround 5.1
REFORGER_SURROUND_APP_NAME Override app name. Default: Arma Reforger
EOF
}
require_command() {
local command="$1"
if ! command -v "${command}" >/dev/null 2>&1; then
printf 'Missing required command: %s\n' "${command}" >&2
exit 127
fi
}
ensure_dependencies() {
require_command pactl
require_command awk
require_command sed
}
require_pulse_connection() {
if ! pactl info >/dev/null 2>&1; then
printf 'Could not connect to PulseAudio/PipeWire through pactl.\n' >&2
printf 'Check that PipeWire and pipewire-pulse are running for this user session.\n' >&2
exit 1
fi
}
ensure_state_dir() {
mkdir -p "${state_dir}"
}
current_default_sink() {
pactl get-default-sink 2>/dev/null || true
}
sink_exists() {
pactl list sinks short | awk -v sink_name="${sink_name}" '$2 == sink_name { found = 1 } END { exit !found }'
}
find_module_id() {
pactl list short modules | awk -v sink_name="${sink_name}" '
$2 == "module-null-sink" && index($0, "sink_name=" sink_name) {
print $1
exit
}
'
}
stored_module_id() {
[[ -s "${module_id_file}" ]] && sed -n '1p' "${module_id_file}"
}
create_sink() {
ensure_state_dir
local module_id
module_id="$(find_module_id || true)"
if [[ -n "${module_id}" ]]; then
printf '%s\n' "${module_id}" > "${module_id_file}"
printf 'Using existing 5.1 sink: %s (module %s)\n' "${sink_name}" "${module_id}"
return 0
fi
if sink_exists; then
printf '5.1 sink already exists: %s\n' "${sink_name}"
return 0
fi
module_id="$(pactl load-module module-null-sink \
"sink_name=${sink_name}" \
"sink_properties=device.description=\"${sink_description}\"" \
"channels=6" \
"channel_map=front-left,front-right,front-center,lfe,rear-left,rear-right" \
"rate=48000")"
printf '%s\n' "${module_id}" > "${module_id_file}"
printf 'Created 5.1 sink: %s (module %s)\n' "${sink_name}" "${module_id}"
}
save_previous_default() {
ensure_state_dir
local default_sink
default_sink="$(current_default_sink)"
if [[ -n "${default_sink}" && "${default_sink}" != "${sink_name}" ]]; then
printf '%s\n' "${default_sink}" > "${previous_default_sink_file}"
printf 'Saved previous default sink: %s\n' "${default_sink}"
fi
}
set_sink_default() {
pactl set-default-sink "${sink_name}"
printf 'Set launch default sink: %s\n' "${sink_name}"
}
up() {
create_sink
save_previous_default
set_sink_default
cat <<EOF
Launch Arma Reforger now. After the game has created its audio stream, run:
reforger-surround restore
reforger-surround check
EOF
}
restore_default() {
local restore_sink
restore_sink="$(sed -n '1p' "${previous_default_sink_file}" 2>/dev/null || true)"
if [[ -z "${restore_sink}" ]]; then
printf 'No saved previous default sink found; nothing to restore.\n'
return 0
fi
if ! pactl list sinks short | awk -v sink_name="${restore_sink}" '$2 == sink_name { found = 1 } END { exit !found }'; then
printf 'Saved previous default sink no longer exists: %s\n' "${restore_sink}" >&2
return 1
fi
pactl set-default-sink "${restore_sink}"
printf 'Restored default sink: %s\n' "${restore_sink}"
}
unload_sink() {
local module_id
module_id="$(find_module_id || true)"
if [[ -z "${module_id}" ]]; then
module_id="$(stored_module_id || true)"
fi
if [[ -z "${module_id}" ]]; then
printf 'No %s module found to unload.\n' "${sink_name}"
return 0
fi
pactl unload-module "${module_id}" 2>/dev/null || true
rm -f "${module_id_file}"
printf 'Unloaded 5.1 sink module: %s\n' "${module_id}"
}
down() {
restore_default || true
unload_sink
}
find_app_sink_input_id() {
pactl list sink-inputs | awk -v app_name="${app_name}" '
/^Sink Input #/ {
id = substr($3, 2)
}
index($0, "application.name = \"" app_name "\"") {
print id
exit
}
'
}
app_stream_sink_name() {
local input_id="$1"
pactl list sink-inputs | awk -v target="Sink Input #${input_id}" '
$0 == target {
in_input = 1
next
}
in_input && $1 == "Sink:" {
print $2
exit
}
in_input && /^Sink Input #/ {
exit
}
' | while read -r sink_id; do
pactl list sinks short | awk -v sink_id="${sink_id}" '$1 == sink_id { print $2; exit }'
done
}
app_stream_lines() {
local input_id="$1"
pactl list sink-inputs | awk -v target="Sink Input #${input_id}" '
$0 == target {
in_input = 1
next
}
in_input && ($1 == "Sink:" || ($1 == "Sample" && $2 == "Specification:") || ($1 == "Channel" && $2 == "Map:")) {
print
}
in_input && /^Sink Input #/ {
exit
}
'
}
app_stream_sample_spec() {
local input_id="$1"
pactl list sink-inputs | awk -v target="Sink Input #${input_id}" '
$0 == target {
in_input = 1
next
}
in_input && $1 == "Sample" && $2 == "Specification:" {
print $0
exit
}
in_input && /^Sink Input #/ {
exit
}
'
}
app_stream_channel_map() {
local input_id="$1"
pactl list sink-inputs | awk -v target="Sink Input #${input_id}" '
$0 == target {
in_input = 1
next
}
in_input && $1 == "Channel" && $2 == "Map:" {
print $0
exit
}
in_input && /^Sink Input #/ {
exit
}
'
}
print_check() {
local input_id sample_spec channel_map sink
input_id="$(find_app_sink_input_id || true)"
if [[ -z "${input_id}" ]]; then
printf 'No running sink-input found for application.name="%s".\n' "${app_name}" >&2
return 1
fi
sample_spec="$(app_stream_sample_spec "${input_id}")"
channel_map="$(app_stream_channel_map "${input_id}")"
sink="$(app_stream_sink_name "${input_id}" || true)"
printf 'Detected %s sink-input #%s\n' "${app_name}" "${input_id}"
printf 'Sink: %s\n' "${sink:-unknown}"
printf '%s\n' "${sample_spec}"
printf '%s\n' "${channel_map}"
case "${sample_spec}" in
*" 6ch "*)
printf 'Result: success, Reforger negotiated surround output.\n'
printf 'Expected useful channels: FL, FR, FC, RL, RR. LFE may be silent.\n'
;;
*" 2ch "*)
printf 'Result: stereo-only. Restart Reforger with the 5.1 sink selected/default before launch.\n'
return 2
;;
*)
printf 'Result: unknown channel count; inspect the sample specification and channel map above.\n'
return 3
;;
esac
}
print_status() {
local module_id input_id
module_id="$(find_module_id || true)"
input_id="$(find_app_sink_input_id || true)"
printf 'default sink: %s\n' "$(current_default_sink)"
printf '5.1 sink: %s\n' "${sink_name}"
printf '5.1 module: %s\n' "${module_id:-not loaded}"
printf 'saved previous default: %s\n' "$(sed -n '1p' "${previous_default_sink_file}" 2>/dev/null || printf 'none')"
if sink_exists; then
pactl list sinks short | awk -v sink_name="${sink_name}" '$2 == sink_name { print }'
fi
if [[ -z "${input_id}" ]]; then
printf '%s stream: not found\n' "${app_name}"
else
printf '%s stream: sink-input #%s\n' "${app_name}" "${input_id}"
app_stream_lines "${input_id}"
fi
}
main() {
local command="${1:-}"
if [[ -z "${command}" ]]; then
usage >&2
exit 1
fi
case "${command}" in
-h|--help|help)
usage
;;
create)
ensure_dependencies
require_pulse_connection
create_sink
;;
up)
ensure_dependencies
require_pulse_connection
up
;;
restore)
ensure_dependencies
require_pulse_connection
restore_default
;;
down)
ensure_dependencies
require_pulse_connection
down
;;
status)
ensure_dependencies
require_pulse_connection
print_status
;;
check)
ensure_dependencies
require_pulse_connection
print_check
;;
*)
usage >&2
exit 1
;;
esac
}
main "$@"