Skip to content

Commit 91a7f15

Browse files
document audio
1 parent d6fef32 commit 91a7f15

605 files changed

Lines changed: 5702 additions & 3626 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmake/custom_targets.cmake

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,67 @@ endfunction()
121121
function(run TARGETFILE)
122122
set(FILENAME "${CMAKE_BINARY_DIR}/iso/kernel.bin")
123123
set(OUTNAME "${CMAKE_BINARY_DIR}/${TARGETFILE}")
124+
124125
# Choose network device based on -DUSE_E1000
125126
if(USE_E1000)
126127
set(NET_DEVICE "-device e1000,netdev=netuser")
127128
else()
128129
set(NET_DEVICE "-device rtl8139,netdev=netuser")
129130
endif()
131+
130132
if (PROFILE_KERNEL)
131133
set(PROFILE "-serial file:callgrind.out")
132134
else()
133135
set(PROFILE "")
134136
endif()
137+
138+
# ---- Audio detection (Linux only) ----
139+
set(AUDIO_OPTS "")
140+
if(UNIX AND NOT APPLE)
141+
# Try the modern help first. Some older QEMU builds return non-zero and print an error.
142+
execute_process(
143+
COMMAND qemu-system-x86_64 -audiodev help
144+
OUTPUT_VARIABLE QEMU_AD_OUT
145+
ERROR_VARIABLE QEMU_AD_ERR
146+
RESULT_VARIABLE QEMU_AD_RC
147+
OUTPUT_STRIP_TRAILING_WHITESPACE
148+
ERROR_STRIP_TRAILING_WHITESPACE
149+
)
150+
# Combine both streams for robust matching
151+
set(QEMU_AD_TEXT "${QEMU_AD_OUT}\n${QEMU_AD_ERR}")
152+
153+
if(QEMU_AD_RC EQUAL 0 OR QEMU_AD_TEXT MATCHES "Available audio drivers")
154+
# Prefer PipeWire if QEMU lists it and host has it; else fall back to PulseAudio
155+
set(_BACKEND "")
156+
if(QEMU_AD_TEXT MATCHES "(^|[ \n\r\t])pipewire([ \n\r\t]|$)")
157+
# Check host has PipeWire
158+
execute_process(COMMAND pipewire --version RESULT_VARIABLE PW_RC OUTPUT_QUIET ERROR_QUIET)
159+
if(PW_RC EQUAL 0)
160+
set(_BACKEND "pipewire")
161+
endif()
162+
endif()
163+
if(_BACKEND STREQUAL "")
164+
if(QEMU_AD_TEXT MATCHES "(^|[ \n\r\t])pa([ \n\r\t]|$)")
165+
# Check host has PulseAudio (or PipeWire's Pulse server)
166+
execute_process(COMMAND pactl --version RESULT_VARIABLE PA_RC OUTPUT_QUIET ERROR_QUIET)
167+
if(PA_RC EQUAL 0)
168+
set(_BACKEND "pa")
169+
endif()
170+
endif()
171+
endif()
172+
173+
if(_BACKEND STREQUAL "pipewire")
174+
set(AUDIO_OPTS "-audiodev pipewire,id=snd0 -device AC97,audiodev=snd0")
175+
elseif(_BACKEND STREQUAL "pa")
176+
# If you strictly only want PipeWire, comment this line out to skip Pulse fallback.
177+
set(AUDIO_OPTS "-audiodev pa,id=snd0 -device AC97,audiodev=snd0")
178+
endif()
179+
endif()
180+
endif()
181+
# ---- end audio detection ----
182+
135183
add_custom_command(OUTPUT ${OUTNAME}
136-
COMMAND echo "qemu-system-x86_64 \
184+
COMMAND echo "qemu-system-x86_64 \
137185
-machine q35,accel=kvm \
138186
-s \
139187
-monitor stdio \
@@ -152,8 +200,8 @@ function(run TARGETFILE)
152200
-debugcon file:debug.log \
153201
-netdev user,id=netuser,hostfwd=tcp::2000-:2000,hostfwd=tcp::2080-:80 \
154202
-object filter-dump,id=dump,netdev=netuser,file=dump.dat \
155-
${NET_DEVICE} ${PROFILE}" >${OUTNAME} && chmod ugo+x ${OUTNAME}
156-
DEPENDS ${FILENAME})
203+
${NET_DEVICE} ${PROFILE} ${AUDIO_OPTS}" >${OUTNAME} && chmod ugo+x ${OUTNAME}
204+
DEPENDS ${FILENAME})
157205
add_custom_target(RUN_${TARGETFILE} ALL DEPENDS ${OUTNAME})
158206
add_dependencies(RUN_${TARGETFILE} "kernel.bin")
159207
endfunction()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
\page audio-basics Audio Introduction
2+
3+
[TOC]
4+
5+
## Audio System Structure
6+
7+
Retro Rocket’s audio system has three main parts:
8+
9+
### Drivers
10+
11+
The driver is the low-level hardware backend (e.g. `MODLOAD "ac97"`).
12+
It is specific to your sound card in your PC. Without a driver, **no audio commands will work**.
13+
14+
15+
### Streams
16+
17+
A stream is a **playback channel** created with `STREAM CREATE`.
18+
19+
* Each stream has its own queue of sounds, volume, and state.
20+
* Multiple streams can run at once (for mixing music, effects, voices, etc.).
21+
* A stream handle is always a positive integer ID.
22+
23+
24+
### Sounds
25+
26+
A sound is **decoded audio data** loaded into memory with `SOUND LOAD`.
27+
28+
* Sounds can be loaded from any .WAV file which contains PCM or FLOAT format audio.
29+
* Sounds are stored in RAM as 44.1 kHz stereo, 16-bit PCM.
30+
* Sound handles are always non-zero integer IDs.
31+
* Sounds must be freed with `SOUND UNLOAD` when no longer needed.
32+
33+
34+
## Flow of audio
35+
36+
```
37+
File on disk (WAV) → SOUND LOAD → Sound handle
38+
Sound handle + Stream → SOUND PLAY → Playback
39+
```
40+
41+
## Example
42+
43+
```basic
44+
MODLOAD "ac97" ' load audio driver
45+
STREAM CREATE music ' create playback channel
46+
SOUND LOAD song, "track.wav" ' load audio into RAM
47+
SOUND PLAY music, song ' play on stream
48+
```
49+
50+
\image html sound.png
51+
52+
## Audio Keywords/Functions
53+
54+
- \ref STREAM "STREAM"
55+
- \ref STREAM "SOUND"
56+
- \ref DECIBELS "DECIBELS"

