public static IntroloopAudio CreateIntroloopAudioAsset(string fileName, AudioClip audioClip)
        {
            IntroloopAudio asset = ScriptableObject.CreateInstance <IntroloopAudio>();

            asset.audioClip = audioClip;
            asset.Volume    = 1;

            string path = AssetDatabase.GetAssetPath(Selection.activeObject);

            if (path == "")
            {
                path = "Assets";
            }
            else if (Path.GetExtension(path) != "")
            {
                path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
            }

            string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/" + fileName + ".asset");

            AssetDatabase.CreateAsset(asset, assetPathAndName);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = asset;

            return(asset);
        }
    private IEnumerator LoadAndPlayRoutine(IntroloopAudio music)
    {
        string musicName        = music.audioClip.name;
        float  startLoadingTime = Time.time;
        float  endLoadingTime;

        music.audioClip.LoadAudioData();
        while (music.audioClip.loadState != AudioDataLoadState.Loaded && music.audioClip.loadState != AudioDataLoadState.Failed)
        {
            yield return(null);
        }
        if (music.audioClip.loadState == AudioDataLoadState.Loaded)
        {
            endLoadingTime = Time.time;
            if (music.audioClip.loadInBackground)
            {
                IntroloopLogger.Log(musicName + " loading successful. (Took " + (endLoadingTime - startLoadingTime) + " seconds loading in background.)");
            }
            else
            {
                IntroloopLogger.Log(musicName + " loading successful.");
            }
            SetupPlayingSchedule(music);
        }
        else
        {
            IntroloopLogger.LogError(musicName + " loading failed!");
        }
    }
    private void SetupPlayingSchedule(IntroloopAudio music)
    {
        IntroloopLogger.Log("Playing \"" + music.audioClip.name + "\"");

        musicAboutToPlay = music;

        musicAboutToPlay = null;
        this.music       = music;
        ApplyVolume();
        nextSourceToPlay = 0;
        isPausing        = false;

        twoSources[0].clip = music.audioClip;
        twoSources[1].clip = music.audioClip;

        //Essential to cancel the Pause
        Stop();

        //PlayScheduled does not reset the playhead!
        twoSources[0].time = 0;
        twoSources[1].time = music.LoopBeginning;         //Waiting at the intro part so it will go looping..

        double absoluteTimeNow = AudioSettings.dspTime;

        if (music.nonLooping)
        {
            PlayScheduled(0, absoluteTimeNow);
            IntroloopLogger.Log("Type : Non-looping");
        }
        else if (music.loopWholeAudio)
        {
            PlayScheduled(0, absoluteTimeNow);
            SetScheduledEndTime(0, absoluteTimeNow + music.ClipLength);
            introFinishDspTime = absoluteTimeNow + music.ClipLength;

            PlayScheduled(1, absoluteTimeNow + music.ClipLength);
            nextScheduleTime = absoluteTimeNow + music.ClipLength * 2;

            IntroloopLogger.Log("Type : Loop whole audio");
        }
        else
        {
            PlayScheduled(0, absoluteTimeNow);
            SetScheduledEndTime(0, absoluteTimeNow + music.IntroLength);
            introFinishDspTime = absoluteTimeNow + music.IntroLength;

            //Followed by looping track

            //If has intro but no looping, music will end and won't loop
            //But in this case it goes to looping track
            PlayScheduled(1, absoluteTimeNow + music.IntroLength);
            nextScheduleTime = (absoluteTimeNow + music.IntroLength) + (music.LoopingLength);
            IntroloopLogger.Log("Type : Introloop");
        }
        playDspTimestamp  = absoluteTimeNow;
        pauseDspTimestamp = 0;
        pauseDspTotal     = 0;
        isPlaying         = true;
    }
Ejemplo n.º 4
0
    public void Init()
    {
        var mixerGroup = mixer.FindMatchingGroups(mixerGroupName)[0];

        Debug.Log($"Audio - IntroLoop - Mixer Group - {mixerGroup.name}");
        IntroloopPlayer.Instance.SetMixerGroup(mixerGroup);
        currentClip = null;
    }
Ejemplo n.º 5
0
 public void SetMainAudio(IntroloopAudio audio)
 {
     MainLoopAudio = audio;
     if (PlayingAudio != null)
     {
         PlayAudio(MainLoopAudio, 1f);
     }
 }
 internal void Unload()
 {
     music.Unload();
     IntroloopLogger.Log(String.Format("Unloaded \"{0}\" from memory.", music.audioClip.name));
     twoSources[0].clip = null;
     twoSources[1].clip = null;
     music            = null;
     musicAboutToPlay = null;
 }
