Any way to get current audio volume from a Timeline Audio Track

title! I want to change the brightness of a material based on the current volume from a timeline audiotrack.

I’m aware of this solution where you sample the clip of an audiosource:

  • but that doesn’t work here because the audiotrack does not set the ‘clip’ property, it just plays through the audiosource directly.

It have a lot of clips being played in sequence so i’d prefer to not have to workaround this by playing the clips the normal way.

One alternative might be getting the volume of an AudioMixer channel instead, and i’m currently working on doing that but I haven’t found anything yet.

I appreciate any help or insight! :smiley:

@swanickj Have you tried implementing a similar solution using OnAudioFilterRead() ? That gives you access to the audiosource data regardless of whether it is being fed from a clip or from some other source.

To get the current volume from a Timeline Audio Track, you can use the PlayableDirector component to access the audio track’s timeline and retrieve the current audio clip being played. You can then use the AudioSource component on the same GameObject as the PlayableDirector to get the current audio volume.

Here’s an example code snippet that should achieve what you’re looking for:
// Get the PlayableDirector component on the GameObject
var director = GetComponent();

// Get the Audio Track from the Timeline
var audioTrack = (AudioTrack)director.playableAsset.GetOutputTrack(0);

// Get the current audio clip being played
var currentClip = audioTrack.GetDefaultPlayable(director).GetInput(0).GetPlayableAsset() as AudioClip;

// Get the AudioSource component on the same GameObject
var audioSource = GetComponent();

// Get the current audio volume
var currentVolume = audioSource.volume;

// Use the current volume to change the material brightness
// …
Note that this code assumes that there is only one AudioSource component on the same GameObject as the PlayableDirector. If there are multiple AudioSource components, you will need to modify the code to retrieve the correct one.