--- title: audio section: audio order: 265 --- Play sounds on the server itself, so a script can sound a physical alarm when a disk reports SMART errors, a RAID array degrades or a UPS goes on battery. Sound goes to the sound card (PulseAudio or ALSA on Linux, Core Audio on macOS, WASAPI on Windows; no CGO, nothing to install). On Linux, tones fall back to the PC speaker when there is no sound card, which is what most rack servers have: that needs ArozOS to run as root and the `pcspkr` kernel module to be loaded. Linux builds for MIPS have no sound card support and use the PC speaker only. **Administrators only:** a sound reaches everyone near the machine, so every function except `presets()` throws `PermissionDenied` for other users. Run alarm scripts as an administrator, for example as a scheduled task. The play functions take an optional `options` object: | Field | Description | |---|---| | `volume` | `0` to `1`, default `0.8` (`0` also means the default). The PC speaker has no volume | | `repeat` | Play the sound this many times, `1` to `100` (default `1`) | | `wave` | Tone waveform: `"sine"`, `"square"`, `"triangle"` or `"sawtooth"` | | `output` | `"auto"` (default: sound card, else PC speaker), `"card"` or `"pcspeaker"` | | `wait` | `true` (default) returns when the sound ends; `false` returns at once and lets it play on after the script ends | A play call returns `true` when the sound played to the end (or started, with `wait: false`) and `false` when `audio.stop()` ended it. It throws an `AudioError` when there is no output, a note or option is out of range, the sound would play longer than 5 minutes (repeats included), or 4 sounds are already playing. A sound started with `wait: false` that fails later is only logged. Every sound played is written to the system log with the user who played it. Limits: frequencies 20 to 20000 Hz (`0` is a rest), notes up to 10 seconds, patterns up to 256 notes. ```javascript requirelib("audio"); audio.alarm("siren", {repeat: 3}); ``` ## `audio.isAvailable()` → `bool` `true` when a sound card or PC speaker works on this server. ```javascript requirelib("audio"); if (!audio.isAvailable()) { sendResp("This server cannot make a sound"); } ``` ## `audio.status()` → object What the server can play on: `{soundCard, pcSpeaker, playing, cardError, speakerError}`. `playing` counts the sounds playing now; the two error fields say why an output is missing. The sound card is started on first use, and only once per run of ArozOS: if it fails, restart ArozOS after fixing it. ```javascript requirelib("audio"); var st = audio.status(); //{soundCard: true, pcSpeaker: false, playing: 0, cardError: "", speakerError: "..."} ``` ## `audio.presets()` → `string[]` Names `audio.alarm()` accepts: `beep`, `chime`, `success`, `error`, `warning`, `alarm`, `siren` and `critical`. Open to every user. ```javascript requirelib("audio"); sendJSONResp(audio.presets()); ``` ## `audio.beep(freq, ms, options)` → `bool` One tone: `freq` Hz (default `880`) for `ms` milliseconds (default `200`), as a square wave unless `options.wave` says otherwise. ```javascript requirelib("audio"); audio.beep(); audio.beep(1200, 500, {volume: 0.5}); ``` ## `audio.playPattern(notes, options)` → `bool` A sequence of notes, as a sine wave unless `options.wave` says otherwise. Each note is `{freq, ms}` or `[freq, ms]`; `freq` `0` is a rest, and `to` (`{freq, ms, to}` or `[freq, ms, to]`) slides the pitch to that frequency during the note. ```javascript requirelib("audio"); //Morse SOS, three times var s = [[1000, 120], [0, 120], [1000, 120], [0, 120], [1000, 120], [0, 360]]; var o = [[1000, 360], [0, 120], [1000, 360], [0, 120], [1000, 360], [0, 360]]; audio.playPattern(s.concat(o, s, [[0, 800]]), {repeat: 3, wave: "square"}); //A rising sweep audio.playPattern([{freq: 400, to: 1600, ms: 1500}]); ``` ## `audio.alarm(name, options)` → `bool` Play a preset (default `"alarm"`, a two tone fire alarm). `beep`, `chime` and `success` suit good news; `error`, `warning`, `alarm`, `siren` and `critical` are increasingly urgent. ```javascript requirelib("audio"); requirelib("sysinfo"); var disks = sysinfo.getDiskInfo(); for (var i = 0; i < disks.length; i++) { if (disks[i].UsedPercent > 95) { audio.alarm("critical", {repeat: 5}); break; } } ``` ## `audio.playFile(vpath, options)` → `bool` Play a WAV (8 to 32 bit PCM or float, mono to 8 channels) or MP3 file from the user's storage, up to 64 MB. Sound card only: the PC speaker only plays tones. ```javascript requirelib("audio"); audio.playFile("user:/Sounds/raid-degraded.mp3", {volume: 0.6, wait: false}); ``` ## `audio.stop()` → `number` Stop every sound that is playing, including sounds other scripts started with `wait: false`. Returns how many it stopped; their play calls return `false`. ```javascript requirelib("audio"); audio.alarm("siren", {repeat: 50, wait: false}); //... later, from another request audio.stop(); ```