예제 #1
0
        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
                {
                    TOCTrack[] 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].Position, LastSector = SectorArray[i + 1].Position, Duration = TimeSpan.FromSeconds((SectorArray[i + 1].Position - SectorArray[i].Position) * 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 = FillCDWithLocalMetadata(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);
        }
예제 #2
0
        public CDMetadata FillCDWithLocalMetadata(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);
                                str = System.Text.Encoding.UTF7.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++)
                {
                    if (!string.IsNullOrEmpty(currentCD.Artist))
                    {
                        currentCD.Tracks[l].Artist = currentCD.Artist;
                    }
                    if (!string.IsNullOrEmpty(currentCD.AlbumTitle))
                    {
                        currentCD.Tracks[l].Album = currentCD.AlbumTitle;
                    }
                    if (!string.IsNullOrEmpty(currentCD.DiscID))
                    {
                        currentCD.Tracks[l].DiscID = currentCD.DiscID;
                    }

                    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);
        }
예제 #3
0
        public async System.Threading.Tasks.Task <CDMetadata> FillCDWithOnlineMetadata(CDMetadata currentCD, string discid)
        {
            try
            {
                //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.albumArtUrl = string.Empty;
                currentCD.DiscID      = string.Empty;
                if (!string.IsNullOrEmpty(discid))
                {
                    char[]   sep   = { '?' };
                    string[] array = discid.Split(sep);
                    if (array.Count() >= 1)
                    {
                        currentCD.DiscID = array[0];
                        DiscIDObject v = await GetCDObjects(discid);

                        if (v != null)
                        {
                            if ((v.releases != null) && (v.releases.Count > 0))
                            {
                                int Index = -1;
                                for (int i = 0; i < v.releases.Count; i++)
                                {
                                    if (IsCDFound(v.releases[i], currentCD.DiscID, true))
                                    {
                                        Index = i;
                                        break;
                                    }
                                    if ((Index < 0) && (IsCDFound(v.releases[i], currentCD.DiscID, false)))
                                    {
                                        Index = i;
                                    }
                                }
                                if (Index >= 0)
                                {
                                    // Found
                                    currentCD.AlbumTitle = v.releases[Index].title;
                                    if ((v.releases[Index].artistcredit != null) &&
                                        (v.releases[Index].artistcredit.Count > 0))
                                    {
                                        currentCD.Artist = v.releases[Index].artistcredit[0].name;
                                    }
                                    currentCD.albumArtUrl = await GetAlbumArtUrl(v.releases[Index].id);

                                    for (int j = 0; j < v.releases[Index].media.Count; j++)
                                    {
                                        if ((string.Equals(v.releases[Index].media[j].format, "CD", StringComparison.OrdinalIgnoreCase)) &&
                                            (v.releases[Index].media[j].discs != null) && (v.releases[Index].media[j].discs.Count > 0))
                                        {
                                            if ((v.releases[Index].media[j].tracks != null) && (v.releases[Index].media[j].tracks.Count == currentCD.Tracks.Count))
                                            {
                                                for (int m = 0; m < v.releases[Index].media[j].tracks.Count; m++)
                                                {
                                                    if (currentCD.Tracks[m].Number == v.releases[Index].media[j].tracks[m].position)
                                                    {
                                                        currentCD.Tracks[m].Artist = currentCD.Artist;
                                                        currentCD.Tracks[m].Album  = currentCD.AlbumTitle;
                                                        currentCD.Tracks[m].Poster = currentCD.albumArtUrl;
                                                        currentCD.Tracks[m].Title  = v.releases[Index].media[j].tracks[m].title;
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    return(currentCD);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception while getting online Medtadata: " + ex.Message);
                currentCD = null;
            }
            return(currentCD);
        }