using System; using System.IO; using System.Linq; using JD.Audio.Annotations; using JD.Audio.Sound; using JD.LogService; using JD.Serialization; using JD.Timeline.Model; using JDSongConverter.Utils; using NAudio.Wave; using TeaBox.Macros; using WaveFileReader = JD.Audio.SoundPlayer.WaveFileReader; namespace JDSongConverter.Tasks { /// /// Repairs a trk file by adding some missing markers at the beginning and at the end /// public class RepairTrkTask : Task { public override string ToString() { return String.Format("Repairing trk file"); } public override string Name { get { return "Repair trk file"; } set { } } public override string ProgressText { get { return "Repairing trk file"; } } public override TaskResult Process(IExecutionContext executionContext) { var wavFile = AppDefinitions.Default.AbsoluteMapPath(MapPathType.WavFile, Context.OutputMap.Folder, Context.Song.Name, JDSongVersion.JD2015, JDFolderLayout.Engine); var trkFile = AppDefinitions.Default.AbsoluteMapPath(MapPathType.TrkFile, Context.OutputMap.Folder, Context.Song.Name, JDSongVersion.JD2015, JDFolderLayout.Engine); //We reapair the trk var wave = new Wave(wavFile); var annotations = new LuaAnnotations(wave); annotations.Load(trkFile); int nbBeatsAddedAtStart; int nbBeatsAddedAtEnd; if (!wave.Annotations.Markers.Any()) { executionContext.LogError("wave file is not valid. It doesn't have any markers."); return TaskResult.Error; } wave.FixMissingMarkers(out nbBeatsAddedAtStart, out nbBeatsAddedAtEnd); if (!wave.Annotations.Signatures.Any()) { wave.Annotations.Signatures.Add(new Signature(new BeatUnit(0),4)); } annotations.Save(trkFile); UpdateWave(wavFile, wave.Annotations, 48000); //We shift var dtapeFile = AppDefinitions.Default.AbsoluteMapPath(MapPathType.DanceTapeFile, Context.OutputMap.Folder, Context.Song.Name, JDSongVersion.JD2015, JDFolderLayout.Engine); var dtape = Tape.CreateTape(dtapeFile); dtape.Clips.ForEach(x => x.StartBeat += nbBeatsAddedAtStart); dtape.Save(dtapeFile); var ktapeFile = AppDefinitions.Default.AbsoluteMapPath(MapPathType.KaraokeTapeFile, Context.OutputMap.Folder, Context.Song.Name, JDSongVersion.JD2015, JDFolderLayout.Engine); var ktape = Tape.CreateTape(ktapeFile); ktape.Clips.ForEach(x => x.StartBeat += nbBeatsAddedAtStart); ktape.Clips.FixCollisions(); ktape.Save(ktapeFile); var mainTapeFile = AppDefinitions.Default.AbsoluteMapPath(MapPathType.MainTape, Context.OutputMap.Folder, Context.Song.Name, JDSongVersion.JD2015, JDFolderLayout.Engine); var mainTape = Tape.CreateTape(mainTapeFile); mainTape.Clips.ForEach(x => x.StartBeat += nbBeatsAddedAtStart); mainTape.Save(mainTapeFile); return TaskResult.Ok; } private static void UpdateWave(string filename, AnnotationCollection annotations, int samplerate) { string tempFile = System.IO.Path.ChangeExtension(filename, ".tmp"); using (var reader = new WaveFileReader(filename)) { var format = new WaveFormat(samplerate, reader.WaveFormat.Channels); using (var writer = new CueWaveFileWriter(tempFile, format)) { int bufferSize = reader.BlockAlign * 1024 * 4; reader.CopyTo(writer, bufferSize); foreach (var marker in annotations.Markers) { writer.AddCue(marker.Sample, marker.Name); } } } File.SetAttributes(filename, FileAttributes.Normal); File.Move(tempFile, filename); } } }