summaryrefslogtreecommitdiff
path: root/Source/Core/AudioCommon
diff options
context:
space:
mode:
authorSepalani <sepalani@hotmail.fr>2025-11-27 14:58:34 +0400
committerSepalani <sepalani@hotmail.fr>2026-11-03 10:26:06 +0400
commit17d34ffc5c472c030a18284fb1ee3a4fd9776ccd (patch)
treefd000f678553c64e1dd0ff935636d06796e552be /Source/Core/AudioCommon
parentdf4bd441dec5d6f3cff606eea76f183963e97a44 (diff)
Move Microphone classes to AudioCommon
Diffstat (limited to 'Source/Core/AudioCommon')
-rw-r--r--Source/Core/AudioCommon/CMakeLists.txt2
-rw-r--r--Source/Core/AudioCommon/Microphone.cpp387
-rw-r--r--Source/Core/AudioCommon/Microphone.h127
3 files changed, 516 insertions, 0 deletions
diff --git a/Source/Core/AudioCommon/CMakeLists.txt b/Source/Core/AudioCommon/CMakeLists.txt
index 04ad967234..0db5600513 100644
--- a/Source/Core/AudioCommon/CMakeLists.txt
+++ b/Source/Core/AudioCommon/CMakeLists.txt
@@ -3,6 +3,8 @@ add_library(audiocommon
AudioCommon.h
CubebStream.h
Enums.h
+ Microphone.cpp
+ Microphone.h
Mixer.cpp
Mixer.h
SurroundDecoder.cpp
diff --git a/Source/Core/AudioCommon/Microphone.cpp b/Source/Core/AudioCommon/Microphone.cpp
new file mode 100644
index 0000000000..6cda1c90bb
--- /dev/null
+++ b/Source/Core/AudioCommon/Microphone.cpp
@@ -0,0 +1,387 @@
+// Copyright 2025 Dolphin Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "AudioCommon/Microphone.h"
+
+#include <algorithm>
+#include <cmath>
+#include <ranges>
+#include <span>
+
+#ifdef HAVE_CUBEB
+#include <cubeb/cubeb.h>
+
+#include "AudioCommon/CubebUtils.h"
+#endif
+
+#include "Common/Logging/Log.h"
+#include "Common/MathUtil.h"
+#include "Common/Swap.h"
+#include "Core/Core.h"
+#include "Core/System.h"
+
+#ifdef _WIN32
+#include <objbase.h>
+#endif
+
+#ifdef ANDROID
+#include "jni/AndroidCommon/IDCache.h"
+#endif
+
+namespace AudioCommon
+{
+#ifdef HAVE_CUBEB
+Microphone::Microphone(const MicrophoneState& sampler, std::string worker_name)
+ : m_sampler(sampler), m_worker(std::move(worker_name))
+{
+}
+#else
+Microphone::Microphone(const MicrophoneState& sampler, std::string worker_name) : m_sampler(sampler)
+{
+}
+#endif
+
+Microphone::~Microphone()
+{
+ StreamTerminate();
+}
+
+void Microphone::Initialize()
+{
+ StreamInit();
+}
+
+#ifndef HAVE_CUBEB
+void Microphone::StreamInit()
+{
+}
+
+void Microphone::StreamStart(u32 /* sampling_rate */)
+{
+}
+
+void Microphone::StreamStop()
+{
+}
+
+void Microphone::StreamTerminate()
+{
+}
+#else
+void Microphone::StreamInit()
+{
+ if (!m_worker.Execute([this] { m_cubeb_ctx = CubebUtils::GetContext(); }))
+ {
+ ERROR_LOG_FMT(AUDIO, "Failed to init microphone stream");
+ return;
+ }
+
+ // TODO: Not here but rather inside the WiiSpeak device if possible?
+ StreamStart(m_sampler.GetDefaultSamplingRate());
+}
+
+void Microphone::StreamTerminate()
+{
+ StreamStop();
+
+ if (m_cubeb_ctx)
+ m_worker.Execute([this] { m_cubeb_ctx.reset(); });
+}
+
+static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state)
+{
+}
+
+void Microphone::StreamStart(u32 sampling_rate)
+{
+ if (!m_cubeb_ctx)
+ return;
+
+ m_worker.Execute([this, sampling_rate] {
+#ifdef ANDROID
+ JNIEnv* env = IDCache::GetEnvForThread();
+ if (jboolean result = env->CallStaticBooleanMethod(
+ IDCache::GetPermissionHandlerClass(),
+ IDCache::GetPermissionHandlerHasRecordAudioPermission(), nullptr);
+ result == JNI_FALSE)
+ {
+ env->CallStaticVoidMethod(IDCache::GetPermissionHandlerClass(),
+ IDCache::GetPermissionHandlerRequestRecordAudioPermission(),
+ nullptr);
+ }
+#endif
+
+ cubeb_stream_params params{};
+ params.format = CUBEB_SAMPLE_S16LE;
+ params.rate = sampling_rate;
+ params.channels = 1;
+ params.layout = CUBEB_LAYOUT_MONO;
+
+ std::lock_guard lock(m_ring_lock);
+ u32 minimum_latency;
+ if (cubeb_get_min_latency(m_cubeb_ctx.get(), &params, &minimum_latency) != CUBEB_OK)
+ {
+ WARN_LOG_FMT(AUDIO, "Error getting minimum latency");
+ minimum_latency = 16;
+ }
+
+ cubeb_devid input_device = CubebUtils::GetInputDeviceById(GetInputDeviceId());
+ if (cubeb_stream_init(m_cubeb_ctx.get(), &m_cubeb_stream, GetCubebStreamName().c_str(),
+ input_device, &params, nullptr, nullptr,
+ std::max<u32>(16, minimum_latency), CubebDataCallback, StateCallback,
+ this) != CUBEB_OK)
+ {
+ ERROR_LOG_FMT(AUDIO, "Error initializing cubeb stream");
+ return;
+ }
+
+ if (cubeb_stream_start(m_cubeb_stream) != CUBEB_OK)
+ {
+ ERROR_LOG_FMT(AUDIO, "Error starting cubeb stream");
+ return;
+ }
+
+ m_stream_buffer.resize(GetStreamSize());
+ m_stream_wpos = 0;
+ INFO_LOG_FMT(AUDIO, "started cubeb stream");
+ });
+}
+
+void Microphone::StreamStop()
+{
+ if (!m_cubeb_stream)
+ return;
+
+ m_worker.Execute([this] {
+ if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK)
+ ERROR_LOG_FMT(AUDIO, "Error stopping cubeb stream");
+ cubeb_stream_destroy(m_cubeb_stream);
+ m_cubeb_stream = nullptr;
+ });
+}
+
+long Microphone::CubebDataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
+ void* /*output_buffer*/, long nframes)
+{
+ // Skip data when core isn't running
+ if (Core::GetState(Core::System::GetInstance()) != Core::State::Running)
+ return nframes;
+
+ auto* mic = static_cast<Microphone*>(user_data);
+
+ // Skip data when HLE Wii Speak is muted
+ // TODO: Update cubeb and use cubeb_stream_set_input_mute
+ if (mic->IsMicrophoneMuted())
+ return nframes;
+
+ return mic->DataCallback(static_cast<const SampleType*>(input_buffer), nframes);
+}
+
+long Microphone::DataCallback(const SampleType* input_buffer, long nframes)
+{
+ std::lock_guard lock(m_ring_lock);
+
+ // Skip data if sampling is off or mute is on
+ if (!m_sampler.IsSampleOn() || m_sampler.IsMuted())
+ return nframes;
+
+ std::span<const SampleType> buffer(input_buffer, nframes);
+ const auto gain = ComputeGain(GetVolumeModifier());
+ const auto apply_gain = [gain](SampleType sample) {
+ return MathUtil::SaturatingCast<SampleType>(sample * gain);
+ };
+
+ const u32 stream_size = GetStreamSize();
+ const bool are_samples_byte_swapped = AreSamplesByteSwapped();
+ for (const SampleType le_sample : std::ranges::transform_view(buffer, apply_gain))
+ {
+ UpdateLoudness(le_sample);
+ m_stream_buffer[m_stream_wpos] =
+ are_samples_byte_swapped ? Common::swap16(le_sample) : le_sample;
+ m_stream_wpos = (m_stream_wpos + 1) % stream_size;
+ }
+
+ m_samples_avail += nframes;
+ if (m_samples_avail > stream_size)
+ {
+ m_samples_avail = stream_size;
+ }
+
+ return nframes;
+}
+#endif
+
+u16 Microphone::ReadIntoBuffer(u8* ptr, u32 size)
+{
+ static constexpr u32 SINGLE_READ_SIZE = BUFF_SIZE_SAMPLES * sizeof(SampleType);
+
+ std::lock_guard lock(m_ring_lock);
+
+ const u32 stream_size = GetStreamSize();
+ u8* begin = ptr;
+ for (u8* end = begin + size; ptr < end; ptr += SINGLE_READ_SIZE, size -= SINGLE_READ_SIZE)
+ {
+ if (size < SINGLE_READ_SIZE || m_samples_avail < BUFF_SIZE_SAMPLES)
+ break;
+
+ SampleType* last_buffer = &m_stream_buffer[m_stream_rpos];
+ std::memcpy(ptr, last_buffer, SINGLE_READ_SIZE);
+
+ m_samples_avail -= BUFF_SIZE_SAMPLES;
+ m_stream_rpos += BUFF_SIZE_SAMPLES;
+ m_stream_rpos %= stream_size;
+ }
+ return static_cast<u16>(ptr - begin);
+}
+
+u16 Microphone::GetLoudnessLevel() const
+{
+ if (m_sampler.IsMuted() || IsMicrophoneMuted())
+ return 0;
+ return m_loudness_level;
+}
+
+// Based on graphical cues on Monster Hunter 3, the level seems properly displayed with values
+// between 0 and 0x3a00.
+//
+// TODO: Proper hardware testing, documentation, formulas...
+void Microphone::UpdateLoudness(const SampleType sample)
+{
+ // Based on MH3 graphical cues, let's use a 0x4000 window
+ static const u32 WINDOW = 0x4000;
+ static const FloatType UNIT = (m_loudness.DB_MAX - m_loudness.DB_MIN) / WINDOW;
+
+ m_loudness.Update(sample);
+
+ if (m_loudness.samples_count >= m_loudness.SAMPLES_NEEDED)
+ {
+ const FloatType amp_db = m_loudness.GetAmplitudeDb();
+ m_loudness_level = static_cast<u16>((amp_db - m_loudness.DB_MIN) / UNIT);
+
+#ifdef WII_SPEAK_LOG_STATS
+ m_loudness.LogStats();
+#endif
+
+ m_loudness.Reset();
+ }
+}
+
+bool Microphone::HasData(u32 sample_count = BUFF_SIZE_SAMPLES) const
+{
+ std::lock_guard lock(m_ring_lock);
+ return m_samples_avail >= sample_count;
+}
+
+Microphone::FloatType Microphone::ComputeGain(FloatType relative_db) const
+{
+ return m_loudness.ComputeGain(relative_db);
+}
+
+void Microphone::SetSamplingRate(u32 sampling_rate)
+{
+ StreamStop();
+ StreamStart(sampling_rate);
+}
+
+const Microphone::FloatType Microphone::Loudness::DB_MIN =
+ 20 * std::log10(FloatType(1) / MAX_AMPLITUDE);
+const Microphone::FloatType Microphone::Loudness::DB_MAX = 20 * std::log10(FloatType(1));
+
+void Microphone::Loudness::Update(const SampleType sample)
+{
+ ++samples_count;
+
+ peak_min = std::min(sample, peak_min);
+ peak_max = std::max(sample, peak_max);
+ absolute_sum += std::abs(sample);
+ square_sum += std::pow(FloatType(sample), FloatType(2));
+}
+
+Microphone::SampleType Microphone::Loudness::GetPeak() const
+{
+ return std::max(std::abs(peak_min), std::abs(peak_max));
+}
+
+Microphone::FloatType Microphone::Loudness::GetDecibel(FloatType value)
+{
+ return 20 * std::log10(value);
+}
+
+Microphone::FloatType Microphone::Loudness::GetAmplitude() const
+{
+ return GetPeak() / MAX_AMPLITUDE;
+}
+
+Microphone::FloatType Microphone::Loudness::GetAmplitudeDb() const
+{
+ return GetDecibel(GetAmplitude());
+}
+
+Microphone::FloatType Microphone::Loudness::GetAbsoluteMean() const
+{
+ return FloatType(absolute_sum) / samples_count;
+}
+
+Microphone::FloatType Microphone::Loudness::GetAbsoluteMeanDb() const
+{
+ return GetDecibel(GetAbsoluteMean());
+}
+
+Microphone::FloatType Microphone::Loudness::GetRootMeanSquare() const
+{
+ return std::sqrt(square_sum / samples_count);
+}
+
+Microphone::FloatType Microphone::Loudness::GetRootMeanSquareDb() const
+{
+ return GetDecibel(GetRootMeanSquare());
+}
+
+Microphone::FloatType Microphone::Loudness::GetCrestFactor() const
+{
+ const auto rms = GetRootMeanSquare();
+ if (rms == 0)
+ return FloatType(0);
+ return GetPeak() / rms;
+}
+
+Microphone::FloatType Microphone::Loudness::GetCrestFactorDb() const
+{
+ return GetDecibel(GetCrestFactor());
+}
+
+Microphone::FloatType Microphone::Loudness::ComputeGain(FloatType db)
+{
+ return std::pow(FloatType(10), db / 20);
+}
+
+void Microphone::Loudness::Reset()
+{
+ samples_count = 0;
+ absolute_sum = 0;
+ square_sum = FloatType(0);
+ peak_min = 0;
+ peak_max = 0;
+}
+
+void Microphone::Loudness::LogStats()
+{
+ const auto amplitude = GetAmplitude();
+ const auto amplitude_db = GetDecibel(amplitude);
+ const auto rms = GetRootMeanSquare();
+ const auto rms_db = GetDecibel(rms);
+ const auto abs_mean = GetAbsoluteMean();
+ const auto abs_mean_db = GetDecibel(abs_mean);
+ const auto crest_factor = GetCrestFactor();
+ const auto crest_factor_db = GetDecibel(crest_factor);
+
+ INFO_LOG_FMT(AUDIO,
+ "Microphone loudness stats (sample count: {}/{}):\n"
+ " - min={} max={} amplitude={} ({} dB)\n"
+ " - rms={} ({} dB) \n"
+ " - abs_mean={} ({} dB)\n"
+ " - crest_factor={} ({} dB)",
+ samples_count, SAMPLES_NEEDED, peak_min, peak_max, amplitude, amplitude_db, rms,
+ rms_db, abs_mean, abs_mean_db, crest_factor, crest_factor_db);
+}
+} // namespace AudioCommon
diff --git a/Source/Core/AudioCommon/Microphone.h b/Source/Core/AudioCommon/Microphone.h
new file mode 100644
index 0000000000..ceabdf1aec
--- /dev/null
+++ b/Source/Core/AudioCommon/Microphone.h
@@ -0,0 +1,127 @@
+// Copyright 2025 Dolphin Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <atomic>
+#include <limits>
+#include <memory>
+#include <mutex>
+#include <type_traits>
+#include <vector>
+
+#include "Common/CommonTypes.h"
+
+#ifdef HAVE_CUBEB
+#include "AudioCommon/CubebUtils.h"
+
+struct cubeb;
+struct cubeb_stream;
+#endif
+
+namespace AudioCommon
+{
+class MicrophoneState
+{
+public:
+ virtual ~MicrophoneState() = default;
+
+ virtual bool IsSampleOn() const = 0;
+ virtual bool IsMuted() const = 0;
+ virtual u32 GetDefaultSamplingRate() const = 0;
+};
+
+class Microphone
+{
+public:
+ using FloatType = float;
+ using SampleType = s16;
+ using UnsignedSampleType = std::make_unsigned_t<SampleType>;
+
+ Microphone(const MicrophoneState& sampler, std::string worker_name);
+ virtual ~Microphone();
+
+ void Initialize();
+ bool HasData(u32 sample_count) const;
+ u16 ReadIntoBuffer(u8* ptr, u32 size);
+ u16 GetLoudnessLevel() const;
+ FloatType ComputeGain(FloatType relative_db) const;
+ void SetSamplingRate(u32 sampling_rate);
+
+protected:
+ static constexpr u32 BUFF_SIZE_SAMPLES = 32;
+
+private:
+#ifdef HAVE_CUBEB
+ static long CubebDataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
+ void* output_buffer, long nframes);
+ virtual std::string GetInputDeviceId() const = 0;
+ virtual std::string GetCubebStreamName() const = 0;
+ virtual s16 GetVolumeModifier() const = 0;
+ virtual bool AreSamplesByteSwapped() const = 0;
+#endif
+
+ long DataCallback(const SampleType* input_buffer, long nframes);
+ virtual bool IsMicrophoneMuted() const = 0;
+ void UpdateLoudness(SampleType sample);
+
+ void StreamInit();
+ void StreamTerminate();
+ void StreamStart(u32 sampling_rate);
+ void StreamStop();
+
+ virtual u32 GetStreamSize() const = 0;
+ std::vector<SampleType> m_stream_buffer{};
+ u32 m_stream_wpos = 0;
+ u32 m_stream_rpos = 0;
+ u32 m_samples_avail = 0;
+
+ // TODO: Find how this level is calculated on real hardware
+ std::atomic<u16> m_loudness_level = 0;
+ struct Loudness
+ {
+ void Update(SampleType sample);
+
+ SampleType GetPeak() const;
+ static FloatType GetDecibel(FloatType value);
+ FloatType GetAmplitude() const;
+ FloatType GetAmplitudeDb() const;
+ FloatType GetAbsoluteMean() const;
+ FloatType GetAbsoluteMeanDb() const;
+ FloatType GetRootMeanSquare() const;
+ FloatType GetRootMeanSquareDb() const;
+ FloatType GetCrestFactor() const;
+ FloatType GetCrestFactorDb() const;
+ static FloatType ComputeGain(FloatType db);
+
+ void Reset();
+ void LogStats();
+
+ // Samples used to compute the loudness level (arbitrarily chosen)
+ static constexpr u16 SAMPLES_NEEDED = 128;
+ static_assert((SAMPLES_NEEDED % BUFF_SIZE_SAMPLES) == 0);
+
+ static constexpr FloatType MAX_AMPLITUDE =
+ UnsignedSampleType{std::numeric_limits<UnsignedSampleType>::max() / 2};
+ static const FloatType DB_MIN;
+ static const FloatType DB_MAX;
+
+ u16 samples_count = 0;
+ u32 absolute_sum = 0;
+ FloatType square_sum = FloatType(0);
+ SampleType peak_min = 0;
+ SampleType peak_max = 0;
+ };
+ Loudness m_loudness;
+
+ mutable std::mutex m_ring_lock;
+
+ const MicrophoneState& m_sampler;
+
+#ifdef HAVE_CUBEB
+ std::shared_ptr<cubeb> m_cubeb_ctx = nullptr;
+ cubeb_stream* m_cubeb_stream = nullptr;
+ CubebUtils::CoInitSyncWorker m_worker;
+#endif
+};
+} // namespace AudioCommon