libnx  v4.2.0
driver.h
Go to the documentation of this file.
1 /**
2  * @file driver.h
3  * @brief Audio driver (audren wrapper).
4  * @author fincs
5  * @copyright libnx Authors
6  */
7 #pragma once
8 #include "../services/audren.h"
9 
10 typedef struct AudioDriverEtc AudioDriverEtc;
11 
12 typedef struct {
13  AudioDriverEtc* etc;
14  AudioRendererConfig config;
15  AudioRendererMemPoolInfoIn* in_mempools;
16  AudioRendererChannelInfoIn* in_channels;
17  AudioRendererVoiceInfoIn* in_voices;
18  AudioRendererMixInfoIn* in_mixes;
19  AudioRendererSinkInfoIn* in_sinks;
20 } AudioDriver;
21 
22 Result audrvCreate(AudioDriver* d, const AudioRendererConfig* config, int num_final_mix_channels);
23 Result audrvUpdate(AudioDriver* d);
24 void audrvClose(AudioDriver* d);
25 
26 //-----------------------------------------------------------------------------
27 
28 int audrvMemPoolAdd(AudioDriver* d, void* buffer, size_t size);
29 bool audrvMemPoolRemove(AudioDriver* d, int id);
30 bool audrvMemPoolAttach(AudioDriver* d, int id);
31 bool audrvMemPoolDetach(AudioDriver* d, int id);
32 
33 //-----------------------------------------------------------------------------
34 
35 typedef enum {
36  AudioDriverWaveBufState_Free,
37  AudioDriverWaveBufState_Waiting,
38  AudioDriverWaveBufState_Queued,
39  AudioDriverWaveBufState_Playing,
40  AudioDriverWaveBufState_Done,
41 } AudioDriverWaveBufState;
42 
44 
46  union {
47  s16* data_pcm16;
48  u8* data_adpcm;
49  const void* data_raw;
50  };
51  u64 size;
52  s32 start_sample_offset;
53  s32 end_sample_offset;
54  const void* context_addr;
55  u64 context_sz;
56  AudioDriverWaveBufState state : 8;
57  bool is_looping;
58  u32 sequence_id;
59  AudioDriverWaveBuf* next;
60 };
61 
62 bool audrvVoiceInit(AudioDriver* d, int id, int num_channels, PcmFormat format, int sample_rate);
63 void audrvVoiceDrop(AudioDriver* d, int id);
64 void audrvVoiceStop(AudioDriver* d, int id);
65 bool audrvVoiceIsPaused(AudioDriver* d, int id);
66 bool audrvVoiceIsPlaying(AudioDriver* d, int id);
67 bool audrvVoiceAddWaveBuf(AudioDriver* d, int id, AudioDriverWaveBuf* wavebuf);
68 u32 audrvVoiceGetWaveBufSeq(AudioDriver* d, int id);
69 u32 audrvVoiceGetPlayedSampleCount(AudioDriver* d, int id);
70 u32 audrvVoiceGetVoiceDropsCount(AudioDriver* d, int id);
71 void audrvVoiceSetBiquadFilter(AudioDriver* d, int id, int biquad_id, float a0, float a1, float a2, float b0, float b1, float b2);
72 
73 static inline void audrvVoiceSetExtraParams(AudioDriver* d, int id, const void* params, size_t params_size)
74 {
75  d->in_voices[id].extra_params_ptr = params;
76  d->in_voices[id].extra_params_sz = params_size;
77 }
78 
79 static inline void audrvVoiceSetDestinationMix(AudioDriver* d, int id, int mix_id)
80 {
81  d->in_voices[id].dest_mix_id = mix_id;
82  d->in_voices[id].dest_splitter_id = AUDREN_UNUSED_SPLITTER_ID;
83 }
84 
85 static inline void audrvVoiceSetMixFactor(AudioDriver* d, int id, float factor, int src_channel_id, int dest_channel_id)
86 {
87  int channel_id = d->in_voices[id].channel_ids[src_channel_id];
88  d->in_channels[channel_id].mix[dest_channel_id] = factor;
89 }
90 
91 static inline void audrvVoiceSetVolume(AudioDriver* d, int id, float volume)
92 {
93  d->in_voices[id].volume = volume;
94 }
95 
96 static inline void audrvVoiceSetPitch(AudioDriver* d, int id, float pitch)
97 {
98  d->in_voices[id].pitch = pitch;
99 }
100 
101 static inline void audrvVoiceSetPriority(AudioDriver* d, int id, int priority)
102 {
103  d->in_voices[id].priority = priority;
104 }
105 
106 static inline void audrvVoiceClearBiquadFilter(AudioDriver* d, int id, int biquad_id)
107 {
108  d->in_voices[id].biquads[biquad_id].enable = false;
109 }
110 
111 static inline void audrvVoiceSetPaused(AudioDriver* d, int id, bool paused)
112 {
113  d->in_voices[id].state = paused ? AudioRendererVoicePlayState_Paused : AudioRendererVoicePlayState_Started;
114 }
115 
116 static inline void audrvVoiceStart(AudioDriver* d, int id)
117 {
118  audrvVoiceSetPaused(d, id, false);
119 }
120 
121 //-----------------------------------------------------------------------------
122 
123 int audrvMixAdd(AudioDriver* d, int sample_rate, int num_channels);
124 void audrvMixRemove(AudioDriver* d, int id);
125 
126 static inline void audrvMixSetDestinationMix(AudioDriver* d, int id, int mix_id)
127 {
128  d->in_mixes[id].dest_mix_id = mix_id;
129  d->in_mixes[id].dest_splitter_id = AUDREN_UNUSED_SPLITTER_ID;
130 }
131 
132 static inline void audrvMixSetMixFactor(AudioDriver* d, int id, float factor, int src_channel_id, int dest_channel_id)
133 {
134  d->in_mixes[id].mix[src_channel_id][dest_channel_id] = factor;
135 }
136 
137 static inline void audrvMixSetVolume(AudioDriver* d, int id, float volume)
138 {
139  d->in_mixes[id].volume = volume;
140 }
141 
142 //-----------------------------------------------------------------------------
143 
144 int audrvDeviceSinkAdd(AudioDriver* d, const char* device_name, int num_channels, const u8* channel_ids);
145 void audrvSinkRemove(AudioDriver* d, int id);
AudioRendererVoiceInfoIn
Definition: audren.h:170
u8
uint8_t u8
8-bit unsigned integer.
Definition: types.h:19
s16
int16_t s16
16-bit signed integer.
Definition: types.h:26
s32
int32_t s32
32-bit signed integer.
Definition: types.h:27
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
AudioDriverWaveBuf
Definition: driver.h:45
u64
uint64_t u64
64-bit unsigned integer.
Definition: types.h:22
PcmFormat
PcmFormat
PcmFormat.
Definition: audio.h:12
Result
u32 Result
Function error code result type.
Definition: types.h:44
AudioDriver
Definition: driver.h:12
AudioRendererMixInfoIn
Definition: audren.h:203
AudioRendererConfig
Definition: audren.h:42