If a program is started from a shell running in e.g. KDE Konsole and if that has been told to honour the ASCII bell control character, then a simple Write(#$07) will result in a sound controlled by the desktop environment's thematicisation.
I was hoping that there would be a way of getting themed control using either libnotify or wmctrl, but so far have had no success. libnotify is oriented towards displaying an alert panel, it can in principle specify a sound name as a hint, but people who definitely wanted an audible alert in a script file were using both send-notify (client to libnotify) and aplay or similar.
General investigation suggests that while aplay is not universally installed, libasound is... at least on systems which purport to be general-purpose. The alsapas libraries are excessively cumbersome for generating a simple beep, and elsewhere it is noted that alsapas includes stuff which varies by ALSA version... I'm already at the position of having to use Docker to generate a binary for one particular server since the ALSA version is different from most of what I'm running around here.
Robert Rozee's approach at
https://forum.lazarus.freepascal.org/index.php/topic,49502.msg359352.html?PHPSESSID=ju53vbrr54cosmk03869a2i6r2#msg359352 appears to work, I'm currently converting it from static to dynamic linkage so that it doesn't force a program to have a hard dependency on libasound.
It would probably be desirable for a GUI app to check whether it has a stdout and has Konsole etc. as a parent, and if those conditions were met to use Write(#$07) since this will output themed (or no) sound as selected by the user, and will interact with any notification hooks he has set up. I've not yet worked out a reliable way to do this, and suspect that it might depend on the desktop environment installed (i.e. KDE vs Gnome etc.).
MarkMLl