157 lines
6.9 KiB
C#
157 lines
6.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using JD.LogService;
|
|
using JD.Serialization;
|
|
using TeaBox.Macros;
|
|
|
|
namespace JDSongConverter.Tasks
|
|
{
|
|
class ConvertKtapeToTimelineTask : Task
|
|
{
|
|
public string TimelineFile { get; set; }
|
|
public string KaraokeTapeFile { get; set; }
|
|
|
|
public void Init(string timelineFile, string danceTapeFile)
|
|
{
|
|
TimelineFile = timelineFile;
|
|
KaraokeTapeFile = danceTapeFile;
|
|
}
|
|
|
|
public override string Name
|
|
{
|
|
get { return "Convert karaoke tape to timeline"; }
|
|
set { }
|
|
}
|
|
|
|
public override string ProgressText
|
|
{
|
|
get { return "Converting karaoke tape to timeline"; }
|
|
}
|
|
|
|
public override TaskResult Process(IExecutionContext executionContext)
|
|
{
|
|
if (!File.Exists(TimelineFile))
|
|
return TaskResult.FileMissing;
|
|
|
|
if (!File.Exists(KaraokeTapeFile))
|
|
{
|
|
executionContext.LogWarning("Karaoke tape file missing in the source map directory");
|
|
return TaskResult.Ok;
|
|
}
|
|
var tape = Tape.CreateTape(KaraokeTapeFile);
|
|
|
|
Convert(tape, TimelineFile);
|
|
return TaskResult.Ok;
|
|
}
|
|
private void Convert(Tape tape, string TimeLinefileName)
|
|
{
|
|
var serializer = new LuaSerializer();
|
|
var actorTemplate = (Actor_Template)serializer.LoadObjectFromFile(TimeLinefileName);
|
|
if (actorTemplate == null)
|
|
{
|
|
throw new Exception(string.Format("No timeline file {0} not found at the target directory", TimeLinefileName));
|
|
}
|
|
|
|
JD_Timeline_Template timeline = actorTemplate.COMPONENTS.OfType<JD_Timeline_Template>().FirstOrDefault();
|
|
if (timeline == null)
|
|
{
|
|
throw new Exception(string.Format("The timeline file {0} format is wrong", TimeLinefileName));
|
|
}
|
|
|
|
//read the existing layer to get the layer Id for the new layers to be added.
|
|
int layerCount = timeline.Layers.LastOrDefault() == null ? 0 : timeline.Layers.LastOrDefault().LayerID;
|
|
|
|
var lyricsLayer = AddTimelineLyricsLayer(timeline, ref layerCount);
|
|
AddTimelineKaraokeLayer(layerCount, timeline);
|
|
WriteTimelineKaraokeScoringDataParams(tape, timeline);
|
|
|
|
timeline.KaraokeScoringData.Vect = new UAFList<SignalAnnotation>();
|
|
timeline.Lyrics = new UAFList<LyricClip>();
|
|
|
|
foreach (var clip in tape.Clips)
|
|
{
|
|
KaraokeClip kClip = clip as KaraokeClip;
|
|
WriteTimelineKaraokeScoringDataVect(kClip, timeline);
|
|
|
|
if (kClip.Lyrics != string.Empty)
|
|
{
|
|
WriteTimelineLyrics(kClip, lyricsLayer, timeline);
|
|
}
|
|
}
|
|
|
|
actorTemplate.Save(TimeLinefileName);
|
|
}
|
|
|
|
private static void WriteTimelineLyrics(KaraokeClip kClip, TimelineLayer lyricsLayer, JD_Timeline_Template timeline)
|
|
{
|
|
LyricClip lyricsClip = new LyricClip();
|
|
lyricsClip.Text = kClip.Lyrics;
|
|
lyricsClip.LayerID = lyricsLayer.LayerID;
|
|
lyricsClip.StartPosition = kClip.StartBeat;
|
|
lyricsClip.StopPosition = kClip.BeatDuration + kClip.StartBeat;
|
|
lyricsClip.IsLineEnding = (uint) (kClip.IsEndOfLine ? 1 : 0);
|
|
timeline.Lyrics.Add(lyricsClip);
|
|
}
|
|
|
|
private static void WriteTimelineKaraokeScoringDataVect(KaraokeClip kClip, JD_Timeline_Template timeline)
|
|
{
|
|
SignalAnnotation singleAnnotation = new SignalAnnotation();
|
|
singleAnnotation.Pitch = kClip.Pitch;
|
|
singleAnnotation.StartTime = kClip.StartBeat;
|
|
singleAnnotation.EndTime = kClip.BeatDuration + kClip.StartBeat;
|
|
singleAnnotation.Tolerance.StartTime = FrameConverter.TimeFromFrame(kClip.StartTimeTolerance) + kClip.StartBeat;
|
|
singleAnnotation.Tolerance.EndTime = FrameConverter.TimeFromFrame(kClip.EndTimeTolerance) +
|
|
(kClip.BeatDuration + kClip.StartBeat);
|
|
singleAnnotation.Tolerance.SemitoneTolerance = kClip.SemitoneTolerance;
|
|
timeline.KaraokeScoringData.Vect.Add(singleAnnotation);
|
|
}
|
|
|
|
private static void WriteTimelineKaraokeScoringDataParams(Tape tape, JD_Timeline_Template timeline)
|
|
{
|
|
timeline.KaraokeScoringData = new TimelineKaraokeScoringData();
|
|
foreach (var metaInfo in tape.MetaInfos)
|
|
{
|
|
if (metaInfo.GetType() == typeof (KaraokeMetaInfo))
|
|
{
|
|
KaraokeMetaInfo kMetaInfo = metaInfo as KaraokeMetaInfo;
|
|
timeline.KaraokeScoringData.Params.HistorySpan = kMetaInfo.HistorySpan;
|
|
timeline.KaraokeScoringData.Params.NoPitchScoring = kMetaInfo.NoPitchScoring;
|
|
timeline.KaraokeScoringData.Params.PitchLowerBound = kMetaInfo.PitchLowerBound;
|
|
timeline.KaraokeScoringData.Params.PitchUpperBound = kMetaInfo.PitchUpperBound;
|
|
timeline.KaraokeScoringData.Params.RMSTendencyToDecrease = kMetaInfo.RMSTendencyToDecrease;
|
|
timeline.KaraokeScoringData.Params.RMSTendencyToIncrease = kMetaInfo.RMSTendencyToIncrease;
|
|
timeline.KaraokeScoringData.Params.RMSThreshold = kMetaInfo.RMSThreshold;
|
|
timeline.KaraokeScoringData.Params.SlidingMeanVariationThreshold = kMetaInfo.SlidingMeanVariationThreshold;
|
|
timeline.KaraokeScoringData.Params.SlidingMeanWindowSize = kMetaInfo.SlidingMeanWindowSize;
|
|
timeline.KaraokeScoringData.Params.WindowHop = kMetaInfo.WindowHop;
|
|
timeline.KaraokeScoringData.Params.WindowSize = kMetaInfo.WindowSize;
|
|
timeline.KaraokeScoringData.Params.YINpitchThreshold = kMetaInfo.YinPitchTreshold;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void AddTimelineKaraokeLayer(int layerCount, JD_Timeline_Template timeline)
|
|
{
|
|
//Add the KaraokeScroing Layer
|
|
TimelineLayer kLayer = new TimelineLayer();
|
|
kLayer.LayerID = ++layerCount;
|
|
kLayer.LayerPosition = kLayer.LayerID;
|
|
kLayer.LayerName = "KaraokeScoring";
|
|
kLayer.LayerType = (uint) LayerTypes.KaraokeScoring;
|
|
timeline.Layers.Add(kLayer);
|
|
}
|
|
|
|
private static TimelineLayer AddTimelineLyricsLayer(JD_Timeline_Template timeline, ref int layerCount)
|
|
{
|
|
// Add the lyrics layer
|
|
TimelineLayer lyricsLayer = new TimelineLayer();
|
|
lyricsLayer.LayerID = ++layerCount;
|
|
lyricsLayer.LayerPosition = lyricsLayer.LayerID;
|
|
lyricsLayer.LayerName = "Lyrics";
|
|
lyricsLayer.LayerType = (uint) LayerTypes.Lyrics;
|
|
timeline.Layers.Add(lyricsLayer);
|
|
return lyricsLayer;
|
|
}
|
|
}
|
|
}
|