diff --git a/Source/Core/AudioCommon/Microphone.cpp b/Source/Core/AudioCommon/Microphone.cpp index 6cda1c90bb..aa656e4428 100644 --- a/Source/Core/AudioCommon/Microphone.cpp +++ b/Source/Core/AudioCommon/Microphone.cpp @@ -142,6 +142,7 @@ void Microphone::StreamStart(u32 sampling_rate) } m_stream_buffer.resize(GetStreamSize()); + m_stream_rpos = 0; m_stream_wpos = 0; INFO_LOG_FMT(AUDIO, "started cubeb stream"); }); @@ -177,6 +178,11 @@ long Microphone::CubebDataCallback(cubeb_stream* stream, void* user_data, const return mic->DataCallback(static_cast(input_buffer), nframes); } +void Microphone::OnOverflow() +{ + m_samples_avail = GetStreamSize(); +} + long Microphone::DataCallback(const SampleType* input_buffer, long nframes) { std::lock_guard lock(m_ring_lock); @@ -204,7 +210,7 @@ long Microphone::DataCallback(const SampleType* input_buffer, long nframes) m_samples_avail += nframes; if (m_samples_avail > stream_size) { - m_samples_avail = stream_size; + OnOverflow(); } return nframes; @@ -213,22 +219,23 @@ long Microphone::DataCallback(const SampleType* input_buffer, long nframes) u16 Microphone::ReadIntoBuffer(u8* ptr, u32 size) { - static constexpr u32 SINGLE_READ_SIZE = BUFF_SIZE_SAMPLES * sizeof(SampleType); + const u32 samples_per_read = GetBufferSizeSamples(); + const u32 bytes_per_read = samples_per_read * 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) + u8* const begin = ptr; + for (u8* const end = begin + size; ptr < end; ptr += bytes_per_read, size -= bytes_per_read) { - if (size < SINGLE_READ_SIZE || m_samples_avail < BUFF_SIZE_SAMPLES) + if (size < bytes_per_read || m_samples_avail < samples_per_read) break; SampleType* last_buffer = &m_stream_buffer[m_stream_rpos]; - std::memcpy(ptr, last_buffer, SINGLE_READ_SIZE); + std::memcpy(ptr, last_buffer, bytes_per_read); - m_samples_avail -= BUFF_SIZE_SAMPLES; - m_stream_rpos += BUFF_SIZE_SAMPLES; + m_samples_avail -= samples_per_read; + m_stream_rpos += samples_per_read; m_stream_rpos %= stream_size; } return static_cast(ptr - begin); @@ -266,7 +273,7 @@ void Microphone::UpdateLoudness(const SampleType sample) } } -bool Microphone::HasData(u32 sample_count = BUFF_SIZE_SAMPLES) const +bool Microphone::HasData(u32 sample_count) const { std::lock_guard lock(m_ring_lock); return m_samples_avail >= sample_count; @@ -283,6 +290,11 @@ void Microphone::SetSamplingRate(u32 sampling_rate) StreamStart(sampling_rate); } +u32 Microphone::GetBufferSizeSamples() const +{ + return BUFF_SIZE_SAMPLES; +} + 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)); diff --git a/Source/Core/AudioCommon/Microphone.h b/Source/Core/AudioCommon/Microphone.h index ceabdf1aec..54fa686982 100644 --- a/Source/Core/AudioCommon/Microphone.h +++ b/Source/Core/AudioCommon/Microphone.h @@ -49,7 +49,9 @@ public: void SetSamplingRate(u32 sampling_rate); protected: + // sic. can be 16, 32 or 64 on GameCube static constexpr u32 BUFF_SIZE_SAMPLES = 32; + u32 m_samples_avail = 0; private: #ifdef HAVE_CUBEB @@ -59,6 +61,7 @@ private: virtual std::string GetCubebStreamName() const = 0; virtual s16 GetVolumeModifier() const = 0; virtual bool AreSamplesByteSwapped() const = 0; + virtual void OnOverflow(); #endif long DataCallback(const SampleType* input_buffer, long nframes); @@ -70,11 +73,11 @@ private: void StreamStart(u32 sampling_rate); void StreamStop(); + virtual u32 GetBufferSizeSamples() const; virtual u32 GetStreamSize() const = 0; std::vector 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 m_loudness_level = 0; diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceMic.cpp b/Source/Core/Core/HW/EXI/EXI_DeviceMic.cpp index 2386f4a314..5097d150d2 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceMic.cpp +++ b/Source/Core/Core/HW/EXI/EXI_DeviceMic.cpp @@ -3,13 +3,8 @@ #include "Core/HW/EXI/EXI_DeviceMic.h" -#include -#include #include -#include - -#include "AudioCommon/CubebUtils.h" #include "Common/BitUtils.h" #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" @@ -20,161 +15,77 @@ #include "Core/HW/SystemTimers.h" #include "Core/System.h" -#ifdef _WIN32 -#include -#endif - namespace ExpansionInterface { -void CEXIMic::StreamInit() +bool GameCubeMicState::IsSampleOn() const { - m_stream_buffer.clear(); - m_samples_avail = 0; - m_stream_wpos = 0; - m_stream_rpos = 0; - -#ifdef _WIN32 - if (!m_coinit_success) - return; - m_work_queue.PushBlocking([this] { -#endif - m_cubeb_ctx = CubebUtils::GetContext(); -#ifdef _WIN32 - }); -#endif + return m_sampling_enabled.load(std::memory_order_relaxed); } -void CEXIMic::StreamTerminate() +bool GameCubeMicState::IsMuted() const { - StreamStop(); + return false; // Not applicable +} - if (m_cubeb_ctx) +u32 GameCubeMicState::GetDefaultSamplingRate() const +{ + return DEFAULT_SAMPLING_RATE << status.sample_rate; +} + +void GameCubeMicState::SyncSamplingState() +{ + m_sampling_enabled.store(status.is_active, std::memory_order_relaxed); +} + +u32 GameCubeMicState::GetSampleRate() const +{ + return DEFAULT_SAMPLING_RATE << status.sample_rate; +} + +u32 GameCubeMicState::GetBufferSize() const +{ + return RING_BASE << status.buff_size; +} + +u32 GameCubeMicState::GetBufferSizeSamples() const +{ + return GetBufferSize() / SAMPLE_SIZE; +} + +namespace +{ +class MicrophoneGameCube : public AudioCommon::Microphone +{ +public: + explicit MicrophoneGameCube(const GameCubeMicState& sampler) + : Microphone(sampler, "GameCube Microphone"), + m_buff_size_samples(sampler.GetBufferSizeSamples()) { -#ifdef _WIN32 - if (!m_coinit_success) - return; - m_work_queue.PushBlocking([this] { -#endif - m_cubeb_ctx.reset(); -#ifdef _WIN32 - }); -#endif - } -} - -static void state_callback(cubeb_stream* stream, void* user_data, cubeb_state state) -{ -} - -long CEXIMic::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, - void* /*output_buffer*/, long nframes) -{ - CEXIMic* mic = static_cast(user_data); - - std::lock_guard lk(mic->m_ring_lock); - - const s16* buff_in = static_cast(input_buffer); - for (long i = 0; i < nframes; i++) - { - mic->m_stream_buffer[mic->m_stream_wpos] = buff_in[i]; - mic->m_stream_wpos = (mic->m_stream_wpos + 1) % mic->m_stream_size; } - mic->m_samples_avail += nframes; - if (mic->m_samples_avail > mic->m_stream_size) + bool GetOverflowBit() { return m_overflow_bit_set.exchange(false, std::memory_order_relaxed); } + +private: +#ifdef HAVE_CUBEB + std::string GetInputDeviceId() const override { return {}; } + std::string GetCubebStreamName() const override { return "Dolphin Emulated GameCube Microphone"; } + s16 GetVolumeModifier() const override { return 0; } + bool AreSamplesByteSwapped() const override { return true; } + void OnOverflow() override { - mic->m_samples_avail = 0; - mic->m_status.buff_ovrflw = 1; + m_samples_avail = 0; + m_overflow_bit_set.store(true, std::memory_order_relaxed); } - - return nframes; -} - -void CEXIMic::StreamStart() -{ - if (!m_cubeb_ctx) - return; - -#ifdef _WIN32 - if (!m_coinit_success) - return; - m_work_queue.PushBlocking([this] { #endif - // Open stream with current parameters - m_stream_size = m_buff_size_samples * 500; - m_stream_buffer.reset(m_stream_size); - cubeb_stream_params params{}; - params.format = CUBEB_SAMPLE_S16LE; - params.rate = m_sample_rate; - params.channels = 1; - params.layout = CUBEB_LAYOUT_MONO; + bool IsMicrophoneMuted() const override { return false; } + u32 GetBufferSizeSamples() const override { return m_buff_size_samples; } + u32 GetStreamSize() const override { return m_buff_size_samples * 500; } - u32 minimum_latency; - if (cubeb_get_min_latency(m_cubeb_ctx.get(), ¶ms, &minimum_latency) != CUBEB_OK) - { - WARN_LOG_FMT(EXPANSIONINTERFACE, "Error getting minimum latency"); - } - - if (cubeb_stream_init(m_cubeb_ctx.get(), &m_cubeb_stream, - "Dolphin Emulated GameCube Microphone", nullptr, ¶ms, nullptr, - nullptr, std::max(m_buff_size_samples, minimum_latency), - DataCallback, state_callback, this) != CUBEB_OK) - { - ERROR_LOG_FMT(EXPANSIONINTERFACE, "Error initializing cubeb stream"); - return; - } - - if (cubeb_stream_start(m_cubeb_stream) != CUBEB_OK) - { - ERROR_LOG_FMT(EXPANSIONINTERFACE, "Error starting cubeb stream"); - return; - } - - INFO_LOG_FMT(EXPANSIONINTERFACE, "started cubeb stream"); -#ifdef _WIN32 - }); -#endif -} - -void CEXIMic::StreamStop() -{ - if (m_cubeb_stream) - { -#ifdef _WIN32 - m_work_queue.PushBlocking([this] { -#endif - if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK) - ERROR_LOG_FMT(EXPANSIONINTERFACE, "Error stopping cubeb stream"); - cubeb_stream_destroy(m_cubeb_stream); - m_cubeb_stream = nullptr; -#ifdef _WIN32 - }); -#endif - } - - m_samples_avail = 0; - m_stream_wpos = 0; - m_stream_rpos = 0; - - m_stream_buffer.clear(); -} - -void CEXIMic::StreamReadOne() -{ - std::lock_guard lk(m_ring_lock); - - if (m_samples_avail >= m_buff_size_samples) - { - s16* last_buffer = &m_stream_buffer[m_stream_rpos]; - std::memcpy(m_ring_buffer.data(), last_buffer, m_buff_size); - - m_samples_avail -= m_buff_size_samples; - - m_stream_rpos += m_buff_size_samples; - m_stream_rpos %= m_stream_size; - } -} + const u32 m_buff_size_samples; + std::atomic_bool m_overflow_bit_set{false}; +}; +} // namespace // EXI Mic Device // This works by opening and starting an input stream when the is_active @@ -182,39 +93,13 @@ void CEXIMic::StreamReadOne() // buffer size settings. When the console handles the interrupt, it will send // cmdGetBuffer, which is when we actually read data from a buffer filled // in the background. -CEXIMic::CEXIMic(Core::System& system, int index) - : IEXIDevice(system), m_slot(index) -#ifdef _WIN32 - , - m_work_queue("Mic Worker") -#endif +CEXIMic::CEXIMic(Core::System& system, int index) : IEXIDevice(system), m_slot(index) { -#ifdef _WIN32 - m_work_queue.PushBlocking([this] { - auto result = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); - m_coinit_success = result == S_OK; - m_should_couninit = result == S_OK || result == S_FALSE; - }); -#endif - - StreamInit(); + m_microphone = std::make_unique(m_sampler); + m_microphone->Initialize(); } -CEXIMic::~CEXIMic() -{ - StreamTerminate(); - -#ifdef _WIN32 - if (m_should_couninit) - { - m_work_queue.PushBlocking([this] { - m_should_couninit = false; - CoUninitialize(); - }); - } - m_coinit_success = false; -#endif -} +CEXIMic::~CEXIMic() = default; bool CEXIMic::IsPresent() const { @@ -242,7 +127,7 @@ bool CEXIMic::IsInterruptSet() if (m_next_int_ticks == 0 || m_system.GetCoreTiming().GetTicks() < m_next_int_ticks) return false; - if (m_status.is_active) + if (m_sampler.IsSampleOn()) UpdateNextInterruptTicks(); else m_next_int_ticks = 0; @@ -274,54 +159,67 @@ void CEXIMic::TransferByte(u8& byte) break; case cmdGetStatus: - if (pos >= sizeof(m_status)) + if (pos >= sizeof(m_sampler.status)) { ERROR_LOG_FMT(EXPANSIONINTERFACE, "GetStatus command will overflow, pos={}", pos); break; } if (pos == 0) - m_status.button = Pad::GetMicButton(m_slot); + { + m_sampler.status.button = Pad::GetMicButton(m_slot); + if (m_microphone && static_cast(m_microphone.get())->GetOverflowBit()) + { + m_sampler.status.buff_ovrflw = 1; + } + } - byte = Common::BitCastPtr(&m_status)[pos ^ 1]; + byte = Common::BitCastPtr(&m_sampler.status)[pos ^ 1]; if (pos == 1) - m_status.buff_ovrflw = 0; + m_sampler.status.buff_ovrflw = 0; break; case cmdSetStatus: { - if (pos >= sizeof(m_status)) + if (pos >= sizeof(m_sampler.status)) { ERROR_LOG_FMT(EXPANSIONINTERFACE, "SetStatus command will overflow, pos={}", pos); break; } - const bool was_active = m_status.is_active; - Common::BitCastPtr(&m_status)[pos ^ 1] = byte; + const bool was_active = m_sampler.IsSampleOn(); + Common::BitCastPtr(&m_sampler.status)[pos ^ 1] = byte; + m_sampler.SyncSamplingState(); // safe to do since these can only be entered if both bytes of status have been written - if (!was_active && m_status.is_active) + if (!was_active && m_sampler.IsSampleOn()) { - m_sample_rate = RATE_BASE << m_status.sample_rate; - m_buff_size = RING_BASE << m_status.buff_size; - m_buff_size_samples = m_buff_size / SAMPLE_SIZE; + m_sample_rate = m_sampler.GetSampleRate(); + m_buff_size = m_sampler.GetBufferSize(); + m_buff_size_samples = m_sampler.GetBufferSizeSamples(); UpdateNextInterruptTicks(); - StreamStart(); + INFO_LOG_FMT(EXPANSIONINTERFACE, "Microphone sampling on (rate={}, samples={})", + m_sample_rate, m_buff_size_samples); + m_microphone = std::make_unique(m_sampler); + m_microphone->Initialize(); } - else if (was_active && !m_status.is_active) + else if (was_active && !m_sampler.IsSampleOn()) { - StreamStop(); + // If IsSampleOn() is false the DataCallback will skip frames. + // + // TODO: Is it worth to stop and destroy the stream each time? + m_microphone.reset(); } } break; case cmdGetBuffer: { - if (m_ring_pos == 0) - StreamReadOne(); + if (m_ring_pos == 0 && m_microphone) + m_microphone->ReadIntoBuffer(m_ring_buffer.data(), m_buff_size); - byte = m_ring_buffer[m_ring_pos ^ 1]; + byte = m_ring_buffer[m_ring_pos]; m_ring_pos = (m_ring_pos + 1) % m_buff_size; } break; diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceMic.h b/Source/Core/Core/HW/EXI/EXI_DeviceMic.h index b75f88fa59..639a3dc81b 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceMic.h +++ b/Source/Core/Core/HW/EXI/EXI_DeviceMic.h @@ -4,11 +4,11 @@ #pragma once #include +#include #include -#include "Common/Buffer.h" +#include "AudioCommon/Microphone.h" #include "Common/CommonTypes.h" -#include "Common/WorkQueueThread.h" #include "Core/HW/EXI/EXI_Device.h" struct cubeb; @@ -16,6 +16,41 @@ struct cubeb_stream; namespace ExpansionInterface { +class GameCubeMicState final : public AudioCommon::MicrophoneState +{ +public: + static constexpr u32 DEFAULT_SAMPLING_RATE = 11025; + static constexpr u32 RING_BASE = 32; + static constexpr u32 SAMPLE_SIZE = sizeof(s16); + + bool IsSampleOn() const override; + bool IsMuted() const override; + u32 GetDefaultSamplingRate() const override; + void SyncSamplingState(); + + u32 GetSampleRate() const; + u32 GetBufferSize() const; + u32 GetBufferSizeSamples() const; + + struct Status + { + u16 out : 4; // MICSet/GetOut...??? + u16 id : 1; // Used for MICGetDeviceID (always 0) + u16 button_unk : 3; // Button bits which appear unused + u16 button : 1; // The actual button on the mic + u16 buff_ovrflw : 1; // Ring buffer wrote over bytes which weren't read by console + u16 gain : 1; // Gain: 0dB or 15dB + u16 sample_rate : 2; // Sample rate, 00-11025, 01-22050, 10-44100, 11-?? + u16 buff_size : 2; // Ring buffer size in bytes, 00-32, 01-64, 10-128, 11-??? + u16 is_active : 1; // If we are sampling or not + }; + + Status status{}; + +private: + std::atomic_bool m_sampling_enabled{false}; +}; + class CEXIMic : public IEXIDevice { public: @@ -27,9 +62,6 @@ public: private: static constexpr std::array EXI_ID{0, 0x0a, 0, 0, 0}; - static constexpr u32 SAMPLE_SIZE = sizeof(s16); - static constexpr u32 RATE_BASE = 11025; - static constexpr u32 RING_BASE = 32; enum { @@ -44,33 +76,15 @@ private: u32 m_position = 0; int m_command = 0; - struct Status - { - u16 out : 4; // MICSet/GetOut...??? - u16 id : 1; // Used for MICGetDeviceID (always 0) - u16 button_unk : 3; // Button bits which appear unused - u16 button : 1; // The actual button on the mic - u16 buff_ovrflw : 1; // Ring buffer wrote over bytes which weren't read by console - u16 gain : 1; // Gain: 0dB or 15dB - u16 sample_rate : 2; // Sample rate, 00-11025, 01-22050, 10-44100, 11-?? - u16 buff_size : 2; // Ring buffer size in bytes, 00-32, 01-64, 10-128, 11-??? - u16 is_active : 1; // If we are sampling or not - }; static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, void* output_buffer, long nframes); void TransferByte(u8& byte) override; - void StreamInit(); - void StreamTerminate(); - void StreamStart(); - void StreamStop(); - void StreamReadOne(); - // 64 is the max size, can be 16 or 32 as well std::size_t m_ring_pos = 0; - std::array m_ring_buffer{}; + std::array m_ring_buffer{}; // 0 to disable interrupts, else it will be checked against current CPU ticks // to determine if interrupt should be raised @@ -81,27 +95,16 @@ private: std::shared_ptr m_cubeb_ctx = nullptr; cubeb_stream* m_cubeb_stream = nullptr; - Status m_status{}; - std::mutex m_ring_lock; // status bits converted to nice numbers - u32 m_sample_rate = RATE_BASE; - u32 m_buff_size = RING_BASE; - u32 m_buff_size_samples = m_buff_size / SAMPLE_SIZE; + u32 m_sample_rate = GameCubeMicState::DEFAULT_SAMPLING_RATE; + u32 m_buff_size = GameCubeMicState::RING_BASE; + u32 m_buff_size_samples = m_buff_size / GameCubeMicState::SAMPLE_SIZE; // Arbitrarily small ringbuffer used by audio input backend in order to // keep delay tolerable - Common::UniqueBuffer m_stream_buffer; - u32 m_stream_size; - u32 m_stream_wpos; - u32 m_stream_rpos; - u32 m_samples_avail; - -#ifdef _WIN32 - Common::AsyncWorkThread m_work_queue; - bool m_coinit_success = false; - bool m_should_couninit = false; -#endif + GameCubeMicState m_sampler{}; + std::unique_ptr m_microphone{}; }; } // namespace ExpansionInterface