Example #1
0
        /// <summary>
        /// Splits the audio track into two separate tracks (the intro and the loop) and caches them.
        /// This MUST be called before the overall track can be played.
        /// </summary>
        public void CreateAndCacheClips()
        {
            try
            {
                // Sometimes the designer might want to adjust the loop points themselves. That's fine, too.
                // If they uncheck the Compensate for Frequency box, we'll just do everything as normal.
                float frequencyRatio = CompensateForFrequency ? FrequencyRatio : 1f;

                float[] clipData              = new float[Track.samples * Channels];
                int     loopStartSampleCount  = Convert.ToInt32(Math.Max(0, LoopStart) * Channels * frequencyRatio);
                int     loopLengthSampleCount = Convert.ToInt32(
                    Math.Min(Track.samples - 1, Math.Max(1, LoopLength)) * frequencyRatio
                    );
                int introLength = Convert.ToInt32(Math.Max(1, LoopStart) * frequencyRatio);

                Track.GetData(clipData, 0);

                IntroClip = AudioClip.Create(
                    Name + " intro",
                    introLength,
                    Channels,
                    Frequency,
                    false);

                IntroClip.SetData(EnsoHelpers.Slice(clipData, 0, introLength * Channels), 0);

                LoopClip = AudioClip.Create(
                    Name + " loop",
                    loopLengthSampleCount,
                    Channels,
                    Frequency,
                    false);

                int maxSafeLoopLength = clipData.Length - loopStartSampleCount;
                LoopClip.SetData(EnsoHelpers.Slice(clipData, loopStartSampleCount,
                                                   Math.Min(loopLengthSampleCount * Channels, maxSafeLoopLength)), 0);
            }
            catch (Exception e)
            {
                Debug.LogError(string.Format(@"[Enso] An exception occurred when creating and caching the ""{0}"" music clip: {1}", Track.name, e.Message));
                throw e;
            }
        }
Example #2
0
        // Update is called once per frame
        void Update()
        {
            if (VolumeStatus != VolumeStatuses.Static)
            {
                if (FadeTimeLeft > 0)
                {
                    FadeTimeLeft -= Time.deltaTime;
                }
            }

            switch (VolumeStatus)
            {
            case VolumeStatuses.FadingIn:
                if (FadeTimeLeft <= 0)
                {
                    OnFadeInComplete();
                }
                else
                {
                    float t = FadeTimeLeft / MaxFadeTime;
                    SetSpeakerVolume(OrigFadeVolume + (DestFadeVolume - OrigFadeVolume) * EnsoHelpers.CalculateEqualPowerCrossfade(t, true));
                }
                break;

            case VolumeStatuses.FadingOut:
                if (FadeTimeLeft <= 0)
                {
                    OnFadeOutComplete();
                }
                else
                {
                    float t = FadeTimeLeft / MaxFadeTime;
                    SetSpeakerVolume(OrigFadeVolume * EnsoHelpers.CalculateEqualPowerCrossfade(t, false));
                }
                break;
            }
        }