public async Task <bool> SaveNoteLibrary(NoteLibrary _noteLibrary) { try { XmlSerializer serializer = new XmlSerializer(typeof(NoteLibrary)); StringWriter stringWriter = new StringWriter(); serializer.Serialize(stringWriter, _noteLibrary); string serializedString = stringWriter.ToString(); StorageFile saveFile = await this.saveFolder.CreateFileAsync(this.musicLibraryFile, Windows.Storage.CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(saveFile, serializedString); Debug.WriteLine("success saving punch key library > " + saveFile); return(true); } catch (Exception exc) { Debug.WriteLine("serializtion and saving punch key library failed >" + exc.InnerException.Message.ToString()); return(false); } }
public async Task <NoteLibrary> LoadNoteLibrary() { try { XmlSerializer serializer = new XmlSerializer(typeof(NoteLibrary)); string loadedString = string.Empty; try { StorageFile saveFile = await this.saveFolder.GetFileAsync(this.musicLibraryFile); loadedString = await FileIO.ReadTextAsync(saveFile); Debug.WriteLine("success loading key punch library > " + saveFile); } catch (Exception exc) { Debug.WriteLine("loading keypunch library failed > " + exc.InnerException.Message.ToString()); } if (loadedString != string.Empty) { NoteLibrary loadedNoteLibrary = (NoteLibrary)serializer.Deserialize(new StringReader(loadedString)); return(loadedNoteLibrary); } else { Debug.WriteLine("keypunch unavailable> "); return(null); } } catch (Exception exct) { Debug.WriteLine("keypunch deserialization fail > " + exct.InnerException.Message.ToString()); } return(null); }
public async Task <bool> GenerateNoteSequence(int musicMode) { //load available punching notes PunchKeySotrageManager punchStorageManager = new PunchKeySotrageManager(); NoteLibrary availableNotebrary = await punchStorageManager.LoadNoteLibrary(); List <int>[] noteArray = new List <int>[4] { new List <int>(), new List <int>(), new List <int>(), new List <int>() }; //indexing -> 0 : 1 key----1: 2 key----2: 3 key----3: 4 key foreach (Note note in availableNotebrary.noteLibrary) { int difficulty = note.serPunchingJoints.Count - 1; if (difficulty >= 0 && difficulty < 4) { noteArray[difficulty].Add(note.noteId); } } //difficulty probability array int[] difficultyProbability; PunchKeyMode = musicMode; switch (PunchKeyMode) { case (int)Music.MusicMode.Exercise: difficultyProbability = new int[5] { 50, 35, 10, 5, 65 }; break; case (int)Music.MusicMode.EasyChallenge: difficultyProbability = new int[5] { 65, 35, 5, 0, 60 }; break; case (int)Music.MusicMode.NormalChallenge: difficultyProbability = new int[5] { 35, 50, 10, 5, 45 }; break; case (int)Music.MusicMode.HardChallenge: difficultyProbability = new int[5] { 25, 55, 10, 10, 30 }; break; default: difficultyProbability = new int[5]; break; } //Generate sequence based on probability NoteSequence = new List <int>(); for (int i = 0; i < Definitions.temporaryNoteLenght; i++) { //create distribution function int rnd = fastRandom(i, 1, 100); int cummuativeDistribution = 0; for (int j = 0; j < noteArray.Length; j++) { cummuativeDistribution += difficultyProbability[j]; //kumulatif cuma dari index 0 sampai 3, 1note, 2note, 3note, 4note if (rnd < cummuativeDistribution) { if (j > 2) { NoteSequence.Add(0); } NoteSequence.Add((noteArray[j])[fastRandom(i + j, 0, noteArray[j].Count)]); //random dari daftar note sesuai kumulatif break; } } //create break function rnd = fastRandom(i, 1, 100); if (rnd < difficultyProbability[4]) { NoteSequence.Add(0); } } return(true); }