- Home /
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;
}
}
Answer by WhipJr · Jun 19, 2018 at 07:51 PM
Found an asset on in the Unity Store that works perfectly, RhythmTool.
Your answer
Follow this Question
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