I'm doing an experimentation with my platform game and I want to see if I can move my platforms based on music. I've tried getting the music frequency or channels but there seems to be no change in it. Code (csharp): using UnityEngine; using System.Collections; public class music : MonoBehaviour { private float music_vol_peak; private AudioSource audioHolder; // Use this for initialization void Start (){ audioHolder = GameObject.FindGameObjectWithTag("Music").GetComponent<AudioSource>(); } // Update is called once per frame void Update () { music_vol_peak = audioHolder.clip.frequency; //Debug.Log(music_vol_peak); } } Thats the code I've been using. Is there anyway to get something like the right and left peak volume channels?
http://docs.unity3d.com/Documentation/ScriptReference/AudioClip.GetData.html This is where the only actual audio data is stored, values from -1.0 to 1.0. frequency, channels, samples, etc are all static numbers that dont change.
Code (csharp): using UnityEngine; using System.Collections; public class music : MonoBehaviour { private AudioClip music_data; private AudioSource audioHolder; // Use this for initialization void Start (){ audioHolder = GameObject.FindGameObjectWithTag("Music").GetComponent<AudioSource>(); music_data = audioHolder.clip; float[] samples = new float[audio.clip.samples * audio.clip.channels]; audio.clip.GetData(samples, 0); int i = 0; while (i < samples.Length) { samples[i] = samples[i] * 0.5F; ++i; } audio.clip.SetData(samples, 0); Debug.Log(music_data); } // Update is called once per frame void Update () { } }
That was the original way I did it but it still came up with the error. I'll try it again and upload the new version if it doesn't work.
Code (csharp): audioHolder = GameObject.FindGameObjectWithTag("Music").GetComponent<AudioSource>(); music_data = audioHolder.clip; float[] samples = new float[music_data.samples * music_data.channels]; music_data.GetData(samples, 0); int i = 0; while (i < samples.Length) { samples[i] = samples[i] * 0.5F; ++i; } music_data.SetData(samples, 0); //Debug.Log(music_data); Same error.
Code is fine Its probably your audio clip change the import settings for Load Type (cant be compressed in memory) for the clip or perhaps use a WAV