Hello. I have some questions about audio in unity.
1: I want to make a game like Amplitude, Frequency, Rock Band etc, that allows player’s to use their own music (automatically generating the gems wont be as accurate, but its close enough). I have a script that uses audio.GetOutputData to see whenever their are sudden increases in volume (I am trying to detect beats, like a drum). Is there anyway to analyze the song before the scene with the gameplay actually starts? Something like having the game run through the song, grab the audio data, generate the level and then, when the level starts, the audio is just playing, not being analyzed?
2: Is there any way to allow the player to use their own music for an audio source(I know there is a file browsing system on the Unity wiki, could that help)?
Thanks.
(Here is the script I talked about in my first question)
var vol: float;
var sampleRate: float;
var timeWindow: float;
private var numSamples: int;
private var window: float[];
function Start() {
numSamples = sampleRate * timeWindow;
window = new float[numSamples];
InvokeRepeating("UpdateVolume", 0, timeWindow);
}
function UpdateVolume() {
window = audio.GetOutputData(numSamples, 0);
vol = RMS(window);
(ExceedsVolumeThreshold(window));
}
function RMS(data: float[]) {
var result = 0.0;
for (i = 0; i < data.Length; i++) {
result += data[i] * data[i];
}
result /= data.Length;
return Mathf.Sqrt(result);
}
//the beat detector part
function ExceedsVolumeThreshold(data: float[]) {
var threshold = 0.7;
for (i = 0; i < data.Length; i++) {
if (Mathf.Abs(data[i]) > threshold) {
//Something
}
}
return false;
}
I don’t know the answers, but I know you’ve asked these questions a few times, so I’ll offer a couple of thoughts.
I’m not aware of anything in the Unity API that would allow you to analyze the audio data associated with an audio clip without actually playing it. That said, I don’t know the entire Unity API by heart, so there could easily be some functionality available that I’m not aware of.
First of all, from everything I’ve read and from my own experience, it seems to me that creating music-based games in Unity can be a bit iffy. Maybe it can be done though, if the circumstances are right.
Although there might be some way to analyze the data in advance from within Unity, a better option might be to do it as a preprocessing step. For example, you could create a scene where the audio file is played back, the audio is analyzed, and the data of interest is written to a file of some sort. The file could then be made available in one form or another for in-game use.
Also, I’d imagine that with the Pro version you could write a plug-in for this (although that won’t help if you’re using the free version).
As for the second question, I don’t have an answer for that off the top of my head. It seems that would require converting an audio file to an audio clip object at run time, and without doing some research I couldn’t really say if/how that could be done.
I realize I didn’t really answer your questions, but maybe an incomplete answer will be better than no answer at all
I looked in the script reference and I couldn’t find anything on speeding up a song, is there a way to do that (having a song be fully played back and just waiting would be boring, although I could just have a notice like “the first time a song is played may be rough due to analyzing”)
To be clear, what I meant by ‘preprocessing’ is that you’d perform the preprocessing step as part of development, and then ship the processed data with the game. (In other words, the end user wouldn’t have to wait at any point for the data to be processed.)
Oh. I thought the hook of the game would be allowing player’s to use their own music (like audiosurf, rhythm zone, etc.), but that might not be (It probably isn’t) possible.