- Home /
AudioSource.GetOutputData example?
Hello. I know that using AudioSource.GetOutputData (http://unity3d.com/support/documentation/ScriptReference/AudioSource.GetOutputData.html) returns a list of values that are dependent on the current audio that is playing. Can someone please give me an example of this? Thanks.
Answer by duck · Nov 27, 2010 at 09:45 AM
I haven't used this myself, but a very simple way to use this would be to get the average of the values in the sample array, and use this as a rough indication of volume. Eg:
function Update() {
var samples = audio.GetOutputData(256, 0);
var average = 0;
for (var sample in samples) {
average += sample;
}
average /= samples.Length;
print("Average reading: "+average);
}
Of course you could do more complex analysis of this data such as a Fourier Transform to get information about the frequencies (for a graphic equaliser effect), but that is a rather large subject in itself!
I put that code in my scene (with audio playing) and the number that was printed always stayed at 0?
Did you actually put it on the object which had the playing AudioSource attached? This function only reads a single audiosource so you have to make sure it's the one which is currently playing.
Yes. I put it on the object with the audio source that is playing...and nothing.
If you want frequency (spectrum) info then use GetSpectrumData() (Then we're doing the Fourier Transformation for you (fast in C++).
Your answer
Follow this Question
Related Questions
Using raycast to sonify point cloud data and geometry of shapes 0 Answers
Unity Audio Output Data 0 Answers
Audio source data? 0 Answers
Audio Analysis? 1 Answer
Extracting or viewing raw audio data from an audio listener 0 Answers