Custom sounds
Besides requests and responses, the application can ask for a sound of its own, by name.
Like everything else in Sonify, this only happens in dev mode by default. In production and in tests the annotation and the injected object are there and do nothing, unless the application opts in.
Playing a sound
With an annotation
Annotate a method of a CDI bean with @PlaySound. The sound is played every time the method is called, before it runs:
import jakarta.enterprise.context.ApplicationScoped;
import io.quarkiverse.sonify.PlaySound;
@ApplicationScoped
public class OrderService {
@PlaySound("order-placed")
public Order place(Cart cart) {
// ...
}
}
On a class, the annotation applies to all of its business methods.
@PlaySound is an interceptor binding, so the usual rules of CDI interception apply: the class must be a CDI bean, and a private method is not intercepted.
From code
Inject a SoundPlayer to play a sound from anywhere:
import jakarta.inject.Inject;
import io.quarkiverse.sonify.SoundPlayer;
public class AuditLog {
@Inject
SoundPlayer soundPlayer;
void record(Entry entry) {
soundPlayer.play("audit");
// ...
}
}
play returns immediately: the sound is mixed on a thread of its own, and is dropped if too many sounds are already playing.
What a custom sound sounds like
A name that is not configured gets a short sound of two notes, derived from the name: different names sound different, and the same name always sounds the same.
To choose the sound, configure the name with a WAV file, or turn it off:
quarkus.sonify.sound."order-placed"=sounds/coin.wav
quarkus.sonify.sound."audit"=off
Defining sounds in code
For anything a property cannot say, implement SonifyCustomizer in a CDI bean.
It can define custom sounds, and replace the sounds of HTTP methods and status codes as well:
import jakarta.enterprise.context.ApplicationScoped;
import io.quarkiverse.sonify.SonifyCustomizer;
import io.quarkiverse.sonify.SoundRegistry;
import io.quarkiverse.sonify.Wave;
@ApplicationScoped
public class MySounds implements SonifyCustomizer {
@Override
public void customize(SoundRegistry sounds) {
// tones: wave, frequency in hertz, start in ms, duration in ms
sounds.sound("shipped")
.tone(Wave.SINE, 392, 0, 150)
.tone(Wave.SINE, 523, 120, 300);
// nothing at all
sounds.status(404).off();
// a sound file, for a whole class of status codes
sounds.statusClass(5).file("sounds/alarm.wav");
// audio that you compute yourself: mono samples between -1 and 1, and their rate
sounds.method("POST").samples(myPcm, 44100);
}
}
A sound is defined in one of six ways:
| Method | Sound |
|---|---|
|
One synthesized tone that fades out. Call it several times to build a sound of several tones. The waves are |
|
A WAV or MIDI file, found the same way as a file in the configuration. |
|
Notes, as a MIDI sequence: see MIDI. |
|
Notes, written as text: see Notes as text. |
|
Computed audio. |
|
Silence. |
A sound lasts two seconds at most, or what quarkus.sonify.max-sound-length says, up to 10 seconds: a tone must end within that time, and files, samples, MIDI and notes are cut.
A sound that is still playing is not started again, so a long sound does not pile up when many requests ask for it.
The customizer is called when the application starts, and in dev mode again on every live reload.
If several beans implement SonifyCustomizer, all of them are called.
A quarkus.sonify.* property for the same sound wins over the customizer, so that a sound defined in code can still be changed or silenced from the configuration, for example in a local .env file.
MIDI
A sound can be music: a MIDI file, or notes defined in code.
A MIDI file works wherever a sound file does, in the configuration and in file(path):
quarkus.sonify.status."202"=sounds/arpeggio.mid
In code, a sound can be any MIDI sequence, built with the javax.sound.midi API of the JDK: instruments, notes, velocities, timing and tempo.
Sequence sequence = new Sequence(Sequence.PPQ, 120);
Track track = sequence.createTrack();
// program 11 is the vibraphone in General MIDI
track.add(new MidiEvent(new ShortMessage(ShortMessage.PROGRAM_CHANGE, 0, 11, 0), 0));
for (int note : new int[] { 65, 69, 72 }) {
track.add(new MidiEvent(new ShortMessage(ShortMessage.NOTE_ON, 0, note, 90), 0));
track.add(new MidiEvent(new ShortMessage(ShortMessage.NOTE_OFF, 0, note, 0), 240));
}
sounds.status(204).midi(sequence);
Like any other sound, MIDI is cut at two seconds, or at quarkus.sonify.max-sound-length.
Notes as text
Notes can also be written as text, in the configuration after notes:, or in code with notes(text):
quarkus.sonify.status."201"=notes:C5/16 E5/16 G5/16 C6/8
quarkus.sonify.status."5xx"=notes:[program=57, tempo=90] G3/4 D3/2
sounds.sound("shipped").notes("[tempo=140] C5/8 E5/8 G5+C6/4.");
| Text | Meaning |
|---|---|
|
A note: a letter, an optional sharp ( |
|
A duration, after a note: a whole note, a half note, a quarter note, down to a sixty-fourth. Without it, a note lasts as long as the one before, and the first one a quarter note. |
|
A dot makes a duration half as long again. |
|
A chord: notes played together. |
|
A rest. |
|
Settings, first and all optional: the General MIDI instrument (0, the piano, by default), the quarter notes per minute (120 by default), and how hard the notes are played, from 1 to 127 (100 by default). |
Notes are played as MIDI, with the instruments of the soundfont if there is one.
A mistake in the text of the configuration is logged with what is wrong and where, and the sound is ignored; in code, notes(text) throws an IllegalArgumentException with the same message.
Instruments and soundfonts
MIDI is played by the synthesizer of the JDK, with its default instruments. On Windows these are the General MIDI instruments of Windows. Elsewhere, the JDK makes a basic set of instruments the first time it needs them, which takes a few seconds.
A soundfont replaces them, for all the MIDI sounds:
quarkus.sonify.soundfont=sounds/piano.sf2
A soundfont is a SoundFont 2 (.sf2) or DLS (.dls) file, found the same way as a sound file.
A MIDI note plays the instrument of the soundfont with its program number, so a soundfont with a single instrument should have it on program 0, and the MIDI should select program 0, or not select one at all.
The soundfont is read once, and again only when its file changes: a large soundfont does not slow down live reloads.
Playing MIDI with a soundfont relies on an interface of the JDK synthesizer that the JDK does not export. The extension asks Quarkus to export it to the application, so there is nothing to set up, with one exception: on Java 21 to 24, Quarkus cannot do it in dev mode and in tests. There, start the JVM with the export yourself:
./mvnw quarkus:dev -Djvm.args="--add-exports java.desktop/com.sun.media.sound=ALL-UNNAMED"
For tests, add the same option to the argLine of Surefire.
A packaged application and a native executable have the export on every Java version.
When the export is missing, Sonify logs a warning and plays MIDI with the default instruments.
Where Sonify makes no sound (outside of dev mode, unless the application opts in), the customizer is never called, and Quarkus removes the bean as unused, unless the application itself injects it.