Audio.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using MediaBrowser.Controller.Providers;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.Dto;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.MediaInfo;
  6. using MediaBrowser.Model.Users;
  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. IThemeMedia,
  24. IArchivable
  25. {
  26. public long? Size { get; set; }
  27. public string Container { get; set; }
  28. public int? TotalBitrate { get; set; }
  29. public ExtraType? ExtraType { get; set; }
  30. /// <summary>
  31. /// Gets or sets the artist.
  32. /// </summary>
  33. /// <value>The artist.</value>
  34. public List<string> Artists { get; set; }
  35. public List<string> AlbumArtists { get; set; }
  36. /// <summary>
  37. /// Gets or sets the album.
  38. /// </summary>
  39. /// <value>The album.</value>
  40. public string Album { get; set; }
  41. [IgnoreDataMember]
  42. public bool IsThemeMedia
  43. {
  44. get
  45. {
  46. return ExtraType.HasValue && ExtraType.Value == Model.Entities.ExtraType.ThemeSong;
  47. }
  48. }
  49. public Audio()
  50. {
  51. Artists = new List<string>();
  52. AlbumArtists = new List<string>();
  53. }
  54. [IgnoreDataMember]
  55. public override bool SupportsAddingToPlaylist
  56. {
  57. get { return LocationType == LocationType.FileSystem && RunTimeTicks.HasValue; }
  58. }
  59. [IgnoreDataMember]
  60. protected override bool SupportsOwnedItems
  61. {
  62. get
  63. {
  64. return false;
  65. }
  66. }
  67. [IgnoreDataMember]
  68. public override Folder LatestItemsIndexContainer
  69. {
  70. get
  71. {
  72. return AlbumEntity;
  73. }
  74. }
  75. [IgnoreDataMember]
  76. public bool IsArchive
  77. {
  78. get
  79. {
  80. if (string.IsNullOrWhiteSpace(Path))
  81. {
  82. return false;
  83. }
  84. var ext = System.IO.Path.GetExtension(Path) ?? string.Empty;
  85. return new[] { ".zip", ".rar", ".7z" }.Contains(ext, StringComparer.OrdinalIgnoreCase);
  86. }
  87. }
  88. public override bool CanDownload()
  89. {
  90. var locationType = LocationType;
  91. return locationType != LocationType.Remote &&
  92. locationType != LocationType.Virtual;
  93. }
  94. [IgnoreDataMember]
  95. public List<string> AllArtists
  96. {
  97. get
  98. {
  99. var list = AlbumArtists.ToList();
  100. list.AddRange(Artists);
  101. return list;
  102. }
  103. }
  104. [IgnoreDataMember]
  105. public MusicAlbum AlbumEntity
  106. {
  107. get { return FindParent<MusicAlbum>(); }
  108. }
  109. /// <summary>
  110. /// Gets the type of the media.
  111. /// </summary>
  112. /// <value>The type of the media.</value>
  113. [IgnoreDataMember]
  114. public override string MediaType
  115. {
  116. get
  117. {
  118. return Model.Entities.MediaType.Audio;
  119. }
  120. }
  121. /// <summary>
  122. /// Creates the name of the sort.
  123. /// </summary>
  124. /// <returns>System.String.</returns>
  125. protected override string CreateSortName()
  126. {
  127. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  128. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  129. }
  130. /// <summary>
  131. /// Gets the user data key.
  132. /// </summary>
  133. /// <returns>System.String.</returns>
  134. protected override string CreateUserDataKey()
  135. {
  136. if (ConfigurationManager.Configuration.EnableStandaloneMusicKeys)
  137. {
  138. var songKey = IndexNumber.HasValue ? IndexNumber.Value.ToString("0000") : string.Empty;
  139. if (ParentIndexNumber.HasValue)
  140. {
  141. songKey = ParentIndexNumber.Value.ToString("0000") + "-" + songKey;
  142. }
  143. songKey+= Name;
  144. if (!string.IsNullOrWhiteSpace(Album))
  145. {
  146. songKey = Album + "-" + songKey;
  147. }
  148. var albumArtist = AlbumArtists.FirstOrDefault();
  149. if (!string.IsNullOrWhiteSpace(albumArtist))
  150. {
  151. songKey = albumArtist + "-" + songKey;
  152. }
  153. return songKey;
  154. }
  155. var parent = AlbumEntity;
  156. if (parent != null)
  157. {
  158. var parentKey = parent.GetUserDataKey();
  159. if (IndexNumber.HasValue)
  160. {
  161. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  162. + (IndexNumber.Value.ToString("0000 - "));
  163. return parentKey + songKey;
  164. }
  165. }
  166. return base.CreateUserDataKey();
  167. }
  168. public override UnratedItem GetBlockUnratedType()
  169. {
  170. return UnratedItem.Music;
  171. }
  172. public SongInfo GetLookupInfo()
  173. {
  174. var info = GetItemLookupInfo<SongInfo>();
  175. info.AlbumArtists = AlbumArtists;
  176. info.Album = Album;
  177. info.Artists = Artists;
  178. return info;
  179. }
  180. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  181. {
  182. var result = new List<MediaSourceInfo>
  183. {
  184. GetVersionInfo(this, enablePathSubstitution)
  185. };
  186. return result;
  187. }
  188. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  189. {
  190. var locationType = i.LocationType;
  191. var info = new MediaSourceInfo
  192. {
  193. Id = i.Id.ToString("N"),
  194. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  195. MediaStreams = MediaSourceManager.GetMediaStreams(i.Id).ToList(),
  196. Name = i.Name,
  197. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  198. RunTimeTicks = i.RunTimeTicks,
  199. Container = i.Container,
  200. Size = i.Size
  201. };
  202. if (string.IsNullOrEmpty(info.Container))
  203. {
  204. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  205. {
  206. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  207. }
  208. }
  209. var bitrate = i.TotalBitrate ??
  210. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  211. .Select(m => m.BitRate ?? 0)
  212. .Sum();
  213. if (bitrate > 0)
  214. {
  215. info.Bitrate = bitrate;
  216. }
  217. return info;
  218. }
  219. }
  220. }