Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Audio How to - get / analyse height / pitch of sound via microphone in device [smartphone] ?

Discussion in 'Audio & Video' started by Avenged90x, Apr 23, 2018.

  1. Avenged90x

    Avenged90x

    Joined:
    Sep 6, 2017
    Posts:
    35
    Hi!

    I need to make something like guitar tuner app - which can analyse height of sound that anyone can play on instrument or sing. Anybody knows how to do that? I want to get some frequences - to convert them into specific values 1,2,3,4,5,6 which will be just names of sounds like E,A,D,G,H,E, etc :)

    Anybody knows how to make that in code (C#) and how to control sound via microphone input in unity to get the height of these sounds ?
     
  2. Marald

    Marald

    Joined:
    Jan 16, 2015
    Posts:
    42
  3. Avenged90x

    Avenged90x

    Joined:
    Sep 6, 2017
    Posts:
    35
    Thanks :D This is it! Now.. time to understand that code about pitch detection... :) I can't find the code lines about converting specific values of frequencies to note names: ""C", "C#", "D", "D#", "E", "F"... etc. There is array of the note names... but it's just my beginner level of programming, so that's why i don't understand that well.

    Thanks again! :)
     
  4. Marald

    Marald

    Joined:
    Jan 16, 2015
    Posts:
    42
    I didn't use the demo or look at the code, but I guess it gives you Hz. So you could compare that to an array with a possible offset (lets say +/- 20 Hz). Than use this information : https://pages.mtu.edu/~suits/notefreqs.html
    to see what each note is in Hz.

    good luck!
     
  5. Ebonicus

    Ebonicus

    Joined:
    Oct 31, 2016
    Posts:
    156

    Anyone have have an updated link to any pitch detection repos?
     
  6. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    290
    You might be able to use
    AudioSource.GetSpectrumData
    to identify the main frequency and run that against something like this to find the closest value. (that's from some personal C++ code)

    Code (CSharp):
    1. static constexpr U8 octaveLimit = 11;
    2.  
    3. static constexpr float C_Hz[octaveLimit] = { 16.3515978312874f, 32.70319566f, 65.40639133f, 130.8127827f, 261.6255653f, 523.2511306f, 1046.502261f, 2093.004522f, 4186.009045f, 8372.01809f, 16744.03618f };
    4. static constexpr float Cs_Hz[octaveLimit] = { 17.3239144360545f, 34.64782887f, 69.29565774f, 138.5913155f, 277.182631f, 554.365262f, 1108.730524f, 2217.461048f, 4434.922096f, 8869.844191f, 17739.68838f };
    5. static constexpr float Df_Hz[octaveLimit] = { 17.3239144360545f, 34.64782887f, 69.29565774f, 138.5913155f, 277.182631f, 554.365262f, 1108.730524f, 2217.461048f, 4434.922096f, 8869.844191f, 17739.68838f };
    6. static constexpr float D_Hz[octaveLimit] = { 18.354047994838f, 36.70809599f, 73.41619198f, 146.832384f, 293.6647679f, 587.3295358f, 1174.659072f, 2349.318143f, 4698.636287f, 9397.272573f, 18794.54515f };
    7. static constexpr float Ds_Hz[octaveLimit] = { 19.4454364826301f, 38.89087297f, 77.78174593f, 155.5634919f, 311.1269837f, 622.2539674f, 1244.507935f, 2489.01587f, 4978.03174f, 9956.063479f, 19912.12696f };
    8. static constexpr float Ef_Hz[octaveLimit] = { 19.4454364826301f, 38.89087297f, 77.78174593f, 155.5634919f, 311.1269837f, 622.2539674f, 1244.507935f, 2489.01587f, 4978.03174f, 9956.063479f, 19912.12696f };
    9. static constexpr float E_Hz[octaveLimit] = { 20.6017223070544f, 41.20344461f, 82.40688923f, 164.8137785f, 329.6275569f, 659.2551138f, 1318.510228f, 2637.020455f, 5274.040911f, 10548.08182f, 21096.16364f };
    10. static constexpr float F_Hz[octaveLimit] = { 21.8267644645627f, 43.65352893f, 87.30705786f, 174.6141157f, 349.2282314f, 698.4564629f, 1396.912926f, 2793.825851f, 5587.651703f, 11175.30341f, 22350.60681f };
    11. static constexpr float Fs_Hz[octaveLimit] = { 23.1246514194772f, 46.24930284f, 92.49860568f, 184.9972114f, 369.9944227f, 739.9888454f, 1479.977691f, 2959.955382f, 5919.910763f, 11839.82153f, 23679.64305f };
    12. static constexpr float Gf_Hz[octaveLimit] = { 23.1246514194772f, 46.24930284f, 92.49860568f, 184.9972114f, 369.9944227f, 739.9888454f, 1479.977691f, 2959.955382f, 5919.910763f, 11839.82153f, 23679.64305f };
    13. static constexpr float G_Hz[octaveLimit] = { 24.4997147488593f, 48.9994295f, 97.998859f, 195.997718f, 391.995436f, 783.990872f, 1567.981744f, 3135.963488f, 6271.926976f, 12543.85395f, 25087.7079f };
    14. static constexpr float Gs_Hz[octaveLimit] = { 25.9565436f, 51.9130872f, 103.8261744f, 207.6523488f, 415.3046976f, 830.6093952f, 1661.21879f, 3322.437581f, 6644.875161f, 13289.75032f, 26579.50065f };
    15. static constexpr float Af_Hz[octaveLimit] = { 25.9565436f, 51.9130872f, 103.8261744f, 207.6523488f, 415.3046976f, 830.6093952f, 1661.21879f, 3322.437581f, 6644.875161f, 13289.75032f, 26579.50065f };
    16. static constexpr float A_Hz[octaveLimit] = { 27.5f, 55.0f, 110.0f, 220.0f, 440.0f, 880.0f, 1760.0f, 3520.0f, 7040.0f, 14080.0f, 28160.0f };
    17. static constexpr float As_Hz[octaveLimit] = { 29.13523509f, 58.27047019f, 116.5409404f, 233.0818808f, 466.1637615f, 932.327523f, 1864.655046f, 3729.310092f, 7458.620184f, 14917.24037f, 29834.48074f };
    18. static constexpr float Bf_Hz[octaveLimit] = { 29.13523509f, 58.27047019f, 116.5409404f, 233.0818808f, 466.1637615f, 932.327523f, 1864.655046f, 3729.310092f, 7458.620184f, 14917.24037f, 29834.48074f };
    19. static constexpr float B_Hz[octaveLimit] = { 30.86770633f, 61.73541266f, 123.4708253f, 246.9416506f, 493.8833013f, 987.7666025f, 1975.533205f, 3951.06641f, 7902.13282f, 15804.26564f, 31608.53128f };