libnx  v4.2.0
audren.h
Go to the documentation of this file.
1 /**
2  * @file audren.h
3  * @brief Audio renderer service.
4  * @author fincs
5  * @copyright libnx Authors
6  */
7 #pragma once
8 
9 #include "../types.h"
10 #include "../audio/audio.h"
11 #include "../sf/service.h"
12 
13 #define AUDREN_TIMER_FREQ_HZ 200.0f
14 #define AUDREN_TIMER_PERIOD_MS 5.0f
15 #define AUDREN_SAMPLES_PER_FRAME_32KHZ 160
16 #define AUDREN_SAMPLES_PER_FRAME_48KHZ 240
17 
18 #define AUDREN_INPUT_PARAM_ALIGNMENT 0x1000
19 #define AUDREN_OUTPUT_PARAM_ALIGNMENT 0x10
20 #define AUDREN_MEMPOOL_ALIGNMENT 0x1000
21 #define AUDREN_BUFFER_ALIGNMENT 0x40
22 
23 #define AUDREN_REVISION_1 0x31564552 // REV1 [1.0.0+]
24 #define AUDREN_REVISION_2 0x32564552 // REV2 [2.0.0+]
25 #define AUDREN_REVISION_3 0x33564552 // REV3 [3.0.0+]
26 #define AUDREN_REVISION_4 0x34564552 // REV4 [4.0.0+]
27 #define AUDREN_REVISION_5 0x35564552 // REV5 [6.0.0+]
28 #define AUDREN_REVISION_6 0x36564552 // REV6 [6.1.0+]
29 
30 #define AUDREN_NODEID(_a,_b,_c) ((((u32)(_a) & 0xF) << 28) | (((u32)(_b) & 0xFFF) << 16) | ((u32)(_c) & 0xFFFF))
31 #define AUDREN_FINAL_MIX_ID 0
32 #define AUDREN_UNUSED_MIX_ID 0x7FFFFFFF
33 #define AUDREN_UNUSED_SPLITTER_ID 0xFFFFFFFF
34 
35 #define AUDREN_DEFAULT_DEVICE_NAME "MainAudioOut"
36 
37 typedef enum {
38  AudioRendererOutputRate_32kHz,
39  AudioRendererOutputRate_48kHz,
40 } AudioRendererOutputRate;
41 
42 typedef struct {
43  AudioRendererOutputRate output_rate;
44  int num_voices;
45  int num_effects;
46  int num_sinks;
47  int num_mix_objs;
48  int num_mix_buffers;
50 
51 /*
52 Input buffer layout:
53 
54 AudioRendererUpdateDataHeader
55 AudioRendererBehaviorInfoIn
56 AudioRendererMemPoolInfoIn * mempool_count
57 AudioRendererChannelInfoIn * channel_count
58 AudioRendererVoiceInfoIn * voice_count
59 (effects would go here)
60 (splitters would go here)
61 AudioRendererMixInfoIn * mix_count (i.e. submix_count+1)
62 AudioRendererSinkInfoIn * sink_count
63 AudioRendererPerformanceBufferInfoIn
64 */
65 
66 /*
67 Output buffer layout:
68 
69 AudioRendererUpdateDataHeader
70 AudioRendererMemPoolInfoOut * mempool_count
71 AudioRendererVoiceInfoOut * voice_count
72 (effects would go here)
73 AudioRendererSinkInfoOut * sink_count
74 AudioRendererPerformanceBufferInfoOut
75 AudioRendererBehaviorInfoOut
76 */
77 
78 typedef struct {
79  u32 revision;
80  u32 behavior_sz;
81  u32 mempools_sz;
82  u32 voices_sz;
83  u32 channels_sz;
84  u32 effects_sz;
85  u32 mixes_sz;
86  u32 sinks_sz;
87  u32 perfmgr_sz;
88  u32 _padding[6];
89  u32 total_sz;
91 
92 typedef struct {
93  u32 revision;
94  u32 _padding1;
95  u64 flags;
97 
98 typedef struct {
99  u64 unknown[20];
100  u64 _padding1[2];
102 
103 typedef enum {
104  AudioRendererMemPoolState_Invalid,
105  AudioRendererMemPoolState_New,
106  AudioRendererMemPoolState_RequestDetach,
107  AudioRendererMemPoolState_Detached,
108  AudioRendererMemPoolState_RequestAttach,
109  AudioRendererMemPoolState_Attached,
110  AudioRendererMemPoolState_Released,
111 } AudioRendererMemPoolState;
112 
113 typedef struct {
114  const void* address;
115  u64 size;
116  AudioRendererMemPoolState state;
117  u32 _padding2[3];
119 
120 typedef struct
121 {
122  AudioRendererMemPoolState new_state;
123  u32 _padding2[3];
125 
126 typedef struct {
127  u32 id;
128  float mix[24];
129  bool is_used;
130  u8 _padding1[11];
132 
133 typedef struct {
134  bool enable;
135  u8 _padding;
136  s16 numerator[3];
137  s16 denominator[2];
139 
140 typedef struct {
141  u16 coefficients[16];
143 
144 typedef struct {
145  u16 index;
146  s16 history0;
147  s16 history1;
149 
150 typedef struct {
151  const void* address;
152  u64 size;
153  s32 start_sample_offset;
154  s32 end_sample_offset;
155  bool is_looping;
156  bool end_of_stream;
157  bool sent_to_server;
158  u8 _padding1[5];
159  const void* context_addr;
160  u64 context_sz;
161  u64 _padding2;
163 
164 typedef enum {
165  AudioRendererVoicePlayState_Started,
166  AudioRendererVoicePlayState_Stopped,
167  AudioRendererVoicePlayState_Paused,
168 } AudioRendererVoicePlayState;
169 
170 typedef struct {
171  u32 id;
172  u32 node_id;
173  bool is_new;
174  bool is_used;
175  AudioRendererVoicePlayState state : 8;
176  PcmFormat sample_format : 8;
177  u32 sample_rate;
178  u32 priority;
179  u32 sorting_order;
180  u32 channel_count;
181  float pitch;
182  float volume;
183  AudioRendererBiquadFilter biquads[2];
184  u32 wavebuf_count;
185  s16 wavebuf_head;
186  u16 _padding1;
187  u32 _padding2;
188  const void* extra_params_ptr;
189  u64 extra_params_sz;
190  u32 dest_mix_id;
191  u32 dest_splitter_id;
192  AudioRendererWaveBuf wavebufs[4];
193  u32 channel_ids[6];
194  u8 _padding3[24];
196 
197 typedef struct {
198  u64 played_sample_count;
199  u32 num_wavebufs_consumed;
200  u32 voice_drops_count;
202 
203 typedef struct {
204  float volume;
205  u32 sample_rate;
206  u32 buffer_count;
207  bool is_used;
208  u8 _padding1[3];
209  u32 mix_id;
210  u32 _padding2;
211  u32 node_id;
212  u32 _padding3[2];
213  float mix[24][24]; // [src_index][dest_index]
214  u32 dest_mix_id;
215  u32 dest_splitter_id;
216  u32 _padding4;
218 
219 typedef struct {
220  u8 coefficients[16];
222 
223 typedef enum {
224  AudioRendererSinkType_Invalid,
225  AudioRendererSinkType_Device,
226  AudioRendererSinkType_CircularBuffer,
227 } AudioRendererSinkType;
228 
229 typedef struct {
230  char name[255];
231  u8 _padding1;
232  u32 input_count;
233  u8 inputs[6];
234  u8 _padding2;
235  bool downmix_params_enabled;
236  AudioRendererDownMixParameters downmix_params;
238 
239 typedef struct {
240  void* buffer_ptr;
241  u32 buffer_sz;
242  u32 input_count;
243  u32 sample_count;
244  u32 last_read_offset;
245  PcmFormat sample_format;
246  u8 inputs[6];
247  u8 _padding2[6];
249 
250 typedef struct {
251  AudioRendererSinkType type : 8;
252  bool is_used;
253  u8 _padding1[2];
254  u32 node_id;
255  u64 _padding2[3];
256  union {
257  AudioRendererDeviceSinkInfoIn device_sink;
258  AudioRendererCircularBufferSinkInfoIn circular_buffer_sink;
259  };
261 
262 typedef struct {
263  u32 last_written_offset;
264  u32 unk1;
265  u64 unk2;
266  u64 _padding1[2];
268 
269 typedef struct {
270  u32 detail_target;
271  u32 _padding1[3];
273 
274 typedef struct {
275  u32 written_sz;
276  u32 _padding1[3];
278 
279 static inline u32 audrenGetRevision(void)
280 {
281  extern u32 g_audrenRevision;
282  return g_audrenRevision;
283 }
284 
285 NX_CONSTEXPR int audrenGetMemPoolCount(const AudioRendererConfig* config)
286 {
287  return config->num_effects + 4 * config->num_voices;
288 }
289 
290 NX_CONSTEXPR size_t audrenGetInputParamSize(const AudioRendererConfig* config)
291 {
292  size_t size = 0;
293  size += sizeof(AudioRendererUpdateDataHeader);
294  size += sizeof(AudioRendererBehaviorInfoIn);
295  size += sizeof(AudioRendererMemPoolInfoIn) * audrenGetMemPoolCount(config);
296  size += sizeof(AudioRendererChannelInfoIn) * config->num_voices;
297  size += sizeof(AudioRendererVoiceInfoIn) * config->num_voices;
298  // todo: effects, splitters
299  size += sizeof(AudioRendererMixInfoIn) * config->num_mix_objs;
300  size += sizeof(AudioRendererSinkInfoIn) * config->num_sinks;
301  size += sizeof(AudioRendererPerformanceBufferInfoIn);
302  return size;
303 }
304 
305 NX_CONSTEXPR size_t audrenGetOutputParamSize(const AudioRendererConfig* config)
306 {
307  size_t size = 0;
308  size += sizeof(AudioRendererUpdateDataHeader);
309  size += sizeof(AudioRendererMemPoolInfoOut) * audrenGetMemPoolCount(config);
310  size += sizeof(AudioRendererVoiceInfoOut) * config->num_voices;
311  // todo: effects
312  size += sizeof(AudioRendererSinkInfoOut) * config->num_sinks;
314  size += sizeof(AudioRendererBehaviorInfoOut);
315  return size;
316 }
317 
318 /// Initialize audren.
320 
321 /// Exit audren.
322 void audrenExit(void);
323 
324 /// Gets the Service object for IAudioRenderer.
326 
327 void audrenWaitFrame(void);
328 Result audrenGetState(u32* out_state);
329 Result audrenRequestUpdateAudioRenderer(const void* in_param_buf, size_t in_param_buf_size, void* out_param_buf, size_t out_param_buf_size, void* perf_buf, size_t perf_buf_size);
330 Result audrenStartAudioRenderer(void);
331 Result audrenStopAudioRenderer(void);
332 Result audrenSetAudioRendererRenderingTimeLimit(int percent);
AudioRendererVoiceInfoIn
Definition: audren.h:170
AudioRendererWaveBuf
Definition: audren.h:150
AudioRendererBiquadFilter
Definition: audren.h:133
audrenExit
void audrenExit(void)
Exit audren.
AudioRendererAdpcmParameters
Definition: audren.h:140
u8
uint8_t u8
8-bit unsigned integer.
Definition: types.h:19
NX_CONSTEXPR
#define NX_CONSTEXPR
Flags a function as constexpr in C++14 and above; or as (always) inline otherwise.
Definition: types.h:88
AudioRendererPerformanceBufferInfoIn
Definition: audren.h:269
s16
int16_t s16
16-bit signed integer.
Definition: types.h:26
AudioRendererDeviceSinkInfoIn
Definition: audren.h:229
AudioRendererCircularBufferSinkInfoIn
Definition: audren.h:239
AudioRendererPerformanceBufferInfoOut
Definition: audren.h:274
s32
int32_t s32
32-bit signed integer.
Definition: types.h:27
AudioRendererBehaviorInfoOut
Definition: audren.h:98
AudioRendererMemPoolInfoIn
Definition: audren.h:113
AudioRendererChannelInfoIn
Definition: audren.h:126
u32
uint32_t u32
32-bit unsigned integer.
Definition: types.h:21
AudioRendererSinkInfoIn
Definition: audren.h:250
u64
uint64_t u64
64-bit unsigned integer.
Definition: types.h:22
AudioRendererUpdateDataHeader
Definition: audren.h:78
PcmFormat
PcmFormat
PcmFormat.
Definition: audio.h:12
Result
u32 Result
Function error code result type.
Definition: types.h:44
audrenGetServiceSession_AudioRenderer
Service * audrenGetServiceSession_AudioRenderer(void)
Gets the Service object for IAudioRenderer.
AudioRendererVoiceInfoOut
Definition: audren.h:197
AudioRendererMixInfoIn
Definition: audren.h:203
AudioRendererSinkInfoOut
Definition: audren.h:262
AudioRendererBehaviorInfoIn
Definition: audren.h:92
AudioRendererAdpcmContext
Definition: audren.h:144
Service
Service object structure.
Definition: service.h:13
AudioRendererConfig
Definition: audren.h:42
AudioRendererMemPoolInfoOut
Definition: audren.h:120
audrenInitialize
Result audrenInitialize(const AudioRendererConfig *config)
Initialize audren.
AudioRendererDownMixParameters
Definition: audren.h:219
u16
uint16_t u16
16-bit unsigned integer.
Definition: types.h:20