DataSourceVersion2Command.cs 4.5 KB

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