Ejemplo n.º 7
0
    //------------------------------------------------------------------------------
    public void StopBgm()
    {
#if USE_INTRO_LOOP
        IntroloopPlayer.Instance.Stop();
        currIntroLoopBgm = null;
#else
        bgmSource.Stop();
        bgmSource.clip = null;
#endif
    }
Ejemplo n.º 8
0
    public void PlaySelectedMusicLooping(IntroloopAudio clipToPlay)
    {
        if (currentClip != null && currentClip.name == clipToPlay.name)
        {
            return;
        }

        currentClip = clipToPlay;
        var volume = PlayerPrefs.GetFloat(volumeValueName, 0.75f);

        mixer.SetFloat(volumeValueName, VolumeCalculation.GetVolumeDecibels(volume, reductionDb));
        IntroloopPlayer.Instance.Play(clipToPlay);
    }
Ejemplo n.º 9
0
    /// <summary>
    /// Play the audio using settings specified in IntroloopAudio file's inspector area with fade-in
    /// or cross fade (if other IntroloopAudio is playing now) length specified by argument.
    /// </summary>
    /// <param name="audio"> An IntroloopAudio asset file to play.</param>
    /// <param name="fadeLengthSeconds"> Fade in/Cross fade length to use.</param>
    public void PlayFade(IntroloopAudio audio, float fadeLengthSeconds)
    {
        //Auto-crossfade old ones. If no fade length specified, a very very small fade will be used to avoid pops/clicks.
        StopFade(fadeLengthSeconds == 0 ? popRemovalFadeTime : fadeLengthSeconds);

        int next = (currentTrack + 1) % 2;

        twoTracks[next].Play(audio, fadeLengthSeconds == 0 ? false : true);
        towardsVolume[next] = 1;
        fadeLength[next]    = fadeLengthSeconds;

        currentTrack = next;
    }
Ejemplo n.º 10
0
    public void PlaySelectedMusicLooping(IntroloopAudio clipToPlay)
    {
        if (currentClip != null && currentClip.name == clipToPlay.name)
        {
            return;
        }

        currentClip = clipToPlay;
        IntroloopPlayer.Instance.Play(clipToPlay);

        var volume = PlayerPrefs.GetFloat(volumeValueName, 0.75f);

        mixer.SetFloat(volumeValueName, Mathf.Log10(volume) * 20);
    }
Ejemplo n.º 11
0
    //------------------------------------------------------------------------------
    public Handle PlayBgm(int index)
    {
#if USE_INTRO_LOOP
        if (0 > index || introLoopBgms.Length <= index)
        {
            return(null);
        }

        if (currIntroLoopBgm == introLoopBgms[index])
        {
            return(bgmHandle);
        }

        currIntroLoopBgm = introLoopBgms[index];
        IntroloopPlayer.Instance.Stop();
        IntroloopPlayer.Instance.Play(currIntroLoopBgm);
#else
        if (0 > index || bgmClips.Length <= index)
        {
            return(null);
        }

        if (bgmSource.clip == bgmClips[index])
        {
            return(bgmHandle);
        }

        bgmSource.Stop();
        bgmSource.clip = bgmClips[index];
        bgmSource.loop = true;
        bgmSource.Play();
#endif

        bgmHandle.ResetParams();
        bgmHandle.frame   = Time.frameCount;
        bgmHandle.IsValid = true;
        bgmHandle.index   = index;

#if USE_INTRO_LOOP
        currIntroLoopBgm.Volume = volume.bgm * bgmHandle.volume;
        IntroloopPlayer.Instance.ApplyVolumeSettingToAllTracks();
#endif
        return(bgmHandle);
    }
Ejemplo n.º 12
0
    internal void Play(IntroloopAudio music, bool isFade)
    {
        pauseDspTimestamp = 0;
        pauseDspTotal     = 0;

        AudioDataLoadState loadState = music.audioClip.loadState;
        string             musicName = music.audioClip.name;

        FadeVolume = isFade ? 0 : 1;
        if (loadState != AudioDataLoadState.Loaded)
        {
            IntroloopLogger.Log("\"" + musicName + "\" not loaded yet. Loading into memory...");
            StartCoroutine(LoadAndPlayRoutine(music));
        }
        else
        {
            IntroloopLogger.Log("\"" + musicName + "\" already loaded in memory.");
            SetupPlayingSchedule(music);
        }
    }
