VoiceInParameter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using Ryujinx.Audio.Common;
  18. using Ryujinx.Audio.Renderer.Common;
  19. using Ryujinx.Audio.Renderer.Dsp;
  20. using Ryujinx.Common.Memory;
  21. using System;
  22. using System.Runtime.CompilerServices;
  23. using System.Runtime.InteropServices;
  24. namespace Ryujinx.Audio.Renderer.Parameter
  25. {
  26. /// <summary>
  27. /// Input information for a voice.
  28. /// </summary>
  29. [StructLayout(LayoutKind.Sequential, Size = 0x170, Pack = 1)]
  30. public struct VoiceInParameter
  31. {
  32. /// <summary>
  33. /// Id of the voice.
  34. /// </summary>
  35. public int Id;
  36. /// <summary>
  37. /// Node id of the voice.
  38. /// </summary>
  39. public int NodeId;
  40. /// <summary>
  41. /// Set to true if the voice is new.
  42. /// </summary>
  43. [MarshalAs(UnmanagedType.I1)]
  44. public bool IsNew;
  45. /// <summary>
  46. /// Set to true if the voice is used.
  47. /// </summary>
  48. [MarshalAs(UnmanagedType.I1)]
  49. public bool InUse;
  50. /// <summary>
  51. /// The voice <see cref="PlayState"/> wanted by the user.
  52. /// </summary>
  53. public PlayState PlayState;
  54. /// <summary>
  55. /// The <see cref="SampleFormat"/> of the voice.
  56. /// </summary>
  57. public SampleFormat SampleFormat;
  58. /// <summary>
  59. /// The sample rate of the voice.
  60. /// </summary>
  61. public uint SampleRate;
  62. /// <summary>
  63. /// The priority of the voice.
  64. /// </summary>
  65. public uint Priority;
  66. /// <summary>
  67. /// Target sorting position of the voice. (Used to sort voices with the same <see cref="Priority"/>)
  68. /// </summary>
  69. public uint SortingOrder;
  70. /// <summary>
  71. /// The total channel count used.
  72. /// </summary>
  73. public uint ChannelCount;
  74. /// <summary>
  75. /// The pitch used on the voice.
  76. /// </summary>
  77. public float Pitch;
  78. /// <summary>
  79. /// The output volume of the voice.
  80. /// </summary>
  81. public float Volume;
  82. /// <summary>
  83. /// Biquad filters to apply to the output of the voice.
  84. /// </summary>
  85. public Array2<BiquadFilterParameter> BiquadFilters;
  86. /// <summary>
  87. /// Total count of <see cref="WaveBufferInternal"/> of the voice.
  88. /// </summary>
  89. public uint WaveBuffersCount;
  90. /// <summary>
  91. /// Current playing <see cref="WaveBufferInternal"/> of the voice.
  92. /// </summary>
  93. public uint WaveBuffersIndex;
  94. /// <summary>
  95. /// Reserved/unused.
  96. /// </summary>
  97. private uint _reserved1;
  98. /// <summary>
  99. /// User state address required by the data source.
  100. /// </summary>
  101. /// <remarks>Only used for <see cref="SampleFormat.Adpcm"/> as the address of the GC-ADPCM coefficients.</remarks>
  102. public ulong DataSourceStateAddress;
  103. /// <summary>
  104. /// User state size required by the data source.
  105. /// </summary>
  106. /// <remarks>Only used for <see cref="SampleFormat.Adpcm"/> as the size of the GC-ADPCM coefficients.</remarks>
  107. public ulong DataSourceStateSize;
  108. /// <summary>
  109. /// The target mix id of the voice.
  110. /// </summary>
  111. public int MixId;
  112. /// <summary>
  113. /// The target splitter id of the voice.
  114. /// </summary>
  115. public uint SplitterId;
  116. /// <summary>
  117. /// The wavebuffer parameters of this voice.
  118. /// </summary>
  119. public Array4<WaveBufferInternal> WaveBuffers;
  120. /// <summary>
  121. /// The channel resource ids associated to the voice.
  122. /// </summary>
  123. public Array6<int> ChannelResourceIds;
  124. /// <summary>
  125. /// Reset the voice drop flag during voice server update.
  126. /// </summary>
  127. [MarshalAs(UnmanagedType.I1)]
  128. public bool ResetVoiceDropFlag;
  129. /// <summary>
  130. /// Flush the amount of wavebuffer specified. This will result in the wavebuffer being skipped and marked played.
  131. /// </summary>
  132. /// <remarks>This was added on REV5.</remarks>
  133. public byte FlushWaveBufferCount;
  134. /// <summary>
  135. /// Reserved/unused.
  136. /// </summary>
  137. private ushort _reserved2;
  138. /// <summary>
  139. /// Change the behaviour of the voice.
  140. /// </summary>
  141. /// <remarks>This was added on REV5.</remarks>
  142. public DecodingBehaviour DecodingBehaviourFlags;
  143. /// <summary>
  144. /// Change the Sample Rate Conversion (SRC) quality of the voice.
  145. /// </summary>
  146. /// <remarks>This was added on REV8.</remarks>
  147. public SampleRateConversionQuality SrcQuality;
  148. /// <summary>
  149. /// This was previously used for opus codec support on the Audio Renderer and was removed on REV3.
  150. /// </summary>
  151. public uint ExternalContext;
  152. /// <summary>
  153. /// This was previously used for opus codec support on the Audio Renderer and was removed on REV3.
  154. /// </summary>
  155. public uint ExternalContextSize;
  156. /// <summary>
  157. /// Reserved/unused.
  158. /// </summary>
  159. private unsafe fixed uint _reserved3[2];
  160. /// <summary>
  161. /// Input information for a voice wavebuffer.
  162. /// </summary>
  163. [StructLayout(LayoutKind.Sequential, Size = 0x38, Pack = 1)]
  164. public struct WaveBufferInternal
  165. {
  166. /// <summary>
  167. /// Address of the wavebuffer data.
  168. /// </summary>
  169. public ulong Address;
  170. /// <summary>
  171. /// Size of the wavebuffer data.
  172. /// </summary>
  173. public ulong Size;
  174. /// <summary>
  175. /// Offset of the first sample to play.
  176. /// </summary>
  177. public uint StartSampleOffset;
  178. /// <summary>
  179. /// Offset of the last sample to play.
  180. /// </summary>
  181. public uint EndSampleOffset;
  182. /// <summary>
  183. /// If set to true, the wavebuffer will loop when reaching <see cref="EndSampleOffset"/>.
  184. /// </summary>
  185. /// <remarks>
  186. /// Starting with REV8, you can specify how many times to loop the wavebuffer (<see cref="LoopCount"/>) and where it should start and end when looping (<see cref="LoopFirstSampleOffset"/> and <see cref="LoopLastSampleOffset"/>)
  187. /// </remarks>
  188. [MarshalAs(UnmanagedType.I1)]
  189. public bool ShouldLoop;
  190. /// <summary>
  191. /// Indicates that this is the last wavebuffer to play of the voice.
  192. /// </summary>
  193. [MarshalAs(UnmanagedType.I1)]
  194. public bool IsEndOfStream;
  195. /// <summary>
  196. /// Indicates if the server should update its internal state.
  197. /// </summary>
  198. [MarshalAs(UnmanagedType.I1)]
  199. public bool SentToServer;
  200. /// <summary>
  201. /// Reserved/unused.
  202. /// </summary>
  203. private byte _reserved;
  204. /// <summary>
  205. /// If set to anything other than 0, specifies how many times to loop the wavebuffer.
  206. /// </summary>
  207. /// <remarks>This was added in REV8.</remarks>
  208. public int LoopCount;
  209. /// <summary>
  210. /// Address of the context used by the sample decoder.
  211. /// </summary>
  212. /// <remarks>This is only currently used by <see cref="SampleFormat.Adpcm"/>.</remarks>
  213. public ulong ContextAddress;
  214. /// <summary>
  215. /// Size of the context used by the sample decoder.
  216. /// </summary>
  217. /// <remarks>This is only currently used by <see cref="SampleFormat.Adpcm"/>.</remarks>
  218. public ulong ContextSize;
  219. /// <summary>
  220. /// If set to anything other than 0, specifies the offset of the first sample to play when looping.
  221. /// </summary>
  222. /// <remarks>This was added in REV8.</remarks>
  223. public uint LoopFirstSampleOffset;
  224. /// <summary>
  225. /// If set to anything other than 0, specifies the offset of the last sample to play when looping.
  226. /// </summary>
  227. /// <remarks>This was added in REV8.</remarks>
  228. public uint LoopLastSampleOffset;
  229. /// <summary>
  230. /// Check if the sample offsets are in a valid range for generic PCM.
  231. /// </summary>
  232. /// <typeparam name="T">The PCM sample type</typeparam>
  233. /// <returns>Returns true if the sample offset are in range of the size.</returns>
  234. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  235. private bool IsSampleOffsetInRangeForPcm<T>() where T : unmanaged
  236. {
  237. uint dataTypeSize = (uint)Unsafe.SizeOf<T>();
  238. return StartSampleOffset * dataTypeSize <= Size &&
  239. EndSampleOffset * dataTypeSize <= Size;
  240. }
  241. /// <summary>
  242. /// Check if the sample offsets are in a valid range for the given <see cref="SampleFormat"/>.
  243. /// </summary>
  244. /// <param name="format">The target <see cref="SampleFormat"/></param>
  245. /// <returns>Returns true if the sample offset are in range of the size.</returns>
  246. public bool IsSampleOffsetValid(SampleFormat format)
  247. {
  248. bool result;
  249. switch (format)
  250. {
  251. case SampleFormat.PcmInt16:
  252. result = IsSampleOffsetInRangeForPcm<ushort>();
  253. break;
  254. case SampleFormat.PcmFloat:
  255. result = IsSampleOffsetInRangeForPcm<float>();
  256. break;
  257. case SampleFormat.Adpcm:
  258. result = AdpcmHelper.GetAdpcmDataSize((int)StartSampleOffset) <= Size &&
  259. AdpcmHelper.GetAdpcmDataSize((int)EndSampleOffset) <= Size;
  260. break;
  261. default:
  262. throw new NotImplementedException($"{format} not implemented!");
  263. }
  264. return result;
  265. }
  266. }
  267. /// <summary>
  268. /// Flag altering the behaviour of wavebuffer decoding.
  269. /// </summary>
  270. [Flags]
  271. public enum DecodingBehaviour : ushort
  272. {
  273. /// <summary>
  274. /// Default decoding behaviour.
  275. /// </summary>
  276. Default = 0,
  277. /// <summary>
  278. /// Reset the played samples accumulator when looping.
  279. /// </summary>
  280. PlayedSampleCountResetWhenLooping = 1,
  281. /// <summary>
  282. /// Skip pitch and Sample Rate Conversion (SRC).
  283. /// </summary>
  284. SkipPitchAndSampleRateConversion = 2
  285. }
  286. /// <summary>
  287. /// Specify the quality to use during Sample Rate Conversion (SRC) and pitch handling.
  288. /// </summary>
  289. /// <remarks>This was added in REV8.</remarks>
  290. public enum SampleRateConversionQuality : byte
  291. {
  292. /// <summary>
  293. /// Resample interpolating 4 samples per output sample.
  294. /// </summary>
  295. Default,
  296. /// <summary>
  297. /// Resample interpolating 8 samples per output sample.
  298. /// </summary>
  299. High,
  300. /// <summary>
  301. /// Resample interpolating 1 samples per output sample.
  302. /// </summary>
  303. Low
  304. }
  305. }
  306. }