Audio.cs 7.3 KB

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