From 742edc60a8c602dbb7c6948df3668edf22b2741c Mon Sep 17 00:00:00 2001 From: NeoCoderMatrix86 <40752681+NeoCoderMatrix86@users.noreply.github.com> Date: Fri, 19 Apr 2024 09:38:57 +0200 Subject: [PATCH] export file generation now works with sections --- .../Model/IO/Export/ExportfileGenerator.cs | 101 +++++------------- .../IO/Export/ExportfileGeneratorTests.cs | 50 ++++----- 2 files changed, 51 insertions(+), 100 deletions(-) diff --git a/AudioCuesheetEditor/Model/IO/Export/ExportfileGenerator.cs b/AudioCuesheetEditor/Model/IO/Export/ExportfileGenerator.cs index 36ee3d6a..a587558e 100644 --- a/AudioCuesheetEditor/Model/IO/Export/ExportfileGenerator.cs +++ b/AudioCuesheetEditor/Model/IO/Export/ExportfileGenerator.cs @@ -44,65 +44,36 @@ public IReadOnlyCollection GenerateExportfiles() { if (Cuesheet.Sections.Count != 0) { - //TODO: Generate only splitpoint content (starting from begin to end) - TimeSpan? previousSplitPointMoment = null; - var begin = Cuesheet.Tracks.Min(x => x.Begin); var counter = 1; String? content = null; String filename = String.Empty; String? audioFileName = null; - foreach (var splitPoint in Cuesheet.Sections.OrderBy(x => x.Begin)) + foreach (var section in Cuesheet.Sections.OrderBy(x => x.Begin)) { - audioFileName = splitPoint.AudiofileName; - if (splitPoint.Validate().Status == ValidationStatus.Success) + audioFileName = section.AudiofileName; + if (section.Validate().Status == ValidationStatus.Success) { switch (ExportType) { case ExportType.Cuesheet: - content = WriteCuesheet(audioFileName, previousSplitPointMoment, splitPoint); + content = WriteCuesheet(audioFileName, section); filename = String.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(ApplicationOptions?.CuesheetFilename), counter, Cuesheet.FileExtension); break; case ExportType.Exportprofile: if (Exportprofile != null) { - content = WriteExport(audioFileName, previousSplitPointMoment, splitPoint); + content = WriteExport(audioFileName, section); filename = String.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(Exportprofile.Filename), counter, Path.GetExtension(Exportprofile.Filename)); } break; } if (content != null) { - if (previousSplitPointMoment != null) - { - begin = previousSplitPointMoment; - } - exportfiles.Add(new Exportfile() { Name = filename, Content = Encoding.UTF8.GetBytes(content), Begin = begin, End = splitPoint.Begin }); + exportfiles.Add(new Exportfile() { Name = filename, Content = Encoding.UTF8.GetBytes(content), Begin = section.Begin, End = section.End }); } - previousSplitPointMoment = splitPoint.Begin; counter++; } } - //After a split point attach the last part - audioFileName = String.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(Cuesheet.Audiofile?.Name), counter, Path.GetExtension(Cuesheet.Audiofile?.Name)); - switch (ExportType) - { - case ExportType.Cuesheet: - content = WriteCuesheet(audioFileName, previousSplitPointMoment); - filename = String.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(ApplicationOptions?.CuesheetFilename), counter, Cuesheet.FileExtension); - break; - case ExportType.Exportprofile: - if (Exportprofile != null) - { - content = WriteExport(audioFileName, previousSplitPointMoment); - filename = String.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(Exportprofile.Filename), counter, Path.GetExtension(Exportprofile.Filename)); - } - break; - } - if (content != null) - { - var end = Cuesheet.Tracks.Max(x => x.End); - exportfiles.Add(new Exportfile() { Name = filename, Content = Encoding.UTF8.GetBytes(content), Begin = previousSplitPointMoment, End = end }); - } } else { @@ -147,7 +118,7 @@ public IReadOnlyCollection GenerateExportfiles() return exportfiles; } - private string WriteCuesheet(String? audiofileName, TimeSpan? from = null, CuesheetSection? splitPoint = null) + private string WriteCuesheet(String? audiofileName, CuesheetSection? section = null) { var builder = new StringBuilder(); if (Cuesheet.Cataloguenumber != null && string.IsNullOrEmpty(Cuesheet.Cataloguenumber.Value) == false && Cuesheet.Cataloguenumber.Validate().Status != ValidationStatus.Error) @@ -158,24 +129,13 @@ private string WriteCuesheet(String? audiofileName, TimeSpan? from = null, Cuesh { builder.AppendLine(string.Format("{0} \"{1}\"", CuesheetConstants.CuesheetCDTextfile, Cuesheet.CDTextfile.Name)); } - builder.AppendLine(string.Format("{0} \"{1}\"", CuesheetConstants.CuesheetTitle, splitPoint != null ? splitPoint.Title : Cuesheet.Title)); - builder.AppendLine(string.Format("{0} \"{1}\"", CuesheetConstants.CuesheetArtist, splitPoint != null ? splitPoint.Artist : Cuesheet.Artist)); + builder.AppendLine(string.Format("{0} \"{1}\"", CuesheetConstants.CuesheetTitle, section != null ? section.Title : Cuesheet.Title)); + builder.AppendLine(string.Format("{0} \"{1}\"", CuesheetConstants.CuesheetArtist, section != null ? section.Artist : Cuesheet.Artist)); builder.AppendLine(string.Format("{0} \"{1}\" {2}", CuesheetConstants.CuesheetFileName, audiofileName, Cuesheet.Audiofile?.AudioFileType)); IEnumerable tracks = Cuesheet.Tracks.OrderBy(x => x.Position); - if (from != null && splitPoint != null) - { - tracks = Cuesheet.Tracks.Where(x => x.Begin <= splitPoint.Begin && x.End >= from).OrderBy(x => x.Position); - } - else + if (section != null) { - if (from != null) - { - tracks = Cuesheet.Tracks.Where(x => x.End >= from).OrderBy(x => x.Position); - } - if (splitPoint != null) - { - tracks = Cuesheet.Tracks.Where(x => x.Begin <= splitPoint.Begin).OrderBy(x => x.Position); - } + tracks = Cuesheet.Tracks.Where(x => x.Begin <= section.End && x.End >= section.Begin).OrderBy(x => x.Position); } if (tracks.Any()) { @@ -197,15 +157,15 @@ private string WriteCuesheet(String? audiofileName, TimeSpan? from = null, Cuesh if (track.Begin.HasValue) { var begin = track.Begin.Value; - if (from != null) + if ((section != null) && (section.Begin.HasValue)) { - if (from >= track.Begin) + if (section.Begin >= track.Begin) { begin = TimeSpan.Zero; } else { - begin = track.Begin.Value - from.Value; + begin = track.Begin.Value - section.Begin.Value; } } builder.AppendLine(string.Format("{0}{1}{2} {3:00}:{4:00}:{5:00}", CuesheetConstants.Tab, CuesheetConstants.Tab, CuesheetConstants.TrackIndex01, Math.Floor(begin.TotalMinutes), begin.Seconds, begin.Milliseconds * 75 / 1000)); @@ -223,14 +183,14 @@ private string WriteCuesheet(String? audiofileName, TimeSpan? from = null, Cuesh return builder.ToString(); } - private String WriteExport(String? audiofileName, TimeSpan? from = null, CuesheetSection? splitPoint = null) + private String WriteExport(String? audiofileName, CuesheetSection? section = null) { var builder = new StringBuilder(); if (Exportprofile != null) { var header = Exportprofile.SchemeHead - .Replace(Exportprofile.SchemeCuesheetArtist, splitPoint != null ? splitPoint.Artist : Cuesheet.Artist) - .Replace(Exportprofile.SchemeCuesheetTitle, splitPoint != null ? splitPoint.Title : Cuesheet.Title) + .Replace(Exportprofile.SchemeCuesheetArtist, section != null ? section.Artist : Cuesheet.Artist) + .Replace(Exportprofile.SchemeCuesheetTitle, section != null ? section.Title : Cuesheet.Title) .Replace(Exportprofile.SchemeCuesheetAudiofile, audiofileName) .Replace(Exportprofile.SchemeCuesheetCDTextfile, Cuesheet.CDTextfile?.Name) .Replace(Exportprofile.SchemeCuesheetCatalogueNumber, Cuesheet.Cataloguenumber?.Value) @@ -239,20 +199,9 @@ private String WriteExport(String? audiofileName, TimeSpan? from = null, Cueshee .Replace(Exportprofile.SchemeTime, DateTime.Now.ToLongTimeString()); builder.AppendLine(header); IEnumerable tracks = Cuesheet.Tracks.OrderBy(x => x.Position); - if (from != null && splitPoint != null) + if (section != null) { - tracks = Cuesheet.Tracks.Where(x => x.Begin <= splitPoint.Begin && x.End >= from).OrderBy(x => x.Position); - } - else - { - if (from != null) - { - tracks = Cuesheet.Tracks.Where(x => x.End >= from).OrderBy(x => x.Position); - } - if (splitPoint != null) - { - tracks = Cuesheet.Tracks.Where(x => x.Begin <= splitPoint.Begin).OrderBy(x => x.Position); - } + tracks = Cuesheet.Tracks.Where(x => x.Begin <= section.End && x.End >= section.Begin).OrderBy(x => x.Position); } if (tracks.Any()) { @@ -265,17 +214,17 @@ private String WriteExport(String? audiofileName, TimeSpan? from = null, Cueshee if (track.Begin.HasValue) { begin = track.Begin.Value; - if (from != null) + if (section?.Begin != null) { - if (from >= track.Begin) + if (section.Begin >= track.Begin) { begin = TimeSpan.Zero; } else { - begin = track.Begin.Value - from.Value; + begin = track.Begin.Value - section.Begin.Value; } - end = track.End - from.Value; + end = track.End - section.Begin.Value; } } else @@ -299,8 +248,8 @@ private String WriteExport(String? audiofileName, TimeSpan? from = null, Cueshee } } var footer = Exportprofile.SchemeFooter - .Replace(Exportprofile.SchemeCuesheetArtist, splitPoint != null ? splitPoint.Artist : Cuesheet.Artist) - .Replace(Exportprofile.SchemeCuesheetTitle, splitPoint != null ? splitPoint.Title : Cuesheet.Title) + .Replace(Exportprofile.SchemeCuesheetArtist, section != null ? section.Artist : Cuesheet.Artist) + .Replace(Exportprofile.SchemeCuesheetTitle, section != null ? section.Title : Cuesheet.Title) .Replace(Exportprofile.SchemeCuesheetAudiofile, audiofileName) .Replace(Exportprofile.SchemeCuesheetCDTextfile, Cuesheet.CDTextfile?.Name) .Replace(Exportprofile.SchemeCuesheetCatalogueNumber, Cuesheet.Cataloguenumber?.Value) diff --git a/AudioCuesheetEditorTests/Model/IO/Export/ExportfileGeneratorTests.cs b/AudioCuesheetEditorTests/Model/IO/Export/ExportfileGeneratorTests.cs index 433c998d..97653e63 100644 --- a/AudioCuesheetEditorTests/Model/IO/Export/ExportfileGeneratorTests.cs +++ b/AudioCuesheetEditorTests/Model/IO/Export/ExportfileGeneratorTests.cs @@ -303,38 +303,39 @@ public void GenerateCuesheetFilesWithSectionsTest() cuesheet.AddTrack(track, testHelper.ApplicationOptions); } var section = cuesheet.AddSection(); - section.Begin = new TimeSpan(2, 0, 0); + section.Begin = new TimeSpan(1, 30, 0); + section.End = new TimeSpan(2, 0, 0); section.Title = "Last part"; section.AudiofileName = "Last part.mp3"; section = cuesheet.AddSection(); - section.Begin = new TimeSpan(0, 30, 0); + section.Begin = new TimeSpan(0, 0, 0); + section.End = new TimeSpan(0, 30, 0); section.AudiofileName = "First part.mp3"; section = cuesheet.AddSection(); - section.Begin = new TimeSpan(1, 0, 0); + section.Begin = new TimeSpan(0, 30, 0); + section.End = new TimeSpan(1, 0, 0); section.Artist = "Demo Artist Part2"; section = cuesheet.AddSection(); section.Artist = "Artist 3"; section.Title = "Title 3"; section.AudiofileName = "Part 3.mp3"; - section.Begin = new TimeSpan(1, 30, 0); + section.Begin = new TimeSpan(1, 0, 0); + section.End = new TimeSpan(1, 30, 0); testHelper.ApplicationOptions.CuesheetFilename = "Unit test.cue"; var generator = new ExportfileGenerator(ExportType.Cuesheet, cuesheet, applicationOptions: testHelper.ApplicationOptions); Assert.AreEqual(ValidationStatus.Success, generator.Validate().Status); var generatedFiles = generator.GenerateExportfiles(); - Assert.AreEqual(5, generatedFiles.Count); + Assert.AreEqual(4, generatedFiles.Count); var position = 1; var counter = 1; //Check split according to sections - Assert.AreEqual(cuesheet.Tracks.Min(x => x.Begin), generatedFiles.First().Begin); + Assert.AreEqual(new TimeSpan(0, 0, 0), generatedFiles.First().Begin); Assert.AreEqual(new TimeSpan(0, 30, 0), generatedFiles.First().End); - Assert.AreEqual(new TimeSpan(0, 30, 0), generatedFiles.ElementAt(1).Begin); Assert.AreEqual(new TimeSpan(1, 0, 0), generatedFiles.ElementAt(1).End); Assert.AreEqual(new TimeSpan(1, 0, 0), generatedFiles.ElementAt(2).Begin); Assert.AreEqual(new TimeSpan(1, 30, 0), generatedFiles.ElementAt(2).End); - Assert.AreEqual(new TimeSpan(1, 30, 0), generatedFiles.ElementAt(3).Begin); - Assert.AreEqual(new TimeSpan(2, 0, 0), generatedFiles.ElementAt(3).End); - Assert.AreEqual(new TimeSpan(2, 0, 0), generatedFiles.ElementAt(4).Begin); - Assert.AreEqual(cuesheet.Tracks.Max(x => x.End), generatedFiles.Last().End); + Assert.AreEqual(new TimeSpan(1, 30, 0), generatedFiles.Last().Begin); + Assert.AreEqual(new TimeSpan(2, 0, 0), generatedFiles.Last().End); foreach (var generatedFile in generatedFiles) { Assert.AreEqual(String.Format("Unit test({0}).cue", counter), generatedFile.Name); @@ -347,7 +348,7 @@ public void GenerateCuesheetFilesWithSectionsTest() File.Delete(fileName); int positionDifference = 1 - position; // Check cuesheet header for artist and title - var sectionForThisFile = cuesheet.Sections.FirstOrDefault(x => x.Begin == generatedFile.End); + var sectionForThisFile = cuesheet.Sections.FirstOrDefault(x => x.Begin == generatedFile.Begin); if (sectionForThisFile != null) { Assert.AreEqual(String.Format("{0} \"{1}\"", CuesheetConstants.CuesheetTitle, sectionForThisFile.Title), fileContent.First()); @@ -628,20 +629,24 @@ public void GenerateExportfilesWithSectionsTest() cuesheet.Cataloguenumber.Value = "0123456789123"; cuesheet.CDTextfile = new CDTextfile("Testfile.cdt"); var section = cuesheet.AddSection(); - section.Begin = new TimeSpan(2, 0, 0); + section.Begin = new TimeSpan(1, 30, 0); + section.End = new TimeSpan(2, 0, 0); section.Title = "Last part"; section.AudiofileName = "Last part.mp3"; section = cuesheet.AddSection(); - section.Begin = new TimeSpan(0, 30, 0); + section.Begin = new TimeSpan(0, 0, 0); + section.End = new TimeSpan(0, 30, 0); section.AudiofileName = "First part.mp3"; section = cuesheet.AddSection(); - section.Begin = new TimeSpan(1, 0, 0); + section.Begin = new TimeSpan(0, 30, 0); + section.End = new TimeSpan(1, 0, 0); section.Artist = "Demo Artist Part2"; section = cuesheet.AddSection(); section.Artist = "Artist 3"; section.Title = "Title 3"; section.AudiofileName = "Part 3.mp3"; - section.Begin = new TimeSpan(1, 30, 0); + section.Begin = new TimeSpan(1, 0, 0); + section.End = new TimeSpan(1, 30, 0); //Test export var exportProfile = new Exportprofile { @@ -652,19 +657,16 @@ public void GenerateExportfilesWithSectionsTest() var generator = new ExportfileGenerator(ExportType.Exportprofile, cuesheet, exportProfile); Assert.AreEqual(ValidationStatus.Success, generator.Validate().Status); var generatedFiles = generator.GenerateExportfiles(); - Assert.AreEqual(5, generatedFiles.Count); + Assert.AreEqual(4, generatedFiles.Count); //Check split according to sections - Assert.AreEqual(cuesheet.Tracks.Min(x => x.Begin), generatedFiles.First().Begin); + Assert.AreEqual(new TimeSpan(0, 0, 0), generatedFiles.First().Begin); Assert.AreEqual(new TimeSpan(0, 30, 0), generatedFiles.First().End); - Assert.AreEqual(new TimeSpan(0, 30, 0), generatedFiles.ElementAt(1).Begin); Assert.AreEqual(new TimeSpan(1, 0, 0), generatedFiles.ElementAt(1).End); Assert.AreEqual(new TimeSpan(1, 0, 0), generatedFiles.ElementAt(2).Begin); Assert.AreEqual(new TimeSpan(1, 30, 0), generatedFiles.ElementAt(2).End); - Assert.AreEqual(new TimeSpan(1, 30, 0), generatedFiles.ElementAt(3).Begin); - Assert.AreEqual(new TimeSpan(2, 0, 0), generatedFiles.ElementAt(3).End); - Assert.AreEqual(new TimeSpan(2, 0, 0), generatedFiles.ElementAt(4).Begin); - Assert.AreEqual(cuesheet.Tracks.Max(x => x.End), generatedFiles.Last().End); + Assert.AreEqual(new TimeSpan(1, 30, 0), generatedFiles.Last().Begin); + Assert.AreEqual(new TimeSpan(2, 0, 0), generatedFiles.Last().End); var counter = 1; var position = 1; foreach (var generatedFile in generatedFiles) @@ -679,7 +681,7 @@ public void GenerateExportfilesWithSectionsTest() File.Delete(fileName); int positionDifference = 1 - position; //Check cuesheet header for artist and title - var sectionForThisFile = cuesheet.Sections.FirstOrDefault(x => x.Begin == generatedFile.End); + var sectionForThisFile = cuesheet.Sections.FirstOrDefault(x => x.Begin == generatedFile.Begin); if (sectionForThisFile != null) { Assert.AreEqual(String.Format("{0};{1};{2};0123456789123;Testfile.cdt", sectionForThisFile.Artist, sectionForThisFile.Title, sectionForThisFile.AudiofileName), fileContent[0]);