Skip to content

AudioIndicator

roosit edited this page Oct 16, 2026 · 1 revision

AudioIndicator

Up | Previous | Next | Русский

Full source with short explanations

Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not stored in the working file AudioIndicator.java: the production code stays clean, and the explanations live in the wiki.

package com.velitask.plugin.official;

import com.velitask.sdk.FrameContext;
import com.velitask.sdk.Indicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.TickContext;
import com.velitask.sdk.properties.AudioSourceProperty;
import com.velitask.sdk.properties.BooleanProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.IntegerSliderProperty;
import com.velitask.sdk.properties.PropertyGroup;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;

/* Audio indicator: a scrolling waveform of an audio source around the current moment, and the sound of that source. */
public class AudioIndicator extends Indicator {

    /* Stable name of the indicator. */
    public static final String NAME = "audio";

    /* Prefix of the indicator localization keys. */
    private static final String KEY = _KEY + "." + NAME;

    /* Fixed palette: background, played part (peak and RMS), future part (peak and RMS), the current-moment line. */
    private static final Color BG = new Color(0x1E2024);
    private static final Color PK_PLAY = new Color(0x3E9E86);
    private static final Color RMS_PLAY = new Color(0x6FE0C2);
    private static final Color PK_FUT = new Color(0x37514B);
    private static final Color RMS_FUT = new Color(0x4E6E66);
    private static final Color HEAD = new Color(0xFFD166);

    /* The selected audio source; not saved in a template. */
    private final AudioSourceProperty mAudio = new AudioSourceProperty();

    /* Width of the waveform window in seconds: 2..60, 10 by default. */
    private final IntegerSliderProperty mWindowSeconds
            = new IntegerSliderProperty(10, "windowSeconds", localized(KEY + ".windowSeconds.title")) {
        {
            setRange(2, 60);
        }
    };

    /* The sound of the source is on by default. */
    private final BooleanProperty mAudioEnabled
            = new BooleanProperty(true, "audioEnabled", localized(KEY + ".audioEnabled.title"));

    /* Volume in percent: 0..100, 100 by default. */
    private final IntegerSliderProperty mVolume
            = new IntegerSliderProperty(100, "volume", localized(KEY + ".volume.title")) {
        {
            setRange(0, 100);
        }
    };

    public AudioSourceProperty getAudio() {
        return mAudio;
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public String getTitle() {
        return localized(KEY + ".title");
    }

    @Override
    public String getDescription() {
        return localized(KEY + ".description");
    }

    @Override
    public String getTags() {
        return localized(KEY + ".tags");
    }

    @Override
    /* Default placement: bottom left corner, margin 50 on the right and at the bottom, size 480x120. */
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.BOTTOM);
        maket.setHorizontal(HorizontalAlign.LEFT);
        maket.setLayerMargin(null, null, 50d, 50d);
        maket.setLayerSize(480d, 120d);
    }

    @Override
    /* The order of the properties = the order in JSON and in the settings panel. */
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mAudio, mWindowSeconds, mAudioEnabled, mVolume
        };
    }

    @Override
    /* The window is in the "Appearance" group, sound and volume are in the "Audio" group. */
    public void configureDisplay(DisplayConfig config) {
        config.set(mWindowSeconds, PropertyGroup.APPEARANCE);
        config.set(mAudioEnabled, PropertyGroup.AUDIO);
        config.set(mVolume, PropertyGroup.AUDIO);
    }

    @Override
    /* Drawing: a time window around the current moment, the loudness envelope as bars, the current-moment line. */
    public void render(IndicatorContext indicatorContext) {
        AudioContext ctx = (AudioContext) indicatorContext;
        /* No source selected - draw nothing. */
        if (ctx.audio.sourceId == 0) {
            return;
        }
        Graphics2D g = ctx.graphics;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int width = Math.max(1, ctx.width);
        int height = Math.max(1, ctx.height);

        /* Bounds of the source in internal time and its full duration. */
        long startRaw = ctx.audio.clampRawToRange(Long.MIN_VALUE);
        long endRaw = ctx.audio.clampRawToRange(Long.MAX_VALUE) + 1;
        long total = Math.max(1, endRaw - startRaw);

        /* The window cannot be longer than the source itself. */
        long windowMs = Math.min((long) ctx.windowSeconds * 1000L, total);
        if (windowMs <= 0) {
            return;
        }

        /* The current moment is in the center of the window; near the edges of the source the window shifts and the line moves away from the center. */
        long rawCenter = ctx.audio.clampRawToRange(ctx.audio.convertToRawTime(ctx.player.time));
        long t0 = rawCenter - windowMs / 2;
        if (t0 < startRaw) {
            t0 = startRaw;
        }
        if (t0 + windowMs > endRaw) {
            t0 = endRaw - windowMs;
        }
        long t1 = t0 + windowMs;

        /* Number of bars: at least 32, roughly one per 4 pixels of width. */
        int buckets = Math.max(32, width / 4);
        /* wave[0] is the peak envelope, wave[1] is the root mean square (RMS), values 0..1. */
        float[][] wave = ctx.audio.windowWaveform(t0, t1, buckets);
        float[] peak = wave[0];
        float[] rms = wave[1];

        /* Background fill. */
        g.setColor(BG);
        g.fillRect(0, 0, width, height);

        int cy = height / 2;
        float half = height * 0.45f;
        int slot = Math.max(1, width / buckets);
        int barW = Math.max(1, slot - 1);
        /* Position of the current-moment line in pixels. */
        int playheadX = (int) ((rawCenter - t0) * width / windowMs);

        /* To the left of the line is the "played" palette, to the right the "future" one; first the wide peak, with the narrow RMS on top of it. */
        for (int i = 0; i < buckets; i++) {
            int x = i * width / buckets;
            boolean played = x <= playheadX;
            int ph = Math.round(peak[i] * half);
            int rh = Math.round(rms[i] * half);
            g.setColor(played ? PK_PLAY : PK_FUT);
            g.fillRect(x, cy - ph, barW, Math.max(1, ph * 2));
            g.setColor(played ? RMS_PLAY : RMS_FUT);
            g.fillRect(x, cy - rh, barW, Math.max(1, rh * 2));
        }

        /* The current-moment line, 2 pixels wide. */
        g.setColor(HEAD);
        g.fillRect(Math.min(width - 2, Math.max(0, playheadX)), 0, 2, height);
    }

    /* Per-frame snapshot of values: the audio source and the window width. */
    public class AudioContext extends IndicatorContext {

        public final AudioSourceProperty.AudioSourceContext audio;
        final int windowSeconds;

        public AudioContext(FrameContext frame) {
            super(frame);
            windowSeconds = mWindowSeconds.get(frame);
            audio = mAudio.createContext(frame);
        }
    }

    @Override
    /* Sound: the engine asks for a voice on every tick; null means no sound. */
    public IndicatorContext.Voice resolveVoice(TickContext tick) {
        if (!mAudioEnabled.get(tick)) {
            return null;
        }
        long id = mAudio.createContext(tick).sourceId;
        return (id == 0) ? null : new IndicatorContext.Voice(id, mVolume.get(tick) / 100.0);
    }

    @Override
    /* The context is created from the FrameContext of the current frame. */
    public IndicatorContext createContext(FrameContext frame) {
        return new AudioContext(frame);
    }
}

