VoiceInParameter.cs 11 KB

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