public static AudioCDInformation ReadAudioCDInformation(char driveLetter) { CdromFileInfo[] files = ReadAllTracks(driveLetter); CdtextData cdtext = ReadCdtextData(driveLetter); CdromFileInfo firstFile = files[0]; CdromFileInfo lastFile = files[files.Length - 1]; AudioCDInformation audioCd = new AudioCDInformation(); audioCd.CDCatalogNumber = cdtext.DiskId; audioCd.CdaDiskSerialNumber = files[0].DiskSerialNumber; audioCd.CddbDiskId = GetCddbDiskId(files); audioCd.TrackCount = files.Length; audioCd.TotalDuration = (lastFile.StartFrom + lastFile.Duration).TotalSeconds; audioCd.Metadata = CreateMetadata(cdtext, 0); audioCd.Tracks = new AudioTrackInformation[files.Length]; for (int i = 0; i < files.Length; i++) { AudioTrackInformation track = new AudioTrackInformation(); track.TrackNumber = i + 1; track.Filename = Path.GetFileName(files[i].Path); track.Metadata = CreateMetadata(cdtext, i + 1); track.StartFrom = files[i].StartFrom.TotalSeconds; track.Duration = files[i].Duration.TotalSeconds; track.StartSector = files[i].StartSector; track.SectorCount = files[i].SectorCount; track.Metadata = CreateMetadata(cdtext, i + 1); audioCd.Tracks[i] = track; } return(audioCd); }
private static AudioMaterialMetadata CreateMetadata(CdtextData cdtext, int itemIndex) { if (cdtext == null) { return(null); } AudioMaterialMetadata metadata = new AudioMaterialMetadata(); if (cdtext.AlbumNames != null && itemIndex < cdtext.AlbumNames.Length) { metadata.AlbumName = cdtext.AlbumNames[itemIndex]; } if (cdtext.Performers != null && itemIndex < cdtext.Performers.Length) { metadata.Performer = cdtext.Performers[itemIndex]; } if (cdtext.Songwriters != null && itemIndex < cdtext.Songwriters.Length) { metadata.Songwriter = cdtext.Songwriters[itemIndex]; } if (cdtext.Composers != null && itemIndex < cdtext.Composers.Length) { metadata.Composer = cdtext.Composers[itemIndex]; } if (cdtext.Arrangers != null && itemIndex < cdtext.Arrangers.Length) { metadata.Arranger = cdtext.Arrangers[itemIndex]; } if (cdtext.Messages != null && itemIndex < cdtext.Messages.Length) { metadata.Message = cdtext.Messages[itemIndex]; } if (cdtext.Genres != null && itemIndex < cdtext.Genres.Length) { metadata.Genre = cdtext.Genres[itemIndex]; } if (cdtext.UpcEans != null && itemIndex < cdtext.UpcEans.Length) { metadata.UpcEan = cdtext.UpcEans[itemIndex]; } return(metadata); }
private static AudioMaterialMetadata CreateMetadata(CdtextData cdtext, int itemIndex) { if (cdtext == null) return null; AudioMaterialMetadata metadata = new AudioMaterialMetadata(); if (cdtext.AlbumNames != null && itemIndex < cdtext.AlbumNames.Length) metadata.AlbumName = cdtext.AlbumNames[itemIndex]; if (cdtext.Performers != null && itemIndex < cdtext.Performers.Length) metadata.Performer = cdtext.Performers[itemIndex]; if (cdtext.Songwriters != null && itemIndex < cdtext.Songwriters.Length) metadata.Songwriter = cdtext.Songwriters[itemIndex]; if (cdtext.Composers != null && itemIndex < cdtext.Composers.Length) metadata.Composer = cdtext.Composers[itemIndex]; if (cdtext.Arrangers != null && itemIndex < cdtext.Arrangers.Length) metadata.Arranger = cdtext.Arrangers[itemIndex]; if (cdtext.Messages != null && itemIndex < cdtext.Messages.Length) metadata.Message = cdtext.Messages[itemIndex]; if (cdtext.Genres != null && itemIndex < cdtext.Genres.Length) metadata.Genre = cdtext.Genres[itemIndex]; if (cdtext.UpcEans != null && itemIndex < cdtext.UpcEans.Length) metadata.UpcEan = cdtext.UpcEans[itemIndex]; return metadata; }
/// <summary> /// Reads CD Text from CD drive. /// </summary> /// <returns>CD Text</returns> public CdtextData ReadCdtext() { const int ReadTocExOutputMaxSize = 0xFFF0; UnsafeCalls.ReadTocEx readTocInput = new UnsafeCalls.ReadTocEx(); bool result; byte[] buffer = new byte[ReadTocExOutputMaxSize]; uint read = 0; result = UnsafeCalls.DeviceIoControl(hCd, UnsafeCalls.IoctlCdromReadTocEx, readTocInput, UnsafeCalls.RawReadInfoSize, buffer, (uint)buffer.Length, ref read, IntPtr.Zero); if (!result) { throw new Win32Exception(UnsafeCalls.GetLastError()); } Dictionary <UnsafeCalls.TocCdtextDataBlockPackType, StringBuilder> strings = new Dictionary <UnsafeCalls.TocCdtextDataBlockPackType, StringBuilder>(); List <UnsafeCalls.TocCdtextDataBlock> blocks = new List <UnsafeCalls.TocCdtextDataBlock>(); int position = 4; // skip length and reserved int lastPosition = 2 + BitConverter.ToUInt16(buffer, 0); while (position < lastPosition) { UnsafeCalls.TocCdtextDataBlock block = UnsafeCalls.GetTocCdtextDataBlock(buffer, position); if (block.PackType == UnsafeCalls.TocCdtextDataBlockPackType.None) { break; // emtpy space? } blocks.Add(block); StringBuilder sb; if (!strings.TryGetValue(block.PackType, out sb)) { strings.Add(block.PackType, sb = new StringBuilder()); } sb.Append(block.GetText()); position += UnsafeCalls.TocCdtextDataBlockSize; } CdtextData cdtext = new CdtextData(); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.AlbumName)) { cdtext.AlbumNames = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.AlbumName]).ToString()); } if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Performer)) { cdtext.Performers = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Performer]).ToString()); } if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Songwriter)) { cdtext.Songwriters = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Songwriter]).ToString()); } if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Composer)) { cdtext.Composers = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Composer]).ToString()); } if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Arranger)) { cdtext.Arrangers = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Arranger]).ToString()); } if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Messages)) { cdtext.Messages = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Messages]).ToString()); } if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Genre)) { cdtext.Genres = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Genre]).ToString()); } if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.DiscId)) { cdtext.DiskId = StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.DiscId]).ToString(); } if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.UpcEan)) { cdtext.UpcEans = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.UpcEan]).ToString()); } return(cdtext); }
/// <summary> /// Reads CD Text from CD drive. /// </summary> /// <returns>CD Text</returns> public CdtextData ReadCdtext() { const int ReadTocExOutputMaxSize = 0xFFF0; UnsafeCalls.ReadTocEx readTocInput = new UnsafeCalls.ReadTocEx(); bool result; byte[] buffer = new byte[ReadTocExOutputMaxSize]; uint read = 0; result = UnsafeCalls.DeviceIoControl(hCd, UnsafeCalls.IoctlCdromReadTocEx, readTocInput, UnsafeCalls.RawReadInfoSize, buffer, (uint)buffer.Length, ref read, IntPtr.Zero); if (!result) throw new Win32Exception(UnsafeCalls.GetLastError()); Dictionary<UnsafeCalls.TocCdtextDataBlockPackType, StringBuilder> strings = new Dictionary<UnsafeCalls.TocCdtextDataBlockPackType, StringBuilder>(); List<UnsafeCalls.TocCdtextDataBlock> blocks = new List<UnsafeCalls.TocCdtextDataBlock>(); int position = 4; // skip length and reserved int lastPosition = 2 + BitConverter.ToUInt16(buffer, 0); while (position < lastPosition) { UnsafeCalls.TocCdtextDataBlock block = UnsafeCalls.GetTocCdtextDataBlock(buffer, position); if (block.PackType == UnsafeCalls.TocCdtextDataBlockPackType.None) break; // emtpy space? blocks.Add(block); StringBuilder sb; if (!strings.TryGetValue(block.PackType, out sb)) { strings.Add(block.PackType, sb = new StringBuilder()); } sb.Append(block.GetText()); position += UnsafeCalls.TocCdtextDataBlockSize; } CdtextData cdtext = new CdtextData(); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.AlbumName)) cdtext.AlbumNames = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.AlbumName]).ToString()); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Performer)) cdtext.Performers = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Performer]).ToString()); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Songwriter)) cdtext.Songwriters = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Songwriter]).ToString()); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Composer)) cdtext.Composers = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Composer]).ToString()); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Arranger)) cdtext.Arrangers = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Arranger]).ToString()); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Messages)) cdtext.Messages = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Messages]).ToString()); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.Genre)) cdtext.Genres = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.Genre]).ToString()); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.DiscId)) cdtext.DiskId = StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.DiscId]).ToString(); if (strings.ContainsKey(UnsafeCalls.TocCdtextDataBlockPackType.UpcEan)) cdtext.UpcEans = SplitByZeros(StripTail(strings[UnsafeCalls.TocCdtextDataBlockPackType.UpcEan]).ToString()); return cdtext; }