docpages/basic-language-reference/INDEX.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ For real-world examples of programs in the operating system, see the [os/program
3232
* \subpage variables
3333
* \subpage keywords
3434
* \subpage builtin-functions
35+
* \subpage audio-basics
3536
* \subpage networking
3637
* \subpage libraries
3738
* \subpage tasks

docpages/basic-language-reference/functions/integer/02_INDEX.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The following functions operate on or return **integer values** in Retro Rocket
1919
* \subpage CURRENTX
2020
* \subpage CURRENTY
2121
* \subpage DAY
22+
* \subpage DECIBELS
2223
* \subpage EOF
2324
* \subpage EXISTSVARI
2425
* \subpage FILESIZE
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
\page DECIBELS DECIBELS Function
2+
3+
```basic
4+
DECIBELS(integer-expression)
5+
```
6+
7+
Converts a **decibel (dB) value** into a volume level suitable for `SOUND VOLUME`.
8+
The return value is always in the range **0–255**, where:
9+
10+
* `0` means completely silent.
11+
* `255` means full volume (unity gain).
12+
13+
---
14+
15+
### What are decibels?
16+
17+
Decibels are a way of describing **relative loudness**.
18+
They don’t measure sound directly, but how much **quieter or louder** something is compared to a reference point:
19+
20+
* `0 dB` means “no change” - the sound plays at its original level.
21+
* Negative values (e.g. `-6 dB`, `-12 dB`) make the sound quieter.
22+
* The further the number is below zero, the quieter the result.
23+
* Positive values are not useful here. Anything above `0 dB` is clamped to maximum.
24+
25+
For example, `-6 dB` is a common setting to “turn something down a bit” (50% loudness in Retro Rocket) without muting it.
26+
27+
---
28+
29+
### Behaviour in Retro Rocket
30+
31+
* `DECIBELS(0)` → 255 (full volume).
32+
* `DECIBELS(-6)` → about half volume (128).
33+
* `DECIBELS(-12)` → quieter still (around 64).
34+
* `DECIBELS(-60)` → 0 (silent).
35+
36+
Values above 0 are treated as 0 dB.
37+
Values below –60 are treated as silence.
38+
39+
This gives a simple but natural way to think about volume, since each **–6 dB step roughly halves the loudness**.
40+
41+
---
42+
43+
### Notes
44+
45+
* Use `DECIBELS()` when you want to set volume in familiar dB terms instead of raw 0–255 values.
46+
* The mapping is not linear: small changes near 0 dB make a noticeable difference, while changes at very low levels fade smoothly towards silence.
47+
* Internally this uses a fast lookup table, so performance is constant time.
48+
49+
---
50+
51+
### Examples
52+
53+
```basic
54+
gain = DECIBELS(-6)
55+
SOUND VOLUME music, gain
56+
57+
gain = DECIBELS(-20)
58+
SOUND VOLUME effects, gain
59+
```
60+
61+
---
62+
63+
**See also**
64+
\ref SOUND "SOUND" VOLUME · \ref STREAM "STREAM"

docpages/basic-language-reference/keywords/00_INDEX.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ The following parameter types are used throughout this documentation:
129129
* \subpage SOCKFLUSH
130130
* \subpage SOCKREAD
131131
* \subpage SOCKWRITE
132+
* \subpage SOUND
132133
* \subpage SPRITEFREE
133134
* \subpage SPRITELOAD
135+
* \subpage STREAM
134136
* \subpage TRIANGLE
135137
* \subpage UDPBIND
136138
* \subpage UDPUNBIND

0 commit comments

Comments
 (0)