Commit 1c5dd9ff authored by Tommé Nöll's avatar Tommé Nöll
Browse files

added old background music system

parent 741bd0d4
using UnityEngine;
public class BackgroundMusic : MonoBehaviour {
AudioSource[] AS;
private int current = 0; //currently used audio source
//public AudioClip Ambience, Combat;
public MusicObject startMusic, startMusicLoop;
private MusicObject currentMusic, queuedMusic;
public int startTime = 0;
public int transitionLagOffset = 2000;
private bool inTransition = false;
private int preTransitionSample = 0;
private double lastUpdate = 0;
// Use this for initialization
void Start () {
startTime *= startMusic.clip.frequency;
AS = GetComponents<AudioSource>();
if (AS.Length != 2) Debug.LogError("Background Music object \"" + gameObject.name + "\" doesn't have exactly 2 Audio Sources");
Play(startMusic, 0, startTime);
TransitionTo(startMusicLoop);
}
void Update()
{
if (!inTransition) return;
if (AS[current].timeSamples != preTransitionSample)
{
inTransition = false;
Debug.Log("Music transition complete, playing " + AS[current].clip.name +
"\nSample new: " + AS[current].timeSamples + ", Sample old: " + AS[1-current].timeSamples +
"\nCurrent global time: " + AudioSettings.dspTime + ", Last update at " + lastUpdate);
}
/*else
{
lastUpdate = AudioSettings.dspTime;
}*/
}
private void Play(MusicObject music, int exitSample, int targetEntrySample)
{
double exitTime;
if (exitSample == 0) exitTime = 0;
else
{
exitSample -= AS[current].timeSamples;
//exitSample += transitionLagOffset;
exitTime = AudioSettings.dspTime + ((double)exitSample) / AS[current].clip.frequency;
}
current = 1-current;
AS[current].clip = music.clip;
AS[current].PlayScheduled(exitTime);
AS[current].timeSamples = targetEntrySample;
AS[current].loop = music.loop;
currentMusic = music;
AS[1-current].SetScheduledEndTime(exitTime);
preTransitionSample = AS[current].timeSamples;
Debug.Log("Queueing transition from " + (AS[1-current].clip ? AS[1-current].clip.name : "Nothing") +
" to " + AS[current].clip.name + " in " + (exitTime - AudioSettings.dspTime) +
"\nCurrent Global Time: " + AudioSettings.dspTime);
}
public void TransitionTo(MusicObject target)
{
if (inTransition)
{
Debug.LogError("Background music already in transition, can't transition to " + currentMusic.clip.name);
return;
}
if (target == currentMusic)
{
Debug.Log("Already playing " + currentMusic.clip.name);
return;
}
inTransition = true;
int iTarget;
for (iTarget = 0; iTarget < currentMusic.targets.Length; iTarget++) //search for TransitionTarget that matches target
{
if (currentMusic.targets[iTarget].musicObject == target)
{
break;
}
}
MusicTransitionObject[] transitions = new MusicTransitionObject[0];
if (iTarget == currentMusic.targets.Length)
{
Debug.Log("No transition found from " + currentMusic.clip.name + " to " + target.clip.name);
}
else
{
transitions = currentMusic.targets[iTarget].transitions;
}
double currentTime = AS[current].timeSamples / AS[current].clip.frequency;
int currentSample = AS[current].timeSamples;
int iTransitions;
for (iTransitions = 0; iTransitions < transitions.Length; iTransitions++) //search for first transition at least 1s in future; maybe replace with binary search
{
if (transitions[iTransitions].exitTime > currentTime + 1) break;
}
double dspTime = AudioSettings.dspTime;
double dspTransitionTime = dspTime - currentTime; //point at which the current song started
int exitSample = 0;
if (iTransitions == transitions.Length)
{
dspTransitionTime += AS[current].clip.length; //no transition found, schedule for after current song ended
exitSample += AS[current].clip.samples;
if (AS[current].loop && transitions.Length != 0) //we can transition in the next loop
{
iTransitions = 0;
}
}
int targetEntrySample = 0;
if (iTransitions != transitions.Length) //transition found
{
dspTransitionTime += transitions[iTransitions].exitTime;
exitSample += (int)(transitions[iTransitions].exitTime * AS[current].clip.frequency);
targetEntrySample = (int)(transitions[iTransitions].targetEntryTime * target.clip.frequency);
}
/*Debug.Log("Exit Sample: " + exitSample +
"\nEntry Sample: "+ targetEntrySample +
"\nGlobal Transition Time: " + dspTransitionTime);*/
Play(target, exitSample, targetEntrySample);
}
}
fileFormatVersion: 2
guid: 63226443539043ad88666eb1da52af02
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
[CreateAssetMenu(fileName = "MusicObject", menuName = "Music/MusicObject")]
public class MusicObject : ScriptableObject {
public MusicTransitionTargetObject[] targets;
public AudioClip clip;
public bool loop = false;
}
fileFormatVersion: 2
guid: 614e8a5273c3faf5ca1c757762551768
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
[CreateAssetMenu(fileName = "MusicTransitionObject", menuName = "Music/MusicTransitionObject")]
public class MusicTransitionObject : ScriptableObject {
public double exitTime, targetEntryTime;
}
fileFormatVersion: 2
guid: aa99b94ea8f43f2c998a46b067410b70
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
[CreateAssetMenu(fileName = "MusicTransitionTargetObject", menuName = "Music/MusicTransitionTargetObject")]
public class MusicTransitionTargetObject : ScriptableObject {
public MusicObject musicObject;
public MusicTransitionObject[] transitions;
}
fileFormatVersion: 2
guid: 3eb8edf3d8c19076596510947a8e59ec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment