VoiceInParameter.cs 12 KB

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