Audio.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using MediaBrowser.Controller.Persistence;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Model.Configuration;
  4. using MediaBrowser.Model.Dto;
  5. using MediaBrowser.Model.Entities;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. namespace MediaBrowser.Controller.Entities.Audio
  11. {
  12. /// <summary>
  13. /// Class Audio
  14. /// </summary>
  15. public class Audio : BaseItem,
  16. IHasAlbumArtist,
  17. IHasArtist,
  18. IHasMusicGenres,
  19. IHasLookupInfo<SongInfo>,
  20. IHasTags,
  21. IHasMediaSources
  22. {
  23. public string FormatName { get; set; }
  24. public long? Size { get; set; }
  25. public string Container { get; set; }
  26. public int? TotalBitrate { get; set; }
  27. public List<string> Tags { get; set; }
  28. public Audio()
  29. {
  30. Artists = new List<string>();
  31. Tags = new List<string>();
  32. }
  33. /// <summary>
  34. /// Gets or sets a value indicating whether this instance has embedded image.
  35. /// </summary>
  36. /// <value><c>true</c> if this instance has embedded image; otherwise, <c>false</c>.</value>
  37. public bool HasEmbeddedImage { get; set; }
  38. /// <summary>
  39. /// Override this to true if class should be grouped under a container in indicies
  40. /// The container class should be defined via IndexContainer
  41. /// </summary>
  42. /// <value><c>true</c> if [group in index]; otherwise, <c>false</c>.</value>
  43. [IgnoreDataMember]
  44. public override bool GroupInIndex
  45. {
  46. get
  47. {
  48. return true;
  49. }
  50. }
  51. /// <summary>
  52. /// Override this to return the folder that should be used to construct a container
  53. /// for this item in an index. GroupInIndex should be true as well.
  54. /// </summary>
  55. /// <value>The index container.</value>
  56. [IgnoreDataMember]
  57. public override Folder IndexContainer
  58. {
  59. get
  60. {
  61. return Parents.OfType<MusicAlbum>().FirstOrDefault() ?? new MusicAlbum { Name = "<Unknown>" };
  62. }
  63. }
  64. /// <summary>
  65. /// Gets or sets the artist.
  66. /// </summary>
  67. /// <value>The artist.</value>
  68. public List<string> Artists { get; set; }
  69. [IgnoreDataMember]
  70. public List<string> AllArtists
  71. {
  72. get
  73. {
  74. var list = new List<string>();
  75. if (!string.IsNullOrEmpty(AlbumArtist))
  76. {
  77. list.Add(AlbumArtist);
  78. }
  79. list.AddRange(Artists);
  80. return list;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets the album.
  85. /// </summary>
  86. /// <value>The album.</value>
  87. public string Album { get; set; }
  88. /// <summary>
  89. /// Gets or sets the album artist.
  90. /// </summary>
  91. /// <value>The album artist.</value>
  92. public string AlbumArtist { get; set; }
  93. /// <summary>
  94. /// Gets the type of the media.
  95. /// </summary>
  96. /// <value>The type of the media.</value>
  97. public override string MediaType
  98. {
  99. get
  100. {
  101. return Model.Entities.MediaType.Audio;
  102. }
  103. }
  104. /// <summary>
  105. /// Creates the name of the sort.
  106. /// </summary>
  107. /// <returns>System.String.</returns>
  108. protected override string CreateSortName()
  109. {
  110. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  111. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  112. }
  113. /// <summary>
  114. /// Determines whether the specified name has artist.
  115. /// </summary>
  116. /// <param name="name">The name.</param>
  117. /// <returns><c>true</c> if the specified name has artist; otherwise, <c>false</c>.</returns>
  118. public bool HasArtist(string name)
  119. {
  120. return Artists.Contains(name, StringComparer.OrdinalIgnoreCase) || string.Equals(AlbumArtist, name, StringComparison.OrdinalIgnoreCase);
  121. }
  122. /// <summary>
  123. /// Gets the user data key.
  124. /// </summary>
  125. /// <returns>System.String.</returns>
  126. public override string GetUserDataKey()
  127. {
  128. var parent = FindParent<MusicAlbum>();
  129. if (parent != null)
  130. {
  131. var parentKey = parent.GetUserDataKey();
  132. if (IndexNumber.HasValue)
  133. {
  134. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  135. + (IndexNumber.Value.ToString("0000 - "));
  136. return parentKey + songKey;
  137. }
  138. }
  139. return base.GetUserDataKey();
  140. }
  141. protected override bool GetBlockUnratedValue(UserConfiguration config)
  142. {
  143. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  144. }
  145. public SongInfo GetLookupInfo()
  146. {
  147. var info = GetItemLookupInfo<SongInfo>();
  148. info.AlbumArtist = AlbumArtist;
  149. info.Album = Album;
  150. info.Artists = Artists;
  151. return info;
  152. }
  153. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  154. {
  155. var result = new List<MediaSourceInfo>
  156. {
  157. GetVersionInfo(this, enablePathSubstitution)
  158. };
  159. return result;
  160. }
  161. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  162. {
  163. var locationType = i.LocationType;
  164. var info = new MediaSourceInfo
  165. {
  166. Id = i.Id.ToString("N"),
  167. LocationType = locationType,
  168. MediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery { ItemId = i.Id }).ToList(),
  169. Name = i.Name,
  170. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  171. RunTimeTicks = i.RunTimeTicks,
  172. Container = i.Container,
  173. Size = i.Size,
  174. Formats = (i.FormatName ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()
  175. };
  176. if (string.IsNullOrEmpty(info.Container))
  177. {
  178. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  179. {
  180. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  181. }
  182. }
  183. var bitrate = i.TotalBitrate ??
  184. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  185. .Select(m => m.BitRate ?? 0)
  186. .Sum();
  187. if (bitrate > 0)
  188. {
  189. info.Bitrate = bitrate;
  190. }
  191. return info;
  192. }
  193. }
  194. }