Example #1
0
    /// <summary>
    /// Spawns an audio source, sets it to the right note, checks to see if the audio source is playing, and deletes it when finished
    /// </summary>
    /// <param name="thisAudio"></param>
    /// <returns></returns>
    IEnumerator AudioSourcePlayer(int noteValue, int echoValue)//echoValue - 0-3
    {
        //spawn the audio source prefab, and set the clip
        GameObject source = Instantiate(soundPrefab, Vector3.zero, Quaternion.identity) as GameObject;

        source.name             = noteMapping.GetNoteString(noteValue) + "_" + echoValue;
        source.transform.parent = soundHolder;

        AudioSource thisAudio = source.GetComponent <AudioSource>();

        //use the note mapper to get the correct clip
        thisAudio.clip = noteMapping.GetNoteSound(noteValue);
        //add the audio source to the audioSources list
        audioSources.Add(thisAudio);

        //make adjustments based on the echoe value:
        for (int i = echoValue; i > 0; i--)
        {
            thisAudio.volume = thisAudio.volume * .75f;     //attenuated by 75%
        }
        yield return(new WaitForSeconds(.25f * echoValue)); //250ms of delay

        //in case we stop playing and thisAudio is destroyed
        if (thisAudio == null)
        {
            yield break;
        }
        thisAudio.Play();
        //Wait for the audio source to stop playing
        while (thisAudio.isPlaying)
        {
            yield return(new WaitForEndOfFrame());

            if (thisAudio == null)
            {
                yield break;
            }
        }
        audioSources.Remove(thisAudio);
        Destroy(thisAudio.gameObject);
    }