/// <summary> /// _gets the audio source /// </summary> private AudioSource _getBestAudioSourceForTrack(Track aTrack) { /* * * TODO: THIS CAN BE GREATLY OPTIMIZED * * * * * **/ //TODO: CREATE ONLY WHAT WE NEED //TEMP: CREATE MORE THAN ENOUGH if (_audioParent_gameobject.GetComponent<AudioSource>() == null) { _audioParent_gameobject.AddComponent<AudioSource>(); _audioParent_gameobject.AddComponent<AudioSource>(); _audioParent_gameobject.AddComponent<AudioSource>(); _audioParent_gameobject.AddComponent<AudioSource>(); _audioParent_gameobject.AddComponent<AudioSource>(); _audioParent_gameobject.AddComponent<AudioSource>(); _audioParent_gameobject.AddComponent<AudioSource>(); _audioParent_gameobject.AddComponent<AudioSource>(); } // AudioSource chosen_audiosource = null; AudioSource[] allAvailable_audiosource = _audioParent_gameobject.GetComponents<AudioSource>(); //1 //TODO: OPTIMIZE //TEMP: ALREADY A MATCH? if (chosen_audiosource == null) { foreach (AudioSource audioSource in allAvailable_audiosource) { if (audioSource.clip == aTrack.audioClip) { chosen_audiosource = audioSource; break; } } } //2 //TODO: OPTIMIZE //TEMP: FIRST ONE NOT PLAYING WILL BE OUR SELECTION if (chosen_audiosource == null) { foreach (AudioSource audioSource in allAvailable_audiosource) { if (audioSource.isPlaying == false) { chosen_audiosource = audioSource; break; } } } //3 //TEMP: ERROR if (chosen_audiosource == null) { throw new MissingReferenceException ("No AudioSource Found for '"+ aTrack.audioClip+"'"); } return chosen_audiosource; }
// PUBLIC STATIC // PRIVATE /// <summary> /// Initializes a new instance of the <see cref="com.rmc.audio.multi_track.MultiTrack"/> class. /// </summary> private void _updateStatusOfTrack(Track aTrack, TrackUpdateType aTrackUpdateType) { AudioSource audioSource = _getBestAudioSourceForTrack (aTrack); switch (aTrackUpdateType) { case TrackUpdateType.FORCE_PLAY: if (_isPlaying_boolean) { if (audioSource.isPlaying == false) { audioSource.clip = aTrack.audioClip; audioSource.loop = true; aTrack._isPlaying_boolean = true; //SEEK TO THE MOMENT TO MATCH THE FIRST TRACK AudioSource first_audioSource = _getBestAudioSourceForTrack (_tracks_list[0]); if (audioSource == first_audioSource) { audioSource.time = 0; //Debug.Log ("First"); } else { audioSource.time = first_audioSource.time; } //FADE VOLUME _doFadeAudioSourceFromStopToPlay(audioSource); } } break; case TrackUpdateType.FORCE_STOP: if (audioSource.isPlaying == true) { aTrack._isPlaying_boolean = false; //FADE VOLUME _doFadeAudioSourceFromPlayToStop(audioSource); } break; case TrackUpdateType.DEPENDS_ON_INTENSITY: if (aTrack.isValidIntensity (_intensity_float)) { _updateStatusOfTrack (aTrack, TrackUpdateType.FORCE_PLAY); } else { _updateStatusOfTrack (aTrack, TrackUpdateType.FORCE_STOP); } break; } //Debug.Log ("updating : " + aTrack.audioClip); }
/// <summary> /// Adds the track. /// </summary> /// <param name="aTrack">A track.</param> public void addTrack(Track aTrack) { _tracks_list.Add (aTrack); if (_tracks_list.Count == 1) { //ARBITRATRILY SET tick duration to match the very first track added _synchonizerTickDuration_float = aTrack.audioClip.length; } //Debug.Log ("added: " + _tracks_list); }