Audio.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 = new List<string>();
  76. if (!string.IsNullOrEmpty(AlbumArtist))
  77. {
  78. list.Add(AlbumArtist);
  79. }
  80. list.AddRange(Artists);
  81. return list;
  82. }
  83. }
  84. /// <summary>
  85. /// Gets or sets the album.
  86. /// </summary>
  87. /// <value>The album.</value>
  88. public string Album { get; set; }
  89. /// <summary>
  90. /// Gets or sets the album artist.
  91. /// </summary>
  92. /// <value>The album artist.</value>
  93. public string AlbumArtist { get; set; }
  94. /// <summary>
  95. /// Gets the type of the media.
  96. /// </summary>
  97. /// <value>The type of the media.</value>
  98. public override string MediaType
  99. {
  100. get
  101. {
  102. return Model.Entities.MediaType.Audio;
  103. }
  104. }
  105. /// <summary>
  106. /// Creates the name of the sort.
  107. /// </summary>
  108. /// <returns>System.String.</returns>
  109. protected override string CreateSortName()
  110. {
  111. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  112. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  113. }
  114. /// <summary>
  115. /// Determines whether the specified name has artist.
  116. /// </summary>
  117. /// <param name="name">The name.</param>
  118. /// <returns><c>true</c> if the specified name has artist; otherwise, <c>false</c>.</returns>
  119. public bool HasArtist(string name)
  120. {
  121. return Artists.Contains(name, StringComparer.OrdinalIgnoreCase) || string.Equals(AlbumArtist, name, StringComparison.OrdinalIgnoreCase);
  122. }
  123. /// <summary>
  124. /// Gets the user data key.
  125. /// </summary>
  126. /// <returns>System.String.</returns>
  127. public override string GetUserDataKey()
  128. {
  129. var parent = FindParent<MusicAlbum>();
  130. if (parent != null)
  131. {
  132. var parentKey = parent.GetUserDataKey();
  133. if (IndexNumber.HasValue)
  134. {
  135. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  136. + (IndexNumber.Value.ToString("0000 - "));
  137. return parentKey + songKey;
  138. }
  139. }
  140. return base.GetUserDataKey();
  141. }
  142. protected override bool GetBlockUnratedValue(UserConfiguration config)
  143. {
  144. return config.BlockUnratedItems.Contains(UnratedItem.Music);
  145. }
  146. public SongInfo GetLookupInfo()
  147. {
  148. var info = GetItemLookupInfo<SongInfo>();
  149. info.AlbumArtist = AlbumArtist;
  150. info.Album = Album;
  151. info.Artists = Artists;
  152. return info;
  153. }
  154. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  155. {
  156. var result = new List<MediaSourceInfo>
  157. {
  158. GetVersionInfo(this, enablePathSubstitution)
  159. };
  160. return result;
  161. }
  162. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  163. {
  164. var locationType = i.LocationType;
  165. var info = new MediaSourceInfo
  166. {
  167. Id = i.Id.ToString("N"),
  168. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  169. MediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery { ItemId = i.Id }).ToList(),
  170. Name = i.Name,
  171. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  172. RunTimeTicks = i.RunTimeTicks,
  173. Container = i.Container,
  174. Size = i.Size,
  175. Formats = (i.FormatName ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()
  176. };
  177. if (string.IsNullOrEmpty(info.Container))
  178. {
  179. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  180. {
  181. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  182. }
  183. }
  184. var bitrate = i.TotalBitrate ??
  185. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  186. .Select(m => m.BitRate ?? 0)
  187. .Sum();
  188. if (bitrate > 0)
  189. {
  190. info.Bitrate = bitrate;
  191. }
  192. return info;
  193. }
  194. }
  195. }