Purpose

AudioIndicator shows a scrolling waveform of the selected audio source (a song, a voice, any sound file of the project) and plays its sound. The center of the window is the current moment of playback; on the left is the sound that has already played, on the right the upcoming sound. The default placement is the bottom left corner, 480×120 with a margin of 50 on the right and at the bottom.

How it works

Data. AudioSourceProperty gives an AudioSourceContext with sourceId, convertToRawTime, clampRawToRange and windowWaveform(t0, t1, buckets). The last one returns a pair of arrays [peak, rms] of length buckets with values 0..1 - the loudness envelope over a time window. The project time ctx.player.time is converted to the internal time of the source and clamped to its bounds.

Settings. Four properties in the order of defineProperties():

  • audioSource (AudioSourceProperty) - selection of the audio source.
  • windowSeconds (IntegerSliderProperty, 2..60, 10) - window width in seconds; the PropertyGroup.APPEARANCE group.
  • audioEnabled (BooleanProperty, true) and volume (IntegerSliderProperty, 0..100, 100) - the PropertyGroup.AUDIO group.

Unlike the video, the indicator has no adaptive source flag, no border and no skins. The colors of the waveform are class constants and cannot be changed in the settings.

Sound. resolveVoice(TickContext) returns IndicatorContext.Voice(sourceId, volume / 100.0) or null (the sound is off or no source is selected). Playback and mixing are done by the engine. The sound flag and the volume of AudioSourceProperty itself are marked as deprecated and are not read by the engine.

Drawing.

  1. If no source is selected (sourceId == 0) - exit.
  2. The source bounds are startRaw..endRaw; the window length is windowMs = min(windowSeconds × 1000, duration).
  3. The center of the window is the current time of the source; the window start t0 is shifted so that the window does not go beyond the source bounds.
  4. The number of bars is buckets = max(32, width / 4); the envelope windowWaveform(t0, t1, buckets) is requested.
  5. The background is filled; for each bucket a wide peak bar is drawn and a narrower RMS bar on top of it, symmetrically about the middle line. The height is limited to 45% of the layer height in both directions.
  6. Bars to the left of the current-moment line are drawn with the "played" palette (lighter), to the right with the "future" one (darker).
  7. On top, a vertical current-moment line 2 pixels wide is drawn.

Features and limitations

  • At the start and at the end of the source the window runs into the bound, so the current-moment line moves from the center to the edge.
  • If the source is shorter than the window, the window equals the whole source.
  • The bar size and the line thickness are set in pixels of the layer and do not depend on the scale (ctx.scale is not used).
  • The minimum bar height is 1 pixel, even in silence.
  • The source is not stored in a template; after applying a template it has to be selected again.
  • The description of the save format is AudioIndicator JSON; the user pages are Audio and Audio JSON.

Clone this wiki locally