Pitch detection for music

I am creating a rhythm based music game in which you should (theoretically) choose whatever song you want. This is created in Unity (5.4.1) with C #.

In real time, the step of the song, which he discovered and showed to the user through visualizers.

The problem I ran into is creating a visualizer, but it barely shows higher notes. I am afraid that this is due to the fact that (like in most musical works) several pitches are played immediately (for testing, I used Darude Sandstorm, waiting for a sound guy to give me music). In the end, I want this definition of pitch to show the user whether to hit a high, medium, or low note.

I tried using    GetComponent<FFT>().PitchValue; as well  AudioListener.GetSpectrumData(2048, 0, FFTWindow.Hamming);

I suppose the fear is that it simply cannot be done on songs with several games that play immediately (i.e., a basic high-pitched overlay).

I would like to hear any advice or find out if anyone has overcome a similar problem.

This is an example of what I'm working with right now, I tried several different online solutions (some of the information is actually related to a tutorial related in the code that has been commented out), which I intend to fully reference if I continue to follow this path (I have been working on this hour for a couple of weeks). It makes a pretty visualizer, but I can not use it fully. This works well as a visualizer without if else instructions, this is exactly where I am doing the trial version and the error at the moment. I follow this tutorial

//the class{
    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 PitchValue;
    public Color mycolour;

    void Start() 
    {
        PitchValue = GameObject.Find("Main Camera").GetComponent<FFT>().PitchValue;//this declares the pitch value but it will always be 0 as it is declared before the musics intro on start
        for (int i = 0; i < numberOfObjects; i++) //loop to create the cubes 
        {
            float angle = i * Mathf.PI * 2 / numberOfObjects;
            Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; // this code is from unity docs https://docs.unity3d.com/Manual/InstantiatingPrefabs.html
            Instantiate(prefab, pos, Quaternion.identity); //instanciate cubes 
        }
        cubes = GameObject.FindGameObjectsWithTag("cube");
    }

// Update is called once per frame
    void Update () {
        PitchValue = GameObject.Find("Main Camera").GetComponent<FFT>().PitchValue;
        print(PitchValue); 
        float[] spectrum = AudioListener.GetSpectrumData(2048, 0, FFTWindow.Hamming); // always move sample to power of 2, max will make cubes move without punch lower numbers less control.
        for (int i = 0; i < numberOfObjects; i++)
        {
            if (PitchValue <= 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 (PitchValue >= 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 (PitchValue >= 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;
            }
        }
    }
} 

, ,

+4
1

, - - , , , .

.

, , FFT ( ) - https://www.youtube.com/watch?v=V_8JSWVT36k JS.

using UnityEngine;
using System.Collections;

public class SPECTRUMANALYZER : MonoBehaviour 
{
    void Update( )
    {
        float[] spectrum = new float[1024];
        AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.Rectangular );
        float l1 = spectrum [0] + spectrum [2] + spectrum [4];
        float l2 = spectrum [10] + spectrum [11] + spectrum [12];
        float l3 = spectrum[20] + spectrum [21] + spectrum [22];
        float l4 = spectrum [40] + spectrum [41] + spectrum [42] + spectrum [43];
        float l5 = spectrum [80] + spectrum [81] + spectrum [82] + spectrum [83];
        float l6 = spectrum [160] + spectrum [161] + spectrum [162] + spectrum [163];
        float l7 = spectrum [320] + spectrum [321] + spectrum [322] + spectrum [323];
        Debug.Log(l7);
        GameObject [] cubes = GameObject.FindGameObjectsWithTag("CUBE");

    for( int i = 1; i < cubes.Length; i++ )
    {
        switch (i)
        {
            case 1:
                cubes[i].gameObject.transform.localScale = new Vector3(1, l1 * 100, 0.5f); // base drum
                break;
            case 2:
                cubes[i].gameObject.transform.localScale = new Vector3(1, l2 * 200, 0.5f); // base guitar
                break;
            case 3:
                cubes[i].gameObject.transform.localScale = new Vector3(1, l3 * 400, 0.5f);
                break;
            case 4:
                cubes[i].gameObject.transform.localScale = new Vector3(1, l4 * 800, 0.5f);
                break;
            case 5:
                cubes[i].gameObject.transform.localScale = new Vector3(1, l5 * 1600, 0.5f);
                break;
            case 6:
                cubes[i].gameObject.transform.localScale = new Vector3(1, l6 * 3200, 0.5f);
                break;
            case 7:
                cubes[i].gameObject.transform.localScale = new Vector3(1, l7 * 6400, 0.5f); //*tsk tsk tsk
                break;
        }           
    }
}

}

0

Source: https://habr.com/ru/post/1662157/


All Articles