编程语言
首页 > 编程语言> > c# – 音乐的音高检测

c# – 音乐的音高检测

作者:互联网

我正在制作一个基于节奏的音乐游戏,你应该(理论上)能够选择你想要的任何歌曲.这是使用C#在Unity(5.4.1)中创建的.

实时地检测并通过可视化器向用户显示的歌曲的音高.

我遇到的问题是我创建了一个可视化工具,但这几乎没有显示更高音调的音符.我担心这是因为(和大多数音乐一样)一次播放一些音高(为了测试我在等待声音人给我音乐时使用Darude Sandstorm).最后,我希望这种音高检测能够告诉用户是高音,中音还是低音.

我试过用了
    GetComponent&LT FFT&GT()PitchValue.

   AudioListener.GetSpectrumData(2048,0,FFTWindow.Hamming);

我想恐惧的是,它不能在一次播放多个音高的歌曲上完成(即基础覆盖高音高).

我很乐意听到任何建议或知道是否有人克服了类似的问题.

这是我现在正在使用的一个例子,我已经尝试了许多不同的在线解决方案(其中一些信息实际上来自于评论代码中链接的教程),如果我继续,我打算完全参考这条道路(我已经在这段时间工作了几个星期).它是一个非常可视化的,但我似乎无法充分利用它.这可以作为没有if else语句的可视化工具,这正是我正在进行一些试验和错误的地方.我跟随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's 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;
            }
        }
    }
} 

Attached image with some of the pitch output values printed

感谢您的评论到目前为止,抱歉我没有在开始时附上这个

解决方法:

如果有人在那里工作类似的东西,我设法分开球场,因为他们使用阵列来访问FFT频谱数据.

欢迎所有反馈.

我通过学习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;
        }           
    }
}

}

标签:c,unity3d,unity5
来源: https://codeday.me/bug/20190710/1429167.html