public async System.Threading.Tasks.Task <CDMetadata> ReadMediaMetadata(string Id) { CDMetadata result = null; var customDevice = await Windows.Devices.Custom.CustomDevice.FromIdAsync(Id, DeviceAccessMode.ReadWrite, DeviceSharingMode.Exclusive); if (customDevice != null) { try { int[] SectorArray = await GetCDSectorArray(customDevice); if ((SectorArray != null) && (SectorArray.Length > 1)) { result = new CDMetadata(); for (int i = 0; i < (SectorArray.Length - 1); i++) { CDTrackMetadata t = new CDTrackMetadata() { Number = i + 1, Title = string.Empty, ISrc = string.Empty, FirstSector = SectorArray[i], LastSector = SectorArray[i + 1], Duration = TimeSpan.FromSeconds((SectorArray[i + 1] - SectorArray[i]) * CD_RAW_SECTOR_SIZE / (44100 * 4)) }; if (i < result.Tracks.Count) { result.Tracks[i] = t; } else { result.Tracks.Add(t); } } byte[] TextArray = await GetCDTextArray(customDevice); if (TextArray != null) { var r = FillCDMetadata(result, TextArray); if (r != null) { result = r; } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception while reading Media Metadata: " + ex.Message); result = null; } } return(result); }
CDMetadata FillCDMetadata(CDMetadata currentCD, byte[] TextArray) { try { int i_track_last = 0; string[] ArrayTrackInfo = new string[99]; int Count = (TextArray.Length - 4) / 18; //Clear CD and Track metadata info: currentCD.ISrc = string.Empty; currentCD.Message = string.Empty; currentCD.Genre = string.Empty; currentCD.AlbumTitle = string.Empty; currentCD.Artist = string.Empty; currentCD.DiscID = string.Empty; for (int i = 0; i < currentCD.Tracks.Count; i++) { currentCD.Tracks[i].ISrc = string.Empty; currentCD.Tracks[i].Title = string.Empty; } for (int i = 0; i < Count; i++) { int i_pack_type = TextArray[4 + 18 * i]; if (i_pack_type < 0x80 || i_pack_type > 0x8f) { continue; } int i_track_number = (TextArray[4 + 18 * i + 1] >> 0) & 0x7f; int i_extension_flag = (TextArray[4 + 18 * i + 1] >> 7) & 0x01; if (i_extension_flag != 0) { continue; } int i_track = i_track_number; int indexTrack = 4 + 18 * i + 4; int indexTrackMax = indexTrack + 12; while (i_track <= 127 && indexTrack < indexTrackMax) { byte[] resArray = new byte[13]; int k = 0; int l = 0; for (k = 0; k < 12; k++, l++) { resArray[l] = TextArray[4 + 18 * i + 4 + k]; if ((resArray[l] == 0x00) || (k == 11)) { string str; str = System.Text.Encoding.UTF8.GetString(resArray, 0, (resArray[l] == 0x00) ? l : l + 1); if (!string.IsNullOrEmpty(str)) { switch (i_pack_type - 0x80) { // Title case 0x00: if (i_track == 0) { currentCD.AlbumTitle += str; } else { if (i_track == currentCD.Tracks.Count + 1) { CDTrackMetadata t = new CDTrackMetadata() { Number = i_track, Title = string.Empty, ISrc = string.Empty, FirstSector = 0, LastSector = 0, Duration = TimeSpan.FromSeconds(0) }; if ((i_track - 1) < currentCD.Tracks.Count) { currentCD.Tracks[i_track - 1] = t; } else { currentCD.Tracks.Add(t); } } if (i_track <= currentCD.Tracks.Count) { currentCD.Tracks[i_track - 1].Title += str; } } break; // DiscID case 0x06: if (i_track == 0) { currentCD.DiscID += str; } break; // Artist case 0x01: if (i_track == 0) { currentCD.Artist += str; } break; // Message case 0x05: if (i_track == 0) { currentCD.Message += str; } break; // Genre case 0x07: if (i_track == 0) { currentCD.Genre += str; } break; // ISRC case 0x0E: if (i_track == 0) { currentCD.ISrc += str; } else { if (i_track == currentCD.Tracks.Count + 1) { CDTrackMetadata t = new CDTrackMetadata() { Number = i_track, Title = string.Empty, ISrc = string.Empty, FirstSector = 0, LastSector = 0, Duration = TimeSpan.FromSeconds(0) }; if ((i_track - 1) < currentCD.Tracks.Count) { currentCD.Tracks[i_track - 1] = t; } else { currentCD.Tracks.Add(t); } } if (i_track <= currentCD.Tracks.Count) { currentCD.Tracks[i_track - 1].ISrc += str; } } break; default: break; } } // System.Diagnostics.Debug.WriteLine("Track: " + i_track.ToString() + " Type: " + (i_pack_type - 0x80).ToString() + " Text: " + str); i_track++; l = -1; } } indexTrack += k; i_track_last = (i_track_last > i_track ? i_track_last : i_track); i_track++; indexTrack += 1 + 12; } } System.Diagnostics.Debug.WriteLine("Title: " + currentCD.AlbumTitle + " Artist: " + currentCD.Artist + " DiscID: " + currentCD.DiscID + " ISrc: " + currentCD.ISrc); for (int l = 0; l < currentCD.Tracks.Count; l++) { System.Diagnostics.Debug.WriteLine("Track : " + currentCD.Tracks[l].Number.ToString() + " Title: " + currentCD.Tracks[l].Title + "Duration: " + currentCD.Tracks[l].Duration.ToString() + " ISRC: " + currentCD.Tracks[l].ISrc); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception while parsing CD Text Array : " + ex.Message); currentCD = null; } return(currentCD); }