119 lines
No EOL
4.6 KiB
C#
119 lines
No EOL
4.6 KiB
C#
using TeaBox.Macros;
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using JD.Audio.Sound;
|
|
using JD.Serialization;
|
|
|
|
namespace JDSongConverter.Tasks
|
|
{
|
|
/// <summary>
|
|
/// Extracts Audio Preview and volume information from the trk file and moves them into the SongDesc.tpl and MusicTrack.tpl, respectively.
|
|
/// Use only until JD2015 Format!
|
|
/// </summary>
|
|
#pragma warning disable 0618 //This warning is about obsolete classes but I need those to convert
|
|
public class ExtractPreviewAndVolumeFromTrkTask : Task
|
|
{
|
|
protected LuaSerializer m_luaSerializer;
|
|
|
|
public override string Name
|
|
{
|
|
get { return "Extract Audio Preview and Volume From Trk"; }
|
|
set { }
|
|
}
|
|
|
|
public override string ProgressText
|
|
{
|
|
get { return "Extracting Audio Preview and Volume From Trk"; }
|
|
}
|
|
|
|
public override TaskResult Process(IExecutionContext executionContext)
|
|
{
|
|
TaskResult progressResult = TaskResult.Ok;
|
|
|
|
if(!Context.OutputMap.IsAlternate) // only classic maps (main maps) have audio preview data
|
|
{
|
|
m_luaSerializer = new LuaSerializer();
|
|
m_luaSerializer.IgnoreDefaultValues = true;
|
|
|
|
var musicTrackTpl = Context.OutputMap.MusicTrack;
|
|
var wave = Wave.LoadFromMusicTrackTpl(musicTrackTpl, Wave.LoadingMode.StructureOnly);
|
|
|
|
int previewStart = (int) wave.Annotations.PreviewEntry;
|
|
int previewStartLoop = (int) wave.Annotations.PreviewLoopStart;
|
|
int previewLoopEnd = (int) wave.Annotations.PreviewLoopEnd;
|
|
float volume = wave.Annotations.Volume;
|
|
|
|
progressResult = SaveAudioPreviewToSongDesc(previewStart, previewStartLoop, previewLoopEnd);
|
|
|
|
if (progressResult == TaskResult.Ok)
|
|
{
|
|
progressResult = SaveVolumeToMusicTrackTpl(volume);
|
|
}
|
|
}
|
|
|
|
return progressResult;
|
|
}
|
|
|
|
private TaskResult SaveAudioPreviewToSongDesc(int previewStart, int previewStartLoop, int previewEndLoop)
|
|
{
|
|
String songDescTplPath = Context.OutputMap.SongDescPath;
|
|
|
|
Actor_Template actorTemplate = m_luaSerializer.LoadObject<Actor_Template>(songDescTplPath);
|
|
JD_SongDescTemplate songDesc = actorTemplate.COMPONENTS.OfType<JD_SongDescTemplate>().FirstOrDefault();
|
|
|
|
if (songDesc == null)
|
|
return TaskResult.Error;
|
|
|
|
if (songDesc.AudioPreviews.Count > 0)
|
|
songDesc.AudioPreviews.Clear();
|
|
|
|
JD_SongDescTemplate.AudioPreview coverflowPreview = new JD_SongDescTemplate.AudioPreview();
|
|
coverflowPreview.Name = "coverflow";
|
|
coverflowPreview.Startbeat = previewStart;
|
|
coverflowPreview.Endbeat = 0; //Not used
|
|
|
|
JD_SongDescTemplate.AudioPreview preLobbyPreview = new JD_SongDescTemplate.AudioPreview();
|
|
preLobbyPreview.Name = "prelobby";
|
|
preLobbyPreview.Startbeat = previewStartLoop;
|
|
preLobbyPreview.Endbeat = previewEndLoop;
|
|
|
|
songDesc.AudioPreviews.Add(coverflowPreview);
|
|
songDesc.AudioPreviews.Add(preLobbyPreview);
|
|
songDesc.AudioPreviewFadeTime = 0.5f;
|
|
|
|
m_luaSerializer.SaveObjectToFile(actorTemplate, songDescTplPath);
|
|
return TaskResult.Ok;
|
|
}
|
|
|
|
private TaskResult SaveVolumeToMusicTrackTpl(float volume)
|
|
{
|
|
String musicTrackCompPath = Context.OutputMap.MusicTrack;
|
|
|
|
String alltext = File.ReadAllText(musicTrackCompPath);
|
|
String replaceText = "";
|
|
|
|
//If volume is already there in template and require updating with new volume
|
|
if (!alltext.Contains("volume"))
|
|
{
|
|
replaceText = "= structure, volume = " + volume;
|
|
alltext = alltext.ReplaceIgnoreCase("= structure", replaceText);
|
|
}
|
|
else
|
|
{
|
|
Actor_Template actorTemplate = m_luaSerializer.LoadObject<Actor_Template>(musicTrackCompPath);
|
|
MusicTrackComponent_Template musicTrackComp = actorTemplate.COMPONENTS.OfType<MusicTrackComponent_Template>().FirstOrDefault();
|
|
|
|
float oldVolume = musicTrackComp.TrackData.Volume;
|
|
|
|
replaceText = "volume = " + volume;
|
|
alltext = alltext.ReplaceIgnoreCase("volume = " + oldVolume, replaceText);
|
|
}
|
|
|
|
File.WriteAllText(musicTrackCompPath, alltext);
|
|
|
|
return TaskResult.Ok;
|
|
}
|
|
|
|
}
|
|
} |