void OnEnable() { MGTarget = target as MusicGroup; serializedTarget = new SerializedObject(MGTarget); playOnAwakeProp = serializedTarget.FindProperty("playOnAwake"); bpmProp = serializedTarget.FindProperty("bpm"); instrumentChannelsProp = serializedTarget.FindProperty("instrumentChannels"); }
private void Awake() { channelsSFX = new AudioSourcePool(gameObject, groupSFX, channelCountSFX); channelsMusic = new AudioSourcePool(gameObject, groupMusic, channelCountMusic, true, 0.0f); groupsMusic = new MusicGroup[groupCountMusic]; for (int i = 0; i < groupCountMusic; ++i) { groupsMusic[i] = new MusicGroup(); } }
private string GetRandomMusic(string groupName) { //this is a group name MusicGroup group = null; foreach (MusicGroup mg in backgroundMusicGroups) { if (mg.groupName == groupName) { group = mg; break; } } if (group == null) { Debug.LogError("Wrong group name: " + groupName); return(null); } List <string> availableList = new List <string>(); List <string> list2 = new List <string>(); foreach (string music in group.musicNameList) { if (HasLocalCopy(music)) { availableList.Add(music); } else { list2.Add(music); } } if (list2.Count > 0) { StartCoroutine(DownloadMusic(list2[0])); } if (availableList.Count > 0) { return(availableList[UnityEngine.Random.Range(0, availableList.Count)]); } else { return(group.defaultMusic); } }
void OnSongChanged(MusicGroup next) { var off = current == null; current = next; if (off) { StartAllPlaying(); } else { fadeOut.Play(); } }
// Play music using the music channels. // If the currently-playing music group is the same as the given music group, // this method will have no effect. public void PlayMusicGroup(SOAMusicChannelData musicGroup, float secondsToFadeOut = 0.0f, float secondsToWaitBeforeFadeIn = 0.0f, float secondsToFadeIn = 0.0f) { MusicGroup currentMusicGroup = GetCurrentMusicGroup(); int channelCount = musicGroup.GetLength(); // Don't move to a new music group if the current one is playing // the same music group as the one passed to this method. if (musicGroup == latestMusicGroupData) { // Instead, fade the channels to the corresponding volumes. for (int i = 0; i < channelCount; ++i) { AudioClip clip = musicGroup.GetClip(i); float targetVolume = musicGroup.GetVolume(i); int channelNumber = currentMusicGroup.GetChannelIndexFromClip(clip); SetMusicChannelVolume(channelNumber, targetVolume, secondsToFadeIn); } // That's all we need to do. return; } latestMusicGroupData = musicGroup; // Fade out the old music group. currentMusicGroup.Fade(0.0f, secondsToFadeOut); // Increment the current music group index. ++groupMusicCurrent; groupMusicCurrent %= groupsMusic.Length; // Get the new current music group and clear its data. currentMusicGroup = GetCurrentMusicGroup(); currentMusicGroup.Annihilate(); // Populate the music group's channels. for (int i = 0; i < channelCount; ++i) { AudioClip clip = musicGroup.GetClip(i); float targetVolume = musicGroup.GetVolume(i); AudioSource source = channelsMusic.GetFreeChannel(); source.volume = 0.0f; currentMusicGroup.AddChannel(source, clip); currentMusicGroup.FadeChannel(i, targetVolume, secondsToFadeIn, secondsToWaitBeforeFadeIn); } }
public void PlayMusic(MusicGroup group) { var source = Pool.Grab(); source.clip = group.Music[0]; source.volume = group.Volume; source.priority = 1024; source.loop = true; var animator = new AudioSourceAnimator(source, TabFade, MasterVolume, MusicVolume); Animators.Add(animator); source.Play(); }
public static void CreateMusicGroup(params string[] musicname) { musicGroup = new MusicGroup(musicname); }
void ApplyMusicGroup(MusicGroup musicGroup) { bpm = musicGroup.bpm; }
private void PlayBGMWithFade(string musicName, float fadeTime) { if (_playingMusicName == musicName) //already playing { return; } //find the group MusicGroup group = null; foreach (MusicGroup mg in backgroundMusicGroups) { if (mg.musicNameList.IndexOf(musicName) >= 0 || mg.defaultMusic == musicName) { group = mg; break; } } if (group == null) { Debug.LogWarning("Music name not found in any group: " + musicName); return; } if (!HasLocalCopy(musicName)) { Debug.LogWarning(musicName + " not downloaded yet. Will play a substitute: " + group.defaultMusic); StartCoroutine(DownloadMusic(musicName)); //find a substitute musicName = GetRandomMusic(group.groupName); if (musicName == _playingMusicName) { return; } } else { Debug.Log("Already downloaded. Just play it."); } //now the music is definitely on local, but could be under different folders _nextMusicName = musicName; if (_isLoadingBGMusic) { return; } _fadeTime = fadeTime; if (_bgmSource.isPlaying) { //fade out current _fadeMode = FadeMode.FadeOut; _fadeTimer = 0; } else { StartCoroutine(StreamPlay(musicName)); } }
/// <summary> /// MusicGroupOrPerson as a MusicGroup. /// </summary> /// <param name="musicGroup">MusicGroupOrPerson as a MusicGroup.</param> public MusicGroupOrPerson(MusicGroup musicGroup) { AsMusicGroup = musicGroup; }