- Home /
How to control scene lighting based on amplitude of AudioSource?
Hi!
In my game I want to control scene lightning based on player's heartbeat (amplitude of heartbeat sound). My idea was find all max and min amplitudes in curve of heartbeat sound (I grabbed sound samples with method AudioClip.GetData), found all max and min values and after that during playing of sound in Update method I'm checking if current sample is equal to founded max and min values (for getting index of current sample I used method AudioSource.timeSamples) and based on that fade in or faded out scene lightning. But this solution looks isn't very reliable. Is there some better solution how do that? Is possible to add listeners on max and min samples of sound curve? Thank you for any ideas and advices.
Answer by IgorAherne · Aug 05, 2015 at 09:28 PM
Hm, you can do it during the runtime, adjusting settings at any given instance:
http://docs.unity3d.com/ScriptReference/AudioSource-pitch.html
look at the pitch and as long as u have an array of light references in your script, you can set the brightness / color based on the sound.
Or access the volume. Check out all the variables in here (including pitch, volume, etc)
in there, they are editing the pitch, but you only need to take a look at it. Using this value you can adjust all the necessary settings
@IgorAherne Thank you for your advice but probably I didn't described my problem enough. In my game I have sound of heart beat and I want to dispatch custom event to gameobject on every heart beat (on time of local max amplitude on sound curve). I play a bit with mentioned pitch property of audiosource but by default it's value is equal to 1 so I don't understand how to use it to for solving of my problem. Can you be more specific? Thanks