Skip to main content

Module speaker

Module speaker 

Source
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:

  1. Blocking (boot): beep() busy-waits : used before scheduler.
  2. 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_CHANNEL2_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 freq Hz for ms milliseconds. 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 ms milliseconds 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.