Audio.cs 7.1 KB

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