/// <summary> /// Gets AudioClip based on Daggerfall sound index. /// </summary> /// <param name="soundIndex">Sound index.</param> /// <returns>AudioClip or null.</returns> public AudioClip GetAudioClip(int soundIndex) { const float divisor = 1.0f / 128.0f; // Must be ready and have a valid input index if (!ReadyCheck() || !soundFile.IsValidIndex(soundIndex)) { return(null); } // Look for clip in cache AudioClip cachedClip = GetCachedClip(soundIndex); if (cachedClip) { return(cachedClip); } // Get clip AudioClip clip; string name = string.Format("DaggerfallClip [Index={0}, ID={1}]", soundIndex, (int)soundFile.BsaFile.GetRecordId(soundIndex)); if (Utility.AssetInjection.SoundReplacement.CustomSoundExist(soundIndex)) { // Get audio clip from sound file on disk WWW customSoundFile = Utility.AssetInjection.SoundReplacement.LoadCustomSound(soundIndex); clip = customSoundFile.audioClip; clip.name = name; StartCoroutine(WaitForSoundFile(customSoundFile, clip)); } else { // Get sound data DFSound dfSound; if (!soundFile.GetSound(soundIndex, out dfSound)) { return(null); } // Create audio clip clip = AudioClip.Create(name, dfSound.WaveData.Length, 1, SndFile.SampleRate, false); // Create data array float[] data = new float[dfSound.WaveData.Length]; for (int i = 0; i < dfSound.WaveData.Length; i++) { data[i] = (dfSound.WaveData[i] - 128) * divisor; } // Set clip data clip.SetData(data, 0); } // Cache the clip CacheClip(soundIndex, clip); return(clip); }
/// <summary> /// Gets AudioClip based on Daggerfall sound index. /// </summary> /// <param name="soundIndex">Sound index.</param> /// <returns>AudioClip or null.</returns> public AudioClip GetAudioClip(int soundIndex) { const float divisor = 1.0f / 128.0f; // Must be ready and have a valid input index if (!ReadyCheck() || !soundFile.IsValidIndex(soundIndex)) { return(null); } // Look for clip in cache AudioClip cachedClip = GetCachedClip(soundIndex); if (cachedClip) { return(cachedClip); } // Get clip AudioClip clip; string name = string.Format("DaggerfallClip [Index={0}, ID={1}]", soundIndex, (int)soundFile.BsaFile.GetRecordId(soundIndex)); if (SoundReplacement.TryImportSound((SoundClips)soundIndex, out clip)) { clip.name = name; } else { // Get sound data DFSound dfSound; if (!soundFile.GetSound(soundIndex, out dfSound)) { return(null); } // Create audio clip clip = AudioClip.Create(name, dfSound.WaveData.Length, 1, SndFile.SampleRate, false); // Create data array float[] data = new float[dfSound.WaveData.Length]; for (int i = 0; i < dfSound.WaveData.Length; i++) { data[i] = (dfSound.WaveData[i] - 128) * divisor; } // Set clip data clip.SetData(data, 0); } // Cache the clip CacheClip(soundIndex, clip); return(clip); }
/// <summary> /// Gets AudioClip based on Daggerfall sound index. /// Caches 2D and 3D sounds independently. /// </summary> /// <param name="soundIndex">Sound index.</param> /// <param name="_3D">True for a 3D sound, otherwise sound is 2D.</param> /// <returns>AudioClip or null.</returns> public AudioClip GetAudioClip(int soundIndex, bool _3D = true) { const float divisor = 1.0f / 128.0f; if (!ReadyCheck()) { return(null); } // Look for clip in cache AudioClip cachedClip = GetCachedClip(soundIndex, _3D); if (cachedClip) { return(cachedClip); } // Get sound data DFSound dfSound; if (!soundFile.GetSound(soundIndex, out dfSound)) { return(null); } // Create audio clip AudioClip clip; string name = string.Format("DaggerfallClip [Index={0}, ID={1}]", soundIndex, (int)soundFile.BsaFile.GetRecordId(soundIndex)); #if UNITY_5_0 clip = AudioClip.Create(name, dfSound.WaveData.Length, 1, SndFile.SampleRate, false); // TODO: Set AudioSource.spatialBlend property where appropriate. #else clip = AudioClip.Create(name, dfSound.WaveData.Length, 1, SndFile.SampleRate, _3D, false); #endif // Create data array float[] data = new float[dfSound.WaveData.Length]; for (int i = 0; i < dfSound.WaveData.Length; i++) { data[i] = (dfSound.WaveData[i] - 128) * divisor; } // Set clip data clip.SetData(data, 0); // Cache the clip CacheClip(soundIndex, _3D, clip); return(clip); }