Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/architecture/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Assembled /preview/ DOM (presentation only)
| Audio device policy | src/LiveAudioEngine.h; engine/device/**; src/Main.cpp currently applies device | Verified driver/pins, epochs, reopen/rebind, explicit physical output safety; independent from logical session document |
| Audio device apply preflight (M2a) | src/NativeDeviceApplyCoordinator.h; src/Main.cpp (remaining orchestration) | Single message-thread transaction guard, JUCE open boundary, typed request/pin checks, accepted readback and fail-closed rollback. Next: extract full apply/rebind orchestration without duplicating DeviceManager |
| Modal setup transaction (M2b) | src/Main.cpp::showDeviceSetup; NativeDeviceApplyCoordinator.h | Persistent one-owner lease across visible JUCE selector, explicit Apply and automatic ASIO restart; repeated Setup clicks never re-mute; selector close completes readback/rebind before releasing lease |
| Live hardware readback (M2c) | src/NativeDeviceApplyCoordinator.h; src/Main.cpp | Consolidated actual device-object readback on CONTROL, strict ASIO driver instance identity and live mask/rate/buffer verification before Apply/rollback; no new device owner or callback |
| Production audio compatibility | src/LiveDryRoutingLab.h; engine/core/UnifiedLegacyProductionAdmission.h | Retain only proven required M4/FAT migration semantics; never create another render authority |
| Built-in DSP and VST lifecycle | engine/builtin_fx/**; src/NativeBuiltInFx.h; src/FactoryFxRegistry.h; src/PluginCatalog.h; engine/core/DynamicChannelFxLeaseBank.h; src/LiveAudioEngine.h | Per-processor DSP; one built-in registration for live insert, offline prepare, restore and native picker. Specialized editors and vendor lease remain with their existing owners. |
| Player | engine/player/**; src/Main.cpp dispatch | Worker decode / buffered PCM, one native graph virtual input, no independent hardware output callback |
Expand Down
60 changes: 23 additions & 37 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,16 +1825,7 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr
if(changedAsioPinLayout)
error="ASIO pin layout changed. Output remains muted; review the new playback pins.";
if(error.isEmpty()) {
armix::device::NativeDeviceApplyCoordinator::AppliedReadback observed;
observed.actual=actual;
observed.backend=manager.getCurrentAudioDeviceType();
observed.open=opened!=nullptr&&opened->isOpen();
if(observed.open) {
observed.activeInputs=opened->getActiveInputChannels();
observed.activeOutputs=opened->getActiveOutputChannels();
observed.deviceRate=opened->getCurrentSampleRate();
observed.deviceBuffer=opened->getCurrentBufferSizeSamples();
}
const auto observed=armix::device::NativeDeviceApplyCoordinator::inspectApplied(manager);
if(!armix::device::NativeDeviceApplyCoordinator::matchesApplied(
requested,candidate,observed))
error="Driver readback mismatch; device setting was not accepted";
Expand Down Expand Up @@ -1881,17 +1872,7 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr
const auto restored=recovery.priorKnownOpen&&previousType.isNotEmpty()&&
openSelectedAudioDevice(previousType,before).isEmpty()&&
manager.getCurrentAudioDeviceType()==previousType;
juce::AudioDeviceManager::AudioDeviceSetup checked;
manager.getAudioDeviceSetup(checked);
armix::device::NativeDeviceApplyCoordinator::RollbackReadback rollback;
rollback.actual=checked;
rollback.backend=manager.getCurrentAudioDeviceType();
auto* recovered=manager.getCurrentAudioDevice();
rollback.open=recovered!=nullptr&&recovered->isOpen();
if(rollback.open) {
rollback.inputNames=recovered->getInputChannelNames();
rollback.outputNames=recovered->getOutputChannelNames();
}
const auto rollback=armix::device::NativeDeviceApplyCoordinator::inspectRollback(manager);
const bool verified=restored&&
armix::device::NativeDeviceApplyCoordinator::matchesRollback(
before,previousType,originalInputNames,originalOutputNames,rollback)&&
Expand Down Expand Up @@ -5714,24 +5695,29 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr
bool finishAudioSetupAndRecoverMixer(MixerBindingMode bindingMode=MixerBindingMode::exact) {
if(logicalVendorEditor_ || mixerSessionWorker_.busy())return false;
const bool bound=engine_.finishManualAudioSetup(bindingMode!=MixerBindingMode::verifiedMainPinApply);
if(!engine_.directRouteAuthorized())return false;
const auto lab=engine_.status().getProperty("dryRoutingLab",{});
const bool hasCheckpoint=armix::session::NativeMixerSessionStore(settingsDirectory()).exists();
const bool recoverPristine=hasCheckpoint && mixerSessionRecoveryPending_ && engine_.hasPristineStartupMixer();
if(static_cast<bool>(lab.getProperty("nativeChannelIdPcmActive",false)) &&
!recoverPristine)return true;
if(engine_.hasRetainedNativeSession() || hasCheckpoint) {
if(bindingMode==MixerBindingMode::exact && engine_.sameRetainedAsioDriver())
bindingMode=MixerBindingMode::verifiedAsioRestart;
const auto error=restoreSavedMixerSession(false,bindingMode);
if(error.isNotEmpty()) {
mixerSessionError_=error;
emitNotice(error.startsWith("NEEDS_REMAP:")?error.fromFirstOccurrenceOf(": ",false,false):error,true);
return false;
}
return true;
const bool checkpoint=armix::session::NativeMixerSessionStore(settingsDirectory()).exists();
using Coordinator=armix::device::NativeDeviceApplyCoordinator;
const auto decision=Coordinator::decideMixerRebind({
engine_.directRouteAuthorized(),
static_cast<bool>(lab.getProperty("nativeChannelIdPcmActive",false)),
checkpoint,mixerSessionRecoveryPending_,engine_.hasPristineStartupMixer(),
engine_.hasRetainedNativeSession(),bound
});
if(decision==Coordinator::MixerRebindAction::retainInstalled ||
decision==Coordinator::MixerRebindAction::acceptFreshBound)return true;
if(decision!=Coordinator::MixerRebindAction::restoreRetained)return false;
// Only a verified retained identity may upgrade exact -> ASIO restart.
// The .smix document remains authoritative and is never reset here.
if(bindingMode==MixerBindingMode::exact && engine_.sameRetainedAsioDriver())
bindingMode=MixerBindingMode::verifiedAsioRestart;
const auto error=restoreSavedMixerSession(false,bindingMode);
if(error.isNotEmpty()) {
mixerSessionError_=error;
emitNotice(error.startsWith("NEEDS_REMAP:")?error.fromFirstOccurrenceOf(": ",false,false):error,true);
return false;
}
return bound;
return true;
}

std::uint64_t asioRecoveryEpoch_=0; // last successfully bound generation
Expand Down
95 changes: 91 additions & 4 deletions src/NativeDeviceApplyCoordinator.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,48 @@ class NativeDeviceApplyCoordinator final {

struct AppliedReadback {
juce::AudioDeviceManager::AudioDeviceSetup actual;
juce::String backend;
juce::String backend, deviceName, deviceType;
bool open=false;
juce::BigInteger activeInputs,activeOutputs;
double deviceRate=0.;
int deviceBuffer=0;
};
// Read *both* manager configuration and the installed JUCE device object.
// The latter is the hardware evidence; it cannot be synthesized from the
// requested/stored setup. CONTROL thread only, after openSelected returns.
static AppliedReadback inspectApplied(juce::AudioDeviceManager& manager) {
AppliedReadback evidence;
manager.getAudioDeviceSetup(evidence.actual);
evidence.backend=manager.getCurrentAudioDeviceType();
if(auto* device=manager.getCurrentAudioDevice();device && device->isOpen()) {
evidence.open=true;
evidence.deviceName=device->getName();
evidence.deviceType=device->getTypeName();
evidence.activeInputs=device->getActiveInputChannels();
evidence.activeOutputs=device->getActiveOutputChannels();
evidence.deviceRate=device->getCurrentSampleRate();
evidence.deviceBuffer=device->getCurrentBufferSizeSamples();
}
return evidence;
}
// A driver may return an OPEN status but no usable clock/buffer evidence.
// Unknown/zero/nonfinite live formats cannot establish playable PCM.
static bool validLiveFormat(double sampleRate,int frames) noexcept {
return std::isfinite(sampleRate) && sampleRate>=8000. &&
sampleRate<=384000. && frames>=16 && frames<=8192;
}
// The acknowledgement is not permission to output sound: it proves only
// that JUCE accepted the selected backend, names, masks, rate and period.
static bool matchesApplied(const Request& requested,
const juce::AudioDeviceManager::AudioDeviceSetup& candidate,
const AppliedReadback& live) noexcept {
const auto& a=live.actual;
return live.open&&live.backend==requested.backend&&
return live.open&&validLiveFormat(live.deviceRate,live.deviceBuffer)&&
live.backend==requested.backend&&
// Strict physical instance identity for ASIO: manager setup alone
// may name the intended driver while another callback is active.
(requested.backend!="ASIO" ||
(live.deviceType=="ASIO" && live.deviceName==requested.playback))&&
a.inputDeviceName==requested.capture&&
a.outputDeviceName==requested.playback&&
(requested.rate<=0.||std::abs(a.sampleRate-requested.rate)<=1.)&&
Expand All @@ -164,10 +193,30 @@ class NativeDeviceApplyCoordinator final {

struct RollbackReadback {
juce::AudioDeviceManager::AudioDeviceSetup actual;
juce::String backend;
juce::String backend, deviceName, deviceType;
bool open=false;
juce::StringArray inputNames,outputNames;
juce::BigInteger activeInputs,activeOutputs;
double deviceRate=0.;
int deviceBuffer=0;
};
static RollbackReadback inspectRollback(juce::AudioDeviceManager& manager) {
RollbackReadback evidence;
manager.getAudioDeviceSetup(evidence.actual);
evidence.backend=manager.getCurrentAudioDeviceType();
if(auto* device=manager.getCurrentAudioDevice();device && device->isOpen()) {
evidence.open=true;
evidence.deviceName=device->getName();
evidence.deviceType=device->getTypeName();
evidence.inputNames=device->getInputChannelNames();
evidence.outputNames=device->getOutputChannelNames();
evidence.activeInputs=device->getActiveInputChannels();
evidence.activeOutputs=device->getActiveOutputChannels();
evidence.deviceRate=device->getCurrentSampleRate();
evidence.deviceBuffer=device->getCurrentBufferSizeSamples();
}
return evidence;
}
// Previously CLOSED driver is never a valid rollback target. For ASIO,
// even a numerically equal mask is unsafe after pin names are reordered.
static bool matchesRollback(
Expand All @@ -177,16 +226,54 @@ class NativeDeviceApplyCoordinator final {
const juce::StringArray& previousOutputs,
const RollbackReadback& live) noexcept {
const auto& a=live.actual;
return live.open&&live.backend==previousBackend&&
return live.open&&validLiveFormat(live.deviceRate,live.deviceBuffer)&&
live.backend==previousBackend&&
(previousBackend!="ASIO" ||
(live.deviceType=="ASIO" && live.deviceName==before.outputDeviceName))&&
a.inputDeviceName==before.inputDeviceName&&
a.outputDeviceName==before.outputDeviceName&&
a.inputChannels==before.inputChannels&&
a.outputChannels==before.outputChannels&&
a.sampleRate==before.sampleRate&&a.bufferSize==before.bufferSize&&
// A manager can remember an old setup even if an ASIO instance
// restarted with fewer ports or a different callback quantum.
live.activeInputs==a.inputChannels&&live.activeOutputs==a.outputChannels&&
live.activeOutputs.countNumberOfSetBits()>=2&&
std::abs(live.deviceRate-a.sampleRate)<=1.&&
live.deviceBuffer==a.bufferSize&&
(previousBackend!="ASIO"||
(live.inputNames==previousInputs&&live.outputNames==previousOutputs));
}


// Device rebind owns the hardware graph, NEVER the accepted logical
// ChannelId/.smix document. Pure control-thread verdict: the caller is
// responsible for performing the exact existing native restore action.
enum class MixerRebindAction : std::uint8_t {
reject, retainInstalled, restoreRetained, acceptFreshBound
};
struct MixerRebindEvidence {
bool physicalAuthorized=false;
bool installedChannelPcm=false;
bool storedCheckpoint=false;
bool pendingStartupRestore=false;
bool pristineStartupMixer=false;
bool retainedNativeSession=false;
bool freshBound=false;
};
[[nodiscard]] static constexpr MixerRebindAction decideMixerRebind(
MixerRebindEvidence e) noexcept {
if(!e.physicalAuthorized)return MixerRebindAction::reject;
const bool mustRestorePristine=e.storedCheckpoint &&
e.pendingStartupRestore && e.pristineStartupMixer;
if(e.installedChannelPcm && !mustRestorePristine)
return MixerRebindAction::retainInstalled;
if(e.retainedNativeSession || e.storedCheckpoint)
return MixerRebindAction::restoreRetained;
return e.freshBound?MixerRebindAction::acceptFreshBound:
MixerRebindAction::reject;
}

using RecordPhase=std::function<void(const juce::String&)>;
static juce::String openSelected(juce::AudioDeviceManager& manager,
const juce::String& backend,
Expand Down
13 changes: 9 additions & 4 deletions tests/mixer_io_operator_contract_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ test('P0 MAONO: one APPLY MUTE-ACKs before switching and native ARM',()=>{
assert.match(js,/deviceNote=ack\?\.ok\?'DEVICE ACCEPTED · OUTPUT MUTED · '\+devicePinNextStep\(\)/);
assert.match(main,/!engine_\.allOutputsDisarmed\(\)/);
assert.match(main,/NativeDeviceApplyCoordinator::matchesApplied/);
assert.match(main,/observed\.activeInputs=opened->getActiveInputChannels\(\)/);
assert.match(main,/NativeDeviceApplyCoordinator::inspectApplied\(manager\)/);
assert.match(applyOwner,/evidence\.activeInputs=device->getActiveInputChannels\(\)/);
assert.match(applyOwner,/live\.deviceName==requested\.playback/);
assert.match(applyOwner,/live\.activeInputs==a\.inputChannels/);
assert.match(applyOwner,/std::abs\(live\.deviceRate-a\.sampleRate\)/);
assert.match(engine,/return isOutputMuted\(\) && !secondaryOutput_\.armed\(\)/);
Expand Down Expand Up @@ -360,9 +362,12 @@ test('DEVICE2 native device list is cached on overlay open; explicit scan is fai
const apply=native.slice(native.indexOf('void applyCustomAudioDevice('),
native.indexOf('void showDeviceSetup()',native.indexOf('void applyCustomAudioDevice(')));
assert.match(apply,/NativeDeviceApplyCoordinator::matchesApplied/);
assert.match(apply,/observed\.activeInputs=opened->getActiveInputChannels\(\)/);
assert.match(apply,/observed\.activeOutputs=opened->getActiveOutputChannels\(\)/);
assert.match(apply,/observed\.deviceBuffer=opened->getCurrentBufferSizeSamples\(\)/);
assert.match(apply,/NativeDeviceApplyCoordinator::inspectApplied\(manager\)/);
assert.match(applyOwner,/evidence\.activeInputs=device->getActiveInputChannels\(\)/);
assert.match(applyOwner,/evidence\.activeOutputs=device->getActiveOutputChannels\(\)/);
assert.match(applyOwner,/evidence\.deviceBuffer=device->getCurrentBufferSizeSamples\(\)/);
assert.match(apply,/NativeDeviceApplyCoordinator::inspectRollback\(manager\)/);
assert.match(applyOwner,/live\.deviceName==before\.outputDeviceName/);
assert.match(applyOwner,/live\.activeInputs==a\.inputChannels/);
assert.match(applyOwner,/live\.activeOutputs==a\.outputChannels/);
assert.match(applyOwner,/live\.deviceBuffer==a\.bufferSize/);
Expand Down
Loading
Loading