public void Run() { Result = new DiscStructure(); Result.Sessions.Add(new Session()); Track lastTrack = null; for (int tnum = TOCRaw.FirstRecordedTrackNumber; tnum <= TOCRaw.LastRecordedTrackNumber; tnum++) { var ti = TOCRaw.TOCItems[tnum]; var track = new Track(); Result.Sessions[0].Tracks.Add(track); track.Number = tnum; track.Control = ti.Control; track.TrackType = ETrackType.Unknown; //not known yet track.Start_ABA = ti.LBATimestamp.Sector + 150; if (lastTrack != null) { lastTrack.LengthInSectors = track.Start_ABA - lastTrack.Start_ABA; } lastTrack = track; } if (lastTrack != null) { lastTrack.LengthInSectors = (TOCRaw.LeadoutTimestamp.Sector + 150) - lastTrack.Start_ABA; } //total length of the disc is counted up to the leadout, including the lead-in. //i guess supporting storing the leadout is future work. Result.LengthInSectors = TOCRaw.LeadoutTimestamp.Sector; }
/// <exception cref="InvalidOperationException">first track of <see cref="TOCRaw"/> is not <c>1</c></exception> public void Run() { var dsr = new DiscSectorReader(IN_Disc) { Policy = { DeterministicClearBuffer = false } }; Result = new DiscStructure(); var session = new DiscStructure.Session(); Result.Sessions.Add(null); //placeholder session for reindexing Result.Sessions.Add(session); session.Number = 1; if (TOCRaw.FirstRecordedTrackNumber != 1) { throw new InvalidOperationException($"Unsupported: {nameof(TOCRaw.FirstRecordedTrackNumber)} != 1"); } //add a lead-in track session.Tracks.Add(new DiscStructure.Track { Number = 0, Control = EControlQ.None, //we'll set this later LBA = -new Timestamp(99, 99, 99).Sector //obvious garbage }); int ntracks = TOCRaw.LastRecordedTrackNumber - TOCRaw.FirstRecordedTrackNumber + 1; for (int i = 0; i < ntracks; i++) { var item = TOCRaw.TOCItems[i + 1]; var track = new DiscStructure.Track { Number = i + 1, Control = item.Control, LBA = item.LBA }; session.Tracks.Add(track); if (!item.IsData) { track.Mode = 0; } else { //determine the mode by a hardcoded heuristic: check mode of first sector track.Mode = dsr.ReadLBA_Mode(track.LBA); } //determine track length according to... how? It isn't clear. //Let's not do this until it's needed. //if (i == ntracks - 1) // track.Length = TOCRaw.LeadoutLBA.Sector - track.LBA; //else track.Length = (TOCRaw.TOCItems[i + 2].LBATimestamp.Sector - track.LBA); } //add lead-out track session.Tracks.Add(new DiscStructure.Track { Number = 0xA0, //right? //kind of a guess, but not completely Control = session.Tracks[session.Tracks.Count - 1].Control, Mode = session.Tracks[session.Tracks.Count - 1].Mode, LBA = TOCRaw.LeadoutLBA }); //link track list for (int i = 0; i < session.Tracks.Count - 1; i++) { session.Tracks[i].NextTrack = session.Tracks[i + 1]; } //fix lead-in track type //guesses: session.Tracks[0].Control = session.Tracks[1].Control; session.Tracks[0].Mode = session.Tracks[1].Mode; }
public void Run() { var dsr = new DiscSectorReader(IN_Disc); dsr.Policy.DeterministicClearBuffer = false; Result = new DiscStructure(); var session = new DiscStructure.Session(); Result.Sessions.Add(null); //placeholder session for reindexing Result.Sessions.Add(session); session.Number = 1; if (TOCRaw.FirstRecordedTrackNumber != 1) throw new InvalidOperationException("Unsupported: FirstRecordedTrackNumber != 1"); //add a lead-in track session.Tracks.Add(new DiscStructure.Track() { Number = 0, Control = EControlQ.None, //we'll set this later LBA = -new Timestamp(99,99,99).Sector //obvious garbage }); int ntracks = TOCRaw.LastRecordedTrackNumber - TOCRaw.FirstRecordedTrackNumber + 1; for (int i = 0; i < ntracks; i++) { var item = TOCRaw.TOCItems[i + 1]; var track = new DiscStructure.Track() { Number = i + 1, Control = item.Control, LBA = item.LBA }; session.Tracks.Add(track); if (!item.IsData) track.Mode = 0; else { //determine the mode by a hardcoded heuristic: check mode of first sector track.Mode = dsr.ReadLBA_Mode(track.LBA); } //determine track length according to... how? It isn't clear. //Let's not do this until it's needed. //if (i == ntracks - 1) // track.Length = TOCRaw.LeadoutLBA.Sector - track.LBA; //else track.Length = (TOCRaw.TOCItems[i + 2].LBATimestamp.Sector - track.LBA); } //add lead-out track session.Tracks.Add(new DiscStructure.Track() { Number = 0xA0, //right? //kind of a guess, but not completely Control = session.Tracks[session.Tracks.Count -1 ].Control, Mode = session.Tracks[session.Tracks.Count - 1].Mode, LBA = TOCRaw.LeadoutLBA }); //link track list for (int i = 0; i < session.Tracks.Count - 1; i++) { session.Tracks[i].NextTrack = session.Tracks[i + 1]; } //fix lead-in track type //guesses: session.Tracks[0].Control = session.Tracks[1].Control; session.Tracks[0].Mode = session.Tracks[1].Mode; }
/// <summary> /// Generates the CUE file for the provided DiscStructure /// </summary> public string GenerateCUE_OneBin(DiscStructure structure, CueBinPrefs prefs) { if (prefs.OneBlobPerTrack) { throw new InvalidOperationException("OneBinPerTrack passed to GenerateCUE_OneBin"); } //this generates a single-file cue!!!!!!! dont expect it to generate bin-per-track! StringBuilder sb = new StringBuilder(); foreach (var session in structure.Sessions) { if (!prefs.SingleSession) { //dont want to screw around with sessions for now sb.AppendFormat("SESSION {0:D2}\n", session.num); if (prefs.AnnotateCue) { sb.AppendFormat("REM ; session (length={0})\n", session.length_aba); } } foreach (var track in session.Tracks) { ETrackType trackType = track.TrackType; //mutate track type according to our principle of canonicalization if (trackType == ETrackType.Mode1_2048 && prefs.DumpECM) { trackType = ETrackType.Mode1_2352; } sb.AppendFormat(" TRACK {0:D2} {1}\n", track.Number, Cue.TrackTypeStringForTrackType(trackType)); if (prefs.AnnotateCue) { sb.AppendFormat(" REM ; track (length={0})\n", track.LengthInSectors); } foreach (var index in track.Indexes) { //cue+bin has an implicit 150 sector pregap which neither the cue nor the bin has any awareness of //except for the baked-in sector addressing. //but, if there is an extra-long pregap, we want to reflect it this way int lba = index.aba - 150; if (lba <= 0 && index.Number == 0 && track.Number == 1) { } //dont emit index 0 when it is the same as index 1, it is illegal for some reason else if (index.Number == 0 && index.aba == track.Indexes[1].aba) { //dont emit index 0 when it is the same as index 1, it confuses some cue parsers } else { sb.AppendFormat(" INDEX {0:D2} {1}\n", index.Number, new Timestamp(lba).Value); } } } } return(sb.ToString()); }
void FromCueInternal(Cue cue, string cueDir, CueBinPrefs prefs) { //TODO - add cue directory to CueBinPrefs???? could make things cleaner... Structure = new DiscStructure(); var session = new DiscStructure.Session { num = 1 }; Structure.Sessions.Add(session); var pregap_sector = new Sector_Zero(); int curr_track = 1; foreach (var cue_file in cue.Files) { //structural validation if (cue_file.Tracks.Count < 1) { throw new Cue.CueBrokenException("`You must specify at least one track per file.`"); } string blobPath = Path.Combine(cueDir, cue_file.Path); if (CueFileResolver.ContainsKey(cue_file.Path)) { blobPath = CueFileResolver[cue_file.Path]; } int blob_sectorsize = Cue.BINSectorSizeForTrackType(cue_file.Tracks[0].TrackType); int blob_length_aba; long blob_length_bytes; IBlob cue_blob; //try any way we can to acquire a file if (!File.Exists(blobPath) && prefs.ExtensionAware) { blobPath = FindAlternateExtensionFile(blobPath, prefs.CaseSensitive, cueDir); } if (!File.Exists(blobPath)) { throw new DiscReferenceException(blobPath, ""); } //some simple rules to mutate the file type if we received something fishy string blobPathExt = Path.GetExtension(blobPath).ToLower(); if (blobPathExt == ".ape") { cue_file.FileType = Cue.CueFileType.Wave; } if (blobPathExt == ".mp3") { cue_file.FileType = Cue.CueFileType.Wave; } if (blobPathExt == ".mpc") { cue_file.FileType = Cue.CueFileType.Wave; } if (blobPathExt == ".flac") { cue_file.FileType = Cue.CueFileType.Wave; } if (blobPathExt == ".ecm") { cue_file.FileType = Cue.CueFileType.ECM; } if (cue_file.FileType == Cue.CueFileType.Binary || cue_file.FileType == Cue.CueFileType.Unspecified) { //make a blob for the file Blob_RawFile blob = new Blob_RawFile { PhysicalPath = blobPath }; Blobs.Add(blob); blob_length_aba = (int)(blob.Length / blob_sectorsize); blob_length_bytes = blob.Length; cue_blob = blob; } else if (cue_file.FileType == Cue.CueFileType.ECM) { if (!Blob_ECM.IsECM(blobPath)) { throw new DiscReferenceException(blobPath, "an ECM file was specified or detected, but it isn't a valid ECM file. You've got issues. Consult your iso vendor."); } Blob_ECM blob = new Blob_ECM(); Blobs.Add(blob); blob.Parse(blobPath); cue_blob = blob; blob_length_aba = (int)(blob.Length / blob_sectorsize); blob_length_bytes = blob.Length; } else if (cue_file.FileType == Cue.CueFileType.Wave) { Blob_WaveFile blob = new Blob_WaveFile(); Blobs.Add(blob); try { //check whether we can load the wav directly bool loaded = false; if (File.Exists(blobPath) && Path.GetExtension(blobPath).ToUpper() == ".WAV") { try { blob.Load(blobPath); loaded = true; } catch { } } //if that didnt work or wasnt possible, try loading it through ffmpeg if (!loaded) { FFMpeg ffmpeg = new FFMpeg(); if (!ffmpeg.QueryServiceAvailable()) { throw new DiscReferenceException(blobPath, "No decoding service was available (make sure ffmpeg.exe is available. even though this may be a wav, ffmpeg is used to load oddly formatted wave files. If you object to this, please send us a note and we'll see what we can do. It shouldn't be too hard.)"); } AudioDecoder dec = new AudioDecoder(); byte[] buf = dec.AcquireWaveData(blobPath); blob.Load(new MemoryStream(buf)); WasSlowLoad = true; } } catch (Exception ex) { throw new DiscReferenceException(blobPath, ex); } blob_length_aba = (int)(blob.Length / blob_sectorsize); blob_length_bytes = blob.Length; cue_blob = blob; } else { throw new Exception("Internal error - Unhandled cue blob type"); } //TODO - make CueTimestamp better, and also make it a struct, and also just make it DiscTimestamp //start timekeeping for the blob. every time we hit an index, this will advance int blob_timestamp = 0; //because we can have different size sectors in a blob, we need to keep a file cursor within the blob long blob_cursor = 0; //the aba that this cue blob starts on int blob_disc_aba_start = Sectors.Count; //this is a bit dodgy.. lets fixup the indices so we have something for index 0 //TODO - I WISH WE DIDNT HAVE TO DO THIS. WE SHOULDNT PAY SO MUCH ATTENTION TO THE INTEGRITY OF THE INDEXES Timestamp blob_ts = new Timestamp(0); for (int t = 0; t < cue_file.Tracks.Count; t++) { var cue_track = cue_file.Tracks[t]; if (!cue_track.Indexes.ContainsKey(1)) { throw new Cue.CueBrokenException("Track was missing an index 01"); } for (int i = 0; i <= 99; i++) { if (cue_track.Indexes.ContainsKey(i)) { blob_ts = cue_track.Indexes[i].Timestamp; } else if (i == 0) { var cti = new Cue.CueTrackIndex(0); cue_track.Indexes[0] = cti; cti.Timestamp = blob_ts; } } } //validate that the first index in the file is 00:00:00 //"The first index of a file must start at 00:00:00" //zero 20-dec-2014 - NOTE - index 0 is OK. we've seen files that 'start' at non-zero but thats only with index 1 -- an index 0 was explicitly listed at time 0 if (cue_file.Tracks[0].Indexes[0].Timestamp.Sector != 0) { throw new Cue.CueBrokenException("`The first index of a blob must start at 00:00:00.`"); } //for each track within the file: for (int t = 0; t < cue_file.Tracks.Count; t++) { var cue_track = cue_file.Tracks[t]; //record the disc ABA that this track started on int track_disc_aba_start = Sectors.Count; //record the pregap location. it will default to the start of the track unless we supplied a pregap command int track_disc_pregap_aba = track_disc_aba_start; int blob_track_start = blob_timestamp; //once upon a time we had a check here to prevent a single blob from containing variant sector sizes. but we support that now. //check integrity of track sequence and setup data structures //TODO - check for skipped tracks in cue parser instead if (cue_track.TrackNum != curr_track) { throw new Cue.CueBrokenException("Found a cue with skipped tracks"); } var toc_track = new DiscStructure.Track(); session.Tracks.Add(toc_track); toc_track.Number = curr_track; toc_track.TrackType = cue_track.TrackType; toc_track.ADR = 1; //safe assumption. CUE can't store this. //choose a Control value based on track type and other flags from cue //TODO - this might need to be controlled by cue loading prefs toc_track.Control = cue_track.Control; if (toc_track.TrackType == ETrackType.Audio) { toc_track.Control |= EControlQ.StereoNoPreEmph; } else { toc_track.Control |= EControlQ.DataUninterrupted; } if (curr_track == 1) { if (cue_track.PreGap.Sector != 0) { throw new InvalidOperationException("not supported (yet): cue files with track 1 pregaps"); } //but now we add one anyway, because every existing cue+bin seems to implicitly specify this cue_track.PreGap = new Timestamp(150); } //check whether a pregap is requested. //this causes empty sectors to get generated without consuming data from the blob if (cue_track.PreGap.Sector > 0) { for (int i = 0; i < cue_track.PreGap.Sector; i++) { Sectors.Add(new SectorEntry(pregap_sector)); } } //look ahead to the next track's index 1 so we can see how long this track's last index is //or, for the last track, use the length of the file int track_length_aba; if (t == cue_file.Tracks.Count - 1) { track_length_aba = blob_length_aba - blob_timestamp; } else { track_length_aba = cue_file.Tracks[t + 1].Indexes[1].Timestamp.Sector - blob_timestamp; } //toc_track.length_aba = track_length_aba; //xxx //find out how many indexes we have int num_indexes = 0; for (num_indexes = 0; num_indexes <= 99; num_indexes++) { if (!cue_track.Indexes.ContainsKey(num_indexes)) { break; } } //for each index, calculate length of index and then emit it for (int index = 0; index < num_indexes; index++) { bool is_last_index = index == num_indexes - 1; //install index into hierarchy var toc_index = new DiscStructure.Index { Number = index }; toc_track.Indexes.Add(toc_index); if (index == 0) { //zero 18-dec-2014 - uhhhh cant make sense of this. //toc_index.aba = track_disc_pregap_aba - (cue_track.Indexes[1].Timestamp.Sector - cue_track.Indexes[0].Timestamp.Sector); toc_index.aba = track_disc_pregap_aba; } else { toc_index.aba = Sectors.Count; } //calculate length of the index //if it is the last index then we use our calculation from before, otherwise we check the next index int index_length_aba; if (is_last_index) { index_length_aba = track_length_aba - (blob_timestamp - blob_track_start); } else { index_length_aba = cue_track.Indexes[index + 1].Timestamp.Sector - blob_timestamp; } //emit sectors for (int aba = 0; aba < index_length_aba; aba++) { bool is_last_aba_in_index = (aba == index_length_aba - 1); bool is_last_aba_in_track = is_last_aba_in_index && is_last_index; switch (cue_track.TrackType) { case ETrackType.Audio: //all 2352 bytes are present case ETrackType.Mode1_2352: //2352 bytes are present, containing 2048 bytes of user data as well as ECM case ETrackType.Mode2_2352: //2352 bytes are present, containing the entirety of a mode2 sector (could be form0,1,2) { //these cases are all 2352 bytes //in all these cases, either no ECM is present or ECM is provided. //so we just emit a Sector_Raw Sector_RawBlob sector_rawblob = new Sector_RawBlob { Blob = cue_blob, Offset = blob_cursor }; blob_cursor += 2352; Sector_Mode1_or_Mode2_2352 sector_raw; if (cue_track.TrackType == ETrackType.Mode1_2352) { sector_raw = new Sector_Mode1_2352(); } else if (cue_track.TrackType == ETrackType.Audio) { sector_raw = new Sector_Mode1_2352(); //TODO should probably make a new sector adapter which errors if 2048B are requested } else if (cue_track.TrackType == ETrackType.Mode2_2352) { sector_raw = new Sector_Mode2_2352(); } else { throw new InvalidOperationException(); } sector_raw.BaseSector = sector_rawblob; Sectors.Add(new SectorEntry(sector_raw)); break; } case ETrackType.Mode1_2048: //2048 bytes are present. ECM needs to be generated to create a full sector { //ECM needs to know the sector number so we have to record that here int curr_disc_aba = Sectors.Count; var sector_2048 = new Sector_Mode1_2048(curr_disc_aba + 150) { Blob = new ECMCacheBlob(cue_blob), Offset = blob_cursor }; blob_cursor += 2048; Sectors.Add(new SectorEntry(sector_2048)); break; } } //switch(TrackType) //we've emitted an ABA, so consume it from the blob blob_timestamp++; } //aba emit loop } //index loop //check whether a postgap is requested. if it is, we need to generate silent sectors for (int i = 0; i < cue_track.PostGap.Sector; i++) { Sectors.Add(new SectorEntry(pregap_sector)); } //we're done with the track now. //record its length: toc_track.LengthInSectors = Sectors.Count - toc_track.Indexes[1].aba; curr_track++; //if we ran off the end of the blob, pad it with zeroes, I guess if (blob_cursor > blob_length_bytes) { //mutate the blob to an encapsulating Blob_ZeroPadAdapter Blobs[Blobs.Count - 1] = new Blob_ZeroPadAdapter(Blobs[Blobs.Count - 1], blob_length_bytes, blob_cursor - blob_length_bytes); } } //track loop } //file loop //finally, analyze the length of the sessions and the entire disc by summing the lengths of the tracks //this is a little more complex than it looks, because the length of a thing is not determined by summing it //but rather by the difference in abas between start and end //EDIT - or is the above nonsense? it should be the amount of data present, full stop. Structure.LengthInSectors = 0; foreach (var toc_session in Structure.Sessions) { var firstTrack = toc_session.Tracks[0]; //track 0, index 0 is actually -150. but cue sheets will never say that //firstTrack.Indexes[0].aba -= 150; var lastTrack = toc_session.Tracks[toc_session.Tracks.Count - 1]; session.length_aba = lastTrack.Indexes[1].aba + lastTrack.LengthInSectors - firstTrack.Indexes[0].aba; Structure.LengthInSectors += toc_session.length_aba; } }
public void Run() { Result = new DiscStructure(); Result.Sessions.Add(new Session()); Track lastTrack = null; for (int tnum = TOCRaw.FirstRecordedTrackNumber; tnum <= TOCRaw.LastRecordedTrackNumber; tnum++) { var ti = TOCRaw.TOCItems[tnum]; var track = new Track(); Result.Sessions[0].Tracks.Add(track); track.Number = tnum; track.Control = ti.Control; track.TrackType = ETrackType.Unknown; //not known yet track.Start_ABA = ti.LBATimestamp.Sector + 150; if (lastTrack != null) { lastTrack.LengthInSectors = track.Start_ABA - lastTrack.Start_ABA; } lastTrack = track; } if(lastTrack != null) lastTrack.LengthInSectors = (TOCRaw.LeadoutTimestamp.Sector + 150) - lastTrack.Start_ABA; //total length of the disc is counted up to the leadout, including the lead-in. //i guess supporting storing the leadout is future work. Result.LengthInSectors = TOCRaw.LeadoutTimestamp.Sector; }
void FromCueInternal(Cue cue, string cueDir, CueBinPrefs prefs) { //TODO - add cue directory to CueBinPrefs???? could make things cleaner... Structure = new DiscStructure(); var session = new DiscStructure.Session {num = 1}; Structure.Sessions.Add(session); var pregap_sector = new Sector_Zero(); int curr_track = 1; foreach (var cue_file in cue.Files) { //structural validation if (cue_file.Tracks.Count < 1) throw new Cue.CueBrokenException("`You must specify at least one track per file.`"); string blobPath = Path.Combine(cueDir, cue_file.Path); if (CueFileResolver.ContainsKey(cue_file.Path)) blobPath = CueFileResolver[cue_file.Path]; int blob_sectorsize = Cue.BINSectorSizeForTrackType(cue_file.Tracks[0].TrackType); int blob_length_aba; long blob_length_bytes; IBlob cue_blob; //try any way we can to acquire a file if (!File.Exists(blobPath) && prefs.ExtensionAware) { blobPath = FindAlternateExtensionFile(blobPath, prefs.CaseSensitive, cueDir); } if (!File.Exists(blobPath)) throw new DiscReferenceException(blobPath, ""); //some simple rules to mutate the file type if we received something fishy string blobPathExt = Path.GetExtension(blobPath).ToLower(); if (blobPathExt == ".ape") cue_file.FileType = Cue.CueFileType.Wave; if (blobPathExt == ".mp3") cue_file.FileType = Cue.CueFileType.Wave; if (blobPathExt == ".mpc") cue_file.FileType = Cue.CueFileType.Wave; if (blobPathExt == ".flac") cue_file.FileType = Cue.CueFileType.Wave; if (blobPathExt == ".ecm") cue_file.FileType = Cue.CueFileType.ECM; if (cue_file.FileType == Cue.CueFileType.Binary || cue_file.FileType == Cue.CueFileType.Unspecified) { //make a blob for the file Blob_RawFile blob = new Blob_RawFile {PhysicalPath = blobPath}; Blobs.Add(blob); blob_length_aba = (int)(blob.Length / blob_sectorsize); blob_length_bytes = blob.Length; cue_blob = blob; } else if (cue_file.FileType == Cue.CueFileType.ECM) { if (!Blob_ECM.IsECM(blobPath)) { throw new DiscReferenceException(blobPath, "an ECM file was specified or detected, but it isn't a valid ECM file. You've got issues. Consult your iso vendor."); } Blob_ECM blob = new Blob_ECM(); Blobs.Add(blob); blob.Parse(blobPath); cue_blob = blob; blob_length_aba = (int)(blob.Length / blob_sectorsize); blob_length_bytes = blob.Length; } else if (cue_file.FileType == Cue.CueFileType.Wave) { Blob_WaveFile blob = new Blob_WaveFile(); Blobs.Add(blob); try { //check whether we can load the wav directly bool loaded = false; if (File.Exists(blobPath) && Path.GetExtension(blobPath).ToUpper() == ".WAV") { try { blob.Load(blobPath); loaded = true; } catch { } } //if that didnt work or wasnt possible, try loading it through ffmpeg if (!loaded) { FFMpeg ffmpeg = new FFMpeg(); if (!ffmpeg.QueryServiceAvailable()) { throw new DiscReferenceException(blobPath, "No decoding service was available (make sure ffmpeg.exe is available. even though this may be a wav, ffmpeg is used to load oddly formatted wave files. If you object to this, please send us a note and we'll see what we can do. It shouldn't be too hard.)"); } AudioDecoder dec = new AudioDecoder(); byte[] buf = dec.AcquireWaveData(blobPath); blob.Load(new MemoryStream(buf)); WasSlowLoad = true; } } catch (Exception ex) { throw new DiscReferenceException(blobPath, ex); } blob_length_aba = (int)(blob.Length / blob_sectorsize); blob_length_bytes = blob.Length; cue_blob = blob; } else throw new Exception("Internal error - Unhandled cue blob type"); //TODO - make CueTimestamp better, and also make it a struct, and also just make it DiscTimestamp //start timekeeping for the blob. every time we hit an index, this will advance int blob_timestamp = 0; //because we can have different size sectors in a blob, we need to keep a file cursor within the blob long blob_cursor = 0; //the aba that this cue blob starts on int blob_disc_aba_start = Sectors.Count; //this is a bit dodgy.. lets fixup the indices so we have something for index 0 //TODO - I WISH WE DIDNT HAVE TO DO THIS. WE SHOULDNT PAY SO MUCH ATTENTION TO THE INTEGRITY OF THE INDEXES Timestamp blob_ts = new Timestamp(0); for (int t = 0; t < cue_file.Tracks.Count; t++) { var cue_track = cue_file.Tracks[t]; if (!cue_track.Indexes.ContainsKey(1)) throw new Cue.CueBrokenException("Track was missing an index 01"); for (int i = 0; i <= 99; i++) { if (cue_track.Indexes.ContainsKey(i)) { blob_ts = cue_track.Indexes[i].Timestamp; } else if (i == 0) { var cti = new Cue.CueTrackIndex(0); cue_track.Indexes[0] = cti; cti.Timestamp = blob_ts; } } } //validate that the first index in the file is 00:00:00 //"The first index of a file must start at 00:00:00" //zero 20-dec-2014 - NOTE - index 0 is OK. we've seen files that 'start' at non-zero but thats only with index 1 -- an index 0 was explicitly listed at time 0 if (cue_file.Tracks[0].Indexes[0].Timestamp.Sector != 0) throw new Cue.CueBrokenException("`The first index of a blob must start at 00:00:00.`"); //for each track within the file: for (int t = 0; t < cue_file.Tracks.Count; t++) { var cue_track = cue_file.Tracks[t]; //record the disc ABA that this track started on int track_disc_aba_start = Sectors.Count; //record the pregap location. it will default to the start of the track unless we supplied a pregap command int track_disc_pregap_aba = track_disc_aba_start; int blob_track_start = blob_timestamp; //once upon a time we had a check here to prevent a single blob from containing variant sector sizes. but we support that now. //check integrity of track sequence and setup data structures //TODO - check for skipped tracks in cue parser instead if (cue_track.TrackNum != curr_track) throw new Cue.CueBrokenException("Found a cue with skipped tracks"); var toc_track = new DiscStructure.Track(); session.Tracks.Add(toc_track); toc_track.Number = curr_track; toc_track.TrackType = cue_track.TrackType; toc_track.ADR = 1; //safe assumption. CUE can't store this. //choose a Control value based on track type and other flags from cue //TODO - this might need to be controlled by cue loading prefs toc_track.Control = cue_track.Control; if (toc_track.TrackType == ETrackType.Audio) toc_track.Control |= EControlQ.StereoNoPreEmph; else toc_track.Control |= EControlQ.DataUninterrupted; if (curr_track == 1) { if (cue_track.PreGap.Sector != 0) throw new InvalidOperationException("not supported (yet): cue files with track 1 pregaps"); //but now we add one anyway, because every existing cue+bin seems to implicitly specify this cue_track.PreGap = new Timestamp(150); } //check whether a pregap is requested. //this causes empty sectors to get generated without consuming data from the blob if (cue_track.PreGap.Sector > 0) { for (int i = 0; i < cue_track.PreGap.Sector; i++) { Sectors.Add(new SectorEntry(pregap_sector)); } } //look ahead to the next track's index 1 so we can see how long this track's last index is //or, for the last track, use the length of the file int track_length_aba; if (t == cue_file.Tracks.Count - 1) track_length_aba = blob_length_aba - blob_timestamp; else track_length_aba = cue_file.Tracks[t + 1].Indexes[1].Timestamp.Sector - blob_timestamp; //toc_track.length_aba = track_length_aba; //xxx //find out how many indexes we have int num_indexes = 0; for (num_indexes = 0; num_indexes <= 99; num_indexes++) if (!cue_track.Indexes.ContainsKey(num_indexes)) break; //for each index, calculate length of index and then emit it for (int index = 0; index < num_indexes; index++) { bool is_last_index = index == num_indexes - 1; //install index into hierarchy var toc_index = new DiscStructure.Index {Number = index}; toc_track.Indexes.Add(toc_index); if (index == 0) { //zero 18-dec-2014 - uhhhh cant make sense of this. //toc_index.aba = track_disc_pregap_aba - (cue_track.Indexes[1].Timestamp.Sector - cue_track.Indexes[0].Timestamp.Sector); toc_index.aba = track_disc_pregap_aba; } else toc_index.aba = Sectors.Count; //calculate length of the index //if it is the last index then we use our calculation from before, otherwise we check the next index int index_length_aba; if (is_last_index) index_length_aba = track_length_aba - (blob_timestamp - blob_track_start); else index_length_aba = cue_track.Indexes[index + 1].Timestamp.Sector - blob_timestamp; //emit sectors for (int aba = 0; aba < index_length_aba; aba++) { bool is_last_aba_in_index = (aba == index_length_aba - 1); bool is_last_aba_in_track = is_last_aba_in_index && is_last_index; switch (cue_track.TrackType) { case ETrackType.Audio: //all 2352 bytes are present case ETrackType.Mode1_2352: //2352 bytes are present, containing 2048 bytes of user data as well as ECM case ETrackType.Mode2_2352: //2352 bytes are present, containing the entirety of a mode2 sector (could be form0,1,2) { //these cases are all 2352 bytes //in all these cases, either no ECM is present or ECM is provided. //so we just emit a Sector_Raw Sector_RawBlob sector_rawblob = new Sector_RawBlob { Blob = cue_blob, Offset = blob_cursor }; blob_cursor += 2352; Sector_Mode1_or_Mode2_2352 sector_raw; if(cue_track.TrackType == ETrackType.Mode1_2352) sector_raw = new Sector_Mode1_2352(); else if (cue_track.TrackType == ETrackType.Audio) sector_raw = new Sector_Mode1_2352(); //TODO should probably make a new sector adapter which errors if 2048B are requested else if (cue_track.TrackType == ETrackType.Mode2_2352) sector_raw = new Sector_Mode2_2352(); else throw new InvalidOperationException(); sector_raw.BaseSector = sector_rawblob; Sectors.Add(new SectorEntry(sector_raw)); break; } case ETrackType.Mode1_2048: //2048 bytes are present. ECM needs to be generated to create a full sector { //ECM needs to know the sector number so we have to record that here int curr_disc_aba = Sectors.Count; var sector_2048 = new Sector_Mode1_2048(curr_disc_aba + 150) { Blob = new ECMCacheBlob(cue_blob), Offset = blob_cursor }; blob_cursor += 2048; Sectors.Add(new SectorEntry(sector_2048)); break; } } //switch(TrackType) //we've emitted an ABA, so consume it from the blob blob_timestamp++; } //aba emit loop } //index loop //check whether a postgap is requested. if it is, we need to generate silent sectors for (int i = 0; i < cue_track.PostGap.Sector; i++) { Sectors.Add(new SectorEntry(pregap_sector)); } //we're done with the track now. //record its length: toc_track.LengthInSectors = Sectors.Count - toc_track.Indexes[1].aba; curr_track++; //if we ran off the end of the blob, pad it with zeroes, I guess if (blob_cursor > blob_length_bytes) { //mutate the blob to an encapsulating Blob_ZeroPadAdapter Blobs[Blobs.Count - 1] = new Blob_ZeroPadAdapter(Blobs[Blobs.Count - 1], blob_length_bytes, blob_cursor - blob_length_bytes); } } //track loop } //file loop //finally, analyze the length of the sessions and the entire disc by summing the lengths of the tracks //this is a little more complex than it looks, because the length of a thing is not determined by summing it //but rather by the difference in abas between start and end //EDIT - or is the above nonsense? it should be the amount of data present, full stop. Structure.LengthInSectors = 0; foreach (var toc_session in Structure.Sessions) { var firstTrack = toc_session.Tracks[0]; //track 0, index 0 is actually -150. but cue sheets will never say that //firstTrack.Indexes[0].aba -= 150; var lastTrack = toc_session.Tracks[toc_session.Tracks.Count - 1]; session.length_aba = lastTrack.Indexes[1].aba + lastTrack.LengthInSectors - firstTrack.Indexes[0].aba; Structure.LengthInSectors += toc_session.length_aba; } }
/// <summary> /// Generates the CUE file for the provided DiscStructure /// </summary> public string GenerateCUE_OneBin(DiscStructure structure, CueBinPrefs prefs) { if (prefs.OneBlobPerTrack) throw new InvalidOperationException("OneBinPerTrack passed to GenerateCUE_OneBin"); //this generates a single-file cue!!!!!!! dont expect it to generate bin-per-track! StringBuilder sb = new StringBuilder(); foreach (var session in structure.Sessions) { if (!prefs.SingleSession) { //dont want to screw around with sessions for now sb.AppendFormat("SESSION {0:D2}\n", session.num); if (prefs.AnnotateCue) sb.AppendFormat("REM ; session (length={0})\n", session.length_aba); } foreach (var track in session.Tracks) { ETrackType trackType = track.TrackType; //mutate track type according to our principle of canonicalization if (trackType == ETrackType.Mode1_2048 && prefs.DumpECM) trackType = ETrackType.Mode1_2352; sb.AppendFormat(" TRACK {0:D2} {1}\n", track.Number, Cue.TrackTypeStringForTrackType(trackType)); if (prefs.AnnotateCue) sb.AppendFormat(" REM ; track (length={0})\n", track.LengthInSectors); foreach (var index in track.Indexes) { //cue+bin has an implicit 150 sector pregap which neither the cue nor the bin has any awareness of //except for the baked-in sector addressing. //but, if there is an extra-long pregap, we want to reflect it this way int lba = index.aba - 150; if (lba <= 0 && index.Number == 0 && track.Number == 1) { } //dont emit index 0 when it is the same as index 1, it is illegal for some reason else if (index.Number == 0 && index.aba == track.Indexes[1].aba) { //dont emit index 0 when it is the same as index 1, it confuses some cue parsers } else { sb.AppendFormat(" INDEX {0:D2} {1}\n", index.Number, new Timestamp(lba).Value); } } } } return sb.ToString(); }