IEnumerator PlayLayers(string audioTag) { yield return(new WaitForSeconds(layerDelaySeconds)); // don't fire one right away while (lastTag == audioTag && playState == AUDIO_PLAY_STATE.Playing && fadeState == AUDIO_FADE_STATE.NoFade) { float secs = layerDelaySeconds; if (playLayers) { int n = UnityEngine.Random.Range(0, 100); if (n <= (layerProbability * 100)) { ManagedAudioClip mc = GetLayerForTag(audioTag); layerPlayer.clip = mc.audioClip; layerPlayer.loop = false; layerPlayer.volume = layerVolume; layerPlayer.Play(); secs = layerDelaySeconds + layerPlayer.clip.length; } } yield return(new WaitForSeconds(secs)); } }
public void AddAudioLayer(AudioClip clip, string audioTag, int priority) { int tagIndex = IndexForTag(audioTag); // adds tag if it does not already exist ManagedAudioClip mc = new ManagedAudioClip(clip, audioTag, priority); mc.compressed = false; if (layeredClips == null) { // no layers exist at all layeredClips = new ManagedAudioClip[tagIndex + 1][]; for (int i = 0; i <= tagIndex; i++) { layeredClips[i] = new ManagedAudioClip[1]; } } else if (layeredClips.Length <= tagIndex) { // no array of layers exists yet for this tag ManagedAudioClip[][] tempAllClips = layeredClips; layeredClips = new ManagedAudioClip[layeredClips.Length + 1][]; tempAllClips.CopyTo(layeredClips, 0); layeredClips[tagIndex] = new ManagedAudioClip[1]; } else { // array of layers already exists for tag, we're adding another layered effect ManagedAudioClip[] tempClips = layeredClips[tagIndex]; layeredClips[tagIndex] = new ManagedAudioClip[tempClips.Length + 1]; tempClips.CopyTo(layeredClips[tagIndex], 0); } int n = layeredClips[tagIndex].Length - 1; layeredClips[tagIndex][n] = mc; }
public void AddOutro(AudioClip clip, string audioTag, int priority) { int tagIndex = IndexForTag(audioTag); // adds tag if it does not already exist ManagedAudioClip mc = new ManagedAudioClip(clip, audioTag, priority); if (outroClips == null) { // no outros exist at all outroClips = new ManagedAudioClip[tagIndex + 1][]; for (int i = 0; i <= tagIndex; i++) { outroClips[i] = new ManagedAudioClip[1]; } } else if (outroClips.Length <= tagIndex) { // no array of outros exists yet for this tag ManagedAudioClip[][] tempAllClips = outroClips; outroClips = new ManagedAudioClip[outroClips.Length + 1][]; tempAllClips.CopyTo(outroClips, 0); outroClips[tagIndex] = new ManagedAudioClip[1]; } else { // array of outros already exists for tag, we're adding another outro ManagedAudioClip[] tempClips = outroClips[tagIndex]; outroClips[tagIndex] = new ManagedAudioClip[tempClips.Length + 1]; tempClips.CopyTo(outroClips[tagIndex], 0); } int n = outroClips[tagIndex].Length - 1; outroClips[tagIndex][n] = mc; }
public void AddAudioClip(AudioClip clip, string audioTag, int priority) { int tagIndex = IndexForTag(audioTag); // adds tag if it does not already exist ManagedAudioClip mc = new ManagedAudioClip(clip, tag, priority); if (audioClips == null) { // no clips exist at all audioClips = new ManagedAudioClip[1][]; audioClips[tagIndex] = new ManagedAudioClip[1]; } else if (audioClips.Length <= tagIndex) { // no array of clips exists yet for this tag ManagedAudioClip[][] tempAllClips = audioClips; audioClips = new ManagedAudioClip[audioClips.Length + 1][]; tempAllClips.CopyTo(audioClips, 0); audioClips[tagIndex] = new ManagedAudioClip[1]; } else { // array of clips already exists for tag, we're adding another clip ManagedAudioClip[] tempClips = audioClips[tagIndex]; audioClips[tagIndex] = new ManagedAudioClip[tempClips.Length + 1]; tempClips.CopyTo(audioClips[tagIndex], 0); } int n = audioClips[tagIndex].Length - 1; audioClips[tagIndex][n] = mc; }
private ManagedAudioClip GetLayerForTag(string audioTag) { // layers are always random // juxtapose lastRandom and lastRandomLayer since we're selecting a layer clip here int tempRand = lastRandom; lastRandom = lastRandomLayer; ManagedAudioClip layerClip = GetClipFromArray(audioTag, layeredClips, -1, true); lastRandomLayer = lastRandom; // set by above method lastRandom = tempRand; // restore random to avoid it being set by layer selection return(layerClip); }
private ManagedAudioClip GetClipFromArray(string audioTag, ManagedAudioClip[][] clipArray, int clipIndex, bool forceRandom) { ManagedAudioClip mc = null; int tagIndex = IndexForTag(audioTag, false); if (tagIndex != -1 && clipArray.Length > tagIndex) { ManagedAudioClip[] clipsForTag = clipArray[tagIndex]; if (clipsForTag.Length > 0) { if (clipIndex == -1) { if (playRandom || forceRandom) { // choose randomly from list int n = UnityEngine.Random.Range(0, clipsForTag.Length); if (lastRandom == n && lastTag == audioTag) { // ensure selection changes n = (n > 0) ? n - 1 : clipsForTag.Length - 1; } lastRandom = n; mc = (ManagedAudioClip)clipsForTag[n]; } else { // choose next clip in the sequence // if looping, revert to beginning upon reaching end ++clipSequenceIndex; if (playLoop && (clipsForTag.Length <= clipSequenceIndex)) { clipSequenceIndex = 0; } mc = (ManagedAudioClip)clipsForTag[clipSequenceIndex]; } } else { // retrieve a specific clip at the specified index if (clipsForTag.Length > clipIndex) { mc = (ManagedAudioClip)clipsForTag[clipIndex]; } } } } return(mc); }
private ManagedAudioClip GetOutroForTag(string audioTag) { // intros and outros are always randomized -- if you desire a specific outro, // then either add only one outro clip to the manager, or add the outro clip // as an audio clip instead of as an outro and play it once, or bake it into the // main audio clip file // juxtapose lastRandom and lastRandomIntro since we're selecting an intro clip int tempRand = lastRandom; lastRandom = lastRandomOutro; ManagedAudioClip mc = GetClipFromArray(audioTag, outroClips, -1, true); lastRandomOutro = lastRandom; // set by above method lastRandom = tempRand; // restore random to avoid it being set by layer selection return(mc); }
IEnumerator PlayOutro(string audioTag) { ManagedAudioClip mc = GetOutroForTag(audioTag); playState = AUDIO_PLAY_STATE.PlayingOutro; musicPlayer.clip = mc.audioClip; bool originalLoop = playLoop; musicPlayer.loop = false; musicPlayer.Play(); yield return(new WaitForSeconds(musicPlayer.clip.length)); if (playState == AUDIO_PLAY_STATE.PlayingOutro) { musicPlayer.Stop(); playState = AUDIO_PLAY_STATE.Stopped; } musicPlayer.loop = originalLoop; }
IEnumerator PlayIntro(string audioTag) { ManagedAudioClip mc = GetIntroForTag(audioTag); musicPlayer.clip = mc.audioClip; bool originalLoop = playLoop; musicPlayer.loop = false; musicPlayer.Play(); playState = AUDIO_PLAY_STATE.PlayingIntro; yield return(new WaitForSeconds(musicPlayer.clip.length)); if (playState == AUDIO_PLAY_STATE.PlayingIntro) { // play original target file ManagedAudioClip mclip = GetClipForTag(audioTag, -1); musicPlayer.clip = mclip.audioClip; musicPlayer.Play(); playState = AUDIO_PLAY_STATE.Playing; } musicPlayer.loop = originalLoop; }
private int NumClipsForTag(string audioTag, ManagedAudioClip[][] clipArray) { int n = 0; try { int tagIndex = IndexForTag(audioTag, false); if (tagIndex != -1 && clipArray != null && clipArray.Length > tagIndex && ((ManagedAudioClip) clipArray[tagIndex][0]).audioClip != null) { n = clipArray[tagIndex].Length; } } catch (Exception) {} return n; }
private ManagedAudioClip GetClipFromArray(string audioTag, ManagedAudioClip[][] clipArray, int clipIndex, bool forceRandom) { ManagedAudioClip mc = null; int tagIndex = IndexForTag(audioTag, false); if (tagIndex != -1 && clipArray.Length > tagIndex) { ManagedAudioClip[] clipsForTag = clipArray[tagIndex]; if (clipsForTag.Length > 0) { if (clipIndex == -1) { if (playRandom || forceRandom) { // choose randomly from list int n = UnityEngine.Random.Range(0, clipsForTag.Length); if (lastRandom == n && lastTag == audioTag) { // ensure selection changes n = (n > 0) ? n - 1 : clipsForTag.Length - 1; } lastRandom = n; mc = (ManagedAudioClip) clipsForTag[n]; } else { // choose next clip in the sequence // if looping, revert to beginning upon reaching end ++clipSequenceIndex; if (playLoop && (clipsForTag.Length <= clipSequenceIndex)) { clipSequenceIndex = 0; } mc = (ManagedAudioClip) clipsForTag[clipSequenceIndex]; } } else { // retrieve a specific clip at the specified index if (clipsForTag.Length > clipIndex) { mc = (ManagedAudioClip) clipsForTag[clipIndex]; } } } } return mc; }
public void AddOutro(AudioClip clip, string audioTag, int priority) { int tagIndex = IndexForTag(audioTag); // adds tag if it does not already exist ManagedAudioClip mc = new ManagedAudioClip(clip, audioTag, priority); if (outroClips == null) { // no outros exist at all outroClips = new ManagedAudioClip[tagIndex + 1][]; for (int i=0; i<=tagIndex; i++) { outroClips[i] = new ManagedAudioClip[1]; } } else if (outroClips.Length <= tagIndex) { // no array of outros exists yet for this tag ManagedAudioClip[][] tempAllClips = outroClips; outroClips = new ManagedAudioClip[outroClips.Length + 1][]; tempAllClips.CopyTo(outroClips, 0); outroClips[tagIndex] = new ManagedAudioClip[1]; } else { // array of outros already exists for tag, we're adding another outro ManagedAudioClip[] tempClips = outroClips[tagIndex]; outroClips[tagIndex] = new ManagedAudioClip[tempClips.Length + 1]; tempClips.CopyTo(outroClips[tagIndex], 0); } int n = outroClips[tagIndex].Length-1; outroClips[tagIndex][n] = mc; }
public void AddAudioLayer(AudioClip clip, string audioTag, int priority) { int tagIndex = IndexForTag(audioTag); // adds tag if it does not already exist ManagedAudioClip mc = new ManagedAudioClip(clip, audioTag, priority); mc.compressed = false; if (layeredClips == null) { // no layers exist at all layeredClips = new ManagedAudioClip[tagIndex + 1][]; for (int i=0; i<=tagIndex; i++) { layeredClips[i] = new ManagedAudioClip[1]; } } else if (layeredClips.Length <= tagIndex) { // no array of layers exists yet for this tag ManagedAudioClip[][] tempAllClips = layeredClips; layeredClips = new ManagedAudioClip[layeredClips.Length + 1][]; tempAllClips.CopyTo(layeredClips, 0); layeredClips[tagIndex] = new ManagedAudioClip[1]; } else { // array of layers already exists for tag, we're adding another layered effect ManagedAudioClip[] tempClips = layeredClips[tagIndex]; layeredClips[tagIndex] = new ManagedAudioClip[tempClips.Length + 1]; tempClips.CopyTo(layeredClips[tagIndex], 0); } int n = layeredClips[tagIndex].Length-1; layeredClips[tagIndex][n] = mc; }
public void AddAudioClip(AudioClip clip, string audioTag, int priority) { int tagIndex = IndexForTag(audioTag); // adds tag if it does not already exist ManagedAudioClip mc = new ManagedAudioClip(clip, tag, priority); if (audioClips == null) { // no clips exist at all audioClips = new ManagedAudioClip[1][]; audioClips[tagIndex] = new ManagedAudioClip[1]; } else if (audioClips.Length <= tagIndex) { // no array of clips exists yet for this tag ManagedAudioClip[][] tempAllClips = audioClips; audioClips = new ManagedAudioClip[audioClips.Length + 1][]; tempAllClips.CopyTo(audioClips, 0); audioClips[tagIndex] = new ManagedAudioClip[1]; } else { // array of clips already exists for tag, we're adding another clip ManagedAudioClip[] tempClips = audioClips[tagIndex]; audioClips[tagIndex] = new ManagedAudioClip[tempClips.Length + 1]; tempClips.CopyTo(audioClips[tagIndex], 0); } int n = audioClips[tagIndex].Length-1; audioClips[tagIndex][n] = mc; }
public AudioClip Play(string audioTag, int clipIndex) { StopCoroutine("MonitorSequence"); musicPlayer.Stop(); if (fadeState == AUDIO_FADE_STATE.FadingIn || fadeState == AUDIO_FADE_STATE.FadingOut) { fadeState = AUDIO_FADE_STATE.NoFade; masterVolume = targetVolume; } musicPlayer.volume = masterVolume; // if looping in sequence, it is at sequence level, not clip level musicPlayer.loop = (playSequence) ? false : playLoop; if (NumClipsForTag(audioTag, audioClips) == 0) { playState = AUDIO_PLAY_STATE.Stopped; return(null); } if (!playSequence && !playRandom && !playLoop && clipIndex == -1) { // play first clip once and then let it stop clipIndex = 0; } if (!playSequence || audioTag != lastTag) { // reset sequenceIndex clipSequenceIndex = -1; } ManagedAudioClip mclip = GetClipForTag(audioTag, clipIndex); musicPlayer.clip = mclip.audioClip; if (fadeInAudio) { // NOTE: Crossfading works only when playing uncompressed intros & outros, so it's unsupported for now fadeState = AUDIO_FADE_STATE.FadingIn; StartCoroutine(FadeIn()); } else { targetVolume = masterVolume; } musicPlayer.Play(); playState = AUDIO_PLAY_STATE.Playing; // if there are any random layers to play with this tag, start the coroutine to handle it if (NumClipsForTag(audioTag, layeredClips) > 1) { StartCoroutine(PlayLayers(audioTag)); } if (playSequence) { StartCoroutine(MonitorSequence(audioTag)); } lastTag = audioTag; return(mclip.audioClip); }