SmoothAudioLoop
Latest revision as of 20:53, 10 January 2012
Author: Jessy
[edit] Description
NOTE: This will not work on the iPhone, using compressed audio.
This is a script that allows for audio to be looped before a playback iteration reaches its conclusion. The main purpose of this is to make it possible to preserve the sound that exists in an audio file after an effective musical phrase has ended. For example, you will never hear the natural decay of a crash cymbal in a standard drum loop, because that would leave a gaping hole before the beat started back up again. Properly set up with the correct duration of your music, this script will permit seamless looping of audio, while preserving sonic tails, potentially resulting in much more natural-sounding loops.
You will likely either need to be a musician, or work closely with one, to really take advantage of this script. Despite its simplicity, it adds important functionality that can not be achieved with the standard "Loop" checkbox provided by Unity on all Audio Sources. Both JavaScript and C# versions are available below.
[edit] JavaScript - Loop.js
// Make sure that "Play On Awake" is on, and "Loop" is off, for your Audio Source. var loopLength : float; // the length of the musical loop in the audio file //the audio loop function while (true) { yield WaitForSeconds (loopLength); audio.PlayOneShot(audio.clip); }
[edit] C# - Loop.cs
// Make sure that "Play On Awake" is on, and "Loop" is off, for your Audio Source. using UnityEngine; using System.Collections; public class Loop : MonoBehaviour { public float loopLength; // the length of the musical loop in the audio file IEnumerator LoopCoroutine () { while (true) { yield return new WaitForSeconds (loopLength); audio.PlayOneShot(audio.clip); } } }