I am trying to create a simple music game in Unity 3D. This will start with a selection of multiple songs available in the list. While doing processing, I can easily see the pitch of the songs with the help of visualizers.
The problem I am facing is with the visualizers and with high pitch notes. I think it has something to do with different voice played at once. I am playing simple Darude Sandstorm (just to test and also, I don’t have the original soundtrack which will run in real time). The pitch is playing a crucial role, as I will use this pitch to tell the user to hit the different notes (with high, small and medium options). By now, I have tested with GetComponent().PitchaValue; and GetSpectrumData(setting1, setting2, setting3);. I am making a rough guess that it is not possible with different pitches as the base voice is overriding the highest pitch played.
I am implementing the lines (vv) given below. Before implementing this, I have worked with multiple lines of code. Please don’t suggest a new solution, as I have worked allot with the visualizer. The code is correctly working with if and else statements, and I am running some trial and some error at the moment.
//Here is the class I am implementing{
using UnityEngine;
using System.Collections;
public class soundspectrume : MonoBehaviour {
public GameObject prefab; // the cube prefab
public int numberOfObjects = 25; // number of cubes to make visualizer
public float radius = 5f; // radius of circle
public GameObject[] cubes; //array of the created cubes
public float PitchAmp;
public Color mycolour;
void Start()
{
PitchAmp = GameObject.Find("Voice Source").GetComponent<FFT>().PitchAmp; //I am declaring the value with simple 0 as the starting value.
for (int iter = 0; iter < totalObj; iter++) //This simple loop will help with creation of cubes
{
float angle = iter * Mathf.PI * 2 / totalObj;
Vector3 position = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; //I am getting this code from Unity docs
Instantiate(prefab, position, Quaternion.identity); //Creating instance of the cubes with properties described above^^
}
cubes = GameObject.FindGameObjectsWithTag("cube");
}
// This is a simple call performed on all the objects at the same time one time with every frame
void Update () {
PitchAmp = GameObject.Find("Voice Source").GetComponent<FFT>().PitchAmp;
print(PitchAmp);
float[] spectrum = AudioListener.GetSpectrumData(2048, 0, FFTWindow.Hamming); // This means less control compared with maximum punch value
for (int i = 0; i < totalObj; i++)
{
if (PitchAmp <= 200)
{
Vector3 previousscale = cubes[i].transform.localScale;
previousscale.y = spectrum[i] * 500; //take spectrum number and multiply, higher frequency songs will need greater multiplier
cubes[i].transform.localScale = previousscale;
}
else if (PitchAmp >= 201)
{
Vector3 previousscale = cubes[i].transform.localScale;
previousscale.y = spectrum[i] * 1; //take spectrum number and multiply, higher frequency songs will need greater multiplier
cubes[i].transform.localScale = previousscale;
}
else if (PitchAmp >= 500)
{
Vector3 previousscale = cubes[i].transform.localScale;
previousscale.y = spectrum[i] * 100; //take spectrum number and multiply, higher frequency songs will need greater multiplier
cubes[i].transform.localScale = previousscale;
}
}
}
}
Here is the image, hope it will help you to solve the problem.
Further information:
I am using a simple mp3 with the help of getting a spectrum of the data using AudioSource, using the AudioListener as an intermediate.
Note: Above code is the property of unturneditemid.com and I am using it after taking the permission from the owner. If you want to use it and you have to provide proper attributes to the author.
Thanks for your time,
