Class WebAudio
java.lang.Object
org.vaadin.firitin.util.WebAudio
Utility for playing simple attention sounds via the browser's Web Audio API.
Handy when a Vaadin UI needs to grab the user's attention with a short beep without shipping any audio assets – the tones are synthesized directly in the browser.
Browsers require a user gesture (typically a click or key press) before
an AudioContext is allowed to produce sound. If you want to play
sounds triggered by something other than a direct user interaction, call
resume() from inside e.g. some click handler once to unlock audio
playback for the rest of the session.
WebAudio.get().playOkBeep(); WebAudio.get().playAlarm(); WebAudio.get().playBeep(150, 600, WebAudio.Waveform.SQUARE);
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic WebAudioget()Plays a dramatic two-tone klaxon for warnings and errors: six cycles of alternating 880 Hz and 600 Hz square-wave tones (200 ms each), with a flat envelope so the sound stays loud throughout.voidplayBeep()Plays a short 100 ms beep at 800 Hz with a sine waveform.voidplayBeep(int durationMs) Plays a sine beep at 800 Hz with the given duration.voidplayBeep(int durationMs, double frequencyHz) Plays a sine beep with the given duration and frequency.voidplayBeep(int durationMs, double frequencyHz, WebAudio.Waveform waveform) Plays a beep with the given duration, frequency and waveform.playBeeps(int count, int durationMs, int gapMs) Playscountsine beeps at 800 Hz, each lastingdurationMsmilliseconds, withgapMsmilliseconds of silence between them.playBeeps(int count, int durationMs, int gapMs, double frequencyHz) Playscountsine beeps with the given duration, frequency and gap.playBeeps(int count, int durationMs, int gapMs, double frequencyHz, WebAudio.Waveform waveform) Playscountbeeps with the given duration, frequency, gap and waveform.voidPlays a short, friendly 100 ms sine beep at 800 Hz – the kind of sound you'd use to confirm a successful action.Plays a "get ready" sequence: five short 800 Hz beeps spaced one second apart, followed by a longer 500 ms beep at 1000 Hz to indicate the actual start.resume()Resumes the underlyingAudioContextif it was suspended.
-
Field Details
-
DEFAULT_FREQUENCY_HZ
public static final double DEFAULT_FREQUENCY_HZ- See Also:
-
-
Constructor Details
-
WebAudio
public WebAudio(com.vaadin.flow.component.UI ui)
-
-
Method Details
-
get
-
playOkBeep
public void playOkBeep()Plays a short, friendly 100 ms sine beep at 800 Hz – the kind of sound you'd use to confirm a successful action. -
playBeep
public void playBeep()Plays a short 100 ms beep at 800 Hz with a sine waveform. -
playBeep
public void playBeep(int durationMs) Plays a sine beep at 800 Hz with the given duration.- Parameters:
durationMs- the beep duration in milliseconds
-
playBeep
public void playBeep(int durationMs, double frequencyHz) Plays a sine beep with the given duration and frequency.- Parameters:
durationMs- the beep duration in millisecondsfrequencyHz- the tone frequency in Hertz
-
playBeep
Plays a beep with the given duration, frequency and waveform. A short exponential fade-out is applied so the tone doesn't end with an audible click. UseWebAudio.Waveform.SQUAREorWebAudio.Waveform.SAWTOOTHfor a harsher, more attention-grabbing sound.- Parameters:
durationMs- the beep duration in millisecondsfrequencyHz- the tone frequency in Hertzwaveform- the oscillator waveform
-
playBeeps
Playscountsine beeps at 800 Hz, each lastingdurationMsmilliseconds, withgapMsmilliseconds of silence between them.- Parameters:
count- the number of beeps to playdurationMs- duration of each beep in millisecondsgapMs- silence between consecutive beeps in milliseconds- Returns:
- a future that completes after the last beep has finished
-
playBeeps
Playscountsine beeps with the given duration, frequency and gap.- Parameters:
count- the number of beeps to playdurationMs- duration of each beep in millisecondsgapMs- silence between consecutive beeps in millisecondsfrequencyHz- the tone frequency in Hertz- Returns:
- a future that completes after the last beep has finished
-
playBeeps
public CompletableFuture<Void> playBeeps(int count, int durationMs, int gapMs, double frequencyHz, WebAudio.Waveform waveform) Playscountbeeps with the given duration, frequency, gap and waveform. All beeps are scheduled in a single round trip to the browser, so timing is sample-accurate.- Parameters:
count- the number of beeps to playdurationMs- duration of each beep in millisecondsgapMs- silence between consecutive beeps in millisecondsfrequencyHz- the tone frequency in Hertzwaveform- the oscillator waveform- Returns:
- a future that completes after the last beep has finished
-
playAlarm
Plays a dramatic two-tone klaxon for warnings and errors: six cycles of alternating 880 Hz and 600 Hz square-wave tones (200 ms each), with a flat envelope so the sound stays loud throughout. Total duration is roughly 2.4 seconds.- Returns:
- a future that completes after the alarm has finished
-
playStartSequence
Plays a "get ready" sequence: five short 800 Hz beeps spaced one second apart, followed by a longer 500 ms beep at 1000 Hz to indicate the actual start. The returned future completes when the last beep has finished playing on the client.- Returns:
- a future that completes after the sequence has finished
-
resume
Resumes the underlyingAudioContextif it was suspended. Most browsers suspend audio contexts created outside of a user gesture; calling this once from inside a click handler is usually enough to unlock subsequent playback.- Returns:
- a future that completes when the context is running again
-