Expand description
PC Speaker driver : beep codes + music playback.
Hardware: PIT channel 2 (port 0x42) generates a square wave, port 0x61 bits 0-1 gate the speaker on/off.
Two modes:
- Blocking (boot):
beep()busy-waits : used before scheduler. - Music (runtime): note queue drained by PIT timer interrupt.
Structsยง
- Note
- A musical note: frequency in Hz and duration code. Duration codes map to note lengths at a given tempo.
Constantsยง
- END
- ERROR_
DURS - ERROR_
NOTES - Error / failure tone.
- IMPERIAL_
MARCH_ DURS - IMPERIAL_
MARCH_ NOTES - The Imperial March (Star Wars) : for panic screens. Notes: (freq Hz, duration code)
- PIT_
CHANNE ๐L2_ CMD - PIT_
FREQ ๐ - REST
- Special frequency values.
- SPEAKER_
DATA_ ๐BIT - SPEAKER_
GATE_ ๐BIT - STARTUP_
DURS - STARTUP_
NOTES - Startup jingle (ascending arpeggio).
Staticsยง
- MUSIC_
PLAYING ๐ - NOTE_
DUR_ ๐PTR - Pointer to the duration array (in milliseconds).
- NOTE_
FREQ ๐ - Current note frequency (Hz). Updated by the music player on timer tick.
- NOTE_
INDEX ๐ - Index into the current note sequence.
- NOTE_
SEQ_ ๐PTR - Pointer to the note sequence array (freq in Hz).
- NOTE_
TICKS_ ๐LEFT - Remaining ticks for the current note.
- NOTE_
TOTAL ๐ - Total number of notes in the current sequence.
- SPEAKER_
ACTIVE ๐ - TEMPO ๐
- Tempo multiplier (100 = normal, 200 = double speed, 50 = half speed).
Functionsยง
- beep
- Play a tone at
freqHz formsmilliseconds. Blocks via PIT busy-wait. - beep_
fail - Long beep : critical failure.
- beep_
off - Silence the speaker immediately.
- beep_ok
- Short beep : milestone reached.
- beep_
panic - Panic melody : descending notes.
- beep_
phase - Play a crescendo beep for boot phase N. Each successive phase uses a higher frequency so you can identify the last completed phase by pitch alone.
- beep_
startup - Startup jingle : ascending notes (happy boot).
- beep_
warn - Double beep : warning.
- music_
playing - Returns whether music is currently playing.
- music_
start โ - Start playing a note sequence. Non-blocking : drained by
speaker_tick(). - music_
stop - Stop music playback.
- pit_
busy_ ๐wait - Busy-wait for
msmilliseconds using PIT channel 2 (mode 0, one-shot). Works before the APIC timer / scheduler are running. - pit_
set_ ๐ โfreq - Set the PIT channel 2 frequency.
hz= 0 disables the tone. - speaker_
enable ๐ โ - Enable or disable the speaker output.
- speaker_
tick - Call this from the PIT timer interrupt (~10ms tick) to advance music playback.