Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Making a music-based game?

Discussion in 'General Discussion' started by MegamanEXE, Nov 22, 2014.

  1. MegamanEXE

    MegamanEXE

    Joined:
    Nov 22, 2014
    Posts:
    2
    Hi, new here. Probably the first time working with a friend on a project. This is gonna be my first Unity game, I believe in setting a good goal before learning as it makes it easier. I know you guys might think this is too complex for a first project but I want to do this because of reasons, and I'm a quick learner (especially in computer stuff).

    I want to do something like AudioSurf or Beat Hazard, but better (not that complex in terms of graphical stuff). I want spawning, velocity, acceleration etc. based on the music. What should I learn to make this possible.

    Simply put, to save you time trying to understand what I'm talking about, it's simply this: I'm looking forsomething of this sort, a function-type thing/script to which you give a 'range' of the waveform and it will simply return me the frequency of that chunk. And I want to use this data to spawn, change velocity etc. AFAIK, this would require Fast-Fourier transform (I don't know it, I just want to make sure before spending time learning it)?

    Next, I want a function-type thing which would give me the amplitude, probably from a PCM, right? I know it's asking a lot, but how can I make this possible? I'm willing to read up on things like FFT and stuff. I'm aware of something called AudioSource.GetSpectrumData in Unity. Can it do things like this? Please let me know, looking forward to your answers. :)

    Oh and I'll be working in 2D, not 3D if that helps. I'm aware that there are a very few articles written for something like this. But mostly they're not for Unity and they're very open. They're questions asking only "how do I build a music based game?". They do not specify to how much extent do they want to analyze the beats. I put only certain requirements (frequency and amplitude) here, so more people could help me. :)

    Thanks in advance!
     
  2. Blacklight

    Blacklight

    Joined:
    Dec 6, 2009
    Posts:
    1,241
    I found this example script when I was making my own music based game a while back. It might make a good starting point.
    Code (csharp):
    1. //the audio clip we want to play
    2. var audioTrack : AudioClip;
    3. //the listener (main camera for this example)
    4. var listener : AudioListener;
    5. //the number of samples we want to take
    6. var sampleRate : float = 64;
    7. //how often we want to take a sample
    8. var timeSpace : float= 0.05;
    9. //the game object we will be using to represent our samples
    10. var visualPrefab : GameObject;
    11. var cam : Transform;
    12.  
    13.  
    14. private var volData : float[];
    15. private var freqData : float[];
    16. private var numSamples : int;
    17. //we'll move along the z plane when placing our objects
    18. private var curZ : int = 0;
    19. //we'll clamp our positions between these values to avoid erratic placements
    20. private var maxX : float = 10.0;
    21. private var minX : float = -10.0;
    22.  
    23.  
    24. function Start() {
    25.  
    26.    //create audio player game object and position it at the same point as our audio listener
    27.    audioPlay = new GameObject("audioPlay");
    28.    audioPlay.AddComponent("AudioSource");
    29.    audioPlay.transform.position = listener.transform.position;
    30.    audioPlay.audio.clip = audioTrack;
    31.    audioPlay.audio.Play();
    32.  
    33.    //prep our number of samples, we clamp it between 64 and 8192 since this is the min and the max for the numSamples argument
    34.    numSamples = Mathf.Clamp(sampleRate * timeSpace, 64, 8192);
    35.  
    36.    //prep our float arrays
    37.    volData = new float[numSamples];
    38.    freqData = new float[numSamples];
    39.  
    40.  
    41.    InvokeRepeating("PlaceNewObject", 0, timeSpace);
    42.  
    43.  
    44. }
    45.  
    46.  
    47.  
    48. function PlaceNewObject() {
    49.    //update z position
    50.    curZ += 2;
    51.  
    52.    //get the output data from the listener
    53.    listener.GetOutputData(volData, 0);
    54.  
    55.    //get the root mean square of the output data (this is the square root of the average of the samples)
    56.    curVol = RMS(volData);
    57.  
    58.    //amplify the volume, and maintain our range of minX and maxX
    59.    xPos = Mathf.Clamp(curVol * 100, minX, maxX);
    60.  
    61.    //only place a new object if we aren't at the extremes of our clamp values
    62.    if (xPos != 10 && xPos != -10) {
    63.      //get the spectrum data from the listener (we use the blackman harris window for maximum contrast)
    64.      listener.GetSpectrumData(freqData, 1, FFTWindow.BlackmanHarris);
    65.  
    66.      //get the root mean square of the spectrum data
    67.      curFreq = RMS(freqData);
    68.  
    69.      //amplify the frequency for more visual impact
    70.      yPos = Mathf.Clamp(curFreq * 200, 0, maxX);
    71.    
    72.      Debug.Log(xPos);
    73.    
    74.      cam.transform.position.z = curZ;
    75.    
    76.      //instantiate our new visual object, adjusting x, y and z position as we go
    77.      newVisual = Instantiate(visualPrefab, Vector3(xPos, yPos, curZ), transform.rotation);
    78.    
    79.    }
    80.  
    81.  
    82. }
    83.  
    84.  
    85.  
    86. function RMS(samples: float[]) {
    87.    var result = 0.0;
    88.    //add sample values together
    89.    for (i = 0; i < samples.Length; i++) {
    90.      result += samples[i] * samples[i];
    91.    }
    92.    //get the average of the sample values
    93.    result /= samples.Length;
    94.    //return the square root of the average
    95.    return result;
    96. }
    97.  
    Throw this into a scene with a song and it'll create a long line of objects whose positions will vary based on the data returned from the audio.

    I'm just going to say now that I actually don't really know all that much about this stuff, so if you need more detail then take a gander through the Audio Source section of the documentation, or go digging around through the forums a bit more. There should be plenty of threads with useful information.
     
  3. sebastian.g

    sebastian.g

    Joined:
    Nov 20, 2014
    Posts:
    8
    wow that's really interesting, I also want to make a music-based game,something in the style of 140,if you had ever played it. If you find any useful documentation for this purpose,would you mind sharing it with us?
     
  4. MegamanEXE

    MegamanEXE

    Joined:
    Nov 22, 2014
    Posts:
    2
    Still looking for more insight. :) Thanks!
    I came across something called Visualizer Studio, it seems to do what I'm trying to accomplish, I think. Although I have no need of too complex scripts, my requirements are still the same and simple as stated above :)
    Thanks!
     
  5. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Visualizer Studio is a cool plugin. The developer uses our Master Audio plugin for his own games, but definitely has his own nice niche with that plugin. I'd check it out, he's good people! And no, Master Audio doesn't do anything like you're describing.

    Oh and by the way, there's now an actual audio forum here (as of a few days ago). I think you should post there.

    http://forum.unity3d.com/forums/audio.74/
     
    Last edited: Nov 30, 2014