/// <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); }