aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Audio/Detail/AudioRenderer.cs
blob: 4d446bba7641f63129d2e4462d80e645de75dfaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using Ryujinx.Audio;
using Ryujinx.Audio.Integration;
using Ryujinx.Audio.Renderer.Server;
using Ryujinx.Common.Memory;
using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Sdk.Sf;
using Ryujinx.Horizon.Sdk.Sf.Hipc;
using System;
using System.Buffers;

namespace Ryujinx.Horizon.Sdk.Audio.Detail
{
    partial class AudioRenderer : IAudioRenderer, IDisposable
    {
        private readonly AudioRenderSystem _renderSystem;
        private int _workBufferHandle;
        private int _processHandle;

        public AudioRenderer(AudioRenderSystem renderSystem, int workBufferHandle, int processHandle)
        {
            _renderSystem = renderSystem;
            _workBufferHandle = workBufferHandle;
            _processHandle = processHandle;
        }

        [CmifCommand(0)]
        public Result GetSampleRate(out int sampleRate)
        {
            sampleRate = (int)_renderSystem.GetSampleRate();

            return Result.Success;
        }

        [CmifCommand(1)]
        public Result GetSampleCount(out int sampleCount)
        {
            sampleCount = (int)_renderSystem.GetSampleCount();

            return Result.Success;
        }

        [CmifCommand(2)]
        public Result GetMixBufferCount(out int mixBufferCount)
        {
            mixBufferCount = (int)_renderSystem.GetMixBufferCount();

            return Result.Success;
        }

        [CmifCommand(3)]
        public Result GetState(out int state)
        {
            state = _renderSystem.IsActive() ? 0 : 1;

            return Result.Success;
        }

        [CmifCommand(4)]
        public Result RequestUpdate(
            [Buffer(HipcBufferFlags.Out | HipcBufferFlags.MapAlias)] Memory<byte> output,
            [Buffer(HipcBufferFlags.Out | HipcBufferFlags.MapAlias)] Memory<byte> performanceOutput,
            [Buffer(HipcBufferFlags.In | HipcBufferFlags.MapAlias)] ReadOnlySequence<byte> input)
        {
            using MemoryHandle outputHandle = output.Pin();
            using MemoryHandle performanceOutputHandle = performanceOutput.Pin();

            Result result = new Result((int)_renderSystem.Update(output, performanceOutput, input));

            return result;
        }

        [CmifCommand(5)]
        public Result Start()
        {
            _renderSystem.Start();

            return Result.Success;
        }

        [CmifCommand(6)]
        public Result Stop()
        {
            _renderSystem.Stop();

            return Result.Success;
        }

        [CmifCommand(7)]
        public Result QuerySystemEvent([CopyHandle] out int eventHandle)
        {
            ResultCode rc = _renderSystem.QuerySystemEvent(out IWritableEvent systemEvent);

            eventHandle = 0;

            if (rc == ResultCode.Success && systemEvent is AudioEvent audioEvent)
            {
                eventHandle = audioEvent.GetReadableHandle();
            }

            return new Result((int)rc);
        }

        [CmifCommand(8)]
        public Result SetRenderingTimeLimit(int percent)
        {
            _renderSystem.SetRenderingTimeLimitPercent((uint)percent);

            return Result.Success;
        }

        [CmifCommand(9)]
        public Result GetRenderingTimeLimit(out int percent)
        {
            percent = (int)_renderSystem.GetRenderingTimeLimit();

            return Result.Success;
        }

        [CmifCommand(10)] // 3.0.0+
        public Result RequestUpdateAuto(
            [Buffer(HipcBufferFlags.Out | HipcBufferFlags.AutoSelect)] Memory<byte> output,
            [Buffer(HipcBufferFlags.Out | HipcBufferFlags.AutoSelect)] Memory<byte> performanceOutput,
            [Buffer(HipcBufferFlags.In | HipcBufferFlags.AutoSelect)] ReadOnlySequence<byte> input)
        {
            return RequestUpdate(output, performanceOutput, input);
        }

        [CmifCommand(11)] // 3.0.0+
        public Result ExecuteAudioRendererRendering()
        {
            return new Result((int)_renderSystem.ExecuteAudioRendererRendering());
        }

        [CmifCommand(12)] // 15.0.0+
        public Result SetVoiceDropParameter(float voiceDropParameter)
        {
            _renderSystem.SetVoiceDropParameter(voiceDropParameter);

            return Result.Success;
        }

        [CmifCommand(13)] // 15.0.0+
        public Result GetVoiceDropParameter(out float voiceDropParameter)
        {
            voiceDropParameter = _renderSystem.GetVoiceDropParameter();

            return Result.Success;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _renderSystem.Dispose();

                if (_workBufferHandle != 0)
                {
                    HorizonStatic.Syscall.CloseHandle(_workBufferHandle);

                    _workBufferHandle = 0;
                }

                if (_processHandle != 0)
                {
                    HorizonStatic.Syscall.CloseHandle(_processHandle);

                    _processHandle = 0;
                }
            }
        }

        public void Dispose()
        {
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }
}