84 lines
2.8 KiB
C#
84 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Web.Script.Serialization;
|
|
using JD.Asset.Types.Map;
|
|
using JD.Audio.Annotations;
|
|
using JD.Audio.Sound;
|
|
using JD.Collections.Extensions;
|
|
using JD.Timeline.Model;
|
|
using JD.Core;
|
|
using TeaBox;
|
|
using TeaBox.Macros;
|
|
using JD.Serialization;
|
|
using Path = System.IO.Path;
|
|
|
|
namespace Traviata.Source.Macros
|
|
{
|
|
[Module]
|
|
public static class TraviataExportMacros
|
|
{
|
|
[Macro(ThreadAffinity.ExecuteAsync)]
|
|
public static void ExportLyricsClipsToJSON(IExecutionContext context, string mapName, string jsonPath)
|
|
{
|
|
string serialized = GetKTapeFileContentAsJson(Map.FromName(mapName));
|
|
File.WriteAllText(jsonPath, serialized);
|
|
Process.Start(jsonPath);
|
|
}
|
|
|
|
private static string GetKTapeFileContentAsJson(Map map)
|
|
{
|
|
var kTape = (Tape)Serializers.LuaSerializer.LoadObjectFromFile(map.MainMap.KaraokeTapePath);
|
|
var wave = Wave.LoadFromMusicTrackTpl(map.MusicTrack, Wave.LoadingMode.Default);
|
|
|
|
List<KaraokeClip> kClips = kTape.Clips.OfType<KaraokeClip>().OrderBy(c => c.StartBeat).ToList();
|
|
var serializer = new JavaScriptSerializer();
|
|
var kclipInfos = new List<KaraokeClipJsonInfo>();
|
|
|
|
foreach (var kClip in kClips)
|
|
{
|
|
kclipInfos.AddOrMerge(new KaraokeClipJsonInfo(kClip, wave));
|
|
}
|
|
|
|
var content = new Object[] { new { VideoStartTime = wave.Annotations.VideoStartTime.TotalMilliseconds }, kclipInfos };
|
|
|
|
return serializer.Serialize(content);
|
|
}
|
|
|
|
private static void AddOrMerge(this List<KaraokeClipJsonInfo> list, KaraokeClipJsonInfo clipInfo)
|
|
{
|
|
if (list.IsEmpty() || list.Last().IsEndOfLine)
|
|
{
|
|
list.Add(clipInfo);
|
|
return;
|
|
}
|
|
|
|
if (clipInfo.IsEndOfLine)
|
|
{
|
|
list.Last().IsEndOfLine = true;
|
|
}
|
|
|
|
list.Last().StopTime = clipInfo.StopTime;
|
|
list.Last().Lyrics += clipInfo.Lyrics;
|
|
}
|
|
|
|
internal class KaraokeClipJsonInfo
|
|
{
|
|
public string Lyrics { get; set; }
|
|
public double StartTime { get; private set; }
|
|
public double StopTime { get; set; }
|
|
public bool IsEndOfLine { get; set; }
|
|
|
|
public KaraokeClipJsonInfo(KaraokeClip kclip, Wave wave)
|
|
{
|
|
this.Lyrics = kclip.Lyrics;
|
|
this.IsEndOfLine = kclip.IsEndOfLine;
|
|
this.StartTime = wave.UnitConverter.TimeFromBeat(new BeatUnit(kclip.StartBeat)).TotalMilliseconds;
|
|
this.StopTime = wave.UnitConverter.TimeFromBeat(new BeatUnit(kclip.StopBeat)).TotalMilliseconds;
|
|
}
|
|
}
|
|
}
|
|
}
|