In previous Unity projects, I’ve sampled an audio source’s current waveform volume (the amplitude of the waveform at the time its currently playing essentially, post effects/ect) and piped it into a simple ‘talker’ script that basically passes that value (multiplied by a constant) to an animation to get the actor to ‘talk’ by opening its mouth when the volume is loud and closing it when its quiet.
Is there a way to access this data easily in Fmod? I dont see anything obvious in the API so i figured I’d ask here.
sampleTime = Mathf.Min (audioSource.timeSamples + sampleOffset, audioSource.clip.samples - sampleDataLength);
currentUpdateTime = 0f;
audioSource.clip.GetData(clipSampleData, sampleTime); //I read 1024 samples, which is about 80 ms on a 44khz stereo clip, beginning at the current sample position of the clip.
clipLoudness = 0f;
i = 0;
for (i = 0; i < clipSampleData.Length; i++)
clipLoudness += Mathf.Abs(clipSampleData[i]);
clipLoudness /= sampleDataLength; //clipLoudness is what you are looking for
is roughly the code we use for unity audio sources if that helps clarify what type of data i’m trying to access.
You can access peak (loudest sample in a given audio buffer) and rms (root mean squared, average sample value) volume data by enabling metering on any of the channel’s DSPs with
DSP::setMeteringEnabled, and retrieve its
Here is a simple example of how to retrieve the left and right volumes of a playing event instance.
public class DisplayEventSound : MonoBehaviour
public FMODUnity.EventReference eventReference;
public float leftVolume;
public float rightVolume;
FMOD.DSP_METERING_INFO inputInfo, outputInfo;
FMODUnity.RuntimeManager.StudioSystem.getEvent(eventReference.Path, out FMOD.Studio.EventDescription eventDescription);
eventDescription.createInstance(out FMOD.Studio.EventInstance eventInstance);
// Need to wait for event instance to play before getting channel group
yield return null;
while (playbackState != FMOD.Studio.PLAYBACK_STATE.PLAYING);
channelGroup.getDSP(FMOD.CHANNELCONTROL_DSP_INDEX.FADER, out dsp);
// Update is called once per frame
if(playbackState == FMOD.Studio.PLAYBACK_STATE.PLAYING)
dsp.getMeteringInfo(out inputInfo, out outputInfo);
leftVolume = outputInfo.rmslevel;
rightVolume = outputInfo.rmslevel;