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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "audio_core/audio_core.h"
#include "core/hle/service/audio/audio_controller.h"
#include "core/hle/service/audio/audio_out_manager.h"
#include "core/core.h"
#include "core/hle/service/cmif_serialization.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/set/system_settings_server.h"
#include "core/hle/service/sm/sm.h"
#include "common/settings.h"
#include <algorithm>
namespace Service::Audio {
IAudioController::IAudioController(Core::System& system_)
: ServiceFramework{system_, "audctl"}, service_context{system, "audctl"} {
// clang-format off
static const FunctionInfo functions[] = {
{0, D<&IAudioController::GetTargetVolume>, "GetTargetVolume"},
{1, D<&IAudioController::SetTargetVolume>, "SetTargetVolume"},
{2, D<&IAudioController::GetTargetVolumeMin>, "GetTargetVolumeMin"},
{3, D<&IAudioController::GetTargetVolumeMax>, "GetTargetVolumeMax"},
{4, D<&IAudioController::IsTargetMute>, "IsTargetMute"},
{5, D<&IAudioController::SetTargetMute>, "SetTargetMute"},
{6, nullptr, "IsTargetConnected"}, //20.0.0+
{7, nullptr, "SetDefaultTarget"},
{8, nullptr, "GetDefaultTarget"},
{9, D<&IAudioController::GetAudioOutputMode>, "GetAudioOutputMode"},
{10, D<&IAudioController::SetAudioOutputMode>, "SetAudioOutputMode"},
{11, nullptr, "SetForceMutePolicy"},
{12, D<&IAudioController::GetForceMutePolicy>, "GetForceMutePolicy"},
{13, D<&IAudioController::GetOutputModeSetting>, "GetOutputModeSetting"},
{14, D<&IAudioController::SetOutputModeSetting>, "SetOutputModeSetting"},
{15, nullptr, "SetOutputTarget"},
{16, nullptr, "SetInputTargetForceEnabled"},
{17, D<&IAudioController::SetHeadphoneOutputLevelMode>, "SetHeadphoneOutputLevelMode"},
{18, D<&IAudioController::GetHeadphoneOutputLevelMode>, "GetHeadphoneOutputLevelMode"},
{19, nullptr, "AcquireAudioVolumeUpdateEventForPlayReport"},
{20, nullptr, "AcquireAudioOutputDeviceUpdateEventForPlayReport"},
{21, nullptr, "GetAudioOutputTargetForPlayReport"},
{22, D<&IAudioController::NotifyHeadphoneVolumeWarningDisplayedEvent>, "NotifyHeadphoneVolumeWarningDisplayedEvent"},
{23, nullptr, "SetSystemOutputMasterVolume"},
{24, nullptr, "GetSystemOutputMasterVolume"},
{25, nullptr, "GetAudioVolumeDataForPlayReport"},
{26, nullptr, "UpdateHeadphoneSettings"},
{27, nullptr, "SetVolumeMappingTableForDev"},
{28, nullptr, "GetAudioOutputChannelCountForPlayReport"},
{29, nullptr, "BindAudioOutputChannelCountUpdateEventForPlayReport"},
{30, D<&IAudioController::SetSpeakerAutoMuteEnabled>, "SetSpeakerAutoMuteEnabled"},
{31, D<&IAudioController::IsSpeakerAutoMuteEnabled>, "IsSpeakerAutoMuteEnabled"},
{32, D<&IAudioController::GetActiveOutputTarget>, "GetActiveOutputTarget"},
{33, nullptr, "GetTargetDeviceInfo"},
{34, D<&IAudioController::AcquireTargetNotification>, "AcquireTargetNotification"},
{35, nullptr, "SetHearingProtectionSafeguardTimerRemainingTimeForDebug"},
{36, nullptr, "GetHearingProtectionSafeguardTimerRemainingTimeForDebug"},
{37, nullptr, "SetHearingProtectionSafeguardEnabled"},
{38, nullptr, "IsHearingProtectionSafeguardEnabled"},
{39, nullptr, "IsHearingProtectionSafeguardMonitoringOutputForDebug"},
{40, nullptr, "GetSystemInformationForDebug"},
{41, nullptr, "SetVolumeButtonLongPressTime"},
{42, nullptr, "SetNativeVolumeForDebug"},
{43, nullptr, "Unknown43"}, //21.0.0+
{5000, D<&IAudioController::Unknown5000>, "Unknown5000"}, //19.0.0+
{10000, nullptr, "NotifyAudioOutputTargetForPlayReport"},
{10001, nullptr, "NotifyAudioOutputChannelCountForPlayReport"},
{10002, nullptr, "NotifyUnsupportedUsbOutputDeviceAttachedForPlayReport"},
{10100, nullptr, "GetAudioVolumeDataForPlayReport"},
{10101, nullptr, "BindAudioVolumeUpdateEventForPlayReport"},
{10102, nullptr, "BindAudioOutputTargetUpdateEventForPlayReport"},
{10103, nullptr, "GetAudioOutputTargetForPlayReport"},
{10104, nullptr, "GetAudioOutputChannelCountForPlayReport"},
{10105, nullptr, "BindAudioOutputChannelCountUpdateEventForPlayReport"}, //14.0.0-19.0.1
{10106, nullptr, "GetDefaultAudioOutputTargetForPlayReport"}, //14.0.0-19.0.1
{10200, nullptr, "Unknown10200"}, //20.0.0+
{50000, nullptr, "SetAnalogInputBoostGainForPrototyping"}, //15.0.0-18.1.0
{50001, nullptr, "OverrideDefaultTargetForDebug"}, //19.0.0-19.0.1
{50003, nullptr, "SetForceOverrideExternalDeviceNameForDebug"}, //19.0.0+
{50004, nullptr, "ClearForceOverrideExternalDeviceNameForDebug"} //19.0.0+
};
// clang-format on
RegisterHandlers(functions);
m_set_sys =
system.ServiceManager().GetService<Service::Set::ISystemSettingsServer>("set:sys", true);
notification_event = service_context.CreateEvent("IAudioController:NotificationEvent");
// Probably shouldn't do this in constructor?
try {
const int ui_volume = Settings::values.volume.GetValue();
const int mapped = static_cast<int>(std::lround((static_cast<double>(ui_volume) / 100.0) * 15.0));
const auto active_idx = static_cast<size_t>(m_active_target);
if (active_idx < m_target_volumes.size()) {
m_target_volumes[active_idx] = std::clamp(mapped, 0, 15);
}
const bool muted = Settings::values.audio_muted.GetValue();
for (auto& m : m_target_muted) {
m = muted;
}
if (!muted && active_idx < m_target_volumes.size()) {
const float vol_f = static_cast<float>(m_target_volumes[active_idx]) / 15.0f;
try {
auto& sink = system.AudioCore().GetOutputSink();
sink.SetSystemVolume(vol_f);
sink.SetDeviceVolume(vol_f);
} catch (...) {
LOG_WARNING(Audio, "Failed to apply initial sink volume from settings");
}
if (auto audout_mgr = system.ServiceManager().GetService<Service::Audio::IAudioOutManager>("audout:u")) {
audout_mgr->SetAllAudioOutVolume(static_cast<float>(m_target_volumes[active_idx]) / 15.0f);
}
}
} catch (...) {
// Catch if something fails, since this is constructor
}
}
IAudioController::~IAudioController() {
service_context.CloseEvent(notification_event);
};
Result IAudioController::GetTargetVolumeMin(Out<s32> out_target_min_volume) {
LOG_DEBUG(Audio, "called.");
// This service function is currently hardcoded on the
// actual console to this value (as of 8.0.0).
*out_target_min_volume = 0;
R_SUCCEED();
}
Result IAudioController::GetTargetVolumeMax(Out<s32> out_target_max_volume) {
LOG_DEBUG(Audio, "called.");
// This service function is currently hardcoded on the
// actual console to this value (as of 8.0.0).
*out_target_max_volume = 15;
R_SUCCEED();
}
Result IAudioController::GetAudioOutputMode(Out<Set::AudioOutputMode> out_output_mode,
Set::AudioOutputModeTarget target) {
const auto result = m_set_sys->GetAudioOutputMode(out_output_mode, target);
LOG_INFO(Service_SET, "called, target={}, output_mode={}", target, *out_output_mode);
R_RETURN(result);
}
Result IAudioController::SetAudioOutputMode(Set::AudioOutputModeTarget target,
Set::AudioOutputMode output_mode) {
LOG_INFO(Service_SET, "called, target={}, output_mode={}", target, output_mode);
R_RETURN(m_set_sys->SetAudioOutputMode(target, output_mode));
}
Result IAudioController::GetForceMutePolicy(Out<ForceMutePolicy> out_mute_policy) {
LOG_WARNING(Audio, "(STUBBED) called");
// Removed on FW 13.2.1+
*out_mute_policy = ForceMutePolicy::Disable;
R_SUCCEED();
}
Result IAudioController::GetOutputModeSetting(Out<Set::AudioOutputMode> out_output_mode,
Set::AudioOutputModeTarget target) {
LOG_WARNING(Audio, "(STUBBED) called, target={}", target);
*out_output_mode = Set::AudioOutputMode::ch_7_1;
R_SUCCEED();
}
Result IAudioController::SetOutputModeSetting(Set::AudioOutputModeTarget target,
Set::AudioOutputMode output_mode) {
LOG_INFO(Service_SET, "called, target={}, output_mode={}", target, output_mode);
R_SUCCEED();
}
Result IAudioController::SetHeadphoneOutputLevelMode(HeadphoneOutputLevelMode output_level_mode) {
LOG_WARNING(Audio, "(STUBBED) called, output_level_mode={}", output_level_mode);
R_SUCCEED();
}
Result IAudioController::GetHeadphoneOutputLevelMode(
Out<HeadphoneOutputLevelMode> out_output_level_mode) {
LOG_INFO(Audio, "called");
*out_output_level_mode = HeadphoneOutputLevelMode::Normal;
R_SUCCEED();
}
Result IAudioController::NotifyHeadphoneVolumeWarningDisplayedEvent() {
LOG_WARNING(Service_Audio, "(STUBBED) called");
R_SUCCEED();
}
Result IAudioController::SetSpeakerAutoMuteEnabled(bool is_speaker_auto_mute_enabled) {
LOG_INFO(Audio, "called, is_speaker_auto_mute_enabled={}", is_speaker_auto_mute_enabled);
R_RETURN(m_set_sys->SetSpeakerAutoMuteFlag(is_speaker_auto_mute_enabled));
}
Result IAudioController::IsSpeakerAutoMuteEnabled(Out<bool> out_is_speaker_auto_mute_enabled) {
const auto result = m_set_sys->GetSpeakerAutoMuteFlag(out_is_speaker_auto_mute_enabled);
LOG_INFO(Audio, "called, is_speaker_auto_mute_enabled={}", *out_is_speaker_auto_mute_enabled);
R_RETURN(result);
}
Result IAudioController::AcquireTargetNotification(
OutCopyHandle<Kernel::KReadableEvent> out_notification_event) {
LOG_WARNING(Service_AM, "(STUBBED) called");
*out_notification_event = ¬ification_event->GetReadableEvent();
R_SUCCEED();
}
Result IAudioController::Unknown5000(Out<SharedPointer<IAudioController>> out_audio_controller) {
LOG_DEBUG(Audio, "Creating duplicate audio controller interface");
// Return a new reference to this controller instance
*out_audio_controller = std::static_pointer_cast<IAudioController>(shared_from_this());
R_SUCCEED();
}
Result IAudioController::GetTargetVolume(Out<s32> out_target_volume, Set::AudioOutputModeTarget target) {
LOG_DEBUG(Audio, "GetTargetVolume called, target={}", target);
const auto idx = static_cast<size_t>(target);
if (idx >= m_target_volumes.size()) {
LOG_ERROR(Audio, "GetTargetVolume invalid target {}", idx);
R_THROW(ResultInvalidArgument);
}
*out_target_volume = m_target_volumes[idx];
R_SUCCEED();
}
Result IAudioController::SetTargetVolume(Set::AudioOutputModeTarget target, s32 target_volume) {
LOG_INFO(Audio, "SetTargetVolume called, target={}, volume={}", target, target_volume);
const auto idx = static_cast<size_t>(target);
if (idx >= m_target_volumes.size()) {
LOG_ERROR(Audio, "SetTargetVolume invalid target {}", idx);
R_THROW(ResultInvalidArgument);
}
if (target_volume < 0) {
target_volume = 0;
} else if (target_volume > 15) {
target_volume = 15;
}
m_target_volumes[idx] = target_volume;
if (m_active_target == target && !m_target_muted[idx]) {
const float vol = static_cast<float>(target_volume) / 15.0f;
// try catch this, as we don't know how it handles it when no output is set.
// we already have audio issues when no output is set, so catch.
try {
auto& sink = system.AudioCore().GetOutputSink();
sink.SetSystemVolume(vol);
sink.SetDeviceVolume(vol);
} catch (...) {
LOG_WARNING(Audio, "Failed to set sink system volume");
}
if (auto audout_mgr = system.ServiceManager().GetService<IAudioOutManager>("audout:u")) {
audout_mgr->SetAllAudioOutVolume(vol);
}
}
if (m_active_target == target) {
const int ui_volume = static_cast<int>(std::lround((static_cast<double>(target_volume) / 15.0) * 100.0));
Settings::values.volume.SetValue(static_cast<u8>(std::clamp(ui_volume, 0, 100)));
}
R_SUCCEED();
}
Result IAudioController::IsTargetMute(Out<bool> out_is_target_muted, Set::AudioOutputModeTarget target) {
LOG_DEBUG(Audio, "called, target={}", target);
const auto idx = static_cast<size_t>(target);
if (idx >= m_target_muted.size()) {
LOG_ERROR(Audio, "invalid target {}", idx);
R_THROW(ResultInvalidArgument);
}
*out_is_target_muted = m_target_muted[idx];
R_SUCCEED();
}
Result IAudioController::SetTargetMute(bool is_muted, Set::AudioOutputModeTarget target) {
LOG_INFO(Audio, "called, target={}, muted={}", target, is_muted);
const auto idx = static_cast<size_t>(target);
if (idx >= m_target_muted.size()) {
LOG_ERROR(Audio, "invalid target {}", idx);
R_THROW(ResultInvalidArgument);
}
m_target_muted[idx] = is_muted;
if (m_active_target == target) {
try {
auto& sink = system.AudioCore().GetOutputSink();
if (is_muted) {
sink.SetSystemVolume(0.0f);
sink.SetDeviceVolume(0.0f);
} else {
const float vol = static_cast<float>(m_target_volumes[idx]) / 15.0f;
sink.SetSystemVolume(vol);
sink.SetDeviceVolume(vol);
}
} catch (...) {
LOG_WARNING(Audio, "Failed to set sink system volume on mute change");
}
// Also update any open audout sessions via the audout:u service.
if (auto audout_mgr = system.ServiceManager().GetService<IAudioOutManager>("audout:u")) {
if (is_muted) {
audout_mgr->SetAllAudioOutVolume(0.0f);
} else {
const float vol = static_cast<float>(m_target_volumes[idx]) / 15.0f;
audout_mgr->SetAllAudioOutVolume(vol);
}
}
}
Settings::values.audio_muted.SetValue(is_muted);
R_SUCCEED();
}
Result IAudioController::GetActiveOutputTarget(Out<Set::AudioOutputModeTarget> out_active_target) {
LOG_DEBUG(Audio, "GetActiveOutputTarget called");
*out_active_target = m_active_target;
R_SUCCEED();
}
} // namespace Service::Audio
|