From 8f42f04af253ebd8315320393b0d749143baa57b Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Fri, 14 Oct 2022 09:39:35 -0400 Subject: [PATCH] improvements to JSON parsing and Vocaloid5 VPR reading --- .../JSONDataFormat.cs | 58 +++++++++++++++++-- .../Vocaloid/VPR/VPRProjectZIPDataFormat.cs | 6 +- .../Vocaloid/VPR/VPRSequenceJSONDataFormat.cs | 57 ++++++++++++------ 3 files changed, 98 insertions(+), 23 deletions(-) diff --git a/Libraries/UniversalEditor.Essential/DataFormats/PropertyList/JavaScriptObjectNotation/JSONDataFormat.cs b/Libraries/UniversalEditor.Essential/DataFormats/PropertyList/JavaScriptObjectNotation/JSONDataFormat.cs index 8b805081..6410dd63 100644 --- a/Libraries/UniversalEditor.Essential/DataFormats/PropertyList/JavaScriptObjectNotation/JSONDataFormat.cs +++ b/Libraries/UniversalEditor.Essential/DataFormats/PropertyList/JavaScriptObjectNotation/JSONDataFormat.cs @@ -33,10 +33,9 @@ namespace UniversalEditor.DataFormats.PropertyList.JavaScriptObjectNotation { private class _Context { + public bool CloseGroup { get; set; } = false; public string CurrentStringRaw { get; set; } = String.Empty; - public Property CurrentProperty { get; set; } = null; - public bool InsideArray { get; set; } = false; public System.Collections.Generic.List CurrentList { get; } = new System.Collections.Generic.List(); @@ -93,10 +92,12 @@ namespace UniversalEditor.DataFormats.PropertyList.JavaScriptObjectNotation // we close current object if (ctx.CurrentString != null) { + /* if (ctx.CurrentProperty != null) { ctx.CurrentProperty.Value = ctx.CurrentString; } + */ } } @@ -224,6 +225,7 @@ namespace UniversalEditor.DataFormats.PropertyList.JavaScriptObjectNotation else if (c == '}' || c == ',') { // could be null? + ctx.CloseGroup = (c == '}'); if (ctx.CurrentStringRaw == "null") { r.Seek(-1, SeekOrigin.Current); @@ -242,6 +244,40 @@ namespace UniversalEditor.DataFormats.PropertyList.JavaScriptObjectNotation ctx.CurrentStringRaw = String.Empty; return false; } + else + { + object value = null; + if (Byte.TryParse(ctx.CurrentStringRaw, out byte r_Byte)) + { + value = r_Byte; + } + else if (Int16.TryParse(ctx.CurrentStringRaw, out short r_Int16)) + { + value = r_Int16; + } + else if (Int32.TryParse(ctx.CurrentStringRaw, out int r_Int32)) + { + value = r_Int32; + } + else if (Int64.TryParse(ctx.CurrentStringRaw, out long r_Int64)) + { + value = r_Int64; + } + else if (Single.TryParse(ctx.CurrentStringRaw, out float r_Single)) + { + value = r_Single; + } + else if (Double.TryParse(ctx.CurrentStringRaw, out double r_Double)) + { + value = r_Double; + } + else if (Decimal.TryParse(ctx.CurrentStringRaw, out decimal r_Decimal)) + { + value = r_Decimal; + } + ctx.CurrentStringRaw = String.Empty; + return value; + } } else { @@ -280,9 +316,23 @@ namespace UniversalEditor.DataFormats.PropertyList.JavaScriptObjectNotation ctx.CurrentString = String.Empty; object obj = ReadNextObject(ctx, r); + if (obj is Group) + { + Group g2 = (obj as Group); + g2.Name = propertyName; + g.Items.Add(g2); + } + else + { + Property p = new Property(propertyName, obj); + g.Items.Add(p); + } + } - Property p = new Property(propertyName, obj); - g.Items.Add(p); + if (ctx.CloseGroup) + { + ctx.CloseGroup = false; + break; } } return g; diff --git a/Plugins/UniversalEditor.Plugins.Vocaloid/DataFormats/Multimedia/Audio/Synthesized/Vocaloid/VPR/VPRProjectZIPDataFormat.cs b/Plugins/UniversalEditor.Plugins.Vocaloid/DataFormats/Multimedia/Audio/Synthesized/Vocaloid/VPR/VPRProjectZIPDataFormat.cs index e0c7e4eb..d872a020 100644 --- a/Plugins/UniversalEditor.Plugins.Vocaloid/DataFormats/Multimedia/Audio/Synthesized/Vocaloid/VPR/VPRProjectZIPDataFormat.cs +++ b/Plugins/UniversalEditor.Plugins.Vocaloid/DataFormats/Multimedia/Audio/Synthesized/Vocaloid/VPR/VPRProjectZIPDataFormat.cs @@ -67,12 +67,11 @@ namespace UniversalEditor.Plugins.Vocaloid.DataFormats.Multimedia.Audio.Synthesi else { // huh? + throw new ObjectModelNotSupportedException(); } } protected override void BeforeSaveInternal(Stack objectModels) { - base.BeforeSaveInternal(objectModels); - ObjectModel whatever = objectModels.Pop(); FileSystemObjectModel fsom = new FileSystemObjectModel(); @@ -89,8 +88,11 @@ namespace UniversalEditor.Plugins.Vocaloid.DataFormats.Multimedia.Audio.Synthesi else { // huh? + throw new ObjectModelNotSupportedException(); } objectModels.Push(fsom); + + base.BeforeSaveInternal(objectModels); } } } diff --git a/Plugins/UniversalEditor.Plugins.Vocaloid/DataFormats/Multimedia/Audio/Synthesized/Vocaloid/VPR/VPRSequenceJSONDataFormat.cs b/Plugins/UniversalEditor.Plugins.Vocaloid/DataFormats/Multimedia/Audio/Synthesized/Vocaloid/VPR/VPRSequenceJSONDataFormat.cs index bbcae1d5..05c51023 100644 --- a/Plugins/UniversalEditor.Plugins.Vocaloid/DataFormats/Multimedia/Audio/Synthesized/Vocaloid/VPR/VPRSequenceJSONDataFormat.cs +++ b/Plugins/UniversalEditor.Plugins.Vocaloid/DataFormats/Multimedia/Audio/Synthesized/Vocaloid/VPR/VPRSequenceJSONDataFormat.cs @@ -55,49 +55,72 @@ namespace UniversalEditor.Plugins.Vocaloid.DataFormats.Multimedia.Audio.Synthesi PropertyListObjectModel plom = (objectModels.Pop() as PropertyListObjectModel); SynthesizedAudioObjectModel vsq = (objectModels.Pop() as SynthesizedAudioObjectModel); + if (plom.Items.Count < 0) + throw new InvalidDataFormatException(); + Group g = (plom.Items[0] as Group); Group gVersion = g.Items.OfType("version"); Property pTracks = g.Items.OfType("tracks"); - Group[] gTracks = pTracks.Value as Group[]; + object[] gTracks = pTracks.Value as object[]; if (gTracks == null) throw new InvalidDataFormatException("tracks is not a Group[]"); for (int i = 0; i < gTracks.Length; i++) { + Group gTrack = gTracks[i] as Group; + if (gTrack == null) throw new InvalidDataFormatException("gTrack is not a Group"); + SynthesizedAudioTrack track = new SynthesizedAudioTrack(); - Property pParts = gTracks[i].Items.OfType("parts"); - Group[] gParts = pParts.Value as Group[]; + Property pParts = gTrack.Items.OfType("parts"); + object[] gParts = pParts?.Value as object[]; if (gParts == null) throw new InvalidDataFormatException("parts is not a Group[]"); for (int j = 0; j < gParts.Length; j++) { - Property pNotes = gParts[j].Items.OfType("notes"); - Group[] gNotes = pNotes.Value as Group[]; - if (gNotes == null) throw new InvalidDataFormatException("notes is not a Group[]"); + object[] pNotes = ((gParts[j] as Group).Items?.OfType("notes"))?.Value as object[]; + if (pNotes == null) throw new InvalidDataFormatException("notes"); - for (int k = 0; k < gNotes.Length; k++) + for (int k = 0; k < pNotes.Length; k++) { + Group gNote = pNotes[k] as Group; + SynthesizedAudioCommandNote note = new SynthesizedAudioCommandNote(); - Property pLyric = gNotes[k].Items.OfType("lyric"); - Property pPhoneme = gNotes[k].Items.OfType("phoneme"); - Property pIsProtected = gNotes[k].Items.OfType("isProtected"); - Property pPos = gNotes[k].Items.OfType("pos"); - Property pDuration = gNotes[k].Items.OfType("duration"); - Property pNumber = gNotes[k].Items.OfType("number"); - Property pVelocity = gNotes[k].Items.OfType("velocity"); + Property pLyric = gNote.Items.OfType("lyric"); + note.Lyric = (string)pLyric.Value; - Group gExp = gNotes[k].Items.OfType("exp"); + Property pPhoneme = gNote.Items.OfType("phoneme"); + note.Phoneme = (string)pPhoneme.Value; + + Property pIsProtected = gNote.Items.OfType("isProtected"); + note.Protected = (bool)pIsProtected.Value; + + Property pPos = gNote.Items.OfType("pos"); + // FIXME: we can't take object (short) and convert using (int) + note.Position = Convert.ToInt32(pPos.Value); + + Property pDuration = gNote.Items.OfType("duration"); + note.Length = Convert.ToInt32(pDuration.Value); + + Property pNumber = gNote.Items.OfType("number"); + note.Frequency = Convert.ToInt32(pNumber.Value); + + Property pVelocity = gNote.Items.OfType("velocity"); + + Group gExp = gNote.Items.OfType("exp"); Property pExpOpening = gExp.Items.OfType("opening"); + note.Opening = Convert.ToInt32(pExpOpening.Value); - Group dvqm = gNotes[k].Items.OfType("dvqm"); + Group dvqm = gNote.Items.OfType("dvqm"); Group dvqmRelease = dvqm.Items.OfType("release"); Property dvqmReleaseCompID = dvqmRelease.Items.OfType("compID"); - Group gVibrato = gNotes[k].Items.OfType("vibrato"); + Group gVibrato = gNote.Items.OfType("vibrato"); Property pVibratoType = gVibrato.Items.OfType("type"); + note.VibratoType = (SynthesizedAudioVibratoType) Convert.ToInt32(pVibratoType.Value); Property pVibratoDuration = gVibrato.Items.OfType("duration"); + note.VibratoLength = Convert.ToInt32(pVibratoDuration.Value); track.Commands.Add(note); }