citra/src/core/hle/service/mic_u.cpp

341 lines
11 KiB
C++
Raw Normal View History

2014-10-30 01:38:33 +00:00
// Copyright 2014 Citra Emulator Project
2014-12-17 05:38:14 +00:00
// Licensed under GPLv2 or any later version
2014-10-30 01:38:33 +00:00
// Refer to the license.txt file included.
2016-09-23 10:47:34 +00:00
#include "common/logging/log.h"
#include "core/core.h"
2019-02-27 22:13:51 +00:00
#include "core/frontend/mic.h"
#include "core/hle/ipc.h"
#include "core/hle/ipc_helpers.h"
2016-09-23 10:47:34 +00:00
#include "core/hle/kernel/event.h"
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/kernel.h"
2016-09-23 10:47:34 +00:00
#include "core/hle/kernel/shared_memory.h"
2014-10-30 01:38:33 +00:00
#include "core/hle/service/mic_u.h"
namespace Service::MIC {
2014-10-30 01:38:33 +00:00
2019-02-27 22:13:51 +00:00
/// Microphone audio encodings.
2016-09-23 10:47:34 +00:00
enum class Encoding : u8 {
2019-02-27 22:13:51 +00:00
PCM8 = 0, ///< Unsigned 8-bit PCM.
PCM16 = 1, ///< Unsigned 16-bit PCM.
PCM8Signed = 2, ///< Signed 8-bit PCM.
PCM16Signed = 3, ///< Signed 16-bit PCM.
2016-09-23 10:47:34 +00:00
};
2019-02-27 22:13:51 +00:00
/// Microphone audio sampling rates.
2016-09-23 10:47:34 +00:00
enum class SampleRate : u8 {
2019-02-27 22:13:51 +00:00
Rate32730 = 0, ///< 32728.498 Hz
Rate16360 = 1, ///< 16364.479 Hz
Rate10910 = 2, ///< 10909.499 Hz
Rate8180 = 3 ///< 8182.1245 Hz
2016-09-23 10:47:34 +00:00
};
2019-02-27 22:13:51 +00:00
constexpr u32 GetSampleRateInHz(SampleRate sample_rate) {
switch (sample_rate) {
case SampleRate::Rate8180:
return 8180;
case SampleRate::Rate10910:
return 10910;
case SampleRate::Rate16360:
return 16360;
case SampleRate::Rate32730:
return 32730;
default:
UNREACHABLE();
}
}
struct MIC_U::Impl {
2018-10-11 19:48:16 +00:00
explicit Impl(Core::System& system) {
buffer_full_event =
system.Kernel().CreateEvent(Kernel::ResetType::OneShot, "MIC_U::buffer_full_event");
}
void MapSharedMem(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x01, 1, 2};
const u32 size = rp.Pop<u32>();
shared_memory = rp.PopObject<Kernel::SharedMemory>();
if (shared_memory) {
shared_memory->SetName("MIC_U:shared_memory");
}
2019-02-27 22:13:51 +00:00
mic->SetBackingMemory(shared_memory->GetPointer(), size);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
LOG_TRACE(Service_MIC, "MIC:U MapSharedMem called, size=0x{:X}", size);
2016-09-23 10:47:34 +00:00
}
void UnmapSharedMem(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x02, 0, 0};
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
shared_memory = nullptr;
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
LOG_TRACE(Service_MIC, "MIC:U UnmapSharedMem called");
}
2016-09-23 10:47:34 +00:00
void StartSampling(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x03, 5, 0};
2019-02-27 22:13:51 +00:00
Encoding encoding = rp.PopEnum<Encoding>();
SampleRate sample_rate = rp.PopEnum<SampleRate>();
u32 audio_buffer_offset = rp.PopRaw<u32>();
u32 audio_buffer_size = rp.Pop<u32>();
bool audio_buffer_loop = rp.Pop<bool>();
auto sign = encoding == Encoding::PCM8Signed || encoding == Encoding::PCM16Signed
? Frontend::Mic::Signedness::Signed
: Frontend::Mic::Signedness::Unsigned;
u8 sample_size = encoding == Encoding::PCM8Signed || encoding == Encoding::PCM8 ? 8 : 16;
mic->StartSampling({sign, sample_size, audio_buffer_loop, GetSampleRateInHz(sample_rate),
audio_buffer_offset, audio_buffer_size});
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
LOG_TRACE(Service_MIC,
"MIC:U StartSampling called, encoding={}, sample_rate={}, "
"audio_buffer_offset={}, audio_buffer_size={}, audio_buffer_loop={}",
static_cast<u32>(encoding), static_cast<u32>(sample_rate), audio_buffer_offset,
audio_buffer_size, audio_buffer_loop);
}
2016-09-23 10:47:34 +00:00
void AdjustSampling(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x04, 1, 0};
2019-02-27 22:13:51 +00:00
SampleRate sample_rate = rp.PopEnum<SampleRate>();
mic->AdjustSampleRate(GetSampleRateInHz(sample_rate));
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
LOG_TRACE(Service_MIC, "MIC:U AdjustSampling sample_rate={}",
static_cast<u32>(sample_rate));
}
2016-09-23 10:47:34 +00:00
void StopSampling(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x05, 0, 0};
2019-02-27 22:13:51 +00:00
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
mic->StopSampling();
LOG_TRACE(Service_MIC, "MIC:U StopSampling called");
}
void IsSampling(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x06, 0, 0};
2019-02-27 22:13:51 +00:00
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
bool is_sampling = mic->IsSampling();
rb.Push<bool>(is_sampling);
2019-02-27 22:13:51 +00:00
LOG_TRACE(Service_MIC, "MIC:U IsSampling: {}", is_sampling);
}
void GetBufferFullEvent(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x07, 0, 0};
2019-02-27 22:13:51 +00:00
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS);
rb.PushCopyObjects(buffer_full_event);
2018-06-29 11:18:07 +00:00
LOG_WARNING(Service_MIC, "(STUBBED) called");
}
void SetGain(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x08, 1, 0};
2019-02-27 22:13:51 +00:00
u8 gain = rp.Pop<u8>();
mic->SetGain(gain);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
LOG_TRACE(Service_MIC, "MIC:U SetGain gain={}", gain);
}
void GetGain(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x09, 0, 0};
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
u8 gain = mic->GetGain();
rb.Push<u8>(gain);
LOG_TRACE(Service_MIC, "MIC:U GetGain gain={}", gain);
}
void SetPower(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x0A, 1, 0};
2019-02-27 22:13:51 +00:00
bool power = rp.Pop<bool>();
mic->SetPower(power);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
LOG_TRACE(Service_MIC, "MIC:U SetPower mic_power={}", power);
}
void GetPower(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x0B, 0, 0};
2019-02-27 22:13:51 +00:00
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS);
2019-02-27 22:13:51 +00:00
bool mic_power = mic->GetPower();
rb.Push<u8>(mic_power);
2019-02-27 22:13:51 +00:00
LOG_TRACE(Service_MIC, "MIC:U GetPower called");
}
void SetIirFilterMic(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x0C, 1, 2};
const u32 size = rp.Pop<u32>();
const Kernel::MappedBuffer& buffer = rp.PopMappedBuffer();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS);
rb.PushMappedBuffer(buffer);
2018-06-29 11:18:07 +00:00
LOG_WARNING(Service_MIC, "(STUBBED) called, size=0x{:X}, buffer=0x{:08X}", size,
2018-06-29 13:56:12 +00:00
buffer.GetId());
}
void SetClamp(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x0D, 1, 0};
clamp = rp.Pop<bool>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
2018-06-29 11:18:07 +00:00
LOG_WARNING(Service_MIC, "(STUBBED) called, clamp={}", clamp);
}
void GetClamp(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x0E, 0, 0};
2019-02-27 22:13:51 +00:00
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS);
rb.Push<bool>(clamp);
2018-06-29 11:18:07 +00:00
LOG_WARNING(Service_MIC, "(STUBBED) called");
}
void SetAllowShellClosed(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x0F, 1, 0};
allow_shell_closed = rp.Pop<bool>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
2018-06-29 11:18:07 +00:00
LOG_WARNING(Service_MIC, "(STUBBED) called, allow_shell_closed={}", allow_shell_closed);
}
void SetClientVersion(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx, 0x10, 1, 0};
const u32 version = rp.Pop<u32>();
2018-06-29 11:18:07 +00:00
LOG_WARNING(Service_MIC, "(STUBBED) called, version: 0x{:08X}", version);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
}
2019-02-27 22:13:51 +00:00
Kernel::SharedPtr<Kernel::Event> buffer_full_event =
Core::System::GetInstance().Kernel().CreateEvent(Kernel::ResetType::OneShot,
"MIC_U::buffer_full_event");
Kernel::SharedPtr<Kernel::SharedMemory> shared_memory;
2019-02-27 22:13:51 +00:00
u32 client_version = 0;
bool allow_shell_closed = false;
bool clamp = false;
2019-02-27 22:13:51 +00:00
std::shared_ptr<Frontend::Mic::Interface> mic;
};
void MIC_U::MapSharedMem(Kernel::HLERequestContext& ctx) {
impl->MapSharedMem(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::UnmapSharedMem(Kernel::HLERequestContext& ctx) {
impl->UnmapSharedMem(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::StartSampling(Kernel::HLERequestContext& ctx) {
impl->StartSampling(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::AdjustSampling(Kernel::HLERequestContext& ctx) {
impl->AdjustSampling(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::StopSampling(Kernel::HLERequestContext& ctx) {
impl->StopSampling(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::IsSampling(Kernel::HLERequestContext& ctx) {
impl->IsSampling(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::GetBufferFullEvent(Kernel::HLERequestContext& ctx) {
impl->GetBufferFullEvent(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::SetGain(Kernel::HLERequestContext& ctx) {
impl->SetGain(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::GetGain(Kernel::HLERequestContext& ctx) {
impl->GetGain(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::SetPower(Kernel::HLERequestContext& ctx) {
impl->SetPower(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::GetPower(Kernel::HLERequestContext& ctx) {
impl->GetPower(ctx);
2016-09-23 10:47:34 +00:00
}
void MIC_U::SetIirFilterMic(Kernel::HLERequestContext& ctx) {
impl->SetIirFilterMic(ctx);
}
2016-11-20 05:50:48 +00:00
void MIC_U::SetClamp(Kernel::HLERequestContext& ctx) {
impl->SetClamp(ctx);
}
2016-11-20 05:50:48 +00:00
void MIC_U::GetClamp(Kernel::HLERequestContext& ctx) {
impl->GetClamp(ctx);
}
2016-11-20 05:50:48 +00:00
void MIC_U::SetAllowShellClosed(Kernel::HLERequestContext& ctx) {
impl->SetAllowShellClosed(ctx);
2016-11-20 05:50:48 +00:00
}
void MIC_U::SetClientVersion(Kernel::HLERequestContext& ctx) {
impl->SetClientVersion(ctx);
}
2014-10-30 01:38:33 +00:00
2018-10-11 19:48:16 +00:00
MIC_U::MIC_U(Core::System& system)
: ServiceFramework{"mic:u", 1}, impl{std::make_unique<Impl>(system)} {
static const FunctionInfo functions[] = {
{0x00010042, &MIC_U::MapSharedMem, "MapSharedMem"},
{0x00020000, &MIC_U::UnmapSharedMem, "UnmapSharedMem"},
{0x00030140, &MIC_U::StartSampling, "StartSampling"},
{0x00040040, &MIC_U::AdjustSampling, "AdjustSampling"},
{0x00050000, &MIC_U::StopSampling, "StopSampling"},
{0x00060000, &MIC_U::IsSampling, "IsSampling"},
{0x00070000, &MIC_U::GetBufferFullEvent, "GetBufferFullEvent"},
{0x00080040, &MIC_U::SetGain, "SetGain"},
{0x00090000, &MIC_U::GetGain, "GetGain"},
{0x000A0040, &MIC_U::SetPower, "SetPower"},
{0x000B0000, &MIC_U::GetPower, "GetPower"},
{0x000C0042, &MIC_U::SetIirFilterMic, "SetIirFilterMic"},
{0x000D0040, &MIC_U::SetClamp, "SetClamp"},
{0x000E0000, &MIC_U::GetClamp, "GetClamp"},
{0x000F0040, &MIC_U::SetAllowShellClosed, "SetAllowShellClosed"},
{0x00100040, &MIC_U::SetClientVersion, "SetClientVersion"},
};
2019-02-27 22:13:51 +00:00
impl->mic = Frontend::GetCurrentMic();
RegisterHandlers(functions);
2016-09-23 10:47:34 +00:00
}
2019-02-27 22:13:51 +00:00
MIC_U::~MIC_U() {
impl->mic->StopSampling();
}
void InstallInterfaces(Core::System& system) {
auto& service_manager = system.ServiceManager();
2018-10-11 19:48:16 +00:00
std::make_shared<MIC_U>(system)->InstallAsService(service_manager);
2014-10-30 01:38:33 +00:00
}
} // namespace Service::MIC