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

need help appearing object from sound source

Discussion in 'Scripting' started by djlevana18, Feb 10, 2021.

  1. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    Hello everyone i make rythm game and i want to know is there any way i can appear object in unity using audio source let's say object will appear if there is bass or snare or any other sound
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
  3. reachdabeach

    reachdabeach

    Joined:
    Jan 28, 2021
    Posts:
    70
    Joe-Censored likes this.
  4. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    thank you for responding also i have a question so i did not read it yet but with this can i spawn object or something with this like i want for a cube to appear on bass let's say it's possible right?
     
  5. reachdabeach

    reachdabeach

    Joined:
    Jan 28, 2021
    Posts:
    70
    All things are possible... :)

    I am not as well versed in the visible game stuff, but I think I would have a cube that wasn't active to copy (Instantiate) and then position and make active. But that might be the beginning of a learning experience...
     
  6. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    well thank you a lot i will replay tomorrow if it's working thank you so much
     
  7. florentRaffray

    florentRaffray

    Joined:
    Feb 10, 2021
    Posts:
    7
    Triggering events based on volume or intensity of a given frequency isn't too hard. However having a specific instrument or beat trigger things is a good bit trickier. This is because we differentiate instruments based on how we hear an instrument's timbre (little notes/frequencies that accompany a dominant frequency), and dynamics (how volume is accelerating and decelerating in intensity). GetSpectrumData just returns the raw frequency data for a given update without differentiating which frequencies are musically intended notes that comprise the melody or beat of a song and which are just part of the timbre of an instrument. It's easy for our ears to differentiate, but to do so with raw data and scripts is tricky for sure and there's not really a straightforward way of splitting out instruments from raw data like that.
    Like the other user said, everything is possible, but I would also let you know that diving into instrument detection is not simple either. It's a deep dive.
    A snare is a good example of why detecting instruments is tricky. Detecting a snare in the middle of a complex song full of other instrumental parts would be complicated. While a snare is loud it's actually lots of frequencies played quietly at the same time (that's why it's perceived as much more percussive than it is melodic). It's almost all timbre and no note, so it would be tricky to detect a snare with GetSpectrumData.
    A general beat might be easier in that you could just look for a large change in overall decibel value which is what the link in the other post explores. That's probably your answer for getting things to spawn on beat. It just might take some work for it to be on beats of a particular instrument, but easier on any generally loud beat.

    If you're interested in getting the most you can out of GetSpectrumData you can check out this asset I made which gathers the return data and converts it to decibels in a more usable range as well as collects and returns only the frequency bins that match musical notes on a piano (it returns 128 musical frequencies instead of 2048 (or whatever power of 2) arbitrary ones. It also calculates and returns the overall decibel value of your audio source on every update.
    It's called GetSpectrumDataHelper if you want to check it out: https://assetstore.unity.com/packages/3d/getspectrumdatahelper-188353