Ejemplo n.º 13
0
    public void PlayAudio(IntroloopAudio audio, float fadeInDuration = 0.5f, float crossfadeDuration = 0.5f, bool forceReplay = false)
    {
        if (PlayingAudio == audio && !forceReplay)
        {
            return;
        }
        var duration = PlayingAudio != null ? crossfadeDuration : fadeInDuration;

        if (PlayingAudio == audio && forceReplay)
        {
            IntroloopPlayer.Instance.Stop();
            IntroloopPlayer.Instance.Play(audio, duration);
            return;
        }
        if (PrintDebugMessages)
        {
            print("LoopAudioPlayer: Played audio " + audio.name);
        }
        PlayingAudio = audio;
        IntroloopPlayer.Instance.Play(audio, duration);
    }
    internal void Play(IntroloopAudio music,bool isFade)
    {
        pauseDspTimestamp = 0;
        pauseDspTotal = 0;

        AudioDataLoadState loadState = music.audioClip.loadState;
        string musicName = music.audioClip.name;
        FadeVolume = isFade ? 0 : 1;
        if(loadState != AudioDataLoadState.Loaded)
        {
            IntroloopLogger.Log("\"" + musicName + "\" not loaded yet. Loading into memory...");
            StartCoroutine(LoadAndPlayRoutine(music));
        }
        else
        {
            IntroloopLogger.Log("\"" + musicName + "\" already loaded in memory.");
            SetupPlayingSchedule(music);
        }
    }
    /// <summary>
    /// Play the audio using settings specified in IntroloopAudio file's inspector area with fade-in 
    /// or cross fade (if other IntroloopAudio is playing now) length specified by argument.
    /// </summary>
    /// <param name="audio"> An IntroloopAudio asset file to play.</param>
    /// <param name="fadeLengthSeconds"> Fade in/Cross fade length to use.</param>
    public void PlayFade(IntroloopAudio audio, float fadeLengthSeconds)
    {
        //Auto-crossfade old ones. If no fade length specified, a very very small fade will be used to avoid pops/clicks.
        StopFade(fadeLengthSeconds== 0 ? popRemovalFadeTime : fadeLengthSeconds);

        int next = (currentTrack + 1) % 2;
        twoTracks[next].Play(audio,fadeLengthSeconds == 0 ? false : true);
        towardsVolume[next] = 1;
        fadeLength[next] = fadeLengthSeconds;

        currentTrack = next;
    }
Ejemplo n.º 16
0
 /// <summary>
 /// If you do not wish to use a template prefab which already has IntroloopAudio ready,
 /// you might do it later like this.
 /// </summary>
 public void LateAssign(IntroloopAudio assault, IntroloopAudio compete)
 {
     this.assault = assault;
     this.compete = compete;
 }
