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 spectral centroid audio feature and push it to a node output. 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.

[Node(Name = "Centroid")]
public class SpectralCentroidNode : NodeBase
{
    [NodeOutput]
    public float Centroid { get; set; }

    public SpectralCentroidNode(IExtensionApi api)
        : base(api)
    {
        // Subscribe to the spectral centroid audio feature via the extension API. 
        Api.Subscribe(AudioFeatureType.SpectralCentroid);
    }

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

        Centroid = centroidAudioFeature.Centroid;
        OnPropertyChanged(nameof(Centroid));
    }
}

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.