JD2022-TU1/main/tools/framework/JD.Timeline/Mashups/MashupTapeExtensions.cs

67 lines
2.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using JD.Application.Utils;
using JD.Serialization;
namespace JD.Timeline
{
/// <summary>
/// Adds behavior for Tape that are suitable for the Mashups.
/// </summary>
public static class MashupTapeExtensions
{
public static IEnumerable<MashupClip> Blocks(this Tape tape)
{
return tape.Clips.OfType<MashupClip>();
}
/// <summary>
/// Auto generate the blocks from the music information.
/// </summary>
public static void GenerateBlocks(this Tape tape)
{
if (tape.Blocks().Any())
{
return; // Already generated.
}
tape.Tracks.Clear();
tape.Tracks.Add(new TapeTrack { Name = "Signatures" });
var annotations = tape.TryGetWave().Annotations;
var songSections = annotations.SongSections.Where(section => section.Time.EntireValue > 0).ToArray();
int sectionIndex = -1;
int start = 0;
while (start < annotations.EndBeat.EntireValue)
{
int end = start + MashupClip.Length;
int nextSectionStart = sectionIndex + 1 < songSections.Length ? songSections[sectionIndex + 1].Time.EntireValue : annotations.EndBeat.EntireValue + 1;
if (end >= nextSectionStart)
{
sectionIndex++;
end = nextSectionStart - 1;
}
int duration = end - start;
var lastSlot = tape.Clips.LastOrDefault();
if (lastSlot != null && (duration < MashupClip.Length || lastSlot.BeatDuration < MashupClip.Length))
{
lastSlot.BeatDuration = end - lastSlot.StartBeat;
}
else
{
MashupClip clip = tape.Id.EndsWith("btape") ? new VideoClip() as MashupClip : new SlotClip();
clip.StartBeat = start;
clip.BeatDuration = end - start;
clip.Track = tape.Tracks[0];
tape.Clips.Add(clip);
}
start = end;
}
}
}
}