Ejemplo n.º 17
0
 /// <summary>
 // An experimental feature in the case that you really want the audio to start in an instant you call Play.
 // By normally using Play and Stop it loads the audio the moment you called Play. It will introduces a
 // small delay in the case of a large audio file.
 // But by using this before actually calling Play it will be instant. However be aware that RAM is managed less efficiently in the following case.
 // Normally Introloop immediately unloads the previous track to minimize memory, but if you use Preload then
 // did not call Play with the same IntroloopAudio afterwards, the loaded memory will be unmanaged.
 // (Just like if you tick "Preload Audio Data" on your clip and have them in a hierarchy somewhere, then did not use it.)
 /// </summary>
 public void Preload(IntroloopAudio audio)
 {
     audio.Preload();
 }
 /// <summary>
 /// Play the audio using settings specified in IntroloopAudio file's inspector area with fade-in 
 /// or cross fade (if other IntroloopAudio is playing now) default length specified in IntroloopSettings component
 /// that is attached to IntroloopPlayer.
 /// </summary>
 /// <param name="audio"> An IntroloopAudio asset file to play.</param>
 public void PlayFade(IntroloopAudio audio)
 {
     PlayFade(audio, introloopSettings.defaultFadeLength);
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Play the audio using settings specified in IntroloopAudio file's inspector area with fade-in
 /// or cross fade (if other IntroloopAudio is playing now) default length specified in IntroloopSettings component
 /// that is attached to IntroloopPlayer.
 /// </summary>
 /// <param name="audio"> An IntroloopAudio asset file to play.</param>
 public void PlayFade(IntroloopAudio audio)
 {
     PlayFade(audio, introloopSettings.defaultFadeLength);
 }
Ejemplo n.º 20
0
 public void StartBattleMusic(IntroloopAudio audio) => Player.Play(audio);
Ejemplo n.º 21
0
 public void Init()
 {
     IntroloopPlayer.Instance.SetMixerGroup(mixer.FindMatchingGroups(volumeValueName)[0]);
     currentClip = null;
 }
 internal void Unload()
 {
     music.Unload();
     IntroloopLogger.Log(String.Format("Unloaded \"{0}\" from memory.",music.audioClip.name));
     twoSources[0].clip = null;
     twoSources[1].clip = null;
     music = null;
     musicAboutToPlay = null;
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Play the audio using settings specified in IntroloopAudio file's inspector area.
 /// </summary>
 /// <param name="audio"> An IntroloopAudio asset file to play.</param>
 public void Play(IntroloopAudio audio)
 {
     PlayFade(audio, 0);
 }
 private IEnumerator LoadAndPlayRoutine(IntroloopAudio music)
 {
     string musicName = music.audioClip.name;
     float startLoadingTime = Time.time;
     float endLoadingTime;
     music.audioClip.LoadAudioData();
     while(music.audioClip.loadState != AudioDataLoadState.Loaded && music.audioClip.loadState != AudioDataLoadState.Failed)
     {
         yield return null;
     }
     if(music.audioClip.loadState == AudioDataLoadState.Loaded)
     {
         endLoadingTime = Time.time;
         if(music.audioClip.loadInBackground)
         {
             IntroloopLogger.Log(musicName + " loading successful. (Took " + (endLoadingTime - startLoadingTime) + " seconds loading in background.)");
         }
         else
         {
             IntroloopLogger.Log(musicName + " loading successful.");
         }
         SetupPlayingSchedule(music);
     }
     else
     {
         IntroloopLogger.LogError(musicName + " loading failed!");
     }
 }
    private void SetupPlayingSchedule(IntroloopAudio music)
    {
        IntroloopLogger.Log("Playing \"" + music.audioClip.name + "\"");

        musicAboutToPlay = music;

        musicAboutToPlay = null;
        this.music = music;
        ApplyVolume();
        nextSourceToPlay = 0;
        isPausing = false;

        twoSources[0].clip = music.audioClip;
        twoSources[1].clip = music.audioClip;

        //Essential to cancel the Pause
        Stop();

        //PlayScheduled does not reset the playhead!
        twoSources[0].time = 0;
        twoSources[1].time = music.introBoundary; //Waiting at the intro part so it will go looping..

        double absoluteTimeNow = AudioSettings.dspTime;
        if(music.nonLooping)
        {
            PlayScheduled(0,absoluteTimeNow);
            IntroloopLogger.Log("Type : Non-looping");
        }
        else if(music.loopWholeAudio)
        {
            PlayScheduled(0,absoluteTimeNow);
            SetScheduledEndTime(0,absoluteTimeNow + music.ClipLength);
            introFinishDspTime = absoluteTimeNow + music.ClipLength;

            PlayScheduled(1,absoluteTimeNow + music.ClipLength);
            nextScheduleTime = absoluteTimeNow + music.ClipLength*2;

            IntroloopLogger.Log("Type : Loop whole audio");
        }
        else {
            PlayScheduled(0, absoluteTimeNow);
            SetScheduledEndTime(0, absoluteTimeNow + music.IntroLength);
            introFinishDspTime = absoluteTimeNow + music.IntroLength;

            //Followed by looping track

            //If has intro but no looping, music will end and won't loop
            //But in this case it goes to looping track
            PlayScheduled(1, absoluteTimeNow + music.IntroLength);
            nextScheduleTime = (absoluteTimeNow + music.IntroLength) + (music.LoopingLength);
            IntroloopLogger.Log("Type : Introloop");
        }
        playDspTimestamp = absoluteTimeNow;
        pauseDspTimestamp = 0;
        pauseDspTotal = 0;
        isPlaying = true;
    }
 /// <summary>
 /// Play the audio using settings specified in IntroloopAudio file's inspector area.
 /// </summary>
 /// <param name="audio"> An IntroloopAudio asset file to play.</param>
 public void Play(IntroloopAudio audio)
 {
     PlayFade(audio, 0);
 }