Ejemplo n.º 1
0
 public RestartingClipPlayerHandler(SoundInfo soundInfo)
 {
     this.soundInfo = soundInfo;
     if (soundInfo.Cached)
     {
         ClipCache.CacheClip(soundInfo);
     }
 }
 public MultiFireClipPlayerHandler(SoundInfo soundInfo)
 {
     this.soundInfo = soundInfo;
     if (soundInfo.Cached)
     {
         ClipCache.CacheClip(soundInfo);
     }
 }
Ejemplo n.º 3
0
        public IEnumerator RetainClipAsync(string path)
        {
            ClipCache cache = null;

            if (m_Cache.ContainsKey(path))
            {
                //D.AudioLog("Playing cached sound! " + path);
                cache = m_Cache[path];
            }
            else
            {
                AudioClip c = null;

                // Prefer file from additional bundle (can be used for localization)
                for (int i = 0; i < m_AdditionalBundles.Count; i++)
                {
                    AssetBundle        b = m_AdditionalBundles[i];
                    AssetBundleRequest r = b.LoadAssetAsync <AudioClip>(path);
                    yield return(r);

                    c = (AudioClip)r.asset;

                    if (c != null)
                    {
                        break;
                    }
                }

                // Load from main bundle
                if (c == null && m_AudioBundle != null)
                {
                    AssetBundleRequest r = m_AudioBundle.LoadAssetAsync <AudioClip>(path);
                    yield return(r);

                    c = (AudioClip)r.asset;
                }

                if (c == null)
                {
                    yield break;
                }

                //D.AudioLog("Caching sound! " + path);
                cache      = new ClipCache();
                cache.Clip = c;
                cache.Path = path;

                m_Cache[path] = cache;
            }

            cache.Usages++;
        }
Ejemplo n.º 4
0
        public AudioClip RetainClip(string path)
        {
            ClipCache cache = null;

            if (m_Cache.ContainsKey(path))
            {
                //D.AudioLog("Playing cached sound! " + path);
                cache = m_Cache[path];
            }
            else
            {
                AudioClip c = null;

                // Prefer file from additional bundle (can be used for localization)
                for (int i = 0; i < m_AdditionalBundles.Count; i++)
                {
                    AssetBundle b = m_AdditionalBundles[i];
                    c = b.LoadAsset <AudioClip>(path);
                    if (c != null)
                    {
                        break;
                    }
                }

                // Load from main bundle
                if (c == null && m_AudioBundle != null)
                {
                    c = m_AudioBundle.LoadAsset <AudioClip>(path);
                }

                if (c == null)
                {
                    return(null);
                }

                //D.AudioLog("Caching sound! " + path);
                cache      = new ClipCache();
                cache.Clip = c;
                cache.Path = path;

                m_Cache[path] = cache;
            }

            cache.Usages++;

            return(cache.Clip);
        }
Ejemplo n.º 5
0
        private void ReleaseClipNowInternal(ClipCache cache, string clipPath, int usages = 1)
        {
            if (cache.Path != clipPath)
            {
                D.AudioError("Trying to release incorrect clip! " + clipPath);
            }

            cache.Usages = cache.Usages - usages;
            //D.AudioLog(clipPath + " usages: " + cache.Usages);

            if (cache.Usages <= 0)
            {
                //D.AudioLog("Removing cached sound! " + cache.Path);
                m_Cache.Remove(cache.Path);
                Resources.UnloadAsset(cache.Clip);
            }
        }
Ejemplo n.º 6
0
        public override void Init(WaveFormat waveformat)
        {
            //TODO Use ConvertingSampleProvider

            provider = cachedClipProvider = ClipCache.GetCachedClip(SoundInfo).GetSampleProvider();

            if (provider.WaveFormat.SampleRate != waveformat.SampleRate)
            {
                if (provider.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
                {
                    provider = ((resampler = new MediaFoundationResampler(provider.ToWaveProvider(), waveformat)) as MediaFoundationResampler).ToSampleProvider();
                }
                else
                {
                    provider = ((resampler = new MediaFoundationResampler(new Wave16ToFloatProvider(provider.ToWaveProvider()), waveformat)) as MediaFoundationResampler).ToSampleProvider();
                }
            }
            else if (provider.WaveFormat.Channels != waveformat.Channels)
            {
                if (waveformat.Channels == 1 & provider.WaveFormat.Channels > 1)
                {
                    provider = provider.ToMono();
                }
                else if (waveformat.Channels == 2)
                {
                    provider = provider.ToStereo();
                }
                else
                {
                    throw new InvalidWaveFormatException($"Couldn´t find a suitable conversion from {provider.WaveFormat.Channels} to {waveformat.Channels} Channels");
                }
            }

            provider = new VolumeSampleProvider(provider)
            {
                Volume = SoundInfo.VolumeModifier
            };
        }