Exemplo n.º 1
0
        /// <summary>
        /// Calculates the File Quality Coefficient extracting information from the MPEG file Header.
        /// For implementation details read the Wiki Page for this coefficient (http://code.google.com/p/p2p-player/wiki/ImplicitQoS#Coefficiente_di_Qualità_del_File)
        /// </summary>
        /// <param name="filepath">Path of the file</param>
        /// <returns>File quality coeffient of the given file</returns>
        private static FileQualityCoefficient calculateFQ(String filepath)
        {
            AudioFile   mpegFile = new AudioFile(filepath);
            AudioHeader header;

            if (AudioHeader.Find(out header, mpegFile, 0))
            {
                double brComp = Math.Truncate((Math.Log10(header.AudioBitrate / 192.0) + 0.78) * 100.0) / 100.0;
                double srComp = 0.0;
                switch (header.AudioSampleRate)
                {
                case 32000:
                    srComp = 0.2;
                    break;

                case 44100:
                    srComp = 0.8;
                    break;

                case 48000:
                    srComp = 1.0;
                    break;

                default:
                    srComp = 0.0;
                    break;
                }
                double cmComp = 0.0;
                switch (header.ChannelMode)
                {
                case ChannelMode.SingleChannel:
                    cmComp = 0.2;
                    break;

                case ChannelMode.DualChannel:
                    cmComp = 0.5;
                    break;

                case ChannelMode.JointStereo:
                    cmComp = 0.8;
                    break;

                case ChannelMode.Stereo:
                    cmComp = 1.0;
                    break;

                default:
                    cmComp = 0.0;
                    break;
                }
                return(new FileQualityCoefficient(brComp, cmComp, srComp));
            }
            else
            {
                return(new FileQualityCoefficient());
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Reads information from a MPEG file and fills the tag fields.
 /// </summary>
 /// <param name="filename">Filename from w</param>
 /// <returns>Filename from whom extract the information for the tag</returns>
 private bool _fillTagMpeg(string filename)
 {
     using (AudioFile mpegFile = new AudioFile(filename))
     {
         TagLib.Tag fileTag = mpegFile.GetTag(TagLib.TagTypes.Id3v2, false);
         if (fileTag == null)
         {
             mpegFile.GetTag(TagLib.TagTypes.Id3v1, false);
         }
         if (fileTag != null)
         {
             this.Title  = fileTag.Title;
             this.Album  = fileTag.Album;
             this.Artist = fileTag.Performers.FirstOrDefault();
             this.Genre  = fileTag.Genres.FirstOrDefault();
             this.Track  = fileTag.Track;
             this.Year   = fileTag.Year;
         }
         else
         {
             return(false);
         }
         AudioHeader header;
         if (AudioHeader.Find(out header, mpegFile, 0))
         {
             this.Bitrate    = header.AudioBitrate;
             this.Length     = (int)mpegFile.Properties.Duration.TotalSeconds;
             this.Channels   = header.AudioChannels;
             this.SampleRate = header.AudioSampleRate;
         }
         else
         {
             return(false);
         }
         FileInfo fi = new FileInfo(filename);
         this.FileSize = fi.Length;
     }
     return(true);
 }
        private void InitShoutcastStream(string url, int timeout, bool skipMetaInfo)
        {
            HttpWebResponse response = null;

            if (netStream != null)
            {
                netStream.Close();
                netStream = null;
            }

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Headers.Clear();
            request.Headers.Add("Icy-MetaData", "1");

            request.Proxy     = AppConfig.GetWebProxy();
            request.KeepAlive = false;
            request.UserAgent = ProTONEConstants.PlayerUserAgent;
            request.ServicePoint.Expect100Continue = false;

            request.Timeout = timeout;

            try
            {
                ToggleAllowUnsafeHeaderParsing(true);
                response = (HttpWebResponse)request.GetResponse();

                bool icyHeaderFound = false;

                Dictionary <string, string> nvc = new Dictionary <string, string>();
                foreach (string key in response.Headers.AllKeys)
                {
                    if (response.Headers[key] != null)
                    {
                        string headerName = key.ToLowerInvariant();
                        nvc.Add(headerName, response.Headers[key].ToLowerInvariant());

                        icyHeaderFound |= headerName.StartsWith("icy");
                    }
                }

                if (!icyHeaderFound)
                {
                    // No ICY header => this is NOT a shoutcast stream.
                    connected = false;

                    // Exit with no exception.
                    return;
                }

                try
                {
                    metaInt = int.Parse(nvc["icy-metaint"]);
                }
                catch { }

                try
                {
                    if (skipMetaInfo == false)
                    {
                        bitrate = int.Parse(nvc["icy-br"]);
                    }
                }
                catch { }

                try
                {
                    contentType = nvc["content-type"];
                }
                catch { }

                switch (contentType)
                {
                case "audio/mpg":
                case "audio/mpeg":
                    //case "audio/aac":
                    //case "audio/aacp":
                    this.ContentType = contentType;
                    break;

                default:
                    throw new NotSupportedException(string.Format("Unsupported content type: {0}.", contentType));
                }

                receivedBytes = 0;

                netStream = response.GetResponseStream();

                if (skipMetaInfo == false)
                {
                    int passes = 0;
                    while (passes < 100 * 1024)
                    {
                        passes++;
                        byte[] buff   = new byte[sizeof(int)];
                        int    result = netStream.Read(buff, 0, buff.Length);
                        if (result > 0)
                        {
                            ByteVector  vector = new ByteVector(buff, result);
                            AudioHeader hdr    = AudioHeader.Unknown;
                            if (AudioHeader.Find(out hdr, vector) == true &&
                                hdr.Version == TagLib.Mpeg.Version.Version1 &&
                                hdr.AudioLayer == 3)
                            {
                                sampleRate = hdr.AudioSampleRate;
                                bitrate    = hdr.AudioBitrate;
                                break;
                            }
                        }
                    }

                    Dictionary <string, string> data = new Dictionary <string, string>();
                    data.Add("TXT_FREQUENCY", sampleRate.ToString());
                    data.Add("TXT_BITRATE", bitrate.ToString());
                    data.Add("Content-Type", contentType);

                    MediaRenderer.DefaultInstance.FireStreamPropertyChanged(data);
                }

                connected = true;
            }
            catch (Exception ex)
            {
                connected = false;
                throw;
            }
            finally
            {
                ToggleAllowUnsafeHeaderParsing(false);
            }
        }