public static AudioClip ReplaceAudioClip(AudioClip original, SoundReplacementMode mode, int number) { //TODO: Reimplement this. //if (BaldisBasics.VersionData.NonReplaceableAudioFiles.ContainsKey(original.name)) return original; try { if (!SoundReplacements.ContainsKey(original.name)) { return(original); //Return the original if it's not in the sound replacements. } SoundReplacement soundReplacement = SoundReplacements[original.name]; switch (mode) { case SoundReplacementMode.PickFirstSound: return(GetAudioClip(soundReplacement.SoundNames[0])); case SoundReplacementMode.PickRandomSound: return(GetAudioClip(soundReplacement.SoundNames[new Random(DateTime.Now.Millisecond).Next(soundReplacement.SoundNames.Count - 1)])); case SoundReplacementMode.SpecificSoundOrder: return(GetAudioClip(soundReplacement.SoundNames[number])); default: return(GetAudioClip(soundReplacement.SoundNames[0])); } } catch { } return(original); }
static void Postfix(ref CoreSoundEffectData.SoundCueDefinition __result, string cueName, CoreSoundEffectData data) { SoundReplacement replacement = FindReplacement(cueName, data.name); if (replacement != null) { __result = replacement.GetReplacementSoundDefininition(); } }
/// <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> /// Play current song. /// </summary> public void Play(SongFiles song) { if (!InitSynth()) { return; } // Stop if playing another song Stop(); // Import custom song from disk if (SoundReplacement.CustomSongExist(song)) { // Load song file WWW customSongFile = SoundReplacement.LoadCustomSong(song); if (customSongFile != null) { // Let www import the song customSong.IsReady = false; // Play song StartCoroutine(PlayCustomSong(customSongFile)); // Settings Song = song; customSong.UseCustomSong = true; IsPlaying = true; return; } Debug.LogError("Can't load custom song " + song); } // Load song data string filename = EnumToFilename(song); byte[] songData = LoadSong(filename); if (songData == null) { return; } // Create song MidiFile midiFile = new MidiFile(new MyMemoryFile(songData, filename)); if (midiSequencer.LoadMidi(midiFile)) { midiSequencer.Play(); currentMidiName = filename; playEnabled = true; IsPlaying = true; } }
private byte[] LoadSong(string filename) { // Get custom midi song if (SoundReplacement.CustomMidiSongExist(filename)) { return(SoundReplacement.LoadCustomMidiSong(filename)); } // Get Daggerfal song TextAsset asset = Resources.Load <TextAsset>(Path.Combine(SongFolder, filename)); if (asset != null) { return(asset.bytes); } DaggerfallUnity.LogMessage(string.Format("DaggerfallSongPlayer: Song file '{0}' not found.", filename)); return(null); }
/// <summary> /// Play current song. /// </summary> public void Play(SongFiles song) { if (!InitSynth()) { return; } // Stop if playing another song Stop(); // Import custom song AudioClip clip; if (isImported = SoundReplacement.TryImportSong(song, out clip)) { Song = song; audioSource.clip = clip; isLoading = true; return; } // Load song data string filename = EnumToFilename(song); byte[] songData = LoadSong(filename); if (songData == null) { return; } // Create song MidiFile midiFile = new MidiFile(new MyMemoryFile(songData, filename)); if (midiSequencer.LoadMidi(midiFile)) { midiSequencer.Play(); currentMidiName = filename; playEnabled = true; IsPlaying = true; } }
private IEnumerator LoadAssets() { yield return(null); try { LoadingMessage(""); //Set the loading message. AssetManager.DataSerializer = new JsonDataSerializer(); foreach (string arg in Environment.GetCommandLineArgs()) { string toLower = arg.ToLower(); if (toLower.StartsWith(modPathStart)) { AssetManager.FilePath = toLower.TrimStart(modPathStart.ToCharArray()); } switch (toLower) { case "verbose": Master.VerboseMode = true; break; case "debug": Master.DebugMode = true; break; } } try { Debug.Log($"Current Directory: {Directory.GetCurrentDirectory()}"); } catch (IndexOutOfRangeException) { } if (!Directory.Exists(AssetManager.FilePath)) { AssetManager.FilePath = "default_mod"; } if (!Directory.Exists(AssetManager.FilePath)) { throw LoadingException.modIsInvalid; //If the mod is invalid, throw an exception. } Debug.Log("Mod folder is " + AssetManager.FilePath); AssetManager.Config = AssetManager.ReadData <Config>(AssetManager.configFilePath); //Get the config data. AssetManager.ModInfo = AssetManager.ReadData <ModInfo>(AssetManager.modInfoPath); //Get the mod info. if (AssetManager.Config.Cursor.UseSystemCursor) { AssetManager.UpdateCursor(); Cursor.visible = true; } else { Cursor.visible = false; } AssetManager.AudioRepository = AssetManager.ReadData <AudioFileRepository>(AssetManager.audioFileRepositoryPath); //Get the audio repository. AssetManager.ImageRepository = AssetManager.ReadData <ImageFileRepository>(AssetManager.imageFileRepositoryPath); //Get the image repository. AssetManager.AnimationRepository = AssetManager.ReadData <AnimationFileRepository>(AssetManager.animationFileRepositoryPath); //Get the animation repository. AssetManager.CheckSoundReplacements(); //Check the sound replacements. AssetManager.CheckTextureReplacements(); //Check the texture replacements. AssetManager.CheckSpriteReplacements(); //Check the sprite replacements. foreach (string soundReplacement in AssetManager.SoundReplacements.Keys) { SoundReplacement data = AssetManager.SoundReplacements[soundReplacement]; if (Master.VersionData.NonReplaceableAudioFiles.ContainsKey(soundReplacement)) { throw LoadingException.DisallowedReplacement(soundReplacement, Master.VersionData.NonReplaceableAudioFiles[soundReplacement]); } } } catch (Exception e) { HandleException(e); yield break; } foreach (string audioFile in AssetManager.AudioRepository.Files.Keys) { string path; FileData data; try { data = AssetManager.AudioRepository.Files[audioFile]; } catch (Exception e) { HandleException(e); yield break; } yield return(LoadFile(AssetManager.AudioRepository.GetPath(audioFile))); try { PreparingMessage(data.Path); while (www.GetAudioClip(false).loadState != AudioDataLoadState.Loaded) { } AssetManager.LoadedAudioFiles[audioFile] = www.GetAudioClip(); } catch (Exception e) { HandleException(e); yield break; } } foreach (string imageFile in AssetManager.ImageRepository.Files.Keys) { string path; FileData data; try { data = AssetManager.ImageRepository.Files[imageFile]; } catch (Exception e) { HandleException(e); yield break; } yield return(LoadFile(AssetManager.ImageRepository.GetPath(imageFile))); try { PreparingMessage(data.Path); Texture2D tex = www.texture; tex.filterMode = AssetManager.ImageRepository.Files[imageFile].FilterMode; AssetManager.LoadedTextures[imageFile] = tex; if (AssetManager.Config.Cursor.CursorImage == imageFile) { AssetManager.UpdateCursor(); Cursor.visible = true; } } catch (Exception e) { HandleException(e); yield break; } } foreach (string animation in AssetManager.AnimationRepository.Files.Keys) { FileData data; try { AssetManager.LoadedAnimations[animation] = AssetManager.ReadData <Data.Animation>(AssetManager.AnimationRepository.GetPathInModFolder(animation), false); } catch (Exception e) { HandleException(e); yield break; } } loadingScreenHandler.FinishedLoading(); /*try { * text.text = AssetManager.Config.WarningText; * * GameObject controller = new GameObject("BaldiModder"); * controller.AddComponent<GameController>(); * * AssetManager.UpdateCursor(); * Cursor.visible = true; * * gameObject.AddComponent(Master.VersionData.GetType("WarningScreen")); * } catch (Exception e) { * HandleException(e); * yield break; * }*/ }