Class WebAudio

java.lang.Object
org.vaadin.firitin.util.WebAudio

public class WebAudio extends Object
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
    Modifier and Type
    Class
    Description
    static enum 
    Oscillator waveform.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    WebAudio(com.vaadin.flow.component.UI ui)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static WebAudio
    get()
     
    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.
    void
    Plays a short 100 ms beep at 800 Hz with a sine waveform.
    void
    playBeep(int durationMs)
    Plays a sine beep at 800 Hz with the given duration.
    void
    playBeep(int durationMs, double frequencyHz)
    Plays a sine beep with the given duration and frequency.
    void
    playBeep(int durationMs, double frequencyHz, WebAudio.Waveform waveform)
    Plays a beep with the given duration, frequency and waveform.
    playBeeps(int count, int durationMs, int gapMs)
    Plays count sine beeps at 800 Hz, each lasting durationMs milliseconds, with gapMs milliseconds of silence between them.
    playBeeps(int count, int durationMs, int gapMs, double frequencyHz)
    Plays count sine beeps with the given duration, frequency and gap.
    playBeeps(int count, int durationMs, int gapMs, double frequencyHz, WebAudio.Waveform waveform)
    Plays count beeps with the given duration, frequency, gap and waveform.
    void
    Plays 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.
    Resumes the underlying AudioContext if it was suspended.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • WebAudio

      public WebAudio(com.vaadin.flow.component.UI ui)
  • Method Details

    • get

      public static WebAudio 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 milliseconds
      frequencyHz - the tone frequency in Hertz
    • playBeep

      public void playBeep(int durationMs, double frequencyHz, WebAudio.Waveform waveform)
      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. Use WebAudio.Waveform.SQUARE or WebAudio.Waveform.SAWTOOTH for a harsher, more attention-grabbing sound.
      Parameters:
      durationMs - the beep duration in milliseconds
      frequencyHz - the tone frequency in Hertz
      waveform - the oscillator waveform
    • playBeeps

      public CompletableFuture<Void> playBeeps(int count, int durationMs, int gapMs)
      Plays count sine beeps at 800 Hz, each lasting durationMs milliseconds, with gapMs milliseconds of silence between them.
      Parameters:
      count - the number of beeps to play
      durationMs - duration of each beep in milliseconds
      gapMs - silence between consecutive beeps in milliseconds
      Returns:
      a future that completes after the last beep has finished
    • playBeeps

      public CompletableFuture<Void> playBeeps(int count, int durationMs, int gapMs, double frequencyHz)
      Plays count sine beeps with the given duration, frequency and gap.
      Parameters:
      count - the number of beeps to play
      durationMs - duration of each beep in milliseconds
      gapMs - silence between consecutive beeps in milliseconds
      frequencyHz - 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)
      Plays count beeps 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 play
      durationMs - duration of each beep in milliseconds
      gapMs - silence between consecutive beeps in milliseconds
      frequencyHz - the tone frequency in Hertz
      waveform - the oscillator waveform
      Returns:
      a future that completes after the last beep has finished
    • playAlarm

      public CompletableFuture<Void> 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

      public CompletableFuture<Void> 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

      public CompletableFuture<Void> resume()
      Resumes the underlying AudioContext if 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