From ea7834c3f8aa6a8f7d4e9b68c95c0c685fc31504 Mon Sep 17 00:00:00 2001 From: ajisth69 Date: Mon, 26 Oct 2026 17:24:04 +0530 Subject: [PATCH 1/2] fix(playback): refactor Auto DJ eager prefetcher and C++ Oboe Automix processor - Eager prefetch within 2 seconds of playback start instead of waiting near track end - Require >= 12.0s of decoded PCM in RAM ring buffer before marking Deck B as ARMED_READY - Freeze Opus-to-FLAC stream hot-swapping during transitions, pre-decoding, or near track end - Safe fallback when buffered < 8.0s: continue natural playback to track end for clean handoff - Eliminate clipping: attenuation-only loudness matching, equal-power correlated-bass headroom compensation, Butterworth Q=0.7071 filters, scaled wet FX returns, and tanh master soft-knee safety limiter - Eliminate zipper noise & clicks: Cytomic SVF crossover, per-sample one-pole parameter smoothing, and continuous cosine S-curve bass swap - Strict lock-free real-time audio thread: pre-allocated scratch/loop buffers, no mutexes or allocations in onAudioReady() --- app/src/main/cpp/AudioEngine.cpp | 1710 +++++++++-------- app/src/main/cpp/AutoDJProcessor.cpp | 32 +- app/src/main/cpp/AutoMixBridge.cpp | 18 + app/src/main/cpp/AutoMixEngine.cpp | 365 ++-- app/src/main/cpp/AutoMixEngine.h | 76 +- app/src/main/cpp/EchoTailProcessor.h | 19 +- app/src/main/cpp/LockFreeRingBuffer.h | 21 + app/src/main/cpp/TransitionPlanner.cpp | 4 +- app/src/main/cpp/test_autodj_processor.cpp | 44 +- .../cpp/tests/test_transition_planner.cpp | 4 +- .../lastwave/app/playback/AutoMixSchedule.kt | 7 +- .../com/lastwave/app/playback/MusicPlayer.kt | 93 +- .../app/playback/NativeAutoMixEngine.kt | 8 + .../app/playback/autodj/AutoDJController.kt | 62 +- .../app/playback/AutoMixScheduleTest.kt | 99 + 15 files changed, 1411 insertions(+), 1151 deletions(-) create mode 100644 app/src/test/java/com/lastwave/app/playback/AutoMixScheduleTest.kt diff --git a/app/src/main/cpp/AudioEngine.cpp b/app/src/main/cpp/AudioEngine.cpp index 911f5eca40..53b9bbe7d4 100644 --- a/app/src/main/cpp/AudioEngine.cpp +++ b/app/src/main/cpp/AudioEngine.cpp @@ -1,850 +1,860 @@ -#include "AudioEngine.h" - -#include - -#include -#include -#include -#include - -namespace lastwave::audio { -namespace { - -constexpr double kPi = 3.1415926535897932384626433832795; - -soxr_t asSoxr(void* handle) noexcept { - return static_cast(handle); -} - -} // namespace - -AudioEngine::~AudioEngine() { - stop(); - std::lock_guard processorLock(mediaProcessorMutex_); - if (mediaResampler_ != nullptr) soxr_delete(asSoxr(mediaResampler_)); -} - -bool AudioEngine::start(std::int32_t preferredOutputSampleRate) { - std::lock_guard controlLock(controlMutex_); - playbackEnabled_.store(false, std::memory_order_release); - inputAlreadyProcessed_.store(false, std::memory_order_release); - renderedFrames_.store(0, std::memory_order_release); - underrunCount_.store(0, std::memory_order_release); - restartAllowed_.store(true, std::memory_order_release); - return startLocked(preferredOutputSampleRate); -} - -bool AudioEngine::startLocked(std::int32_t preferredOutputSampleRate) { - if (stream_ != nullptr) return true; - - // The engine never assumes a device accepted a configuration: everything - // downstream keys off openedStream->getSampleRate(), read back below. - const std::int32_t requestedRate = std::max(preferredOutputSampleRate, 0); - requestedOutputSampleRate_.store(requestedRate, std::memory_order_release); - // Streaming music values route stability over minimum round-trip latency. - // Shared mode coexists reliably with Bluetooth, calls, accessibility and - // OEM spatializers; exclusive mode is intentionally avoided. The default - // performance mode (not LowLatency) matches Poweramp/VLC-class players: - // music must survive aggressive OEM CPU throttling, and One UI's Dolby - // Atmos/Sound Assistant effect chains behave best with regular buffers - // instead of MMAP fast-track deadlines. - std::shared_ptr openedStream; - oboe::AudioStreamBuilder builder; - builder.setDirection(oboe::Direction::Output) - ->setPerformanceMode(oboe::PerformanceMode::None) - ->setSharingMode(oboe::SharingMode::Shared) - ->setUsage(oboe::Usage::Media) - ->setContentType(oboe::ContentType::Music) - ->setFormat(oboe::AudioFormat::Float) - ->setChannelCount(kOutputChannels) - ->setDataCallback(this) - ->setErrorCallback(this); - if (requestedRate > 0) { - builder.setSampleRate(requestedRate); - } - const auto openResult = builder.openStream(openedStream); - if (openResult != oboe::Result::OK || openedStream == nullptr) { - restartAllowed_.store(false, std::memory_order_release); - return false; - } - - const std::int32_t sampleRate = openedStream->getSampleRate(); - if (sampleRate <= 0 || sampleRate > 384000 || - openedStream->getChannelCount() != kOutputChannels || - openedStream->getFormat() != oboe::AudioFormat::Float) { - // A device reporting an unusable configuration must never be trusted - // with audio: fail cleanly and let the caller pick its fallback path. - openedStream->close(); - restartAllowed_.store(false, std::memory_order_release); - return false; - } - // Oboe's OpenSL ES compatibility backend varies widely across older OEM - // devices. Media3 AudioTrack is the safer music path there; retain Oboe for - // its native AAudio backend and fall back cleanly otherwise. - if (!openedStream->usesAAudio()) { - openedStream->close(); - restartAllowed_.store(false, std::memory_order_release); - return false; - } - streamOpenCount_.fetch_add(1, std::memory_order_release); - if (requestedRate > 0 && sampleRate != requestedRate) { - // The device silently substituted its own rate (common on Samsung - // paths). Count it so diagnostics can distinguish "we asked for X and - // got Y" from a genuine native-rate selection. - rateAdaptationCount_.fetch_add(1, std::memory_order_release); - } - - const auto framesPerBurst = std::max(openedStream->getFramesPerBurst(), 1); - // A medium device buffer (~60 ms when capacity allows) absorbs OEM CPU - // governor spikes — the primary source of One UI crackle. The ring - // buffer in front of the device already provides gapless continuity, so - // this costs no perceptible latency for a streaming music player. - const auto targetDeviceBufferFrames = std::min( - openedStream->getBufferCapacityInFrames(), - std::max( - framesPerBurst * 4, - static_cast( - static_cast(sampleRate) * - kTargetDeviceBufferMilliseconds / 1000))); - if (targetDeviceBufferFrames > 0) { - (void) openedStream->setBufferSizeInFrames(targetDeviceBufferFrames); - } - - { - std::lock_guard producerLock(producerMutex_); - // Never let a stale ring pointer outlive the state rebuild below. - activeRingBuffer_.store(nullptr, std::memory_order_release); - releaseProducerStateLocked(); - const auto capacitySamples = static_cast(sampleRate) * - kRingMilliseconds * kOutputChannels / 1000U; - ringBuffer_ = std::make_unique>(capacitySamples); - rawScratch_.resize(kProducerChunkFrames * kOutputChannels); - stereoScratch_.resize(kProducerChunkFrames * kOutputChannels); - resampledScratch_.resize(kMaxResampledFrames * kOutputChannels); - } - - oboeDsp_.setPeakProtectionEnabled(true); - oboeDsp_.configure(sampleRate); - buildFadeCurve(sampleRate); - underrunFadePosition_ = fadeInCurve_.size(); - recoveryFadePosition_ = fadeInCurve_.size(); - underrunActive_ = false; - recoveryFading_ = false; - prebuffering_ = true; - prebufferWaitCallbacks_ = kPrebufferMaxCallbacks; - prebufferFrames_ = std::min( - static_cast(sampleRate) * kRingMilliseconds / 2000U, - std::max( - static_cast(framesPerBurst * 4), - static_cast(sampleRate) * kPrebufferMilliseconds / 1000U)); - prebufferBaseFrames_ = prebufferFrames_; - underrunDecayIntervalFrames_ = - static_cast(sampleRate) * kUnderrunDecaySeconds; - cleanFrames_ = 0; - underrunAnchor_.fill(0.0F); - lastOutput_.fill(0.0F); - currentOutputVolume_ = std::clamp( - targetOutputVolume_.load(std::memory_order_acquire), 0.0F, 1.0F); - outputVolumeRampStep_ = 1.0F / - std::max(1.0F, static_cast(sampleRate) * 0.005F); - - outputSampleRate_.store(sampleRate, std::memory_order_release); - activeRingBuffer_.store(ringBuffer_.get(), std::memory_order_release); - stream_ = std::move(openedStream); - if (stream_->requestStart() != oboe::Result::OK) { - activeRingBuffer_.store(nullptr, std::memory_order_release); - stream_->close(); - stream_.reset(); - outputSampleRate_.store(0, std::memory_order_release); - std::lock_guard producerLock(producerMutex_); - releaseProducerStateLocked(); - restartAllowed_.store(false, std::memory_order_release); - return false; - } - return true; -} - -void AudioEngine::stop() noexcept { - std::lock_guard controlLock(controlMutex_); - playbackEnabled_.store(false, std::memory_order_release); - restartAllowed_.store(false, std::memory_order_release); - activeRingBuffer_.store(nullptr, std::memory_order_release); - if (stream_ != nullptr) { - stream_->requestStop(); - stream_->close(); - stream_.reset(); - } - outputSampleRate_.store(0, std::memory_order_release); - renderedFrames_.store(0, std::memory_order_release); - std::lock_guard producerLock(producerMutex_); - releaseProducerStateLocked(); -} - -bool AudioEngine::isRunning() const noexcept { - std::lock_guard controlLock(controlMutex_); - return stream_ != nullptr && - outputSampleRate_.load(std::memory_order_acquire) > 0; -} - -void AudioEngine::flushOutput() noexcept { - std::lock_guard controlLock(controlMutex_); - auto* ring = activeRingBuffer_.load(std::memory_order_acquire); - if (ring == nullptr || stream_ == nullptr) return; - // Park the consumer on silence first so the callback stops touching the - // ring while its contents are dropped. The stream itself stays open: - // reopening AAudio per track/seek is what produced pops on OEM stacks. - activeRingBuffer_.store(nullptr, std::memory_order_release); - { - std::lock_guard producerLock(producerMutex_); - ring->clear(); - resamplerFlushing_ = false; - } - underrunActive_ = false; - recoveryFading_ = false; - underrunFadePosition_ = fadeInCurve_.size(); - recoveryFadePosition_ = 0; - prebuffering_ = true; - prebufferWaitCallbacks_ = kPrebufferMaxCallbacks; - lastOutput_.fill(0.0F); - underrunAnchor_.fill(0.0F); - cleanFrames_ = 0; - activeRingBuffer_.store(ring, std::memory_order_release); -} - -std::size_t AudioEngine::writePcm( - const void* pcm, - std::size_t frameCount, - PcmFormat format, - std::int32_t inputSampleRate, - std::int32_t inputChannelCount) { - if (pcm == nullptr || frameCount == 0 || inputSampleRate <= 0 || - (inputChannelCount != 1 && inputChannelCount != kOutputChannels) || - PcmConverter::bytesPerSample(format) == 0) { - return 0; - } - - std::lock_guard producerLock(producerMutex_); - auto* ring = activeRingBuffer_.load(std::memory_order_acquire); - const std::int32_t destinationRate = outputSampleRate_.load(std::memory_order_acquire); - if (ring == nullptr || destinationRate <= 0 || - !configureResamplerLocked(inputSampleRate)) { - return 0; - } - inputAlreadyProcessed_.store(false, std::memory_order_release); - - const auto* inputBytes = static_cast(pcm); - const std::size_t bytesPerFrame = PcmConverter::bytesPerSample(format) * - static_cast(inputChannelCount); - const double ratio = static_cast(destinationRate) / inputSampleRate; - const std::size_t ratioSafeChunk = std::max( - 1, - static_cast((kMaxResampledFrames - 256) / - std::max(1.0, ratio))); - - std::size_t consumedFrames = 0; - while (consumedFrames < frameCount) { - const std::size_t chunkFrames = std::min({ - frameCount - consumedFrames, - kProducerChunkFrames, - ratioSafeChunk, - }); - const void* chunkInput = inputBytes + consumedFrames * bytesPerFrame; - const std::size_t inputSamples = chunkFrames * - static_cast(inputChannelCount); - if (!PcmConverter::toFloat(chunkInput, inputSamples, format, rawScratch_.data())) { - break; - } - - if (inputChannelCount == 1) { - for (std::size_t frame = 0; frame < chunkFrames; ++frame) { - stereoScratch_[frame * 2U] = rawScratch_[frame]; - stereoScratch_[frame * 2U + 1U] = rawScratch_[frame]; - } - } else { - std::memcpy( - stereoScratch_.data(), - rawScratch_.data(), - chunkFrames * kOutputChannels * sizeof(float)); - } - - if (resampler_ == nullptr) { - const std::size_t writtenSamples = ring->write( - stereoScratch_.data(), - chunkFrames * kOutputChannels); - const std::size_t writtenFrames = writtenSamples / kOutputChannels; - consumedFrames += writtenFrames; - if (writtenFrames != chunkFrames) break; - continue; - } - - const std::size_t availableOutputFrames = std::min( - kMaxResampledFrames, - ring->availableToWrite() / kOutputChannels); - if (availableOutputFrames == 0) break; - - std::size_t inputDone = 0; - std::size_t outputDone = 0; - const soxr_error_t error = soxr_process( - asSoxr(resampler_), - stereoScratch_.data(), - chunkFrames, - &inputDone, - resampledScratch_.data(), - availableOutputFrames, - &outputDone); - if (error != nullptr) break; - - const std::size_t writtenSamples = ring->write( - resampledScratch_.data(), - outputDone * kOutputChannels); - if (writtenSamples != outputDone * kOutputChannels) break; - consumedFrames += inputDone; - if (inputDone == 0) break; - } - return consumedFrames; -} - -std::size_t AudioEngine::writeProcessedPcm( - const float* pcm, - std::size_t frameCount, - std::int32_t inputSampleRate, - std::int32_t inputChannelCount) { - if (pcm == nullptr || frameCount == 0 || inputSampleRate <= 0 || - (inputChannelCount != 1 && inputChannelCount != kOutputChannels)) { - return 0; - } - - std::lock_guard producerLock(producerMutex_); - auto* ring = activeRingBuffer_.load(std::memory_order_acquire); - const auto destinationRate = outputSampleRate_.load(std::memory_order_acquire); - if (ring == nullptr || inputSampleRate != destinationRate) return 0; - inputAlreadyProcessed_.store(true, std::memory_order_release); - - const std::size_t writableFrames = std::min( - frameCount, - ring->availableToWrite() / kOutputChannels); - if (writableFrames == 0) return 0; - if (inputChannelCount == kOutputChannels) { - return ring->write(pcm, writableFrames * kOutputChannels) / kOutputChannels; - } - - std::size_t writtenFrames = 0; - while (writtenFrames < writableFrames) { - const std::size_t chunkFrames = std::min( - writableFrames - writtenFrames, - kProducerChunkFrames); - for (std::size_t frame = 0; frame < chunkFrames; ++frame) { - const float sample = pcm[writtenFrames + frame]; - stereoScratch_[frame * kOutputChannels] = sample; - stereoScratch_[frame * kOutputChannels + 1U] = sample; - } - const std::size_t writtenSamples = ring->write( - stereoScratch_.data(), - chunkFrames * kOutputChannels); - const std::size_t chunkWrittenFrames = writtenSamples / kOutputChannels; - writtenFrames += chunkWrittenFrames; - if (chunkWrittenFrames != chunkFrames) break; - } - return writtenFrames; -} - -void AudioEngine::setPlaying(bool playing) noexcept { - playbackEnabled_.store(playing, std::memory_order_release); -} - -void AudioEngine::setOutputVolume(float volume) noexcept { - if (!std::isfinite(volume)) volume = 1.0F; - targetOutputVolume_.store(std::clamp(volume, 0.0F, 1.0F), std::memory_order_release); -} - -void AudioEngine::flushResampler() { - std::lock_guard producerLock(producerMutex_); - auto* ring = activeRingBuffer_.load(std::memory_order_acquire); - if (ring == nullptr || resampler_ == nullptr) return; - resamplerFlushing_ = true; - - while (true) { - const std::size_t outputCapacity = std::min( - kMaxResampledFrames, - ring->availableToWrite() / kOutputChannels); - if (outputCapacity == 0) return; - std::size_t outputDone = 0; - const soxr_error_t error = soxr_process( - asSoxr(resampler_), - nullptr, - 0, - nullptr, - resampledScratch_.data(), - outputCapacity, - &outputDone); - if (error != nullptr || outputDone == 0) { - soxr_delete(asSoxr(resampler_)); - resampler_ = nullptr; - resamplerInputRate_ = 0; - resamplerFlushing_ = false; - return; - } - if (ring->write(resampledScratch_.data(), outputDone * kOutputChannels) != - outputDone * kOutputChannels) { - return; - } - } -} - -void AudioEngine::setStudioMasterClarity(bool enabled) noexcept { - oboeDsp_.setStudioMasterClarity(enabled); - mediaDsp_.setStudioMasterClarity(enabled); -} - -void AudioEngine::setBitPerfect(bool enabled) noexcept { - oboeDsp_.setBitPerfect(enabled); - mediaDsp_.setBitPerfect(enabled); -} - -bool AudioEngine::isBitPerfect() const noexcept { - return oboeDsp_.isBitPerfectEnabled() && mediaDsp_.isBitPerfectEnabled(); -} - -void AudioEngine::setEqualizer( - bool enabled, - const float* gainsDb, - std::size_t gainCount) noexcept { - oboeDsp_.setEqualizer(enabled, gainsDb, gainCount); - mediaDsp_.setEqualizer(enabled, gainsDb, gainCount); -} - -bool AudioEngine::configureMediaProcessor( - std::int32_t inputSampleRate, - std::int32_t outputSampleRate, - std::int32_t channelCount) { - if (inputSampleRate <= 0 || outputSampleRate <= 0 || - (channelCount != 1 && channelCount != 2)) { - return false; - } - std::lock_guard processorLock(mediaProcessorMutex_); - if (mediaResampler_ != nullptr) { - soxr_delete(asSoxr(mediaResampler_)); - mediaResampler_ = nullptr; - } - mediaInputSampleRate_ = inputSampleRate; - mediaOutputSampleRate_ = outputSampleRate; - mediaChannelCount_ = channelCount; - mediaScratch_.resize(kProducerChunkFrames * static_cast(channelCount)); - // Resample first, then run tone/peak processing at the actual output rate - // so sinc overshoot is included in final peak protection. - mediaDsp_.setPeakProtectionEnabled(true); - mediaDsp_.configure(outputSampleRate); - return configureMediaResamplerLocked(); -} - -bool AudioEngine::processMediaPcm( - const void* pcm, - std::size_t frameCount, - PcmFormat format, - float* output, - std::size_t outputCapacityFrames, - std::size_t& outputFrameCount) { - outputFrameCount = 0; - if (pcm == nullptr || output == nullptr || frameCount == 0 || - outputCapacityFrames == 0) { - return false; - } - std::lock_guard processorLock(mediaProcessorMutex_); - if (mediaInputSampleRate_ <= 0 || mediaOutputSampleRate_ <= 0 || - mediaChannelCount_ <= 0 || mediaScratch_.empty()) { - return false; - } - const std::size_t channels = static_cast(mediaChannelCount_); - const std::size_t bytesPerFrame = PcmConverter::bytesPerSample(format) * channels; - if (bytesPerFrame == 0) return false; - - if (mediaResampler_ == nullptr && mediaInputSampleRate_ == mediaOutputSampleRate_) { - if (outputCapacityFrames < frameCount || - !PcmConverter::toFloat(pcm, frameCount * channels, format, output)) { - return false; - } - mediaDsp_.process(output, static_cast(frameCount), mediaChannelCount_); - outputFrameCount = frameCount; - return true; - } - if (mediaResampler_ == nullptr) return false; - - const auto* inputBytes = static_cast(pcm); - std::size_t consumedFrames = 0; - while (consumedFrames < frameCount) { - const std::size_t chunkFrames = std::min( - frameCount - consumedFrames, - kProducerChunkFrames); - if (!PcmConverter::toFloat( - inputBytes + consumedFrames * bytesPerFrame, - chunkFrames * channels, - format, - mediaScratch_.data())) { - return false; - } - std::size_t chunkConsumed = 0; - while (chunkConsumed < chunkFrames) { - std::size_t inputDone = 0; - std::size_t outputDone = 0; - const std::size_t remainingOutput = outputCapacityFrames - outputFrameCount; - if (remainingOutput == 0) break; - const soxr_error_t error = soxr_process( - asSoxr(mediaResampler_), - mediaScratch_.data() + chunkConsumed * channels, - chunkFrames - chunkConsumed, - &inputDone, - output + outputFrameCount * channels, - remainingOutput, - &outputDone); - if (error != nullptr) return false; - if (outputDone > 0) { - mediaDsp_.process( - output + outputFrameCount * channels, - static_cast(outputDone), - mediaChannelCount_); - outputFrameCount += outputDone; - } - if (inputDone == 0 && outputDone == 0) { - // Soxr completed buffering for this pass - break; - } - chunkConsumed += inputDone; - consumedFrames += inputDone; - } - if (chunkConsumed < chunkFrames && outputCapacityFrames == outputFrameCount) { - break; - } - } - return true; -} - -bool AudioEngine::flushMediaProcessor( - float* output, - std::size_t outputCapacityFrames, - std::size_t& outputFrameCount) { - outputFrameCount = 0; - if (output == nullptr || outputCapacityFrames == 0) return false; - std::lock_guard processorLock(mediaProcessorMutex_); - if (mediaResampler_ == nullptr) return true; - const std::size_t channels = static_cast(mediaChannelCount_); - while (outputFrameCount < outputCapacityFrames) { - std::size_t outputDone = 0; - const soxr_error_t error = soxr_process( - asSoxr(mediaResampler_), - nullptr, - 0, - nullptr, - output + outputFrameCount * channels, - outputCapacityFrames - outputFrameCount, - &outputDone); - if (error != nullptr) return false; - mediaDsp_.process( - output + outputFrameCount * channels, - static_cast(outputDone), - mediaChannelCount_); - outputFrameCount += outputDone; - if (outputDone == 0) break; - } - return true; -} - -void AudioEngine::resetMediaProcessor() { - std::lock_guard processorLock(mediaProcessorMutex_); - mediaDsp_.reset(); - if (mediaResampler_ != nullptr) { - soxr_delete(asSoxr(mediaResampler_)); - mediaResampler_ = nullptr; - } - (void) configureMediaResamplerLocked(); -} - -bool AudioEngine::configureMediaResamplerLocked() { - if (mediaInputSampleRate_ <= 0 || mediaOutputSampleRate_ <= 0 || - (mediaChannelCount_ != 1 && mediaChannelCount_ != 2)) { - return false; - } - if (mediaInputSampleRate_ == mediaOutputSampleRate_) return true; - soxr_error_t error = nullptr; - const soxr_io_spec_t ioSpec = soxr_io_spec(SOXR_FLOAT32_I, SOXR_FLOAT32_I); - // HQ is transparent for playback while avoiding the large CPU spikes of - // VHQ on low-end ARM cores. VHQ's extra offline-grade precision is not - // worth real-time underruns on mobile hardware. - const soxr_quality_spec_t qualitySpec = soxr_quality_spec(SOXR_HQ, 0); - const soxr_runtime_spec_t runtimeSpec = soxr_runtime_spec(1); - mediaResampler_ = soxr_create( - static_cast(mediaInputSampleRate_), - static_cast(mediaOutputSampleRate_), - static_cast(mediaChannelCount_), - &error, - &ioSpec, - &qualitySpec, - &runtimeSpec); - if (error != nullptr || mediaResampler_ == nullptr) { - if (mediaResampler_ != nullptr) soxr_delete(asSoxr(mediaResampler_)); - mediaResampler_ = nullptr; - return false; - } - return true; -} - -std::size_t AudioEngine::bufferedFrames() const noexcept { - std::lock_guard producerLock(producerMutex_); - const auto* ring = activeRingBuffer_.load(std::memory_order_acquire); - return ring == nullptr ? 0 : ring->availableToRead() / kOutputChannels; -} - -oboe::DataCallbackResult AudioEngine::onAudioReady( - oboe::AudioStream* /* audioStream */, - void* audioData, - std::int32_t numFrames) { - auto* output = static_cast(audioData); - auto* ring = activeRingBuffer_.load(std::memory_order_acquire); - if (output == nullptr || numFrames <= 0) return oboe::DataCallbackResult::Continue; - - const std::size_t requestedFrames = static_cast(numFrames); - // While the ring is unpublished, stop()/flushOutput()/restart own every - // piece of fade/prebuffer state below. The callback then renders plain - // silence and touches nothing, which turns "null ring" into a proper - // handoff instead of a data race. - if (ring == nullptr) { - std::memset(output, 0, requestedFrames * kOutputChannels * sizeof(float)); - return oboe::DataCallbackResult::Continue; - } - - const bool playingNow = playbackEnabled_.load(std::memory_order_acquire); - // While paused, keep draining any queued tail under the 5 ms output - // volume ramp instead of hard-cutting to silence — a mid-sample gate is - // audible as a click on every device class. - const bool drainingPauseTail = - !playingNow && currentOutputVolume_ > 0.0F && - ring->availableToRead() >= kOutputChannels; - if (!playingNow && !drainingPauseTail) { - std::memset(output, 0, requestedFrames * kOutputChannels * sizeof(float)); - underrunActive_ = false; - recoveryFading_ = false; - prebuffering_ = true; - prebufferWaitCallbacks_ = kPrebufferMaxCallbacks; - lastOutput_.fill(0.0F); - return oboe::DataCallbackResult::Continue; - } - const std::size_t availableFrames = ring->availableToRead() / kOutputChannels; - if (prebuffering_ && playingNow && - (availableFrames == 0 || - (availableFrames < prebufferFrames_ && prebufferWaitCallbacks_-- > 0))) { - std::memset(output, 0, requestedFrames * kOutputChannels * sizeof(float)); - return oboe::DataCallbackResult::Continue; - } - if (prebuffering_) { - prebuffering_ = false; - recoveryFading_ = true; - recoveryFadePosition_ = 0; - } - const std::size_t readSamples = ring->read(output, requestedFrames * kOutputChannels); - const std::size_t validFrames = readSamples / kOutputChannels; - const bool wasUnderrun = underrunActive_; - - if (validFrames > 0) { - if (!inputAlreadyProcessed_.load(std::memory_order_acquire)) { - oboeDsp_.process(output, static_cast(validFrames)); - } - if (wasUnderrun) { - recoveryFading_ = true; - recoveryFadePosition_ = 0; - } - - // Bit-perfect must not scale valid PCM: recovery fades and the - // output-volume ramp are bypassed so the gain stays exactly 1.0. - // Starvation fill below only covers missing frames (no valid - // samples exist to preserve there). - const bool outputBitPerfect = oboeDsp_.isBitPerfectEnabled(); - for (std::size_t frame = 0; frame < validFrames; ++frame) { - float recoveryGain = 1.0F; - if (!outputBitPerfect && recoveryFading_) { - if (recoveryFadePosition_ < fadeInCurve_.size()) { - recoveryGain = fadeInCurve_[recoveryFadePosition_++]; - } else { - recoveryFading_ = false; - } - } - const std::size_t offset = frame * kOutputChannels; - // Pausing fades toward zero through the same ramp; resuming fades - // back up — both click-free. - const float volumeTarget = playingNow - ? targetOutputVolume_.load(std::memory_order_relaxed) - : 0.0F; - const float volumeDelta = volumeTarget - currentOutputVolume_; - currentOutputVolume_ += std::clamp( - volumeDelta, - -outputVolumeRampStep_, - outputVolumeRampStep_); - const float gain = outputBitPerfect - ? 1.0F - : recoveryGain * currentOutputVolume_; - output[offset] *= gain; - output[offset + 1U] *= gain; - } - lastOutput_[0] = output[(validFrames - 1U) * kOutputChannels]; - lastOutput_[1] = output[(validFrames - 1U) * kOutputChannels + 1U]; - renderedFrames_.fetch_add(validFrames, std::memory_order_release); - } - - if (validFrames < requestedFrames) { - if (playingNow) { - if (!wasUnderrun || validFrames > 0) { - underrunCount_.fetch_add(1, std::memory_order_release); - } - if (!wasUnderrun || validFrames > 0) { - underrunAnchor_ = lastOutput_; - underrunFadePosition_ = 0; - } - recoveryFading_ = false; - for (std::size_t frame = validFrames; frame < requestedFrames; ++frame) { - const float fadeOut = underrunFadePosition_ < fadeInCurve_.size() - ? 1.0F - fadeInCurve_[underrunFadePosition_++] - : 0.0F; - const std::size_t offset = frame * kOutputChannels; - output[offset] = underrunAnchor_[0] * fadeOut; - output[offset + 1U] = underrunAnchor_[1] * fadeOut; - } - underrunActive_ = true; - prebuffering_ = true; - prebufferWaitCallbacks_ = kPrebufferMaxCallbacks; - cleanFrames_ = 0; - // Adaptive resume runway: every episode doubles the required - // prebuffer (capped at half the ring) so cascading starvation on - // a throttling device self-extinguishes instead of repeating. - const auto capacityFrames = ring->capacity() / kOutputChannels; - prebufferFrames_ = std::max( - prebufferBaseFrames_, - std::min(prebufferFrames_ * 2U, capacityFrames / 2U)); - } else { - // Pause tail fully drained: plain silence with no accounting so a - // long pause is never mistaken for playback starvation. - std::memset( - output + validFrames * kOutputChannels, - 0, - (requestedFrames - validFrames) * kOutputChannels * sizeof(float)); - underrunActive_ = false; - recoveryFading_ = false; - } - } else { - underrunActive_ = false; - // Sustained glitch-free playback gradually forgives past underruns so - // a single transient CPU spike (navigation, camera, OEM throttling) - // can never accumulate into a permanent session downgrade, and steps - // the adaptive prebuffer back toward its baseline. - cleanFrames_ += requestedFrames; - while (cleanFrames_ >= underrunDecayIntervalFrames_) { - cleanFrames_ -= underrunDecayIntervalFrames_; - if (prebufferFrames_ > prebufferBaseFrames_) { - prebufferFrames_ = std::max( - prebufferBaseFrames_, prebufferFrames_ / 2U); - } - auto current = underrunCount_.load(std::memory_order_acquire); - if (current == 0) break; - while (current > 0 && !underrunCount_.compare_exchange_weak( - current, current - 1, std::memory_order_release)) { - } - } - } - - return oboe::DataCallbackResult::Continue; -} - -void AudioEngine::onErrorAfterClose( - oboe::AudioStream* audioStream, - oboe::Result /* error */) { - if (!restartAllowed_.load(std::memory_order_acquire)) return; - std::lock_guard controlLock(controlMutex_); - if (!restartAllowed_.load(std::memory_order_acquire) || - stream_ == nullptr || stream_.get() != audioStream) { - return; - } - - streamRestartCount_.fetch_add(1, std::memory_order_release); - activeRingBuffer_.store(nullptr, std::memory_order_release); - stream_.reset(); - outputSampleRate_.store(0, std::memory_order_release); - { - std::lock_guard producerLock(producerMutex_); - releaseProducerStateLocked(); - } - try { - (void) startLocked(requestedOutputSampleRate_.load(std::memory_order_acquire)); - } catch (...) { - // This callback runs on Oboe's recovery thread, outside a JNI frame. - // Never allow an allocation failure during restart to terminate the - // whole process. - restartAllowed_.store(false, std::memory_order_release); - activeRingBuffer_.store(nullptr, std::memory_order_release); - outputSampleRate_.store(0, std::memory_order_release); - } -} - -void AudioEngine::releaseProducerStateLocked() noexcept { - if (resampler_ != nullptr) { - soxr_delete(asSoxr(resampler_)); - resampler_ = nullptr; - } - resamplerInputRate_ = 0; - resamplerFlushing_ = false; - ringBuffer_.reset(); - rawScratch_.clear(); - stereoScratch_.clear(); - resampledScratch_.clear(); -} - -bool AudioEngine::configureResamplerLocked(std::int32_t inputSampleRate) { - const std::int32_t destinationRate = outputSampleRate_.load(std::memory_order_acquire); - if (destinationRate <= 0) return false; - // A NULL-input soxr flush is terminal. A new track/configuration gets a - // fresh resampler even when its sample rate matches the previous track. - if (resamplerFlushing_) { - if (resampler_ != nullptr) soxr_delete(asSoxr(resampler_)); - resampler_ = nullptr; - resamplerInputRate_ = 0; - resamplerFlushing_ = false; - } - if (resamplerInputRate_ == inputSampleRate) return true; - - if (resampler_ != nullptr) { - soxr_delete(asSoxr(resampler_)); - resampler_ = nullptr; - } - resamplerInputRate_ = inputSampleRate; - if (inputSampleRate == destinationRate) return true; - - soxr_error_t error = nullptr; - const soxr_io_spec_t ioSpec = soxr_io_spec(SOXR_FLOAT32_I, SOXR_FLOAT32_I); - const soxr_quality_spec_t qualitySpec = soxr_quality_spec(SOXR_HQ, 0); - const soxr_runtime_spec_t runtimeSpec = soxr_runtime_spec(1); - resampler_ = soxr_create( - static_cast(inputSampleRate), - static_cast(destinationRate), - kOutputChannels, - &error, - &ioSpec, - &qualitySpec, - &runtimeSpec); - if (error != nullptr || resampler_ == nullptr) { - if (resampler_ != nullptr) soxr_delete(asSoxr(resampler_)); - resampler_ = nullptr; - resamplerInputRate_ = 0; - return false; - } - return true; -} - -void AudioEngine::buildFadeCurve(std::int32_t sampleRate) { - const std::size_t fadeFrames = std::max( - 1, - static_cast(std::llround(sampleRate * 0.002))); - fadeInCurve_.resize(fadeFrames); - for (std::size_t frame = 0; frame < fadeFrames; ++frame) { - const double phase = kPi * static_cast(frame + 1U) / - static_cast(fadeFrames); - fadeInCurve_[frame] = static_cast(0.5 * (1.0 - std::cos(phase))); - } -} - -} // namespace lastwave::audio +#include "AudioEngine.h" + +#include + +#include +#include +#include +#include + +namespace lastwave::audio { +namespace { + +constexpr double kPi = 3.1415926535897932384626433832795; + +soxr_t asSoxr(void* handle) noexcept { + return static_cast(handle); +} + +} // namespace + +AudioEngine::~AudioEngine() { + stop(); + std::lock_guard processorLock(mediaProcessorMutex_); + if (mediaResampler_ != nullptr) soxr_delete(asSoxr(mediaResampler_)); +} + +bool AudioEngine::start(std::int32_t preferredOutputSampleRate) { + std::lock_guard controlLock(controlMutex_); + playbackEnabled_.store(false, std::memory_order_release); + inputAlreadyProcessed_.store(false, std::memory_order_release); + renderedFrames_.store(0, std::memory_order_release); + underrunCount_.store(0, std::memory_order_release); + restartAllowed_.store(true, std::memory_order_release); + return startLocked(preferredOutputSampleRate); +} + +bool AudioEngine::startLocked(std::int32_t preferredOutputSampleRate) { + if (stream_ != nullptr) return true; + + // The engine never assumes a device accepted a configuration: everything + // downstream keys off openedStream->getSampleRate(), read back below. + const std::int32_t requestedRate = std::max(preferredOutputSampleRate, 0); + requestedOutputSampleRate_.store(requestedRate, std::memory_order_release); + // Streaming music values route stability over minimum round-trip latency. + // Shared mode coexists reliably with Bluetooth, calls, accessibility and + // OEM spatializers; exclusive mode is intentionally avoided. The default + // performance mode (not LowLatency) matches Poweramp/VLC-class players: + // music must survive aggressive OEM CPU throttling, and One UI's Dolby + // Atmos/Sound Assistant effect chains behave best with regular buffers + // instead of MMAP fast-track deadlines. + std::shared_ptr openedStream; + oboe::AudioStreamBuilder builder; + builder.setDirection(oboe::Direction::Output) + ->setPerformanceMode(oboe::PerformanceMode::None) + ->setSharingMode(oboe::SharingMode::Shared) + ->setUsage(oboe::Usage::Media) + ->setContentType(oboe::ContentType::Music) + ->setFormat(oboe::AudioFormat::Float) + ->setChannelCount(kOutputChannels) + ->setDataCallback(this) + ->setErrorCallback(this); + if (requestedRate > 0) { + builder.setSampleRate(requestedRate); + } + const auto openResult = builder.openStream(openedStream); + if (openResult != oboe::Result::OK || openedStream == nullptr) { + restartAllowed_.store(false, std::memory_order_release); + return false; + } + + const std::int32_t sampleRate = openedStream->getSampleRate(); + if (sampleRate <= 0 || sampleRate > 384000 || + openedStream->getChannelCount() != kOutputChannels || + openedStream->getFormat() != oboe::AudioFormat::Float) { + // A device reporting an unusable configuration must never be trusted + // with audio: fail cleanly and let the caller pick its fallback path. + openedStream->close(); + restartAllowed_.store(false, std::memory_order_release); + return false; + } + // Oboe's OpenSL ES compatibility backend varies widely across older OEM + // devices. Media3 AudioTrack is the safer music path there; retain Oboe for + // its native AAudio backend and fall back cleanly otherwise. + if (!openedStream->usesAAudio()) { + openedStream->close(); + restartAllowed_.store(false, std::memory_order_release); + return false; + } + streamOpenCount_.fetch_add(1, std::memory_order_release); + if (requestedRate > 0 && sampleRate != requestedRate) { + // The device silently substituted its own rate (common on Samsung + // paths). Count it so diagnostics can distinguish "we asked for X and + // got Y" from a genuine native-rate selection. + rateAdaptationCount_.fetch_add(1, std::memory_order_release); + } + + const auto framesPerBurst = std::max(openedStream->getFramesPerBurst(), 1); + // A medium device buffer (~60 ms when capacity allows) absorbs OEM CPU + // governor spikes — the primary source of One UI crackle. The ring + // buffer in front of the device already provides gapless continuity, so + // this costs no perceptible latency for a streaming music player. + const auto targetDeviceBufferFrames = std::min( + openedStream->getBufferCapacityInFrames(), + std::max( + framesPerBurst * 4, + static_cast( + static_cast(sampleRate) * + kTargetDeviceBufferMilliseconds / 1000))); + if (targetDeviceBufferFrames > 0) { + (void) openedStream->setBufferSizeInFrames(targetDeviceBufferFrames); + } + + { + std::lock_guard producerLock(producerMutex_); + // Never let a stale ring pointer outlive the state rebuild below. + activeRingBuffer_.store(nullptr, std::memory_order_release); + releaseProducerStateLocked(); + const auto capacitySamples = static_cast(sampleRate) * + kRingMilliseconds * kOutputChannels / 1000U; + ringBuffer_ = std::make_unique>(capacitySamples); + rawScratch_.resize(kProducerChunkFrames * kOutputChannels); + stereoScratch_.resize(kProducerChunkFrames * kOutputChannels); + resampledScratch_.resize(kMaxResampledFrames * kOutputChannels); + } + + oboeDsp_.setPeakProtectionEnabled(true); + oboeDsp_.configure(sampleRate); + buildFadeCurve(sampleRate); + underrunFadePosition_ = fadeInCurve_.size(); + recoveryFadePosition_ = fadeInCurve_.size(); + underrunActive_ = false; + recoveryFading_ = false; + prebuffering_ = true; + prebufferWaitCallbacks_ = kPrebufferMaxCallbacks; + prebufferFrames_ = std::min( + static_cast(sampleRate) * kRingMilliseconds / 2000U, + std::max( + static_cast(framesPerBurst * 4), + static_cast(sampleRate) * kPrebufferMilliseconds / 1000U)); + prebufferBaseFrames_ = prebufferFrames_; + underrunDecayIntervalFrames_ = + static_cast(sampleRate) * kUnderrunDecaySeconds; + cleanFrames_ = 0; + underrunAnchor_.fill(0.0F); + lastOutput_.fill(0.0F); + currentOutputVolume_ = std::clamp( + targetOutputVolume_.load(std::memory_order_acquire), 0.0F, 1.0F); + outputVolumeRampStep_ = 1.0F / + std::max(1.0F, static_cast(sampleRate) * 0.005F); + + outputSampleRate_.store(sampleRate, std::memory_order_release); + activeRingBuffer_.store(ringBuffer_.get(), std::memory_order_release); + stream_ = std::move(openedStream); + if (stream_->requestStart() != oboe::Result::OK) { + activeRingBuffer_.store(nullptr, std::memory_order_release); + stream_->close(); + stream_.reset(); + outputSampleRate_.store(0, std::memory_order_release); + std::lock_guard producerLock(producerMutex_); + releaseProducerStateLocked(); + restartAllowed_.store(false, std::memory_order_release); + return false; + } + return true; +} + +void AudioEngine::stop() noexcept { + std::lock_guard controlLock(controlMutex_); + playbackEnabled_.store(false, std::memory_order_release); + restartAllowed_.store(false, std::memory_order_release); + activeRingBuffer_.store(nullptr, std::memory_order_release); + if (stream_ != nullptr) { + stream_->requestStop(); + stream_->close(); + stream_.reset(); + } + outputSampleRate_.store(0, std::memory_order_release); + renderedFrames_.store(0, std::memory_order_release); + std::lock_guard producerLock(producerMutex_); + releaseProducerStateLocked(); +} + +bool AudioEngine::isRunning() const noexcept { + std::lock_guard controlLock(controlMutex_); + return stream_ != nullptr && + outputSampleRate_.load(std::memory_order_acquire) > 0; +} + +void AudioEngine::flushOutput() noexcept { + std::lock_guard controlLock(controlMutex_); + auto* ring = activeRingBuffer_.load(std::memory_order_acquire); + if (ring == nullptr || stream_ == nullptr) return; + // Park the consumer on silence first so the callback stops touching the + // ring while its contents are dropped. The stream itself stays open: + // reopening AAudio per track/seek is what produced pops on OEM stacks. + activeRingBuffer_.store(nullptr, std::memory_order_release); + { + std::lock_guard producerLock(producerMutex_); + ring->clear(); + resamplerFlushing_ = false; + } + underrunActive_ = false; + recoveryFading_ = false; + underrunFadePosition_ = fadeInCurve_.size(); + recoveryFadePosition_ = 0; + prebuffering_ = true; + prebufferWaitCallbacks_ = kPrebufferMaxCallbacks; + lastOutput_.fill(0.0F); + underrunAnchor_.fill(0.0F); + cleanFrames_ = 0; + activeRingBuffer_.store(ring, std::memory_order_release); +} + +std::size_t AudioEngine::writePcm( + const void* pcm, + std::size_t frameCount, + PcmFormat format, + std::int32_t inputSampleRate, + std::int32_t inputChannelCount) { + if (pcm == nullptr || frameCount == 0 || inputSampleRate <= 0 || + (inputChannelCount != 1 && inputChannelCount != kOutputChannels) || + PcmConverter::bytesPerSample(format) == 0) { + return 0; + } + + std::lock_guard producerLock(producerMutex_); + auto* ring = activeRingBuffer_.load(std::memory_order_acquire); + const std::int32_t destinationRate = outputSampleRate_.load(std::memory_order_acquire); + if (ring == nullptr || destinationRate <= 0 || + !configureResamplerLocked(inputSampleRate)) { + return 0; + } + inputAlreadyProcessed_.store(false, std::memory_order_release); + + const auto* inputBytes = static_cast(pcm); + const std::size_t bytesPerFrame = PcmConverter::bytesPerSample(format) * + static_cast(inputChannelCount); + const double ratio = static_cast(destinationRate) / inputSampleRate; + const std::size_t ratioSafeChunk = std::max( + 1, + static_cast((kMaxResampledFrames - 256) / + std::max(1.0, ratio))); + + std::size_t consumedFrames = 0; + while (consumedFrames < frameCount) { + const std::size_t chunkFrames = std::min({ + frameCount - consumedFrames, + kProducerChunkFrames, + ratioSafeChunk, + }); + const void* chunkInput = inputBytes + consumedFrames * bytesPerFrame; + const std::size_t inputSamples = chunkFrames * + static_cast(inputChannelCount); + if (!PcmConverter::toFloat(chunkInput, inputSamples, format, rawScratch_.data())) { + break; + } + + if (inputChannelCount == 1) { + for (std::size_t frame = 0; frame < chunkFrames; ++frame) { + stereoScratch_[frame * 2U] = rawScratch_[frame]; + stereoScratch_[frame * 2U + 1U] = rawScratch_[frame]; + } + } else { + std::memcpy( + stereoScratch_.data(), + rawScratch_.data(), + chunkFrames * kOutputChannels * sizeof(float)); + } + + if (resampler_ == nullptr) { + const std::size_t writtenSamples = ring->write( + stereoScratch_.data(), + chunkFrames * kOutputChannels); + const std::size_t writtenFrames = writtenSamples / kOutputChannels; + consumedFrames += writtenFrames; + if (writtenFrames != chunkFrames) break; + continue; + } + + const std::size_t availableOutputFrames = std::min( + kMaxResampledFrames, + ring->availableToWrite() / kOutputChannels); + if (availableOutputFrames == 0) break; + + std::size_t inputDone = 0; + std::size_t outputDone = 0; + const soxr_error_t error = soxr_process( + asSoxr(resampler_), + stereoScratch_.data(), + chunkFrames, + &inputDone, + resampledScratch_.data(), + availableOutputFrames, + &outputDone); + if (error != nullptr) break; + + const std::size_t writtenSamples = ring->write( + resampledScratch_.data(), + outputDone * kOutputChannels); + if (writtenSamples != outputDone * kOutputChannels) break; + consumedFrames += inputDone; + if (inputDone == 0) break; + } + return consumedFrames; +} + +std::size_t AudioEngine::writeProcessedPcm( + const float* pcm, + std::size_t frameCount, + std::int32_t inputSampleRate, + std::int32_t inputChannelCount) { + if (pcm == nullptr || frameCount == 0 || inputSampleRate <= 0 || + (inputChannelCount != 1 && inputChannelCount != kOutputChannels)) { + return 0; + } + + std::lock_guard producerLock(producerMutex_); + auto* ring = activeRingBuffer_.load(std::memory_order_acquire); + const auto destinationRate = outputSampleRate_.load(std::memory_order_acquire); + if (ring == nullptr || inputSampleRate != destinationRate) return 0; + inputAlreadyProcessed_.store(true, std::memory_order_release); + + const std::size_t writableFrames = std::min( + frameCount, + ring->availableToWrite() / kOutputChannels); + if (writableFrames == 0) return 0; + if (inputChannelCount == kOutputChannels) { + return ring->write(pcm, writableFrames * kOutputChannels) / kOutputChannels; + } + + std::size_t writtenFrames = 0; + while (writtenFrames < writableFrames) { + const std::size_t chunkFrames = std::min( + writableFrames - writtenFrames, + kProducerChunkFrames); + for (std::size_t frame = 0; frame < chunkFrames; ++frame) { + const float sample = pcm[writtenFrames + frame]; + stereoScratch_[frame * kOutputChannels] = sample; + stereoScratch_[frame * kOutputChannels + 1U] = sample; + } + const std::size_t writtenSamples = ring->write( + stereoScratch_.data(), + chunkFrames * kOutputChannels); + const std::size_t chunkWrittenFrames = writtenSamples / kOutputChannels; + writtenFrames += chunkWrittenFrames; + if (chunkWrittenFrames != chunkFrames) break; + } + return writtenFrames; +} + +void AudioEngine::setPlaying(bool playing) noexcept { + playbackEnabled_.store(playing, std::memory_order_release); +} + +void AudioEngine::setOutputVolume(float volume) noexcept { + if (!std::isfinite(volume)) volume = 1.0F; + targetOutputVolume_.store(std::clamp(volume, 0.0F, 1.0F), std::memory_order_release); +} + +void AudioEngine::flushResampler() { + std::lock_guard producerLock(producerMutex_); + auto* ring = activeRingBuffer_.load(std::memory_order_acquire); + if (ring == nullptr || resampler_ == nullptr) return; + resamplerFlushing_ = true; + + while (true) { + const std::size_t outputCapacity = std::min( + kMaxResampledFrames, + ring->availableToWrite() / kOutputChannels); + if (outputCapacity == 0) return; + std::size_t outputDone = 0; + const soxr_error_t error = soxr_process( + asSoxr(resampler_), + nullptr, + 0, + nullptr, + resampledScratch_.data(), + outputCapacity, + &outputDone); + if (error != nullptr || outputDone == 0) { + soxr_delete(asSoxr(resampler_)); + resampler_ = nullptr; + resamplerInputRate_ = 0; + resamplerFlushing_ = false; + return; + } + if (ring->write(resampledScratch_.data(), outputDone * kOutputChannels) != + outputDone * kOutputChannels) { + return; + } + } +} + +void AudioEngine::setStudioMasterClarity(bool enabled) noexcept { + oboeDsp_.setStudioMasterClarity(enabled); + mediaDsp_.setStudioMasterClarity(enabled); +} + +void AudioEngine::setBitPerfect(bool enabled) noexcept { + oboeDsp_.setBitPerfect(enabled); + mediaDsp_.setBitPerfect(enabled); +} + +bool AudioEngine::isBitPerfect() const noexcept { + return oboeDsp_.isBitPerfectEnabled() && mediaDsp_.isBitPerfectEnabled(); +} + +void AudioEngine::setEqualizer( + bool enabled, + const float* gainsDb, + std::size_t gainCount) noexcept { + oboeDsp_.setEqualizer(enabled, gainsDb, gainCount); + mediaDsp_.setEqualizer(enabled, gainsDb, gainCount); +} + +bool AudioEngine::configureMediaProcessor( + std::int32_t inputSampleRate, + std::int32_t outputSampleRate, + std::int32_t channelCount) { + if (inputSampleRate <= 0 || outputSampleRate <= 0 || + (channelCount != 1 && channelCount != 2)) { + return false; + } + std::lock_guard processorLock(mediaProcessorMutex_); + if (mediaResampler_ != nullptr) { + soxr_delete(asSoxr(mediaResampler_)); + mediaResampler_ = nullptr; + } + mediaInputSampleRate_ = inputSampleRate; + mediaOutputSampleRate_ = outputSampleRate; + mediaChannelCount_ = channelCount; + mediaScratch_.resize(kProducerChunkFrames * static_cast(channelCount)); + // Resample first, then run tone/peak processing at the actual output rate + // so sinc overshoot is included in final peak protection. + mediaDsp_.setPeakProtectionEnabled(true); + mediaDsp_.configure(outputSampleRate); + return configureMediaResamplerLocked(); +} + +bool AudioEngine::processMediaPcm( + const void* pcm, + std::size_t frameCount, + PcmFormat format, + float* output, + std::size_t outputCapacityFrames, + std::size_t& outputFrameCount) { + outputFrameCount = 0; + if (pcm == nullptr || output == nullptr || frameCount == 0 || + outputCapacityFrames == 0) { + return false; + } + std::lock_guard processorLock(mediaProcessorMutex_); + if (mediaInputSampleRate_ <= 0 || mediaOutputSampleRate_ <= 0 || + mediaChannelCount_ <= 0 || mediaScratch_.empty()) { + return false; + } + const std::size_t channels = static_cast(mediaChannelCount_); + const std::size_t bytesPerFrame = PcmConverter::bytesPerSample(format) * channels; + if (bytesPerFrame == 0) return false; + + if (mediaResampler_ == nullptr && mediaInputSampleRate_ == mediaOutputSampleRate_) { + if (outputCapacityFrames < frameCount || + !PcmConverter::toFloat(pcm, frameCount * channels, format, output)) { + return false; + } + mediaDsp_.process(output, static_cast(frameCount), mediaChannelCount_); + outputFrameCount = frameCount; + return true; + } + if (mediaResampler_ == nullptr) return false; + + const auto* inputBytes = static_cast(pcm); + std::size_t consumedFrames = 0; + while (consumedFrames < frameCount) { + const std::size_t chunkFrames = std::min( + frameCount - consumedFrames, + kProducerChunkFrames); + if (!PcmConverter::toFloat( + inputBytes + consumedFrames * bytesPerFrame, + chunkFrames * channels, + format, + mediaScratch_.data())) { + return false; + } + std::size_t chunkConsumed = 0; + while (chunkConsumed < chunkFrames) { + std::size_t inputDone = 0; + std::size_t outputDone = 0; + const std::size_t remainingOutput = outputCapacityFrames - outputFrameCount; + if (remainingOutput == 0) break; + const soxr_error_t error = soxr_process( + asSoxr(mediaResampler_), + mediaScratch_.data() + chunkConsumed * channels, + chunkFrames - chunkConsumed, + &inputDone, + output + outputFrameCount * channels, + remainingOutput, + &outputDone); + if (error != nullptr) return false; + if (outputDone > 0) { + mediaDsp_.process( + output + outputFrameCount * channels, + static_cast(outputDone), + mediaChannelCount_); + outputFrameCount += outputDone; + } + if (inputDone == 0 && outputDone == 0) { + // Soxr completed buffering for this pass + break; + } + chunkConsumed += inputDone; + consumedFrames += inputDone; + } + if (chunkConsumed < chunkFrames && outputCapacityFrames == outputFrameCount) { + break; + } + } + return true; +} + +bool AudioEngine::flushMediaProcessor( + float* output, + std::size_t outputCapacityFrames, + std::size_t& outputFrameCount) { + outputFrameCount = 0; + if (output == nullptr || outputCapacityFrames == 0) return false; + std::lock_guard processorLock(mediaProcessorMutex_); + if (mediaResampler_ == nullptr) return true; + const std::size_t channels = static_cast(mediaChannelCount_); + while (outputFrameCount < outputCapacityFrames) { + std::size_t outputDone = 0; + const soxr_error_t error = soxr_process( + asSoxr(mediaResampler_), + nullptr, + 0, + nullptr, + output + outputFrameCount * channels, + outputCapacityFrames - outputFrameCount, + &outputDone); + if (error != nullptr) return false; + mediaDsp_.process( + output + outputFrameCount * channels, + static_cast(outputDone), + mediaChannelCount_); + outputFrameCount += outputDone; + if (outputDone == 0) break; + } + return true; +} + +void AudioEngine::resetMediaProcessor() { + std::lock_guard processorLock(mediaProcessorMutex_); + mediaDsp_.reset(); + if (mediaResampler_ != nullptr) { + soxr_delete(asSoxr(mediaResampler_)); + mediaResampler_ = nullptr; + } + (void) configureMediaResamplerLocked(); +} + +bool AudioEngine::configureMediaResamplerLocked() { + if (mediaInputSampleRate_ <= 0 || mediaOutputSampleRate_ <= 0 || + (mediaChannelCount_ != 1 && mediaChannelCount_ != 2)) { + return false; + } + if (mediaInputSampleRate_ == mediaOutputSampleRate_) return true; + soxr_error_t error = nullptr; + const soxr_io_spec_t ioSpec = soxr_io_spec(SOXR_FLOAT32_I, SOXR_FLOAT32_I); + // HQ is transparent for playback while avoiding the large CPU spikes of + // VHQ on low-end ARM cores. VHQ's extra offline-grade precision is not + // worth real-time underruns on mobile hardware. + const soxr_quality_spec_t qualitySpec = soxr_quality_spec(SOXR_HQ, 0); + const soxr_runtime_spec_t runtimeSpec = soxr_runtime_spec(1); + mediaResampler_ = soxr_create( + static_cast(mediaInputSampleRate_), + static_cast(mediaOutputSampleRate_), + static_cast(mediaChannelCount_), + &error, + &ioSpec, + &qualitySpec, + &runtimeSpec); + if (error != nullptr || mediaResampler_ == nullptr) { + if (mediaResampler_ != nullptr) soxr_delete(asSoxr(mediaResampler_)); + mediaResampler_ = nullptr; + return false; + } + return true; +} + +std::size_t AudioEngine::bufferedFrames() const noexcept { + std::lock_guard producerLock(producerMutex_); + const auto* ring = activeRingBuffer_.load(std::memory_order_acquire); + return ring == nullptr ? 0 : ring->availableToRead() / kOutputChannels; +} + +oboe::DataCallbackResult AudioEngine::onAudioReady( + oboe::AudioStream* /* audioStream */, + void* audioData, + std::int32_t numFrames) { + auto* output = static_cast(audioData); + auto* ring = activeRingBuffer_.load(std::memory_order_acquire); + if (output == nullptr || numFrames <= 0) return oboe::DataCallbackResult::Continue; + + const std::size_t requestedFrames = static_cast(numFrames); + // While the ring is unpublished, stop()/flushOutput()/restart own every + // piece of fade/prebuffer state below. The callback then renders plain + // silence and touches nothing, which turns "null ring" into a proper + // handoff instead of a data race. + if (ring == nullptr) { + std::memset(output, 0, requestedFrames * kOutputChannels * sizeof(float)); + return oboe::DataCallbackResult::Continue; + } + + const bool playingNow = playbackEnabled_.load(std::memory_order_acquire); + // While paused, keep draining any queued tail under the 5 ms output + // volume ramp instead of hard-cutting to silence — a mid-sample gate is + // audible as a click on every device class. + const bool drainingPauseTail = + !playingNow && currentOutputVolume_ > 0.0F && + ring->availableToRead() >= kOutputChannels; + if (!playingNow && !drainingPauseTail) { + std::memset(output, 0, requestedFrames * kOutputChannels * sizeof(float)); + underrunActive_ = false; + recoveryFading_ = false; + prebuffering_ = true; + prebufferWaitCallbacks_ = kPrebufferMaxCallbacks; + lastOutput_.fill(0.0F); + return oboe::DataCallbackResult::Continue; + } + const std::size_t availableFrames = ring->availableToRead() / kOutputChannels; + if (prebuffering_ && playingNow && + (availableFrames == 0 || + (availableFrames < prebufferFrames_ && prebufferWaitCallbacks_-- > 0))) { + std::memset(output, 0, requestedFrames * kOutputChannels * sizeof(float)); + return oboe::DataCallbackResult::Continue; + } + if (prebuffering_) { + prebuffering_ = false; + recoveryFading_ = true; + recoveryFadePosition_ = 0; + } + const std::size_t readSamples = ring->read(output, requestedFrames * kOutputChannels); + const std::size_t validFrames = readSamples / kOutputChannels; + const bool wasUnderrun = underrunActive_; + + if (validFrames > 0) { + if (!inputAlreadyProcessed_.load(std::memory_order_acquire)) { + oboeDsp_.process(output, static_cast(validFrames)); + } + if (wasUnderrun) { + recoveryFading_ = true; + recoveryFadePosition_ = 0; + } + + // Bit-perfect must not scale valid PCM: recovery fades and the + // output-volume ramp are bypassed so the gain stays exactly 1.0. + // Starvation fill below only covers missing frames (no valid + // samples exist to preserve there). + const bool outputBitPerfect = oboeDsp_.isBitPerfectEnabled(); + for (std::size_t frame = 0; frame < validFrames; ++frame) { + float recoveryGain = 1.0F; + if (!outputBitPerfect && recoveryFading_) { + if (recoveryFadePosition_ < fadeInCurve_.size()) { + recoveryGain = fadeInCurve_[recoveryFadePosition_++]; + } else { + recoveryFading_ = false; + } + } + const std::size_t offset = frame * kOutputChannels; + // Pausing fades toward zero through the same ramp; resuming fades + // back up — both click-free. + const float volumeTarget = playingNow + ? targetOutputVolume_.load(std::memory_order_relaxed) + : 0.0F; + const float volumeDelta = volumeTarget - currentOutputVolume_; + currentOutputVolume_ += std::clamp( + volumeDelta, + -outputVolumeRampStep_, + outputVolumeRampStep_); + const float gain = outputBitPerfect + ? 1.0F + : recoveryGain * currentOutputVolume_; + output[offset] *= gain; + output[offset + 1U] *= gain; + + if (!outputBitPerfect) { + float s0 = output[offset]; + if (std::abs(s0) > 0.85F) s0 = std::tanh(s0); + output[offset] = std::clamp(s0, -0.98F, 0.98F); + + float s1 = output[offset + 1U]; + if (std::abs(s1) > 0.85F) s1 = std::tanh(s1); + output[offset + 1U] = std::clamp(s1, -0.98F, 0.98F); + } + } + lastOutput_[0] = output[(validFrames - 1U) * kOutputChannels]; + lastOutput_[1] = output[(validFrames - 1U) * kOutputChannels + 1U]; + renderedFrames_.fetch_add(validFrames, std::memory_order_release); + } + + if (validFrames < requestedFrames) { + if (playingNow) { + if (!wasUnderrun || validFrames > 0) { + underrunCount_.fetch_add(1, std::memory_order_release); + } + if (!wasUnderrun || validFrames > 0) { + underrunAnchor_ = lastOutput_; + underrunFadePosition_ = 0; + } + recoveryFading_ = false; + for (std::size_t frame = validFrames; frame < requestedFrames; ++frame) { + const float fadeOut = underrunFadePosition_ < fadeInCurve_.size() + ? 1.0F - fadeInCurve_[underrunFadePosition_++] + : 0.0F; + const std::size_t offset = frame * kOutputChannels; + output[offset] = underrunAnchor_[0] * fadeOut; + output[offset + 1U] = underrunAnchor_[1] * fadeOut; + } + underrunActive_ = true; + prebuffering_ = true; + prebufferWaitCallbacks_ = kPrebufferMaxCallbacks; + cleanFrames_ = 0; + // Adaptive resume runway: every episode doubles the required + // prebuffer (capped at half the ring) so cascading starvation on + // a throttling device self-extinguishes instead of repeating. + const auto capacityFrames = ring->capacity() / kOutputChannels; + prebufferFrames_ = std::max( + prebufferBaseFrames_, + std::min(prebufferFrames_ * 2U, capacityFrames / 2U)); + } else { + // Pause tail fully drained: plain silence with no accounting so a + // long pause is never mistaken for playback starvation. + std::memset( + output + validFrames * kOutputChannels, + 0, + (requestedFrames - validFrames) * kOutputChannels * sizeof(float)); + underrunActive_ = false; + recoveryFading_ = false; + } + } else { + underrunActive_ = false; + // Sustained glitch-free playback gradually forgives past underruns so + // a single transient CPU spike (navigation, camera, OEM throttling) + // can never accumulate into a permanent session downgrade, and steps + // the adaptive prebuffer back toward its baseline. + cleanFrames_ += requestedFrames; + while (cleanFrames_ >= underrunDecayIntervalFrames_) { + cleanFrames_ -= underrunDecayIntervalFrames_; + if (prebufferFrames_ > prebufferBaseFrames_) { + prebufferFrames_ = std::max( + prebufferBaseFrames_, prebufferFrames_ / 2U); + } + auto current = underrunCount_.load(std::memory_order_acquire); + if (current == 0) break; + while (current > 0 && !underrunCount_.compare_exchange_weak( + current, current - 1, std::memory_order_release)) { + } + } + } + + return oboe::DataCallbackResult::Continue; +} + +void AudioEngine::onErrorAfterClose( + oboe::AudioStream* audioStream, + oboe::Result /* error */) { + if (!restartAllowed_.load(std::memory_order_acquire)) return; + std::lock_guard controlLock(controlMutex_); + if (!restartAllowed_.load(std::memory_order_acquire) || + stream_ == nullptr || stream_.get() != audioStream) { + return; + } + + streamRestartCount_.fetch_add(1, std::memory_order_release); + activeRingBuffer_.store(nullptr, std::memory_order_release); + stream_.reset(); + outputSampleRate_.store(0, std::memory_order_release); + { + std::lock_guard producerLock(producerMutex_); + releaseProducerStateLocked(); + } + try { + (void) startLocked(requestedOutputSampleRate_.load(std::memory_order_acquire)); + } catch (...) { + // This callback runs on Oboe's recovery thread, outside a JNI frame. + // Never allow an allocation failure during restart to terminate the + // whole process. + restartAllowed_.store(false, std::memory_order_release); + activeRingBuffer_.store(nullptr, std::memory_order_release); + outputSampleRate_.store(0, std::memory_order_release); + } +} + +void AudioEngine::releaseProducerStateLocked() noexcept { + if (resampler_ != nullptr) { + soxr_delete(asSoxr(resampler_)); + resampler_ = nullptr; + } + resamplerInputRate_ = 0; + resamplerFlushing_ = false; + ringBuffer_.reset(); + rawScratch_.clear(); + stereoScratch_.clear(); + resampledScratch_.clear(); +} + +bool AudioEngine::configureResamplerLocked(std::int32_t inputSampleRate) { + const std::int32_t destinationRate = outputSampleRate_.load(std::memory_order_acquire); + if (destinationRate <= 0) return false; + // A NULL-input soxr flush is terminal. A new track/configuration gets a + // fresh resampler even when its sample rate matches the previous track. + if (resamplerFlushing_) { + if (resampler_ != nullptr) soxr_delete(asSoxr(resampler_)); + resampler_ = nullptr; + resamplerInputRate_ = 0; + resamplerFlushing_ = false; + } + if (resamplerInputRate_ == inputSampleRate) return true; + + if (resampler_ != nullptr) { + soxr_delete(asSoxr(resampler_)); + resampler_ = nullptr; + } + resamplerInputRate_ = inputSampleRate; + if (inputSampleRate == destinationRate) return true; + + soxr_error_t error = nullptr; + const soxr_io_spec_t ioSpec = soxr_io_spec(SOXR_FLOAT32_I, SOXR_FLOAT32_I); + const soxr_quality_spec_t qualitySpec = soxr_quality_spec(SOXR_HQ, 0); + const soxr_runtime_spec_t runtimeSpec = soxr_runtime_spec(1); + resampler_ = soxr_create( + static_cast(inputSampleRate), + static_cast(destinationRate), + kOutputChannels, + &error, + &ioSpec, + &qualitySpec, + &runtimeSpec); + if (error != nullptr || resampler_ == nullptr) { + if (resampler_ != nullptr) soxr_delete(asSoxr(resampler_)); + resampler_ = nullptr; + resamplerInputRate_ = 0; + return false; + } + return true; +} + +void AudioEngine::buildFadeCurve(std::int32_t sampleRate) { + const std::size_t fadeFrames = std::max( + 1, + static_cast(std::llround(sampleRate * 0.002))); + fadeInCurve_.resize(fadeFrames); + for (std::size_t frame = 0; frame < fadeFrames; ++frame) { + const double phase = kPi * static_cast(frame + 1U) / + static_cast(fadeFrames); + fadeInCurve_[frame] = static_cast(0.5 * (1.0 - std::cos(phase))); + } +} + +} // namespace lastwave::audio diff --git a/app/src/main/cpp/AutoDJProcessor.cpp b/app/src/main/cpp/AutoDJProcessor.cpp index f5a8a8083d..2591e90c82 100644 --- a/app/src/main/cpp/AutoDJProcessor.cpp +++ b/app/src/main/cpp/AutoDJProcessor.cpp @@ -46,17 +46,12 @@ AutoDJDecision AutoDJProcessor::evaluate( if (nextStage == TrackLifecycleStage::STREAM_RESOLVED) { return AutoDJDecision::DECODE_HEAD_WINDOW; } - if (nextStage == TrackLifecycleStage::DECODING_HEAD_WINDOW) { - return AutoDJDecision::ARM_STANDBY; - } return AutoDJDecision::DO_NOTHING; } const int64_t remainingMs = std::max(0, durationMs - currentPositionMs); - const int64_t leadWindowMs = m_latencyEstimator.calculateDynamicLeadWindowMs(transitionDurationMs); - const int64_t armLeadThresholdMs = ADVANCE_ARM_LEAD_MS + transitionDurationMs; // 40s + transition - // 5. Track finished or at transition edge + // 5. Track finished: remainingMs <= 0 if (remainingMs <= 0) { if (nextStage == TrackLifecycleStage::ARMED_READY) { return AutoDJDecision::TRIGGER_TRANSITION; @@ -74,33 +69,20 @@ AutoDJDecision AutoDJProcessor::evaluate( // 6. Active playback progression if (isPlaying) { - // Trigger transition right at exit cue + // Trigger transition right at exit cue ONLY if armed and ready if (remainingMs <= transitionDurationMs && nextStage == TrackLifecycleStage::ARMED_READY) { return AutoDJDecision::TRIGGER_TRANSITION; } - // Keep index + 1 fully armed at least 40s before planned exit cue - if (remainingMs <= armLeadThresholdMs) { - if (nextStage == TrackLifecycleStage::STREAM_RESOLVED) { - return AutoDJDecision::DECODE_HEAD_WINDOW; - } - if (nextStage == TrackLifecycleStage::DECODING_HEAD_WINDOW) { - return AutoDJDecision::ARM_STANDBY; - } - } - - // Never wait until track end: start resolving immediate next track as early as leadWindowMs - // (or immediately at track start) + // Immediate eager prefetch and pre-decode: if (nextStage == TrackLifecycleStage::QUEUED) { return AutoDJDecision::RESOLVE_NEXT_STREAM; } - - // If inside transition window and next track is still in-flight - if (remainingMs <= transitionDurationMs && - (nextStage == TrackLifecycleStage::RESOLVING_STREAM || - nextStage == TrackLifecycleStage::DECODING_HEAD_WINDOW)) { - return AutoDJDecision::WAIT_FOR_LATE_TRACK; + if (nextStage == TrackLifecycleStage::STREAM_RESOLVED) { + return AutoDJDecision::DECODE_HEAD_WINDOW; } + + // At transition edge, if not armed ready, return DO_NOTHING so Deck A plays naturally to end } return AutoDJDecision::DO_NOTHING; diff --git a/app/src/main/cpp/AutoMixBridge.cpp b/app/src/main/cpp/AutoMixBridge.cpp index 3fb9c7a63d..8fe715004c 100644 --- a/app/src/main/cpp/AutoMixBridge.cpp +++ b/app/src/main/cpp/AutoMixBridge.cpp @@ -228,6 +228,24 @@ Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBLeadingTrimFram return (engine != nullptr) ? static_cast(engine->getDeckBLeadingTrimFrames()) : 0L; } +JNIEXPORT jfloat JNICALL +Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBAvailableSeconds( + JNIEnv*, + jclass, + jlong handle) { + auto* engine = fromHandle(handle); + return (engine != nullptr) ? engine->getDeckBAvailableSeconds() : 0.0F; +} + +JNIEXPORT jlong JNICALL +Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBAvailableFrames( + JNIEnv*, + jclass, + jlong handle) { + auto* engine = fromHandle(handle); + return (engine != nullptr) ? static_cast(engine->getDeckBAvailableFrames()) : 0L; +} + JNIEXPORT jfloat JNICALL Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckABpm( JNIEnv*, diff --git a/app/src/main/cpp/AutoMixEngine.cpp b/app/src/main/cpp/AutoMixEngine.cpp index e97de474b6..4eb68bb00d 100644 --- a/app/src/main/cpp/AutoMixEngine.cpp +++ b/app/src/main/cpp/AutoMixEngine.cpp @@ -107,7 +107,8 @@ void SubtlePhaser::process(float& left, float& right, float send) noexcept { // AutoMixEngine Implementation // ============================================================================ -AutoMixEngine::AutoMixEngine() noexcept { +AutoMixEngine::AutoMixEngine() noexcept + : deckBRingBuffer_(192000 * 40 * 2) { configure(48000.0); } @@ -115,9 +116,14 @@ void AutoMixEngine::configure(double sampleRate) noexcept { if (sampleRate <= 0.0) sampleRate = 48000.0; sampleRate_ = sampleRate; - deckAFilter_.configure(sampleRate_, 180.0); - deckBFilter_.configure(sampleRate_, 180.0); - deckBHpf200_.configure(sampleRate_, 200.0); + deckAHpfL_.reset(); + deckAHpfR_.reset(); + deckBHpfL_.reset(); + deckBHpfR_.reset(); + currentGainA_ = 1.0f; + currentGainB_ = 0.0f; + currentCutoffA_ = 20.0f; + currentCutoffB_ = 200.0f; deckBStretcher_.configure(sampleRate_, 2); deckADetector_.configure(sampleRate_); @@ -125,30 +131,25 @@ void AutoMixEngine::configure(double sampleRate) noexcept { deckAEcho_.configure(sampleRate_, 120.0f); subtlePhaser_.configure(sampleRate_); - deckAHpfResonant_.update(ResonantFilter::Mode::HighPass, sampleRate_, 60.0, 1.25); deckAVocalDuckFilter_.update(ResonantFilter::Mode::PeakingCut, sampleRate_, 1200.0, 0.7, -9.0); - const std::size_t maxFrames = static_cast(sampleRate_ * 40.0); // 40s capacity - { - std::lock_guard lock(bufferMutex_); - deckBRingBuffer_.assign(maxFrames * 2, 0.0f); - deckBReadPos_ = 0; - deckBWritePos_ = 0; - deckBAvailableFrames_ = 0; - deckBTrimCalculated_ = false; - } + deckBRingBuffer_.clear(); + deckBLeadingTrimFrames_ = 0; + deckBFirstBeatMs_ = 0; + deckBDropBeatMs_ = 0; + deckBTrimCalculated_ = false; const std::size_t rollFrames = static_cast(sampleRate_ * 6.0); // 6s rolling buffer deckARollingBuffer_.assign(rollFrames * 2, 0.0f); deckARollingWritePos_ = 0; - deckAOneBarLoopBuffer_.clear(); + deckAOneBarLoopBuffer_.assign(rollFrames * 2, 0.0f); deckALoopFrames_ = 0; deckALoopReadPos_ = 0; deckALoopActive_ = false; - deckBScratch_.resize(4096 * 2, 0.0f); - deckBStretchedScratch_.resize(8192 * 2, 0.0f); - conversionScratch_.resize(4096 * 2, 0.0f); + deckBScratch_.assign(16384 * 2, 0.0f); + deckBStretchedScratch_.assign(32768 * 2, 0.0f); + conversionScratch_.assign(32768 * 2, 0.0f); } void AutoMixEngine::setEnabled(bool enabled) noexcept { @@ -179,8 +180,16 @@ bool AutoMixEngine::isPaused() const noexcept { } void AutoMixEngine::setTransitionPlan(const TransitionPlan& plan) noexcept { - std::lock_guard lock(planMutex_); - currentPlan_ = plan; + { + std::lock_guard lock(planMutex_); + currentPlan_ = plan; + } + + planBpmA_.store(plan.bpmA, std::memory_order_relaxed); + planBpmB_.store(plan.bpmB, std::memory_order_relaxed); + planBpmRatio_.store(plan.bpmRatio, std::memory_order_relaxed); + planGainOffsetDb_.store(std::clamp(plan.gainOffsetDb, -6.0f, 0.0f), std::memory_order_relaxed); + planTextureFx_.store(static_cast(plan.textureFx), std::memory_order_relaxed); if (plan.bpmRatio > 0.0f && std::abs(plan.bpmRatio - 1.0f) <= 0.08f) { deckBStretcher_.setTimeRatio(plan.bpmRatio); @@ -189,7 +198,7 @@ void AutoMixEngine::setTransitionPlan(const TransitionPlan& plan) noexcept { } deckAEcho_.configure(sampleRate_, plan.bpmA); - activeGainOffsetDb_ = plan.gainOffsetDb; + activeGainOffsetDb_ = std::clamp(plan.gainOffsetDb, -6.0f, 0.0f); } TransitionPlan AutoMixEngine::getTransitionPlan() const noexcept { @@ -251,11 +260,15 @@ void AutoMixEngine::startTransition(AutoMixState mode, int bars) noexcept { deckBStretcher_.setTimeRatio(1.0f); } - deckAFilter_.reset(); - deckBFilter_.reset(); - deckBHpf200_.reset(); - deckAHpfResonant_.reset(); - deckAHpfResonant_.update(ResonantFilter::Mode::HighPass, sampleRate_, 60.0, 1.25); + deckAHpfL_.reset(); + deckAHpfR_.reset(); + deckBHpfL_.reset(); + deckBHpfR_.reset(); + currentGainA_ = 1.0f; + currentGainB_ = 0.0f; + currentCutoffA_ = 20.0f; + currentCutoffB_ = 200.0f; + deckAVocalDuckFilter_.reset(); deckAVocalDuckFilter_.update(ResonantFilter::Mode::PeakingCut, sampleRate_, 1200.0, 0.7, -9.0); subtlePhaser_.reset(); @@ -296,19 +309,21 @@ void AutoMixEngine::reset() noexcept { deckAEcho_.setEnabled(false); subtlePhaser_.reset(); - deckAFilter_.reset(); - deckBFilter_.reset(); - deckBHpf200_.reset(); - deckAHpfResonant_.reset(); + deckAHpfL_.reset(); + deckAHpfR_.reset(); + deckBHpfL_.reset(); + deckBHpfR_.reset(); + currentGainA_ = 1.0f; + currentGainB_ = 0.0f; + currentCutoffA_ = 20.0f; + currentCutoffB_ = 200.0f; deckAVocalDuckFilter_.reset(); - { - std::lock_guard lock(bufferMutex_); - deckBReadPos_ = 0; - deckBWritePos_ = 0; - deckBAvailableFrames_ = 0; - deckBTrimCalculated_ = false; - } + deckBRingBuffer_.clear(); + deckBLeadingTrimFrames_ = 0; + deckBFirstBeatMs_ = 0; + deckBDropBeatMs_ = 0; + deckBTrimCalculated_ = false; deckALoopActive_ = false; deckALoopFrames_ = 0; @@ -323,42 +338,34 @@ void AutoMixEngine::reset() noexcept { std::size_t AutoMixEngine::writeDeckBPcm(const float* stereoPcm, std::size_t frames) noexcept { if (stereoPcm == nullptr || frames == 0) return 0; - std::size_t written = 0; - { - std::lock_guard lock(bufferMutex_); - const std::size_t capacityFrames = deckBRingBuffer_.size() / 2; - const std::size_t spaceFrames = capacityFrames - deckBAvailableFrames_; - written = std::min(frames, spaceFrames); - - for (std::size_t f = 0; f < written; ++f) { - deckBRingBuffer_[deckBWritePos_ * 2] = stereoPcm[f * 2]; - deckBRingBuffer_[deckBWritePos_ * 2 + 1] = stereoPcm[f * 2 + 1]; - deckBWritePos_ = (deckBWritePos_ + 1) % capacityFrames; - } - deckBAvailableFrames_ += written; - } + const std::size_t writtenSamples = deckBRingBuffer_.write(stereoPcm, frames * 2); + const std::size_t writtenFrames = writtenSamples / 2; - deckBDetector_.process(stereoPcm, written); + deckBDetector_.process(stereoPcm, writtenFrames); - if (!deckBTrimCalculated_ && deckBAvailableFrames_ >= static_cast(sampleRate_ * 3.0)) { + if (!deckBTrimCalculated_ && deckBRingBuffer_.availableToRead() / 2 >= static_cast(sampleRate_ * 3.0)) { detectDeckBFirstBeatAndSilence(); } - return written; + return writtenFrames; } void AutoMixEngine::detectDeckBFirstBeatAndSilence() noexcept { - std::lock_guard lock(bufferMutex_); - if (deckBAvailableFrames_ < static_cast(sampleRate_ * 1.5)) return; + const std::size_t availableFrames = deckBRingBuffer_.availableToRead() / 2; + if (availableFrames < static_cast(sampleRate_ * 1.5)) return; - const std::size_t totalCapacityFrames = deckBRingBuffer_.size() / 2; const std::size_t searchLimit = std::min( - deckBAvailableFrames_, + availableFrames, static_cast(sampleRate_ * 35.0)); - // 2nd-order Butterworth filters: 60-140 Hz sub-bass kick + std::vector searchBuf(searchLimit * 2); + const std::size_t peekedSamples = deckBRingBuffer_.peek(searchBuf.data(), searchLimit * 2); + const std::size_t peekedFrames = peekedSamples / 2; + if (peekedFrames < static_cast(sampleRate_ * 1.5)) return; + + // 2nd-order Butterworth filters: 60-140 Hz sub-bass kick (Q = 0.7071) const double wKick = 2.0 * M_PI * 95.0 / sampleRate_; - const double alphaKick = std::sin(wKick) / (2.0 * 1.2); + const double alphaKick = std::sin(wKick) / (2.0 * 0.7071); const double a0K = 1.0 + alphaKick; const double b0K = alphaKick / a0K, b1K = 0.0, b2K = -alphaKick / a0K; const double a1K = (-2.0 * std::cos(wKick)) / a0K, a2K = (1.0 - alphaKick) / a0K; @@ -376,8 +383,8 @@ void AutoMixEngine::detectDeckBFirstBeatAndSilence() noexcept { for (std::size_t b = 0; b < totalBlocks; ++b) { double kSum = 0.0, peak = 0.0; for (std::size_t j = 0; j < B; ++j) { - const std::size_t idx = (deckBReadPos_ + b * B + j) % totalCapacityFrames; - const double mono = (deckBRingBuffer_[idx * 2] + deckBRingBuffer_[idx * 2 + 1]) * 0.5; + const std::size_t idx = b * B + j; + const double mono = (searchBuf[idx * 2] + searchBuf[idx * 2 + 1]) * 0.5; const double kOut = b0K * mono + kZ1; kZ1 = b1K * mono - a1K * kOut + kZ2; @@ -421,7 +428,7 @@ void AutoMixEngine::detectDeckBFirstBeatAndSilence() noexcept { if (matched >= 4) { foundGridLock = true; dropBeatTimeSec = t0; - entryCueTimeSec = std::max(0.0, t0 - 4.0 * T); // 1 bar before drop + entryCueTimeSec = (t0 >= 4.0 * T) ? (t0 - 4.0 * T) : t0; // 1 bar before drop if available, else first beat break; } } @@ -432,9 +439,8 @@ void AutoMixEngine::detectDeckBFirstBeatAndSilence() noexcept { // Fallback: silence threshold search constexpr float kSilenceThreshold = 0.00562341325f; for (std::size_t f = 0; f < searchLimit; ++f) { - const std::size_t idx = (deckBReadPos_ + f) % totalCapacityFrames; - const float l = std::abs(deckBRingBuffer_[idx * 2]); - const float r = std::abs(deckBRingBuffer_[idx * 2 + 1]); + const float l = std::abs(searchBuf[f * 2]); + const float r = std::abs(searchBuf[f * 2 + 1]); if (l >= kSilenceThreshold || r >= kSilenceThreshold) { dropBeatTimeSec = static_cast(f) / sampleRate_; entryCueTimeSec = dropBeatTimeSec; @@ -445,9 +451,8 @@ void AutoMixEngine::detectDeckBFirstBeatAndSilence() noexcept { } const std::size_t chosenCueFrame = static_cast(entryCueTimeSec * sampleRate_); - if (chosenCueFrame > 0 && chosenCueFrame < deckBAvailableFrames_) { - deckBReadPos_ = (deckBReadPos_ + chosenCueFrame) % totalCapacityFrames; - deckBAvailableFrames_ -= chosenCueFrame; + if (chosenCueFrame > 0 && chosenCueFrame < availableFrames) { + deckBRingBuffer_.discard(chosenCueFrame * 2); deckBLeadingTrimFrames_ = chosenCueFrame; } @@ -455,20 +460,11 @@ void AutoMixEngine::detectDeckBFirstBeatAndSilence() noexcept { deckBDropBeatMs_ = static_cast(dropBeatTimeSec * 1000.0); deckBTrimCalculated_ = true; } - float AutoMixEngine::applySoftKneeLimiter(float x) noexcept { - // -0.5 dBFS soft-knee tanh limiter (threshold ~ 0.944f) - constexpr float kThreshold = 0.94406f; - constexpr float kMargin = 1.0f - kThreshold; - - const float absX = std::abs(x); - if (absX <= kThreshold) { - return x; + if (std::abs(x) > 0.85f) { + x = std::tanh(x); } - - const float excess = absX - kThreshold; - const float compressed = kThreshold + kMargin * std::tanh(excess / kMargin); - return std::copysign(compressed, x); + return std::clamp(x, -0.98f, 0.98f); } void AutoMixEngine::processDeckA(float* deckAStereo, std::size_t frames, float progress) noexcept { @@ -477,35 +473,25 @@ void AutoMixEngine::processDeckA(float* deckAStereo, std::size_t frames, float p progress = std::clamp(progress, 0.0f, 1.0f); - // 1. Vocal-Safe Loop / Beat-Repeat Slicer (t >= 0.55): - // If Deck A is verified vocal-free, lock into captured 1-bar drum loop - if (progress >= 0.55f && deckAVocalFree_.load(std::memory_order_acquire)) { - if (!deckALoopActive_) { - captureDeckALoop(deckADetector_.getResult().bpm); - } - if (deckALoopActive_ && deckALoopFrames_ > 0) { - for (std::size_t f = 0; f < frames; ++f) { - deckAStereo[f * 2] = deckAOneBarLoopBuffer_[deckALoopReadPos_ * 2]; - deckAStereo[f * 2 + 1] = deckAOneBarLoopBuffer_[deckALoopReadPos_ * 2 + 1]; - deckALoopReadPos_ = (deckALoopReadPos_ + 1) % deckALoopFrames_; - } + // 1. Vocal-Pocket Ducking: dynamic -9.0 dB peaking cut at 1200 Hz + if (deckBVocalsActive_.load(std::memory_order_relaxed)) { + for (std::size_t f = 0; f < frames; ++f) { + deckAStereo[f * 2] = deckAVocalDuckFilter_.tick(deckAStereo[f * 2], 0); + deckAStereo[f * 2 + 1] = deckAVocalDuckFilter_.tick(deckAStereo[f * 2 + 1], 1); } } - // 2. Vocal-Pocket Ducking: dynamic -9.0 dB peaking cut at 1200 Hz if Deck B has vocals - if (deckBVocalsActive_.load(std::memory_order_acquire)) { + // 2. Vocal-Safe Loop / Beat-Repeat Exit Buffer (t >= 0.55): + if (progress >= 0.55f && deckALoopActive_ && deckALoopFrames_ > 0) { for (std::size_t f = 0; f < frames; ++f) { - deckAStereo[f * 2] = deckAVocalDuckFilter_.tick(deckAStereo[f * 2], 0); - deckAStereo[f * 2 + 1] = deckAVocalDuckFilter_.tick(deckAStereo[f * 2 + 1], 1); + deckAStereo[f * 2] = deckAOneBarLoopBuffer_[deckALoopReadPos_ * 2]; + deckAStereo[f * 2 + 1] = deckAOneBarLoopBuffer_[deckALoopReadPos_ * 2 + 1]; + deckALoopReadPos_ = (deckALoopReadPos_ + 1) % deckALoopFrames_; } } // 3. Single Texture FX Auto-Chooser (t >= 0.40): - TransitionTextureFx fx = TransitionTextureFx::None; - { - std::lock_guard lock(planMutex_); - fx = currentPlan_.textureFx; - } + const auto fx = static_cast(planTextureFx_.load(std::memory_order_relaxed)); if (progress >= 0.40f && fx != TransitionTextureFx::None) { const float send = std::clamp((progress - 0.40f) / 0.20f, 0.0f, 0.35f); if (fx == TransitionTextureFx::SubtlePhaser) { @@ -513,7 +499,6 @@ void AutoMixEngine::processDeckA(float* deckAStereo, std::size_t frames, float p subtlePhaser_.process(deckAStereo[f * 2], deckAStereo[f * 2 + 1], send); } } else if (fx == TransitionTextureFx::StereoWidthExpander) { - // Stereo width expander on Deck A const float width = 1.0f + send * 0.8f; for (std::size_t f = 0; f < frames; ++f) { const float m = (deckAStereo[f * 2] + deckAStereo[f * 2 + 1]) * 0.5f; @@ -524,37 +509,45 @@ void AutoMixEngine::processDeckA(float* deckAStereo, std::size_t frames, float p } } - // 4. Equal-power gain curve with -1.5 dB center overlap trim - // gainA = cos(t * PI / 2) * centerTrim - const float gainA = std::cos(progress * static_cast(M_PI) * 0.5f); - const float centerTrim = 1.0f - 0.1586f * (4.0f * progress * (1.0f - progress)); - const float effGainA = gainA * centerTrim; - - // 5. Complementary 3-Band Crossover & Hard Downbeat Bass-Swap - if (progress < 0.55f) { - // Phase 1 (t = 0.00 to 0.55): Deck A holds 100% of the sub-bass (<180 Hz at 0.0 dB)! - deckAFilter_.processBassAttenuate(deckAStereo, frames, 1.0f, 1.0f); + // 4. Equal-power gain curve with correlated-bass headroom compensation factor + const float headroomComp = 1.0f - 0.25f * std::sin(progress * static_cast(M_PI)); + const float targetGainA = std::cos(progress * static_cast(M_PI) * 0.5f) * headroomComp; - for (std::size_t i = 0; i < frames * 2; ++i) { - deckAStereo[i] *= effGainA; - } + // 5. Smooth 3-Second S-Curve Bass Swap: + // Across the middle 35% to 65% of transition, cross-fade Deck A HPF from 20 Hz up to 350 Hz + // using continuous cosine S-curve: (0.5f - 0.5f * cos(PI * localProgress)) + float targetCutoffA = 20.0f; + if (progress < 0.35f) { + targetCutoffA = 20.0f; + } else if (progress > 0.65f) { + targetCutoffA = 350.0f; } else { - // Midpoint Downbeat Swap (t = 0.55): - // Over 1 beat (~400ms), sweep Deck A's resonant High-Pass Filter (Q = 1.25) from 60 Hz up to 550 Hz - const float swapProgress = std::clamp((progress - 0.55f) / 0.10f, 0.0f, 1.0f); - const double cutoffHz = 60.0 + swapProgress * (550.0 - 60.0); - deckAHpfResonant_.update(ResonantFilter::Mode::HighPass, sampleRate_, cutoffHz, 1.25); + const float localProgress = (progress - 0.35f) / 0.30f; + const float sCurve = 0.5f - 0.5f * std::cos(static_cast(M_PI) * localProgress); + targetCutoffA = 20.0f + sCurve * (350.0f - 20.0f); + } - for (std::size_t f = 0; f < frames; ++f) { - deckAStereo[f * 2] = deckAHpfResonant_.tick(deckAStereo[f * 2], 0) * effGainA; - deckAStereo[f * 2 + 1] = deckAHpfResonant_.tick(deckAStereo[f * 2 + 1], 1) * effGainA; - } + // Zero Zipper Noise: Per-sample smoothing (one-pole low-pass: val += 0.0015f * (target - val)) + Cytomic SVF + const float sr = static_cast(sampleRate_); + for (std::size_t f = 0; f < frames; ++f) { + currentGainA_ += 0.0015f * (targetGainA - currentGainA_); + currentCutoffA_ += 0.0015f * (targetCutoffA - currentCutoffA_); + + const float inL = deckAStereo[f * 2]; + const float inR = deckAStereo[f * 2 + 1]; + deckAStereo[f * 2] = deckAHpfL_.tickHighPass(inL, currentCutoffA_, sr, 0.7071f) * currentGainA_; + deckAStereo[f * 2 + 1] = deckAHpfR_.tickHighPass(inR, currentCutoffA_, sr, 0.7071f) * currentGainA_; + } - // Outgoing Reverb Wash Tail (t = 0.50 to 1.00 + 1.4s): - // Auxiliary send (0.0 -> 0.50 wet) into stereo damping reverb + 1/2-beat echo - const float wetSend = std::clamp((progress - 0.50f) / 0.50f * 0.50f, 0.0f, 0.50f); - const float dryGain = (1.0f - progress); - deckAEcho_.process(deckAStereo, frames, wetSend, dryGain, /*apply55WetMix=*/true); + // 6. Outgoing Reverb Wash Tail (t = 0.50 to 1.00): + // Scale reverb/echo wet returns by 0.22f maximum and attenuate dry Deck A proportionally (dryGain *= (1.0f - 0.35f * wetSend)) + if (progress >= 0.50f) { + const float rawWet = std::clamp((progress - 0.50f) / 0.50f, 0.0f, 1.0f); + const float wetSend = std::clamp(rawWet * 0.22f, 0.0f, 0.22f); + float dryGain = (1.0f - progress); + dryGain *= (1.0f - 0.35f * wetSend); + const float wetReturn = wetSend; + deckAEcho_.process(deckAStereo, frames, wetSend, dryGain, wetReturn); } } @@ -564,48 +557,53 @@ void AutoMixEngine::processDeckB(float* deckBStereo, std::size_t frames, float p progress = std::clamp(progress, 0.0f, 1.0f); - // 1. Equal-power gain curve with -1.5 dB center overlap trim - // gainB = sin(t * PI / 2) * centerTrim - const float gainB = std::sin(progress * static_cast(M_PI) * 0.5f); - const float centerTrim = 1.0f - 0.1586f * (4.0f * progress * (1.0f - progress)); + // 1. Equal-power gain curve with correlated-bass headroom compensation factor + const float headroomComp = 1.0f - 0.25f * std::sin(progress * static_cast(M_PI)); - // 2. Windowed LUFS / RMS Loudness Makeup Gain + // 2. Windowed RMS Loudness Matching: attenuation only, clamp(targetRmsDb - deckRmsDb, -6.0f, 0.0f) + // Never amplify a deck above 1.0f (0.0 dBFS) float makeupLinear = 1.0f; - { - std::lock_guard lock(planMutex_); - if (state_.load(std::memory_order_acquire) == AutoMixState::PostRestore) { - // Decay gainOffsetDb linearly back to 0.0 dB over 10.0 seconds - if (postRestoreTotalFrames_ > 0 && postRestoreFramesElapsed_ < postRestoreTotalFrames_) { - const float decayFactor = 1.0f - static_cast(postRestoreFramesElapsed_) / static_cast(postRestoreTotalFrames_); - const float decayingDb = currentPlan_.gainOffsetDb * decayFactor; - makeupLinear = std::pow(10.0f, decayingDb / 20.0f); - postRestoreFramesElapsed_ += frames; - } else { - makeupLinear = 1.0f; - } + const float planGain = planGainOffsetDb_.load(std::memory_order_relaxed); + const float clampedGainDb = std::clamp(planGain, -6.0f, 0.0f); // Attenuation only! + if (state_.load(std::memory_order_acquire) == AutoMixState::PostRestore) { + if (postRestoreTotalFrames_ > 0 && postRestoreFramesElapsed_ < postRestoreTotalFrames_) { + const float decayFactor = 1.0f - static_cast(postRestoreFramesElapsed_) / static_cast(postRestoreTotalFrames_); + const float decayingDb = clampedGainDb * decayFactor; + makeupLinear = std::pow(10.0f, decayingDb / 20.0f); + postRestoreFramesElapsed_ += frames; } else { - makeupLinear = std::pow(10.0f, currentPlan_.gainOffsetDb / 20.0f); + makeupLinear = 1.0f; } + } else { + makeupLinear = std::pow(10.0f, clampedGainDb / 20.0f); } - const float effGainB = gainB * centerTrim * makeupLinear; - - // 3. Complementary 3-Band Crossover & Hard Downbeat Bass-Swap - if (progress < 0.55f) { - // Phase 1 (t = 0.00 to 0.55): Deck B has 2nd-order Linkwitz-Riley HPF at 200 Hz (-18 dB on sub-bass) - // Hi-hats, percussion, and intro melody glide in cleanly without clashing kicks - deckBHpf200_.processHighPass(deckBStereo, frames); + const float targetGainB = std::sin(progress * static_cast(M_PI) * 0.5f) * headroomComp * makeupLinear; - for (std::size_t i = 0; i < frames * 2; ++i) { - deckBStereo[i] *= effGainB; - } + // 3. Smooth 3-Second S-Curve Bass Swap: + // Across the middle 35% to 65% of transition, cross-fade Deck B HPF from 200 Hz down to 20 Hz + // using continuous cosine S-curve: (0.5f - 0.5f * cos(PI * localProgress)) + float targetCutoffB = 200.0f; + if (progress < 0.35f) { + targetCutoffB = 200.0f; + } else if (progress > 0.65f) { + targetCutoffB = 20.0f; } else { - // Midpoint Downbeat Swap (t = 0.55): - // Instantaneously drop Deck B HPF to 20 Hz (full 0.0 dB sub-bass slam)! - // Zero bass holes, 100% full sub-bass punch and crisp high-end - for (std::size_t i = 0; i < frames * 2; ++i) { - deckBStereo[i] *= effGainB; - } + const float localProgress = (progress - 0.35f) / 0.30f; + const float sCurve = 0.5f - 0.5f * std::cos(static_cast(M_PI) * localProgress); + targetCutoffB = 200.0f - sCurve * (200.0f - 20.0f); + } + + // Zero Zipper Noise: Per-sample smoothing (one-pole low-pass: val += 0.0015f * (target - val)) + Cytomic SVF + const float sr = static_cast(sampleRate_); + for (std::size_t f = 0; f < frames; ++f) { + currentGainB_ += 0.0015f * (targetGainB - currentGainB_); + currentCutoffB_ += 0.0015f * (targetCutoffB - currentCutoffB_); + + const float inL = deckBStereo[f * 2]; + const float inR = deckBStereo[f * 2 + 1]; + deckBStereo[f * 2] = deckBHpfL_.tickHighPass(inL, currentCutoffB_, sr, 0.7071f) * currentGainB_; + deckBStereo[f * 2 + 1] = deckBHpfR_.tickHighPass(inR, currentCutoffB_, sr, 0.7071f) * currentGainB_; } } @@ -614,7 +612,7 @@ void AutoMixEngine::process(float* deckAStereo, std::size_t frames) noexcept { if (!enabled_.load(std::memory_order_acquire)) return; if (paused_.load(std::memory_order_acquire)) return; - deckADetector_.process(deckAStereo, frames); + // Strict lock-free real-time audio thread: zero FFT/BPM calculations on audio thread feedDeckARollingBuffer(deckAStereo, frames); AutoMixState currState = state_.load(std::memory_order_acquire); @@ -647,32 +645,17 @@ void AutoMixEngine::process(float* deckAStereo, std::size_t frames) noexcept { : 1.0f; transitionProgress_ = std::clamp(p, 0.0f, 1.0f); - if (deckBScratch_.size() < frames * 2) { - deckBScratch_.resize(frames * 2, 0.0f); - } - - std::size_t actualDeckBFrames = 0; - { - std::lock_guard lock(bufferMutex_); - const std::size_t totalCapacityFrames = deckBRingBuffer_.size() / 2; - actualDeckBFrames = std::min(frames, deckBAvailableFrames_); - for (std::size_t f = 0; f < actualDeckBFrames; ++f) { - deckBScratch_[f * 2] = deckBRingBuffer_[deckBReadPos_ * 2]; - deckBScratch_[f * 2 + 1] = deckBRingBuffer_[deckBReadPos_ * 2 + 1]; - deckBReadPos_ = (deckBReadPos_ + 1) % totalCapacityFrames; - } - deckBAvailableFrames_ -= actualDeckBFrames; - } + const std::size_t framesToRead = std::min(frames, deckBScratch_.size() / 2); + const std::size_t readSamples = deckBRingBuffer_.read(deckBScratch_.data(), framesToRead * 2); + const std::size_t actualDeckBFrames = readSamples / 2; if (actualDeckBFrames < frames) { - std::fill(deckBScratch_.begin() + actualDeckBFrames * 2, deckBScratch_.begin() + frames * 2, 0.0f); + std::fill(deckBScratch_.data() + actualDeckBFrames * 2, deckBScratch_.data() + frames * 2, 0.0f); } // Time-stretch Deck B - if (deckBStretchedScratch_.size() < frames * 4) { - deckBStretchedScratch_.resize(frames * 4, 0.0f); - } + const std::size_t stretchedCap = deckBStretchedScratch_.size() / 2; std::size_t stretchedFrames = deckBStretcher_.process( - deckBScratch_.data(), frames, deckBStretchedScratch_.data(), frames * 2); + deckBScratch_.data(), frames, deckBStretchedScratch_.data(), stretchedCap); if (stretchedFrames < frames) { std::copy(deckBScratch_.begin() + stretchedFrames * 2, deckBScratch_.begin() + frames * 2, deckBStretchedScratch_.begin() + stretchedFrames * 2); @@ -682,7 +665,7 @@ void AutoMixEngine::process(float* deckAStereo, std::size_t frames) noexcept { processDeckA(deckAStereo, frames, transitionProgress_); processDeckB(deckBStretchedScratch_.data(), frames, transitionProgress_); - // Sum signals and apply -0.5 dBFS soft-knee tanh limiter + // Sum signals and apply -0.85f threshold / 0.98f ceiling soft-knee limiter for (std::size_t i = 0; i < frames * 2; ++i) { const float mixed = deckAStereo[i] + deckBStretchedScratch_[i]; deckAStereo[i] = applySoftKneeLimiter(mixed); @@ -703,6 +686,7 @@ void AutoMixEngine::process(float* deckAStereo, std::size_t frames) noexcept { } } + bool AutoMixEngine::processDeckBuffer( void* pcm, std::size_t frameCount, @@ -718,9 +702,7 @@ bool AutoMixEngine::processDeckBuffer( return false; } - if (conversionScratch_.size() < frameCount * 2) { - conversionScratch_.resize(frameCount * 2); - } + frameCount = std::min(frameCount, conversionScratch_.size() / 2); float* scratch = conversionScratch_.data(); // 1. Unpack PCM to Float32 stereo @@ -838,9 +820,8 @@ void AutoMixEngine::captureDeckALoop(float bpmA) noexcept { const std::size_t loopFrames = static_cast(barSec * sampleRate_); const std::size_t capacityFrames = deckARollingBuffer_.size() / 2; - if (loopFrames == 0 || loopFrames > capacityFrames) return; + if (loopFrames == 0 || loopFrames > capacityFrames || loopFrames * 2 > deckAOneBarLoopBuffer_.size()) return; - deckAOneBarLoopBuffer_.resize(loopFrames * 2); const std::size_t startPos = (deckARollingWritePos_ + capacityFrames - loopFrames) % capacityFrames; for (std::size_t f = 0; f < loopFrames; ++f) { const std::size_t rPos = (startPos + f) % capacityFrames; diff --git a/app/src/main/cpp/AutoMixEngine.h b/app/src/main/cpp/AutoMixEngine.h index eabdf8fce4..923ca6ff8a 100644 --- a/app/src/main/cpp/AutoMixEngine.h +++ b/app/src/main/cpp/AutoMixEngine.h @@ -5,6 +5,7 @@ #include "BpmPhaseDetector.h" #include "EchoTailProcessor.h" #include "TransitionPlanner.h" +#include "LockFreeRingBuffer.h" #include #include @@ -12,6 +13,7 @@ #include #include #include +#include namespace lastwave::audio { @@ -25,6 +27,51 @@ enum class AutoMixState : int { PostRestore = 6 }; +// Trapezoidal Integrated State Variable Filter (Cytomic SVF) with Butterworth Q=0.7071 +struct CytomicSVF final { + float s1{0.0f}; + float s2{0.0f}; + + void reset() noexcept { + s1 = 0.0f; + s2 = 0.0f; + } + + [[nodiscard]] inline float tickHighPass(float v0, float cutoffHz, float sampleRate, float Q = 0.7071f) noexcept { + cutoffHz = std::clamp(cutoffHz, 10.0f, sampleRate * 0.45f); + const float g = std::tan(static_cast(M_PI) * cutoffHz / sampleRate); + const float k = 1.0f / std::max(0.01f, Q); + const float a1 = 1.0f / (1.0f + g * (g + k)); + const float a2 = g * a1; + const float a3 = g * a2; + + const float v3 = v0 - s2; + const float v1 = a1 * s1 + a2 * v3; + const float v2 = s2 + a2 * s1 + a3 * v3; + s1 = 2.0f * v1 - s1; + s2 = 2.0f * v2 - s2; + + return v0 - k * v1 - v2; + } + + [[nodiscard]] inline float tickLowPass(float v0, float cutoffHz, float sampleRate, float Q = 0.7071f) noexcept { + cutoffHz = std::clamp(cutoffHz, 10.0f, sampleRate * 0.45f); + const float g = std::tan(static_cast(M_PI) * cutoffHz / sampleRate); + const float k = 1.0f / std::max(0.01f, Q); + const float a1 = 1.0f / (1.0f + g * (g + k)); + const float a2 = g * a1; + const float a3 = g * a2; + + const float v3 = v0 - s2; + const float v1 = a1 * s1 + a2 * v3; + const float v2 = s2 + a2 * s1 + a3 * v3; + s1 = 2.0f * v1 - s1; + s2 = 2.0f * v2 - s2; + + return v2; + } +}; + // 2nd-order resonant Biquad filter for DJ sweeps and peaking cuts struct ResonantFilter final { enum class Mode { HighPass, LowPass, PeakingCut }; @@ -104,6 +151,8 @@ class AutoMixEngine final { [[nodiscard]] AutoMixState getState() const noexcept; [[nodiscard]] float getTransitionProgress() const noexcept; [[nodiscard]] std::size_t getDeckBLeadingTrimFrames() const noexcept; + [[nodiscard]] std::size_t getDeckBAvailableFrames() const noexcept; + [[nodiscard]] float getDeckBAvailableSeconds() const noexcept; [[nodiscard]] std::int64_t getDeckBFirstBeatMs() const noexcept; [[nodiscard]] float getDeckABpm() const noexcept; [[nodiscard]] float getDeckBBpm() const noexcept; @@ -120,30 +169,35 @@ class AutoMixEngine final { std::atomic state_{AutoMixState::Idle}; std::atomic paused_{false}; - // Filters and Processors - LinkwitzRileyFilter deckAFilter_; - LinkwitzRileyFilter deckBFilter_; - LinkwitzRileyFilter deckBHpf200_; // 2nd-order LR HPF at 200 Hz for Deck B (Phase 1: t in [0.0, 0.55]) + // Filters and Processors: Cytomic SVF for high-pass/low-pass crossover and S-curve bass swap + CytomicSVF deckAHpfL_; + CytomicSVF deckAHpfR_; + CytomicSVF deckBHpfL_; + CytomicSVF deckBHpfR_; + float currentGainA_{1.0f}; + float currentGainB_{0.0f}; + float currentCutoffA_{20.0f}; + float currentCutoffB_{200.0f}; + WsolaTimeStretcher deckBStretcher_; BpmPhaseDetector deckADetector_; BpmPhaseDetector deckBDetector_; EchoTailProcessor deckAEcho_; - ResonantFilter deckAHpfResonant_; // Resonant HPF (Q=1.25) swept from 60 Hz to 550 Hz on midpoint bass swap ResonantFilter deckAVocalDuckFilter_; // Dynamic -9.0 dB peaking cut (1200 Hz, Q=0.7) when Deck B has vocals SubtlePhaser subtlePhaser_; mutable std::mutex planMutex_; TransitionPlan currentPlan_{}; + std::atomic planBpmA_{120.0f}; + std::atomic planBpmB_{120.0f}; + std::atomic planBpmRatio_{1.0f}; + std::atomic planGainOffsetDb_{0.0f}; + std::atomic planTextureFx_{0}; std::atomic deckBVocalsActive_{false}; std::atomic deckAVocalFree_{true}; - mutable std::mutex bufferMutex_; - std::vector deckBRingBuffer_; - std::size_t deckBReadPos_{0}; - std::size_t deckBWritePos_{0}; - std::size_t deckBAvailableFrames_{0}; - + LockFreeRingBuffer deckBRingBuffer_; std::size_t deckBLeadingTrimFrames_{0}; std::int64_t deckBFirstBeatMs_{0}; std::int64_t deckBDropBeatMs_{0}; diff --git a/app/src/main/cpp/EchoTailProcessor.h b/app/src/main/cpp/EchoTailProcessor.h index d21a268f23..01d6b8d13e 100644 --- a/app/src/main/cpp/EchoTailProcessor.h +++ b/app/src/main/cpp/EchoTailProcessor.h @@ -87,8 +87,6 @@ class EchoTailProcessor final { // 1.5s Schroeder reverb decay time (T60 = 1.5s) const double decayTime = 1.5; - // Comb feedback: g = exp(-6.907755 * (combDelaySec / decayTime)) - const double sScale = sampleRate_ / 44100.0; const std::array combDelaysMs{29.7, 37.1, 41.1, 43.7}; for (std::size_t i = 0; i < 4; ++i) { @@ -145,15 +143,18 @@ class EchoTailProcessor final { return delayFrames_; } - // Processes Deck A audio: sends into 1/2-beat delay + 1.5s Schroeder reverb tail with 55% wet mix + // Processes Deck A audio: sends into 1/2-beat delay + 1.5s Schroeder reverb tail void process( float* stereoInterleaved, std::size_t frames, float sendGain, float dryGain, - bool apply55WetMix = false) noexcept { + float wetReturn = 0.75F) noexcept { if (stereoInterleaved == nullptr || frames == 0) return; + sendGain = std::clamp(sendGain, 0.0F, 1.0F); + wetReturn = std::clamp(wetReturn, 0.0F, 1.0F); + if (!enabled_ && sendGain <= 0.0F) { if (std::abs(dryGain - 1.0F) > 0.0001F) { for (std::size_t i = 0; i < frames * 2; ++i) { @@ -207,14 +208,8 @@ class EchoTailProcessor final { const float wetL = echoL * 0.70F + revL * 0.45F; const float wetR = echoR * 0.70F + revR * 0.45F; - if (apply55WetMix) { - // Exact 55% wet mix: 45% dry + 55% wet - stereoInterleaved[inOffset] = (inL * dryGain) * 0.45F + wetL * 0.55F; - stereoInterleaved[inOffset + 1] = (inR * dryGain) * 0.45F + wetR * 0.55F; - } else { - stereoInterleaved[inOffset] = inL * dryGain + wetL * sendGain; - stereoInterleaved[inOffset + 1] = inR * dryGain + wetR * sendGain; - } + stereoInterleaved[inOffset] = inL * dryGain + wetL * wetReturn; + stereoInterleaved[inOffset + 1] = inR * dryGain + wetR * wetReturn; } } diff --git a/app/src/main/cpp/LockFreeRingBuffer.h b/app/src/main/cpp/LockFreeRingBuffer.h index 00fd65c7f9..6c77d441d9 100644 --- a/app/src/main/cpp/LockFreeRingBuffer.h +++ b/app/src/main/cpp/LockFreeRingBuffer.h @@ -58,6 +58,27 @@ class LockFreeRingBuffer final { return readable; } + std::size_t peek(T* destination, std::size_t count) const noexcept { + if (destination == nullptr || count == 0) return 0; + const auto read = readIndex_.load(std::memory_order_relaxed); + const auto write = writeIndex_.load(std::memory_order_acquire); + const auto readable = std::min(count, static_cast(write - read)); + const auto offset = static_cast(read % capacity_); + const auto first = std::min(readable, capacity_ - offset); + std::memcpy(destination, data_.get() + offset, first * sizeof(T)); + std::memcpy(destination + first, data_.get(), (readable - first) * sizeof(T)); + return readable; + } + + std::size_t discard(std::size_t count) noexcept { + if (count == 0) return 0; + const auto read = readIndex_.load(std::memory_order_relaxed); + const auto write = writeIndex_.load(std::memory_order_acquire); + const auto readable = std::min(count, static_cast(write - read)); + readIndex_.store(read + readable, std::memory_order_release); + return readable; + } + // Drops all buffered samples. The producer must be quiesced and the // consumer parked on another buffer before calling this. Advancing the // read index (instead of zeroing both) keeps any already-in-flight read diff --git a/app/src/main/cpp/TransitionPlanner.cpp b/app/src/main/cpp/TransitionPlanner.cpp index 16b916a613..ab9c45e7f6 100644 --- a/app/src/main/cpp/TransitionPlanner.cpp +++ b/app/src/main/cpp/TransitionPlanner.cpp @@ -384,8 +384,8 @@ TransitionPlan TransitionPlanner::planTransition( ); const float rmsB_dB = computeGatedRmsDb(deckBHeadPcm, frames15sB); - // Makeup gain offset: clamp(rmsA_dB - rmsB_dB, -4.0f, +5.5f) - plan.gainOffsetDb = std::clamp(rmsA_dB - rmsB_dB, -4.0f, 5.5f); + // Attenuation only: clamp(rmsA_dB - rmsB_dB, -6.0f, 0.0f) to prevent clipping/distortion + plan.gainOffsetDb = std::clamp(rmsA_dB - rmsB_dB, -6.0f, 0.0f); // 7. Vocal-Safe Loop / Beat-Repeat Slicer Eligibility on Deck A (t >= 0.55) // Check if Deck A is vocal-free from outCueMs + 0.55 * durationMs to track end diff --git a/app/src/main/cpp/test_autodj_processor.cpp b/app/src/main/cpp/test_autodj_processor.cpp index 13d6d6246f..32dd306203 100644 --- a/app/src/main/cpp/test_autodj_processor.cpp +++ b/app/src/main/cpp/test_autodj_processor.cpp @@ -12,9 +12,9 @@ void testLatencyEstimator() { assert(estimator.getStdDevMs() == 1000.0); // Initial lead window with 5000ms transition: - // 5000 + 2500 + 2.5 * 1000 + 5000 = 15000 + // clamped to [20000, 70000] int64_t initialLead = estimator.calculateDynamicLeadWindowMs(5000); - assert(initialLead == 15000); + assert(initialLead == 20000); // Record a 4000ms slow sample estimator.recordSample(4000.0, true); @@ -23,7 +23,7 @@ void testLatencyEstimator() { int64_t updatedLead = estimator.calculateDynamicLeadWindowMs(5000); assert(updatedLead >= initialLead); - assert(updatedLead <= 60000); + assert(updatedLead <= 70000); std::cout << " Mean: " << estimator.getMeanMs() << " ms, StdDev: " << estimator.getStdDevMs() << " ms, Lead: " << updatedLead << " ms" << std::endl; @@ -37,36 +37,36 @@ void testAutoDJProcessorDecisions() { const int64_t trackDuration = 180000; // 3 min const int64_t transitionMs = 5000; - // 1. Beginning of track (pos = 10s): should DO_NOTHING + // 1. Beginning of track (pos = 10s): should eagerly prefetch next track immediately auto d1 = processor.evaluate(10000, trackDuration, true, transitionMs, TrackLifecycleStage::QUEUED, 0, 5); - assert(d1 == AutoDJDecision::DO_NOTHING); + assert(d1 == AutoDJDecision::RESOLVE_NEXT_STREAM); - // 2. Lead window reached (pos = 168s, remaining = 12s < 15s): should PREFETCH_NEXT - auto d2 = processor.evaluate(168000, trackDuration, true, transitionMs, TrackLifecycleStage::QUEUED, 0, 5); - assert(d2 == AutoDJDecision::PREFETCH_NEXT); + // 2. Track resolved: should decode head window immediately + auto d2 = processor.evaluate(168000, trackDuration, true, transitionMs, TrackLifecycleStage::STREAM_RESOLVED, 0, 5); + assert(d2 == AutoDJDecision::DECODE_HEAD_WINDOW); - // 3. Track resolved: should PREPARE_STANDBY - auto d3 = processor.evaluate(168000, trackDuration, true, transitionMs, TrackLifecycleStage::RESOLVED, 0, 5); - assert(d3 == AutoDJDecision::PREPARE_STANDBY); + // 3. Decoding head window in progress: DO_NOTHING until PCM buffer is verified + auto d3 = processor.evaluate(168000, trackDuration, true, transitionMs, TrackLifecycleStage::DECODING_HEAD_WINDOW, 0, 5); + assert(d3 == AutoDJDecision::DO_NOTHING); - // 4. Transition window reached (pos = 176s, remaining = 4s <= 5s) and READY: should TRIGGER_TRANSITION - auto d4 = processor.evaluate(176000, trackDuration, true, transitionMs, TrackLifecycleStage::READY, 0, 5); + // 4. Transition window reached (pos = 176s, remaining = 4s <= 5s) and ARMED_READY: should TRIGGER_TRANSITION + auto d4 = processor.evaluate(176000, trackDuration, true, transitionMs, TrackLifecycleStage::ARMED_READY, 0, 5); assert(d4 == AutoDJDecision::TRIGGER_TRANSITION); - // 5. Track ends but next track is still RESOLVING: should WAIT_FOR_LATE_TRACK - auto d5 = processor.evaluate(180000, trackDuration, true, transitionMs, TrackLifecycleStage::RESOLVING, 0, 5); + // 5. Track ends but next track is still in-flight: should WAIT_FOR_LATE_TRACK + auto d5 = processor.evaluate(180000, trackDuration, true, transitionMs, TrackLifecycleStage::RESOLVING_STREAM, 0, 5); assert(d5 == AutoDJDecision::WAIT_FOR_LATE_TRACK); - // 6. Next track failed with retryCount = 1 (< 3): should PREFETCH_NEXT (retry) - auto d6 = processor.evaluate(168000, trackDuration, true, transitionMs, TrackLifecycleStage::FAILED, 1, 5); - assert(d6 == AutoDJDecision::PREFETCH_NEXT); + // 6. Next track failed with retryCount = 1 (< 2): should RESOLVE_NEXT_STREAM (retry) + auto d6 = processor.evaluate(168000, trackDuration, true, transitionMs, TrackLifecycleStage::FAILED_RETRYING, 1, 5); + assert(d6 == AutoDJDecision::RESOLVE_NEXT_STREAM); - // 7. Next track failed with retryCount = 3 (>= 3): should SKIP_FAILED_TRACK - auto d7 = processor.evaluate(168000, trackDuration, true, transitionMs, TrackLifecycleStage::FAILED, 3, 5); - assert(d7 == AutoDJDecision::SKIP_FAILED_TRACK); + // 7. Next track failed with retryCount = 2 (>= 2): should SKIP_UNPLAYABLE + auto d7 = processor.evaluate(168000, trackDuration, true, transitionMs, TrackLifecycleStage::FAILED_RETRYING, 2, 5); + assert(d7 == AutoDJDecision::SKIP_UNPLAYABLE); // 8. Unplayed queue count <= 2: should REFILL_QUEUE - auto d8 = processor.evaluate(120000, trackDuration, true, transitionMs, TrackLifecycleStage::READY, 0, 2); + auto d8 = processor.evaluate(120000, trackDuration, true, transitionMs, TrackLifecycleStage::ARMED_READY, 0, 2); assert(d8 == AutoDJDecision::REFILL_QUEUE); std::cout << "[PASS] AutoDJProcessor decision test passed." << std::endl; diff --git a/app/src/main/cpp/tests/test_transition_planner.cpp b/app/src/main/cpp/tests/test_transition_planner.cpp index 31a5bd5fa8..050b93b58b 100644 --- a/app/src/main/cpp/tests/test_transition_planner.cpp +++ b/app/src/main/cpp/tests/test_transition_planner.cpp @@ -167,7 +167,7 @@ int main() { rmsB_p25 = std::sqrt(rmsB_p25 / (N - 1000)); std::cout << "t=0.25 Sub-bass RMS: Deck A = " << rmsA_p25 << ", Deck B = " << rmsB_p25 << "\n"; - assert(rmsA_p25 > 0.55f); // Deck A keeps full bass + assert(rmsA_p25 > 0.50f); // Deck A keeps bass (> 0.50f with correlated-bass headroom compensation) assert(rmsB_p25 < 0.08f); // Deck B 200 Hz HPF removes sub-bass // Midpoint Bass Swap (t = 0.70): @@ -187,7 +187,7 @@ int main() { std::cout << "t=0.70 Sub-bass RMS: Deck A = " << rmsA_p70 << ", Deck B = " << rmsB_p70 << "\n"; assert(rmsA_p70 < 0.10f); // Deck A bass swept away - assert(rmsB_p70 > 0.50f); // Deck B delivers full bass punch! + assert(rmsB_p70 > 0.45f); // Deck B delivers full bass punch! // 2. Vocal-Pocket Ducking: dynamic -9.0 dB peaking cut at 1200 Hz when Deck B has vocals std::vector sine1200Hz(N * 2); diff --git a/app/src/main/java/com/lastwave/app/playback/AutoMixSchedule.kt b/app/src/main/java/com/lastwave/app/playback/AutoMixSchedule.kt index 7c00c78487..6cdb62800a 100644 --- a/app/src/main/java/com/lastwave/app/playback/AutoMixSchedule.kt +++ b/app/src/main/java/com/lastwave/app/playback/AutoMixSchedule.kt @@ -109,12 +109,9 @@ fun planAutoMix(input: AutoMixPlanInput): AutoMixDecision = with(input) { else -> null } if (resolveReason != null) { - if (remainingMs > refreshWindowMs) { - return AutoMixDecision.Idle(AutoMixIdleReason.TooEarly) - } return AutoMixDecision.ResolveNext(resolveReason) } - if (!standbyArmedForNext && remainingMs <= refreshWindowMs) { + if (!standbyArmedForNext) { return AutoMixDecision.Arm } return AutoMixDecision.Idle(AutoMixIdleReason.OutsideClamped30sWindow) @@ -130,7 +127,7 @@ fun planAutoMix(input: AutoMixPlanInput): AutoMixDecision = with(input) { } if (!standbyArmedForNext) return AutoMixDecision.Arm - if (!standbyReady) return AutoMixDecision.WaitingForReady + if (!standbyReady || standbyBufferedMs < 8000L) return AutoMixDecision.WaitingForReady if (remainingMs > transitionMs) return AutoMixDecision.WaitingForWindow val sustainableMs = standbyBufferedMs.takeIf { it > 0L } ?: transitionMs diff --git a/app/src/main/java/com/lastwave/app/playback/MusicPlayer.kt b/app/src/main/java/com/lastwave/app/playback/MusicPlayer.kt index 3d61b39bc4..b2381be6ac 100644 --- a/app/src/main/java/com/lastwave/app/playback/MusicPlayer.kt +++ b/app/src/main/java/com/lastwave/app/playback/MusicPlayer.kt @@ -854,6 +854,7 @@ class MusicPlayer @Inject constructor( unavailableMediaIds.remove(it) unavailableAttemptCounts.remove(it) } + autoDJController.get().onPlaybackStarted() } } override fun onPlayWhenReadyChanged(playWhenReady: Boolean, playWhenReadyChangeReason: Int) { @@ -1733,11 +1734,18 @@ class MusicPlayer @Inject constructor( val standby = currentStandby() val nextIndex = player.nextMediaItemIndex if (standby != null && nextIndex != C.INDEX_UNSET && crossfadeStandbyArmed && standby.playbackState == Player.STATE_READY) { + val bufferedMs = getStandbyBufferedMsInternal(standby) + if (bufferedMs < 8_000L) { + android.util.Log.w("MusicPlayer", "AutoDJ triggerAutoTransition abort: Deck B buffered ${bufferedMs}ms < 8000ms, falling back to natural track end") + return@launch + } handOffCrossfade(standby, overlapMs) } } } + override fun getStandbyBufferedMs(): Long = getStandbyBufferedMsInternal() + override fun setLateTrackBuffering(isWaiting: Boolean) { applicationScope.launch(Dispatchers.Main.immediate) { _state.update { it.copy(isBuffering = isWaiting) } @@ -3223,6 +3231,18 @@ class MusicPlayer @Inject constructor( * Re-armed automatically when the item at [nextIndex] is replaced (a TTL * refresh swaps it), and torn down by [cancelCrossfade] on every queue edit. */ + @MainThread + private fun getStandbyBufferedMsInternal(standby: ExoPlayer? = currentStandby()): Long { + val availableSec = if (nativeAutoMixEngine.get().isAvailable) { + nativeAutoMixEngine.get().deckBAvailableSeconds + } else 0f + return if (availableSec > 0f) { + (availableSec * 1000f).toLong() + } else if (standby != null && crossfadeStandbyArmed) { + runCatching { standby.bufferedPosition }.getOrDefault(0L) + } else 0L + } + @MainThread private fun armCrossfadeStandby(nextIndex: Int, nextItem: MediaItem): ExoPlayer { val standby = standbyOrCreate() @@ -3231,14 +3251,15 @@ class MusicPlayer @Inject constructor( standby.volume = 0f standby.setAudioAttributes(player.audioAttributes, false) standby.pause() - standby.setMediaItems(standbyQueue, nextIndex, 0L) + val firstBeatMs = if (nativeAutoMixEngine.get().isAvailable) { + nativeAutoMixEngine.get().deckBFirstBeatMs + } else null + val startPosMs = if (firstBeatMs != null && firstBeatMs in 100L..35_000L) firstBeatMs else 0L + standby.setMediaItems(standbyQueue, nextIndex, startPosMs) standby.prepare() crossfadeStandbyArmed = true crossfadeStandbyPreparing = true val nextTrack = nextItem.toPlayableTrack() - val firstBeatMs = if (nativeAutoMixEngine.get().isAvailable) { - nativeAutoMixEngine.get().deckBFirstBeatMs - } else null if (automixEnabled) { _state.update { it.copy( @@ -3396,9 +3417,7 @@ class MusicPlayer @Inject constructor( standbyArmedForNext = armedForNext, standbyReady = armedForNext && standby?.playbackState == Player.STATE_READY, standbyDurationMs = if (armedForNext) standby?.duration ?: 0L else 0L, - standbyBufferedMs = if (armedForNext) { - runCatching { standby?.bufferedPosition ?: 0L }.getOrDefault(0L) - } else 0L, + standbyBufferedMs = if (armedForNext) getStandbyBufferedMsInternal(standby) else 0L, ) )) { is AutoMixDecision.Idle -> CrossfadeDecision.Idle( @@ -3521,6 +3540,14 @@ class MusicPlayer @Inject constructor( cancelCrossfade() return false } + val bufferedMs = getStandbyBufferedMsInternal(standby) + if (automixEnabled && bufferedMs < 8_000L) { + logCrossfadeDecision( + "handoff-fallback-unbuffered", + "Deck B buffered ${bufferedMs}ms < 8000ms at outCueMs, falling back to natural track end", + ) + return false + } handOffCrossfade(standby, plan.overlapMs) return true } @@ -3553,8 +3580,11 @@ class MusicPlayer @Inject constructor( nativeAutoMixEngine.get().deckBFirstBeatMs } else 0L if (firstBeatMs in 100L..35_000L) { - android.util.Log.i("MusicPlayer", "AutoMix: Seeking Deck B directly to first beat onset ($firstBeatMs ms)") - standby.seekTo(firstBeatMs) + val currentPos = standby.currentPosition + if (currentPos < firstBeatMs - 150L || currentPos > firstBeatMs + 150L) { + android.util.Log.i("MusicPlayer", "AutoMix: Seeking Deck B directly to first beat onset ($firstBeatMs ms, was $currentPos ms)") + standby.seekTo(firstBeatMs) + } } if (nativeAutoMixEngine.get().isAvailable) { nativeAutoMixEngine.get().startTransition(overlapMs.toInt()) @@ -7393,6 +7423,29 @@ class MusicPlayer @Inject constructor( return false } + @MainThread + private fun isStreamSwapLocked(): Boolean { + if (!playerDelegate.isInitialized()) return true + val dur = player.duration + val pos = player.currentPosition + if (dur > 0L && pos > dur - 35_000L) return true + if (outgoingPlayer != null || crossfadeStandbyPreparing || crossfadeStandbyArmed) return true + if (_state.value.isAutoMixActive) return true + val autoMix = autoDJController.get() + val nextIndex = player.nextMediaItemIndex + if (nextIndex in 0 until player.mediaItemCount) { + val nextTrack = player.getMediaItemAt(nextIndex).toPlayableTrack() + val stage = autoMix.getTrackStage(nextTrack.mediaIdKey()) + if (stage == TrackLifecycleStage.DECODING_HEAD_WINDOW || + stage == TrackLifecycleStage.ARMED_READY || + stage == TrackLifecycleStage.TRANSITIONING + ) { + return true + } + } + return false + } + private fun scheduleQualityUpgrade( track: PlayableTrack, expectedMediaId: String, @@ -7403,6 +7456,10 @@ class MusicPlayer @Inject constructor( android.util.Log.i("MusicPlayer", "[STREAM UPGRADE] already running for '${track.title}'") return } + if (isStreamSwapLocked()) { + android.util.Log.i("MusicPlayer", "[STREAM UPGRADE] Stream swap locked within 35s of track end or during pre-decoding/transition, omitting upgrade") + return + } val misc = runCatching { runBlocking { settingsPreferences.settings.first() } }.getOrDefault(MiscSettings()) val wantLossless = misc.preferLosslessStreaming && misc.losslessQuality != LosslessMusicApi.QUALITY_YOUTUBE @@ -7515,14 +7572,9 @@ class MusicPlayer @Inject constructor( val currentItem = player.getMediaItemAt(currentIndex) if (currentItem.mediaId != expectedMediaId && currentItem.mediaId != track.mediaIdKey()) return@withContext -1L - val dur = player.duration val pos = player.currentPosition - if (dur > 0L && pos > dur - 12_000L) { - android.util.Log.d("MusicPlayer", "[STREAM UPGRADE] Near end of track (${pos}/${dur}ms), omitting swap") - return@withContext -1L - } - if (outgoingPlayer != null || crossfadeStandbyPreparing || crossfadeStandbyArmed) { - android.util.Log.d("MusicPlayer", "[STREAM UPGRADE] Crossfade in flight, omitting swap") + if (isStreamSwapLocked()) { + android.util.Log.d("MusicPlayer", "[STREAM UPGRADE] Stream swap locked within 35s of track end or during pre-decoding/transition, omitting swap") return@withContext -1L } pos @@ -7596,14 +7648,9 @@ class MusicPlayer @Inject constructor( val currentItem = player.getMediaItemAt(currentIndex) if (currentItem.mediaId != expectedMediaId && currentItem.mediaId != track.mediaIdKey()) return@withContext - val dur = player.duration val swapPos = player.currentPosition - if (dur > 0L && swapPos > dur - 10_000L) { - android.util.Log.d("MusicPlayer", "[STREAM UPGRADE] Near end of track (${swapPos}/${dur}ms), omitting swap") - return@withContext - } - if (outgoingPlayer != null || crossfadeStandbyPreparing || crossfadeStandbyArmed) { - android.util.Log.d("MusicPlayer", "[STREAM UPGRADE] Crossfade in flight, omitting swap") + if (isStreamSwapLocked()) { + android.util.Log.d("MusicPlayer", "[STREAM UPGRADE] Stream swap locked within 35s of track end or during pre-decoding/transition, omitting swap") return@withContext } diff --git a/app/src/main/java/com/lastwave/app/playback/NativeAutoMixEngine.kt b/app/src/main/java/com/lastwave/app/playback/NativeAutoMixEngine.kt index 3cf4bd7ff0..2893830329 100644 --- a/app/src/main/java/com/lastwave/app/playback/NativeAutoMixEngine.kt +++ b/app/src/main/java/com/lastwave/app/playback/NativeAutoMixEngine.kt @@ -131,6 +131,12 @@ class NativeAutoMixEngine @Inject constructor() : Closeable { val deckBLeadingTrimFrames: Long get() = withHandle(0L, ::nativeGetDeckBLeadingTrimFrames) + val deckBAvailableSeconds: Float + get() = withHandle(0.0F, ::nativeGetDeckBAvailableSeconds) + + val deckBAvailableFrames: Long + get() = withHandle(0L, ::nativeGetDeckBAvailableFrames) + val deckBFirstBeatMs: Long get() = withHandle(0L, ::nativeGetDeckBFirstBeatMs) @@ -259,6 +265,8 @@ class NativeAutoMixEngine @Inject constructor() : Closeable { @JvmStatic private external fun nativeGetState(handle: Long): Int @JvmStatic private external fun nativeGetTransitionProgress(handle: Long): Float @JvmStatic private external fun nativeGetDeckBLeadingTrimFrames(handle: Long): Long + @JvmStatic private external fun nativeGetDeckBAvailableSeconds(handle: Long): Float + @JvmStatic private external fun nativeGetDeckBAvailableFrames(handle: Long): Long @JvmStatic private external fun nativeGetDeckBFirstBeatMs(handle: Long): Long @JvmStatic private external fun nativeGetDeckBDropBeatMs(handle: Long): Long @JvmStatic private external fun nativeGetDeckABpm(handle: Long): Float diff --git a/app/src/main/java/com/lastwave/app/playback/autodj/AutoDJController.kt b/app/src/main/java/com/lastwave/app/playback/autodj/AutoDJController.kt index c782c4c92e..1f68417a15 100644 --- a/app/src/main/java/com/lastwave/app/playback/autodj/AutoDJController.kt +++ b/app/src/main/java/com/lastwave/app/playback/autodj/AutoDJController.kt @@ -50,6 +50,7 @@ interface AutoDJPlaybackHost { fun decodeHeadWindowForNext(index: Int, track: PlayableTrack) fun getRecentPlayedTrackIds(): List fun scoreAndReorderCandidates(fromIndex: Int, count: Int, currentBpm: Float) + fun getStandbyBufferedMs(): Long } sealed interface AutoDJStatus { @@ -216,6 +217,29 @@ class AutoDJController @Inject constructor( } } + fun onPlaybackStarted() { + val h = host ?: return + val nextIndex = h.getCurrentIndex() + 1 + if (nextIndex in 0 until h.getQueueSize()) { + val nextTrack = h.getTrackAt(nextIndex) + if (nextTrack != null && getTrackStage(nextTrack.mediaIdKey()) == TrackLifecycleStage.QUEUED) { + prefetchTrack(nextIndex, nextTrack) + } + } + } + + fun onDeckBArmedReady(index: Int) { + val h = host ?: return + if (index in 0 until h.getQueueSize()) { + val track = h.getTrackAt(index) + if (track != null) { + val key = track.mediaIdKey() + trackStages[key] = TrackLifecycleStage.ARMED_READY + _status.value = AutoDJStatus.ArmedReady(track.title, index) + } + } + } + fun cancelAllPrefetches(reason: String) { inFlightJobs.values.forEach { it.cancel() } inFlightJobs.clear() @@ -246,12 +270,30 @@ class AutoDJController @Inject constructor( val nextStage = if (nextKey.isNotEmpty()) getTrackStage(nextKey) else TrackLifecycleStage.COMPLETED val retryCount = if (nextKey.isNotEmpty()) retryCounts[nextKey] ?: 0 else 0 + // Eager prefetch trigger within 2s of playback start if track is still queued + if (isPlaying && currentPositionMs <= 2000L && nextTrack != null && nextStage == TrackLifecycleStage.QUEUED) { + prefetchTrack(nextIndex, nextTrack) + } + + // Check if head window decoding has reached ready threshold (>= 12.0s raw float PCM) + if (nextStage == TrackLifecycleStage.DECODING_HEAD_WINDOW && nextTrack != null) { + val bufferedMs = h.getStandbyBufferedMs() + if (bufferedMs >= 12_000L || (nextTrack.durationMs in 1..11999L && bufferedMs >= nextTrack.durationMs)) { + trackStages[nextKey] = TrackLifecycleStage.ARMED_READY + h.armStandbyForNext(nextIndex, nextTrack) + _status.value = AutoDJStatus.ArmedReady(nextTrack.title, nextIndex) + Log.i(TAG, "[HEAD_WINDOW_READY] index=$nextIndex title='${nextTrack.title}' buffered=${bufferedMs}ms >= 12000ms -> ARMED_READY") + } + } + + val effectiveStage = if (nextKey.isNotEmpty()) getTrackStage(nextKey) else TrackLifecycleStage.COMPLETED + val decision = nativeEngine.evaluate( currentPositionMs = currentPositionMs, durationMs = durationMs, isPlaying = isPlaying, transitionDurationMs = transitionDurationMs, - nextStage = nextStage, + nextStage = effectiveStage, retryCount = retryCount, unplayedCount = unplayedCount, ) @@ -295,9 +337,15 @@ class AutoDJController @Inject constructor( AutoDJDecision.ARM_STANDBY -> { if (nextTrack != null && nextIndex in 0 until queueSize) { - trackStages[nextKey] = TrackLifecycleStage.ARMED_READY - h.armStandbyForNext(nextIndex, nextTrack) - _status.value = AutoDJStatus.ArmedReady(nextTrack.title, nextIndex) + val bufferedMs = h.getStandbyBufferedMs() + if (bufferedMs >= 12_000L || (nextTrack.durationMs in 1..11999L && bufferedMs >= nextTrack.durationMs)) { + trackStages[nextKey] = TrackLifecycleStage.ARMED_READY + h.armStandbyForNext(nextIndex, nextTrack) + _status.value = AutoDJStatus.ArmedReady(nextTrack.title, nextIndex) + } else { + trackStages[nextKey] = TrackLifecycleStage.DECODING_HEAD_WINDOW + _status.value = AutoDJStatus.Decoding(nextTrack.title, nextIndex) + } } } @@ -378,10 +426,10 @@ class AutoDJController @Inject constructor( host?.registerResolvedStream(index, track, resolvedStream) - // Immediately decode first 35 seconds into Deck B + // Immediately decode first 35 seconds into Deck B ring buffer host?.decodeHeadWindowForNext(index, track) - trackStages[key] = TrackLifecycleStage.ARMED_READY - _status.value = AutoDJStatus.ArmedReady(track.title, index) + trackStages[key] = TrackLifecycleStage.DECODING_HEAD_WINDOW + _status.value = AutoDJStatus.Decoding(track.title, index) // Optionally warm-prefetch stream URL for index + 2 prefetchWarmUpcoming(index + 1) diff --git a/app/src/test/java/com/lastwave/app/playback/AutoMixScheduleTest.kt b/app/src/test/java/com/lastwave/app/playback/AutoMixScheduleTest.kt new file mode 100644 index 0000000000..cf5176a832 --- /dev/null +++ b/app/src/test/java/com/lastwave/app/playback/AutoMixScheduleTest.kt @@ -0,0 +1,99 @@ +package com.lastwave.app.playback + +import com.google.common.truth.Truth.assertThat +import org.junit.Test + +class AutoMixScheduleTest { + + private fun base( + durationMs: Long = 200_000L, + positionMs: Long = 185_000L, // 15s remaining <= 30s clamp + barMode: Int = 8, + bpm: Float = 120f, + automixEnabled: Boolean = true, + bitPerfectEnabled: Boolean = false, + sequentialAlbumBypass: Boolean = false, + isPlaying: Boolean = true, + repeatOne: Boolean = false, + hasNextItem: Boolean = true, + nextItemResolved: Boolean = true, + streamExpired: Boolean = false, + standbyArmedForNext: Boolean = true, + standbyReady: Boolean = true, + standbyDurationMs: Long = 200_000L, + standbyBufferedMs: Long = 15_000L, + ) = AutoMixPlanInput( + automixEnabled = automixEnabled, + bitPerfectEnabled = bitPerfectEnabled, + sequentialAlbumBypass = sequentialAlbumBypass, + isPlaying = isPlaying, + repeatOne = repeatOne, + durationMs = durationMs, + positionMs = positionMs, + barMode = barMode, + bpm = bpm, + hasNextItem = hasNextItem, + nextItemResolved = nextItemResolved, + streamExpired = streamExpired, + standbyArmedForNext = standbyArmedForNext, + standbyReady = standbyReady, + standbyDurationMs = standbyDurationMs, + standbyBufferedMs = standbyBufferedMs, + ) + + @Test + fun resolvesImmediatelyAtTrackStart() { + val plan = planAutoMix( + base(positionMs = 1000L, nextItemResolved = false), + ) + assertThat(plan).isEqualTo(AutoMixDecision.ResolveNext(AutoMixResolveReason.Placeholder)) + } + + @Test + fun armsStandbyWhenResolvedEvenOutsideClamped30sWindow() { + val plan = planAutoMix( + base(positionMs = 1000L, standbyArmedForNext = false), + ) + assertThat(plan).isEqualTo(AutoMixDecision.Arm) + } + + @Test + fun waitsForReadyIfBufferedBelow8000Ms() { + val plan = planAutoMix( + base( + positionMs = 185_000L, + standbyArmedForNext = true, + standbyReady = true, + standbyBufferedMs = 7_999L, + ), + ) + assertThat(plan).isEqualTo(AutoMixDecision.WaitingForReady) + } + + @Test + fun handsOffWhenArmedReadyAndBufferedAtLeast8000Ms() { + val plan = planAutoMix( + base( + positionMs = 185_000L, + standbyArmedForNext = true, + standbyReady = true, + standbyBufferedMs = 8_000L, + ), + ) + assertThat(plan).isInstanceOf(AutoMixDecision.HandOff::class.java) + } + + @Test + fun idlesOutside30sClampWhenArmedAndReady() { + val plan = planAutoMix( + base( + durationMs = 200_000L, + positionMs = 150_000L, // 50s remaining > 30s + standbyArmedForNext = true, + standbyReady = true, + standbyBufferedMs = 20_000L, + ), + ) + assertThat(plan).isEqualTo(AutoMixDecision.Idle(AutoMixIdleReason.OutsideClamped30sWindow)) + } +} From 178d1d3eff8b3e51c5d520ae22fc5cfa612ca077 Mon Sep 17 00:00:00 2001 From: ajisth69 Date: Mon, 26 Oct 2026 17:41:10 +0530 Subject: [PATCH 2/2] fix(compile): resolve CancellationException in InnerTubeMusicApi and nullability in AutoDJController --- .../java/com/lastwave/app/data/music/InnerTubeMusicApi.kt | 2 +- .../com/lastwave/app/playback/autodj/AutoDJController.kt | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/lastwave/app/data/music/InnerTubeMusicApi.kt b/app/src/main/java/com/lastwave/app/data/music/InnerTubeMusicApi.kt index a0b130ebdf..8e11bc39c9 100644 --- a/app/src/main/java/com/lastwave/app/data/music/InnerTubeMusicApi.kt +++ b/app/src/main/java/com/lastwave/app/data/music/InnerTubeMusicApi.kt @@ -443,7 +443,7 @@ class InnerTubeMusicApi @Inject constructor( try { nextPage = browseContinuation(currentToken, authenticated = authenticatedAs) break - } catch (cancellation: CancellationException) { + } catch (cancellation: kotlinx.coroutines.CancellationException) { throw cancellation } catch (e: Exception) { if (attempt == 3) { diff --git a/app/src/main/java/com/lastwave/app/playback/autodj/AutoDJController.kt b/app/src/main/java/com/lastwave/app/playback/autodj/AutoDJController.kt index 1f68417a15..5f19f6869e 100644 --- a/app/src/main/java/com/lastwave/app/playback/autodj/AutoDJController.kt +++ b/app/src/main/java/com/lastwave/app/playback/autodj/AutoDJController.kt @@ -278,7 +278,8 @@ class AutoDJController @Inject constructor( // Check if head window decoding has reached ready threshold (>= 12.0s raw float PCM) if (nextStage == TrackLifecycleStage.DECODING_HEAD_WINDOW && nextTrack != null) { val bufferedMs = h.getStandbyBufferedMs() - if (bufferedMs >= 12_000L || (nextTrack.durationMs in 1..11999L && bufferedMs >= nextTrack.durationMs)) { + val trackDur = nextTrack.durationMs + if (bufferedMs >= 12_000L || (trackDur != null && trackDur in 1L..11999L && bufferedMs >= trackDur)) { trackStages[nextKey] = TrackLifecycleStage.ARMED_READY h.armStandbyForNext(nextIndex, nextTrack) _status.value = AutoDJStatus.ArmedReady(nextTrack.title, nextIndex) @@ -338,7 +339,8 @@ class AutoDJController @Inject constructor( AutoDJDecision.ARM_STANDBY -> { if (nextTrack != null && nextIndex in 0 until queueSize) { val bufferedMs = h.getStandbyBufferedMs() - if (bufferedMs >= 12_000L || (nextTrack.durationMs in 1..11999L && bufferedMs >= nextTrack.durationMs)) { + val trackDur = nextTrack.durationMs + if (bufferedMs >= 12_000L || (trackDur != null && trackDur in 1L..11999L && bufferedMs >= trackDur)) { trackStages[nextKey] = TrackLifecycleStage.ARMED_READY h.armStandbyForNext(nextIndex, nextTrack) _status.value = AutoDJStatus.ArmedReady(nextTrack.title, nextIndex)