Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WhipJr · Jun 19, 2018 at 03:46 AM · audioaudiosourceparsesample

How can I pre-load samples from an audio file to spawn objects?

I am currently making a Guitar Hero-esque game, I want to generate the object patterns before the player hears the song so the objects can be hit in time with the song.

Currently, I have got part of this working. I have not been able to get the objects to cross the designated point in rhythm. This is because the audio is being read and the objects are spawning as it is heard by the player through the Audio Listener, and not ahead of time.

I have found this post that explains something like what I want to do, however, the answer doesn't tell how to implement the code and that is where I am having issues. The lines of code from the other post have comments

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AudioParse : MonoBehaviour {
 
     public GameObject note;
     public const int  SAMPLE_SIZE = 1024;
     public int spawnedNotesLow = 0;
     public int spawnedNotesMid = 0;
     public int spawnedNotesHigh = 0;
     public float spawnTimer = 1;
     public float spawnRate = 1;
     public float spawnedNotes = 0;
     public bool spawned = false;
     private float rmsValue;
     public float highestRMS = 0;
     private float dbValue;
     public float lastDB = 0;
     public float highestDB = 0;
     private float pitchValue;
     public float highestPitch = 0;
     public float maxVisualScale = 25.0f;
     public float visualModifier = 50.0f;
     public float smoothSpeed = 10.0f;
     private AudioSource source;
     public float[] samples;
     private float[] spectrum;
     private float sampleRate;
     private Transform[] visualList;
     private float[] visualScale;
     private int amnVisual = 32;

     void Start () {
         source = GetComponent<AudioSource>();
         samples = new float[SAMPLE_SIZE];
         spectrum = new float[SAMPLE_SIZE];
         sampleRate = AudioSettings.outputSampleRate;
         source.clip.GetData(samples, 0);
     }

     void Update () {
         AnalyzeSound();
         SpawnNotes();
     }
     
     public void SpawnNotes()
     {
         int rand = Random.Range(-9, 9);
         int randTwo = Random.Range(0, 6);
         int rand = Random.Range(-15, 15);
     int randTwo = Random.Range(0, 8);
     if (spawnTimer <= 0 && pitchValue >10)
     {
         if (pitchValue < 50f && pitchValue > 30)
         {
             GameObject go = Instantiate(note);
             go.transform.position += Vector3.right * rand;
             go.transform.position += Vector3.up * randTwo;
             go.transform.position += -Vector3.forward * Mathf.Abs(rand);
             go.transform.LookAt(GameObject.FindGameObjectWithTag("Player").transform.position);
             //go.transform.position += Vector3.up * -3;
             //spawnedNotes += 2;
             spawnedNotesLow += 1;
             spawnTimer = spawnRate;
         }

         if (pitchValue > 1000)
         {
             GameObject go = Instantiate(note);
             go.transform.position += Vector3.right * rand;
             go.transform.position += Vector3.up * randTwo;
             go.transform.position += -Vector3.forward * Mathf.Abs(rand);
             go.transform.LookAt(GameObject.FindGameObjectWithTag("Player").transform.position);
             //go.transform.position += Vector3.up * -3;
             //spawnedNotes += 2;
             spawnedNotesLow += 1;
             spawnTimer = spawnRate;
         }
     }
     else
     {
         spawnTimer -= Time.deltaTime;
     }
     }
 
     public void AnalyzeSound()
     {
         ///////////////////////////////////
         //source.GetOutputData(samples, 0); works while able to hear the sound but produces objects out of sync with the desired input.
         ///////////////////////////////////
         source.clip.GetData(samples, 0); // This is from the post I linked to. trying to use this code to replace mine above ^ 
         ///////////////////////////////////
         // Get the RMS
         int i = 0;
         float sum = 0;
         for (; i < SAMPLE_SIZE; i++)
         {
             sum += samples[i] * samples[i];
         }
         rmsValue = Mathf.Sqrt(sum / SAMPLE_SIZE);
         if (rmsValue > highestRMS)
             highestRMS = rmsValue;
         // Get DB Value
         dbValue = 20 * Mathf.Log10(rmsValue / .1f);
         if (dbValue > highestDB)
             highestDB = dbValue;
         // Get Spectrum
         ///////////////////////////////////
         //source.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris); works while able to hear the sound but produces objects out of sync with the desired input.
         ///////////////////////////////////
         spectrum = Util.GetSpectrum(samples); // This is from the post I linked to. trying to use this code to replace mine above ^ 
         ///////////////////////////////////
         // get Pitch
         float maxV = 0;
         var maxN = 0;
         for (i = 0; i < SAMPLE_SIZE; i++)
         {
             if (!(spectrum[i] > maxV) || !(spectrum[i] > 0.0f))
                 continue;
             maxV = spectrum[i];
             maxN = i;
         }
         float freqN = maxN;
         if (maxN > 0 && maxN < SAMPLE_SIZE - 1)
         {
             var dL = spectrum[maxN - 1] / spectrum[maxN];
             var dR = spectrum[maxN + 1] / spectrum[maxN];
             freqN += .05f * (dR * dR - dL * dL);
         }
         pitchValue = freqN * (sampleRate / 2) / SAMPLE_SIZE;
         if (pitchValue > highestPitch)
             highestPitch = pitchValue;
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by WhipJr · Jun 19, 2018 at 07:51 PM

Found an asset on in the Unity Store that works perfectly, RhythmTool.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

162 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Music Visualiser Troubles 0 Answers

How to AudioSource.GetSpectrumData while muted? 1 Answer

Play audio clip without a variable 1 Answer

Is there a way to create a random Audiosource loop? 2 Answers

Audio not playing... 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges