Skip to content

Subscribe to Audio Features

Info

Use this method only if your node explicitly relies on a specific audio feature. Alternatively, you can hook up a node input, which the user can connect to any other audio feature or signal node as well.

Let's assume, we would like to subscribe to the temporal beat audio feature and change the color on the canvas on each beat. You can subscribe to audio features via the extension API Subscribe() method. All processed and subscribed audio features will be provided via the OnProcessAudioFeature(AudioFeature audioFeature) method.

[EffectLayer(Name = "Beat Color Fill")]
public class BeatColorFillEffectLayer : PixelEffectLayerBase
{
    private SKColor _color = SKColors.Black;
    private Random _random = new();

    public SpectralCentroidNode(IExtensionApi api, int canvasWidth, int canvasHeight)
        : base(api, canvasWidth, canvasHeight)
    {
        // Subscribe to the temporal beat audio feature via the extension API. 
        Api.Subscribe(AudioFeatureType.TemporalBeats);
    }

    protected override SKColor RenderPixel() => _color;

    // Override this method to receive all processed and subscribed audio features. 
    public override void OnProcessAudioFeature(AudioFeature audioFeature)
    {
        // We only expect TemporalBeatAudioFeature here. However, you can subscribe 
        // to multiple audio features as well. 
        if (audioFeature is not TemporalBeatAudioFeature beatAudioFeature)
            return;

        if (beatAudioFeature.BeatDetected)
            _color = SKColor.FromHsv(_random.Next(360), 255, 255);
    }
}

The subscription automatically ends, once the node gets disposed. Alternatively, you can also call the method UnsubscribeAll() on the extension API to unsubscribe from all previously subscribed audio features.