/// <summary>
        ///     Gets the current BPM.
        /// </summary>
        /// <returns>The current BPM</returns>
        public decimal GetCurrentBpm()
        {
            var bpm = 100M;

            if (CurrentTrack != null)
            {
                var trackPosition = GetPositionNoLock();
                var position      = trackPosition.Positition;
                if (position < 0)
                {
                    position = 0;
                }

                if (position < CurrentTrack.FullStartLoopLength)
                {
                    bpm = CurrentTrack.StartBpm;
                }
                else
                {
                    var range           = CurrentTrack.EndBpm - CurrentTrack.StartBpm;
                    var percentComplete = (decimal)(position / (double)trackPosition.Length);
                    bpm = CurrentTrack.StartBpm + range * percentComplete;
                }
            }

            bpm = BpmHelper.NormaliseBpm(bpm);
            return(bpm);
        }
Exemple #2
0
        /// <summary>
        /// Loads the track details from the tags in the associate mp3
        /// </summary>
        /// <param name="track">The track to load the details of.</param>
        /// <param name="updateLength">if set to <c>true</c> [update length].</param>
        public static void LoadTrack(Track track, bool updateLength = true)
        {
            if (!File.Exists(track.Filename))
            {
                return;
            }
            if (!track.Filename.ToLower().EndsWith(".mp3"))
            {
                return;
            }

            DebugHelper.WriteLine("Library - LoadTrack - " + track.Description);

            GuessTrackDetailsFromFileName(track);

            var dateLastModified = GetTrackLastModified(track.Filename);

            track.LastModified = dateLastModified;

            if (ID3v2Tag.DoesTagExist(track.Filename))
            {
                var tags = new ID3v2Tag(track.Filename);

                if (!string.IsNullOrEmpty(tags.Artist))
                {
                    track.Artist = tags.Artist.Trim();
                }
                if (!string.IsNullOrEmpty(tags.Artist))
                {
                    track.AlbumArtist = tags.Artist.Trim();
                }
                if (!string.IsNullOrEmpty(tags.Title))
                {
                    track.Title = tags.Title.Trim();
                }
                if (!string.IsNullOrEmpty(tags.Album))
                {
                    track.Album = tags.Album.Trim();
                }
                if (!string.IsNullOrEmpty(tags.Genre))
                {
                    track.Genre = tags.Genre.Trim();
                }
                if (!string.IsNullOrEmpty(tags.InitialKey))
                {
                    var tagKey = tags.InitialKey.Trim();
                    track.Key = KeyHelper.ParseKey(tagKey);
                }

                LoadArtistAndAlbumArtist(track);

                if (tags.LengthMilliseconds.HasValue)
                {
                    track.Length = (decimal)tags.LengthMilliseconds / 1000M;
                }

                decimal bpm;
                if (decimal.TryParse(tags.BPM, out bpm))
                {
                    track.Bpm = bpm;
                }

                track.Bpm      = BpmHelper.NormaliseBpm(track.Bpm);
                track.EndBpm   = track.Bpm;
                track.StartBpm = track.Bpm;
                track.Bpm      = BpmHelper.GetAdjustedBpmAverage(track.StartBpm, track.EndBpm);

                int trackNumber;
                var trackNumberTag = (tags.TrackNumber + "/").Split('/')[0].Trim();
                if (int.TryParse(trackNumberTag, out trackNumber))
                {
                    track.TrackNumber = trackNumber;
                }

                if (GenreCode.IsGenreCode(track.Genre))
                {
                    track.Genre = GenreCode.GetGenre(track.Genre);
                }
                if (track.Artist == "")
                {
                    track.Artist = NoValue;
                }
                if (track.AlbumArtist == "")
                {
                    track.AlbumArtist = NoValue;
                }
                if (track.Title == "")
                {
                    track.Title = NoValue;
                }
                if (track.Album == "")
                {
                    track.Album = NoValue;
                }
                if (track.Genre == "")
                {
                    track.Genre = NoValue;
                }
            }

            track.OriginalDescription = track.Description;
            track.FullLength          = track.Length;


            var audioFile = AudioFile.Create(track.Filename, true);

            track.Bitrate = audioFile.Bitrate;
            track.Length  = audioFile.TotalSeconds;

            if (updateLength)
            {
                UpdateLength(track);
            }

            //UpdateKey(track);

            track.Bpm = BpmHelper.GetAdjustedBpmAverage(track.StartBpm, track.EndBpm);

            track.OriginalDescription = track.Description;

            if (track.EndBpm == 0 || track.EndBpm == 100)
            {
                track.EndBpm = track.Bpm;
            }
            if (track.StartBpm == 0 || track.StartBpm == 100)
            {
                track.StartBpm = track.Bpm;
            }
        }
        /// <summary>
        ///     Loads the shuffler details for a track
        /// </summary>
        /// <param name="track">The track.</param>
        public static Dictionary <string, string> LoadShufflerDetails(Track track)
        {
            //if(track.Title.Contains("Escobar"))
            //    DebugHelper.WriteLine("Stop");

            track.Key = KeyHelper.ParseKey(track.Key);

            track.IsShufflerTrack = ExtenedAttributesHelper.HasExtendedAttributes(track.Description);

            if (!track.IsShufflerTrack)
            {
                return(null);
            }

            var attributes = ExtenedAttributesHelper.GetExtendedAttributes(track.Description);


            if (attributes.ContainsKey("Rank"))
            {
                track.Rank = ConversionHelper.ToInt(attributes["Rank"], 1);
            }

            decimal start = 0;

            if (attributes.ContainsKey("FadeIn"))
            {
                start = ConversionHelper.ToDecimal(attributes["FadeIn"], start);
            }
            var end = track.Length;

            if (attributes.ContainsKey("FadeOut"))
            {
                end = ConversionHelper.ToDecimal(attributes["FadeOut"], end);
            }
            var length = end - start;

            var inLoopCount = 0;

            if (attributes.ContainsKey("StartLoopCount"))
            {
                inLoopCount = ConversionHelper.ToInt(attributes["StartLoopCount"], inLoopCount);
            }

            decimal inLoopLength = 0;

            if (attributes.ContainsKey("FadeInLengthInSeconds"))
            {
                inLoopLength = ConversionHelper.ToDecimal(attributes["FadeInLengthInSeconds"]);
            }
            if (inLoopLength > 0)
            {
                track.StartBpm = BpmHelper.GetBpmFromLoopLength(Convert.ToDouble(inLoopLength));
            }

            inLoopCount = inLoopCount - 1;
            if (inLoopCount > 0)
            {
                length = length + inLoopCount * inLoopLength;
            }

            decimal skipLength = 0;

            if (attributes.ContainsKey("SkipLengthInSeconds"))
            {
                skipLength = ConversionHelper.ToDecimal(attributes["SkipLengthInSeconds"]);
            }
            if (skipLength > 0)
            {
                length = length - skipLength;
            }

            track.PowerDown = false;
            if (attributes.ContainsKey("PowerDown"))
            {
                track.PowerDown = ConversionHelper.ToBoolean(attributes["PowerDown"]);
            }

            if (attributes.ContainsKey("Key"))
            {
                track.Key = KeyHelper.ParseKey(attributes["Key"]);
            }

            decimal outLoopLength = 0;

            if (attributes.ContainsKey("FadeOutLengthInSeconds"))
            {
                outLoopLength = ConversionHelper.ToDecimal(attributes["FadeOutLengthInSeconds"], 0);
            }
            if (outLoopLength > 0)
            {
                track.EndBpm = BpmHelper.GetBpmFromLoopLength(Convert.ToDouble(outLoopLength));
            }

            track.Length = length;

            if (attributes.ContainsKey("StartBPM"))
            {
                track.StartBpm = BpmHelper.NormaliseBpm(ConversionHelper.ToDecimal(attributes["StartBPM"], track.Bpm));
            }
            if (attributes.ContainsKey("EndBPM"))
            {
                track.EndBpm = BpmHelper.NormaliseBpm(ConversionHelper.ToDecimal(attributes["EndBPM"], track.Bpm));
            }

            track.Bpm = BpmHelper.GetAdjustedBpmAverage(track.StartBpm, track.EndBpm);

            return(attributes);
        }