From 1da29464a80d7551d60cfb732acd00ae768c6d2c Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 16 Sep 2020 14:09:02 -0700 Subject: [PATCH] Add writing of cuesheets --- DICUI.Library/CueSheets/CueFile.cs | 51 ++++++++++++- DICUI.Library/CueSheets/CueIndex.cs | 14 +++- DICUI.Library/CueSheets/CueSheet.cs | 46 ++++++++++++ DICUI.Library/CueSheets/CueTrack.cs | 106 +++++++++++++++++++++++++++- DICUI.Library/CueSheets/PostGap.cs | 12 +++- DICUI.Library/CueSheets/PreGap.cs | 12 +++- 6 files changed, 234 insertions(+), 7 deletions(-) diff --git a/DICUI.Library/CueSheets/CueFile.cs b/DICUI.Library/CueSheets/CueFile.cs index 80642e67..db3f2eff 100644 --- a/DICUI.Library/CueSheets/CueFile.cs +++ b/DICUI.Library/CueSheets/CueFile.cs @@ -65,7 +65,7 @@ namespace DICUI.CueSheets } /// - /// Fill a FILE from a stream reader + /// Fill a FILE from an array of lines /// /// File name to set /// File type to set @@ -122,11 +122,29 @@ namespace DICUI.CueSheets } } + /// + /// Write the FILE out to a stream + /// + /// StreamWriter to write to + public void Write(StreamWriter sw) + { + // If we don't have any tracks, it's invalid + if (this.Tracks == null || this.Tracks.Count == 0) + return; // TODO: Make this throw an exception + + sw.WriteLine($"FILE \"{this.FileName}\" {FromFileType(this.FileType)}"); + + foreach (var track in Tracks) + { + track.Write(sw); + } + } + /// /// Get the file type from a given string /// /// String to get value from - /// CueFileType, if possible (default BINARY) + /// CueFileType, if possible private CueFileType GetFileType(string fileType) { switch (fileType.ToLowerInvariant()) @@ -150,5 +168,34 @@ namespace DICUI.CueSheets return CueFileType.BINARY; } } + + /// + /// Get the string from a given file type + /// + /// CueFileType to get value from + /// String, if possible (default BINARY) + private string FromFileType(CueFileType fileType) + { + switch (fileType) + { + case CueFileType.BINARY: + return "BINARY"; + + case CueFileType.MOTOROLA: + return "MOTOROLA"; + + case CueFileType.AIFF: + return "AIFF"; + + case CueFileType.WAVE: + return "WAVE"; + + case CueFileType.MP3: + return "MP3"; + + default: + return string.Empty; + } + } } } diff --git a/DICUI.Library/CueSheets/CueIndex.cs b/DICUI.Library/CueSheets/CueIndex.cs index 58bb17e9..7a5c010b 100644 --- a/DICUI.Library/CueSheets/CueIndex.cs +++ b/DICUI.Library/CueSheets/CueIndex.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.IO; +using System.Linq; /// /// Information sourced from http://web.archive.org/web/20070221154246/http://www.goldenhawk.com/download/cdrwin.pdf @@ -40,7 +41,7 @@ namespace DICUI.CueSheets } /// - /// Fill a INDEX from a stream reader + /// Fill a INDEX from an array of lines /// /// Index to set /// Start time to set @@ -92,5 +93,14 @@ namespace DICUI.CueSheets this.Seconds = lengthSegments[1]; this.Frames = lengthSegments[2]; } + + /// + /// Write the INDEX out to a stream + /// + /// StreamWriter to write to + public void Write(StreamWriter sw) + { + sw.WriteLine($" INDEX {this.Index:D2} {this.Minutes:D2}:{this.Seconds:D2}:{this.Frames:D2}"); + } } } diff --git a/DICUI.Library/CueSheets/CueSheet.cs b/DICUI.Library/CueSheets/CueSheet.cs index e7e71241..dabda876 100644 --- a/DICUI.Library/CueSheets/CueSheet.cs +++ b/DICUI.Library/CueSheets/CueSheet.cs @@ -150,5 +150,51 @@ namespace DICUI.CueSheets } } } + + /// + /// Write the cuesheet out to a file + /// + /// File path to write to + public void Write(string filename) + { + using (var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) + { + Write(fs); + } + } + + /// + /// Write the cuesheet out to a stream + /// + /// Stream to write to + public void Write(Stream stream) + { + // If we don't have any files, it's invalid + if (this.Files == null || this.Files.Count == 0) + return; // TODO: Make this throw an exception + + using (var sw = new StreamWriter(stream)) + { + if (!string.IsNullOrEmpty(this.Catalog)) + sw.WriteLine($"CATALOG {this.Catalog}"); + + if (!string.IsNullOrEmpty(this.CdTextFile)) + sw.WriteLine($"CDTEXTFILE {this.CdTextFile}"); + + if (!string.IsNullOrEmpty(this.Performer)) + sw.WriteLine($"PERFORMER {this.Performer}"); + + if (!string.IsNullOrEmpty(this.Songwriter)) + sw.WriteLine($"SONGWRITER {this.Songwriter}"); + + if (!string.IsNullOrEmpty(this.Title)) + sw.WriteLine($"TITLE {this.Title}"); + + foreach (var file in Files) + { + file.Write(sw); + } + } + } } } diff --git a/DICUI.Library/CueSheets/CueTrack.cs b/DICUI.Library/CueSheets/CueTrack.cs index 7f9b80f4..7158b171 100644 --- a/DICUI.Library/CueSheets/CueTrack.cs +++ b/DICUI.Library/CueSheets/CueTrack.cs @@ -150,7 +150,7 @@ namespace DICUI.CueSheets } /// - /// Fill a TRACK from a stream reader + /// Fill a TRACK from an array of lines /// /// Number to set /// Data type to set @@ -276,6 +276,45 @@ namespace DICUI.CueSheets } } + /// + /// Write the TRACK out to a stream + /// + /// StreamWriter to write to + public void Write(StreamWriter sw) + { + // If we don't have any indices, it's invalid + if (this.Indices == null || this.Indices.Count == 0) + return; // TODO: Make this throw an exception + + sw.WriteLine($" TRACK {this.Number:D2} {FromDataType(this.DataType)}"); + + if (this.Flags != 0) + sw.WriteLine($" FLAGS {FromFlags(this.Flags)}"); + + if (!string.IsNullOrEmpty(this.ISRC)) + sw.WriteLine($"ISRC {this.ISRC}"); + + if (!string.IsNullOrEmpty(this.Performer)) + sw.WriteLine($"PERFORMER {this.Performer}"); + + if (!string.IsNullOrEmpty(this.Songwriter)) + sw.WriteLine($"SONGWRITER {this.Songwriter}"); + + if (!string.IsNullOrEmpty(this.Title)) + sw.WriteLine($"TITLE {this.Title}"); + + if (this.PreGap != null) + this.PreGap.Write(sw); + + foreach (var index in Indices) + { + index.Write(sw); + } + + if (this.PostGap != null) + this.PostGap.Write(sw); + } + /// /// Get the data type from a given string /// @@ -314,6 +353,44 @@ namespace DICUI.CueSheets } } + /// + /// Get the string from a given data type + /// + /// CueTrackDataType to get value from + /// string, if possible + private string FromDataType(CueTrackDataType dataType) + { + switch (dataType) + { + case CueTrackDataType.AUDIO: + return "AUDIO"; + + case CueTrackDataType.CDG: + return "CDG"; + + case CueTrackDataType.MODE1_2048: + return "MODE1/2048"; + + case CueTrackDataType.MODE1_2352: + return "MODE1/2352"; + + case CueTrackDataType.MODE2_2336: + return "MODE2/2336"; + + case CueTrackDataType.MODE2_2352: + return "MODE2/2352"; + + case CueTrackDataType.CDI_2336: + return "CDI/2336"; + + case CueTrackDataType.CDI_2352: + return "CDI/2352"; + + default: + return string.Empty; + } + } + /// /// Get the flag value for an array of strings /// @@ -356,5 +433,32 @@ namespace DICUI.CueSheets return flag; } + + /// + /// Get the string value for a set of track flags + /// + /// CueTrackFlag to get value from + /// String value representing the CueTrackFlag, if possible + private string FromFlags(CueTrackFlag flags) + { + string outputFlagString = string.Empty; + + if (flags.HasFlag(CueTrackFlag.DCP)) + outputFlagString += "DCP "; + + if (flags.HasFlag(CueTrackFlag.FourCH)) + outputFlagString += "4CH "; + + if (flags.HasFlag(CueTrackFlag.PRE)) + outputFlagString += "PRE "; + + if (flags.HasFlag(CueTrackFlag.SCMS)) + outputFlagString += "SCMS "; + + if (flags.HasFlag(CueTrackFlag.DATA)) + outputFlagString += "DATA "; + + return outputFlagString.Trim(); + } } } diff --git a/DICUI.Library/CueSheets/PostGap.cs b/DICUI.Library/CueSheets/PostGap.cs index e5c572b6..b48296da 100644 --- a/DICUI.Library/CueSheets/PostGap.cs +++ b/DICUI.Library/CueSheets/PostGap.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.IO; +using System.Linq; /// /// Information sourced from http://web.archive.org/web/20070221154246/http://www.goldenhawk.com/download/cdrwin.pdf @@ -78,5 +79,14 @@ namespace DICUI.CueSheets this.Seconds = lengthSegments[1]; this.Frames = lengthSegments[2]; } + + /// + /// Write the POSTGAP out to a stream + /// + /// StreamWriter to write to + public void Write(StreamWriter sw) + { + sw.WriteLine($" POSTGAP {this.Minutes:D2}:{this.Seconds:D2}:{this.Frames:D2}"); + } } } diff --git a/DICUI.Library/CueSheets/PreGap.cs b/DICUI.Library/CueSheets/PreGap.cs index 0cd65e42..d1644a02 100644 --- a/DICUI.Library/CueSheets/PreGap.cs +++ b/DICUI.Library/CueSheets/PreGap.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.IO; +using System.Linq; /// /// Information sourced from http://web.archive.org/web/20070221154246/http://www.goldenhawk.com/download/cdrwin.pdf @@ -79,5 +80,14 @@ namespace DICUI.CueSheets this.Seconds = lengthSegments[1]; this.Frames = lengthSegments[2]; } + + /// + /// Write the PREGAP out to a stream + /// + /// StreamWriter to write to + public void Write(StreamWriter sw) + { + sw.WriteLine($" PREGAP {this.Minutes:D2}:{this.Seconds:D2}:{this.Frames:D2}"); + } } }