-
Notifications
You must be signed in to change notification settings - Fork 20
Voice: client-side capture, relay and spatial mixing (M2) #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /* | ||
| * MafiaHub OSS license | ||
| * Copyright (c) 2026, MafiaHub. All rights reserved. | ||
| * | ||
| * This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework. | ||
| * See LICENSE file in the source repository for information regarding licensing. | ||
| */ | ||
|
|
||
| #include "audio_device.h" | ||
|
|
||
| #include <logging/logger.h> | ||
| #include <miniaudio.h> | ||
|
|
||
| #include <cstring> | ||
| #include <new> | ||
|
|
||
| namespace Framework::Voice { | ||
| namespace { | ||
| // The period is a latency hint, not a contract: a backend may ignore it, and the | ||
| // rings decouple both directions anyway. | ||
| void ApplyCommonConfig(ma_device_config &config) { | ||
| config.sampleRate = kSampleRate; | ||
| config.periodSizeInFrames = kFrameSamples; | ||
| config.performanceProfile = ma_performance_profile_low_latency; | ||
| config.noPreSilencedOutputBuffer = MA_TRUE; | ||
| } | ||
| } // namespace | ||
|
|
||
| void CaptureDevice::OnCapture(ma_device *device, void *output, const void *input, uint32_t frameCount) { | ||
| (void)output; | ||
|
|
||
| auto *self = static_cast<CaptureDevice *>(device->pUserData); | ||
| if (self == nullptr || input == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| // A failed Push means the main thread has not drained for a third of a second. | ||
| // Dropping beats stalling the device thread. | ||
| self->_ring.Push(static_cast<const int16_t *>(input), frameCount); | ||
| } | ||
|
|
||
| bool CaptureDevice::Start() { | ||
| if (_device != nullptr) { | ||
| return true; | ||
| } | ||
|
|
||
| ma_device_config config = ma_device_config_init(ma_device_type_capture); | ||
| config.capture.format = ma_format_s16; | ||
| config.capture.channels = kChannels; | ||
| config.dataCallback = &CaptureDevice::OnCapture; | ||
| config.pUserData = this; | ||
| ApplyCommonConfig(config); | ||
|
|
||
| auto *device = new (std::nothrow) ma_device {}; | ||
| if (device == nullptr) { | ||
| return false; | ||
| } | ||
|
|
||
| if (ma_device_init(nullptr, &config, device) != MA_SUCCESS) { | ||
| delete device; | ||
| Logging::GetLogger(FRAMEWORK_INNER_CLIENT)->warn("Voice: no usable capture device; continuing listen-only"); | ||
| return false; | ||
| } | ||
|
|
||
| if (ma_device_start(device) != MA_SUCCESS) { | ||
| ma_device_uninit(device); | ||
| delete device; | ||
| Logging::GetLogger(FRAMEWORK_INNER_CLIENT)->warn("Voice: capture device failed to start; continuing listen-only"); | ||
| return false; | ||
| } | ||
|
|
||
| _device = device; | ||
| Logging::GetLogger(FRAMEWORK_INNER_CLIENT)->debug("Voice: capture device '{}' running at {}Hz", _device->capture.name, _device->sampleRate); | ||
| return true; | ||
| } | ||
|
|
||
| void CaptureDevice::Stop() { | ||
| if (_device == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| // uninit joins the device thread, so no callback can be in flight afterwards. | ||
| ma_device_uninit(_device); | ||
| delete _device; | ||
| _device = nullptr; | ||
| _ring.Clear(); | ||
| } | ||
|
|
||
| CaptureDevice::~CaptureDevice() { | ||
| Stop(); | ||
| } | ||
|
|
||
| bool CaptureDevice::ReadFrame(int16_t *out) { | ||
| return _ring.Pop(out, kFrameSamples); | ||
| } | ||
|
|
||
| void PlaybackDevice::OnPlayback(ma_device *device, void *output, const void *input, uint32_t frameCount) { | ||
| (void)input; | ||
|
|
||
| auto *self = static_cast<PlaybackDevice *>(device->pUserData); | ||
| if (self == nullptr || output == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| auto *stereoOut = static_cast<float *>(output); | ||
|
|
||
| // noPreSilencedOutputBuffer is set, and the mixer accumulates. | ||
| std::memset(stereoOut, 0, static_cast<size_t>(frameCount) * 2 * sizeof(float)); | ||
|
|
||
| if (self->_render != nullptr) { | ||
| self->_render(stereoOut, frameCount, self->_user); | ||
| } | ||
| } | ||
|
|
||
| bool PlaybackDevice::Start(RenderFn render, void *user) { | ||
| if (_device != nullptr) { | ||
| return true; | ||
| } | ||
|
|
||
| if (render == nullptr) { | ||
| return false; | ||
| } | ||
|
|
||
| _render = render; | ||
| _user = user; | ||
|
|
||
| ma_device_config config = ma_device_config_init(ma_device_type_playback); | ||
| config.playback.format = ma_format_f32; | ||
| config.playback.channels = 2; | ||
| config.dataCallback = &PlaybackDevice::OnPlayback; | ||
| config.pUserData = this; | ||
| ApplyCommonConfig(config); | ||
|
|
||
| auto *device = new (std::nothrow) ma_device {}; | ||
| if (device == nullptr) { | ||
| return false; | ||
| } | ||
|
|
||
| if (ma_device_init(nullptr, &config, device) != MA_SUCCESS) { | ||
| delete device; | ||
| Logging::GetLogger(FRAMEWORK_INNER_CLIENT)->warn("Voice: no usable playback device; remote speakers will be inaudible"); | ||
| return false; | ||
| } | ||
|
|
||
| if (ma_device_start(device) != MA_SUCCESS) { | ||
| ma_device_uninit(device); | ||
| delete device; | ||
| Logging::GetLogger(FRAMEWORK_INNER_CLIENT)->warn("Voice: playback device failed to start; remote speakers will be inaudible"); | ||
| return false; | ||
| } | ||
|
|
||
| _device = device; | ||
| Logging::GetLogger(FRAMEWORK_INNER_CLIENT)->debug("Voice: playback device '{}' running at {}Hz", _device->playback.name, _device->sampleRate); | ||
| return true; | ||
| } | ||
|
|
||
| void PlaybackDevice::Stop() { | ||
| if (_device == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| // uninit joins the device thread, so the render function cannot be running once this | ||
| // returns -- which is what lets the owner tear down the state it reads. | ||
| ma_device_uninit(_device); | ||
| delete _device; | ||
| _device = nullptr; | ||
| } | ||
|
|
||
| PlaybackDevice::~PlaybackDevice() { | ||
| Stop(); | ||
| } | ||
| } // namespace Framework::Voice |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.