DataSourceVersion2Command.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 System;
  20. using static Ryujinx.Audio.Renderer.Parameter.VoiceInParameter;
  21. namespace Ryujinx.Audio.Renderer.Dsp.Command
  22. {
  23. public class DataSourceVersion2Command : ICommand
  24. {
  25. public bool Enabled { get; set; }
  26. public int NodeId { get; }
  27. public CommandType CommandType { get; }
  28. public ulong EstimatedProcessingTime { get; set; }
  29. public ushort OutputBufferIndex { get; }
  30. public uint SampleRate { get; }
  31. public float Pitch { get; }
  32. public WaveBuffer[] WaveBuffers { get; }
  33. public Memory<VoiceUpdateState> State { get; }
  34. public ulong ExtraParameter { get; }
  35. public ulong ExtraParameterSize { get; }
  36. public uint ChannelIndex { get; }
  37. public uint ChannelCount { get; }
  38. public DecodingBehaviour DecodingBehaviour { get; }
  39. public SampleFormat SampleFormat { get; }
  40. public SampleRateConversionQuality SrcQuality { get; }
  41. public DataSourceVersion2Command(ref Server.Voice.VoiceState serverState, Memory<VoiceUpdateState> state, ushort outputBufferIndex, ushort channelIndex, int nodeId)
  42. {
  43. Enabled = true;
  44. NodeId = nodeId;
  45. ChannelIndex = channelIndex;
  46. ChannelCount = serverState.ChannelsCount;
  47. SampleFormat = serverState.SampleFormat;
  48. SrcQuality = serverState.SrcQuality;
  49. CommandType = GetCommandTypeBySampleFormat(SampleFormat);
  50. OutputBufferIndex = (ushort)(channelIndex + outputBufferIndex);
  51. SampleRate = serverState.SampleRate;
  52. Pitch = serverState.Pitch;
  53. WaveBuffers = new WaveBuffer[Constants.VoiceWaveBufferCount];
  54. for (int i = 0; i < WaveBuffers.Length; i++)
  55. {
  56. ref Server.Voice.WaveBuffer voiceWaveBuffer = ref serverState.WaveBuffers[i];
  57. WaveBuffers[i] = voiceWaveBuffer.ToCommon(2);
  58. }
  59. if (SampleFormat == SampleFormat.Adpcm)
  60. {
  61. ExtraParameter = serverState.DataSourceStateAddressInfo.GetReference(true);
  62. ExtraParameterSize = serverState.DataSourceStateAddressInfo.Size;
  63. }
  64. State = state;
  65. DecodingBehaviour = serverState.DecodingBehaviour;
  66. }
  67. private static CommandType GetCommandTypeBySampleFormat(SampleFormat sampleFormat)
  68. {
  69. switch (sampleFormat)
  70. {
  71. case SampleFormat.Adpcm:
  72. return CommandType.AdpcmDataSourceVersion2;
  73. case SampleFormat.PcmInt16:
  74. return CommandType.PcmInt16DataSourceVersion2;
  75. case SampleFormat.PcmFloat:
  76. return CommandType.PcmFloatDataSourceVersion2;
  77. default:
  78. throw new NotImplementedException($"{sampleFormat}");
  79. }
  80. }
  81. public void Process(CommandList context)
  82. {
  83. Span<float> outputBuffer = context.GetBuffer(OutputBufferIndex);
  84. DataSourceHelper.WaveBufferInformation info = new DataSourceHelper.WaveBufferInformation
  85. {
  86. SourceSampleRate = SampleRate,
  87. SampleFormat = SampleFormat,
  88. Pitch = Pitch,
  89. DecodingBehaviour = DecodingBehaviour,
  90. ExtraParameter = ExtraParameter,
  91. ExtraParameterSize = ExtraParameterSize,
  92. ChannelIndex = (int)ChannelIndex,
  93. ChannelCount = (int)ChannelCount,
  94. SrcQuality = SrcQuality
  95. };
  96. DataSourceHelper.ProcessWaveBuffers(context.MemoryManager, outputBuffer, ref info, WaveBuffers, ref State.Span[0], context.SampleRate, (int)context.SampleCount);
  97. }
  98. }
  99. }