| 31 | } |
| 32 | |
| 33 | Sample SignalConditioner::processSample(const Sample& sample) { |
| 34 | if (!sample.isValid() || sample.size() == 0) { |
| 35 | return Sample(); // Return empty sample |
| 36 | } |
| 37 | |
| 38 | const auto pcm = sample.pcm(); |
| 39 | const size sampleCount = pcm.size(); |
| 40 | |
| 41 | // Reserve buffers to avoid repeated allocations |
| 42 | mValidMask.clear(); |
| 43 | mValidMask.reserve(sampleCount); |
| 44 | mTempBuffer.clear(); |
| 45 | mTempBuffer.reserve(sampleCount); |
| 46 | mOutputBuffer.clear(); |
| 47 | mOutputBuffer.reserve(sampleCount); |
| 48 | |
| 49 | // Stage 1: Spike filtering (if enabled) |
| 50 | if (mConfig.enableSpikeFilter) { |
| 51 | filterSpikes(pcm, mValidMask); |
| 52 | } else { |
| 53 | // All samples valid if spike filtering disabled |
| 54 | mValidMask.assign(sampleCount, true); |
| 55 | } |
| 56 | |
| 57 | // Stage 2: DC offset removal (if enabled) |
| 58 | // Uses per-buffer instantaneous DC calculation for accuracy, |
| 59 | // plus per-sample DCBlocker for cross-buffer continuity. |
| 60 | i32 dcOffset = 0; |
| 61 | if (mConfig.enableDCRemoval) { |
| 62 | dcOffset = calculateDCOffset(pcm, mValidMask); |
| 63 | removeDCOffset(pcm, dcOffset, mTempBuffer); |
| 64 | } else { |
| 65 | // Copy samples to temp buffer without DC removal |
| 66 | mTempBuffer.assign(pcm.begin(), pcm.end()); |
| 67 | } |
| 68 | |
| 69 | // Stage 3: Noise gate (if enabled) |
| 70 | if (mConfig.enableNoiseGate) { |
| 71 | applyNoiseGate(mTempBuffer, mOutputBuffer); |
| 72 | } else { |
| 73 | // Copy temp buffer to output without gating |
| 74 | mOutputBuffer = mTempBuffer; |
| 75 | } |
| 76 | |
| 77 | // Update stats |
| 78 | mStats.dcOffset = dcOffset; |
| 79 | mStats.noiseGateOpen = mNoiseGateOpen; |
| 80 | mStats.samplesProcessed += sampleCount; |
| 81 | |
| 82 | // Create new Sample from cleaned PCM |
| 83 | SampleImplPtr impl = fl::make_shared<SampleImpl>(); |
| 84 | impl->assign(mOutputBuffer.begin(), mOutputBuffer.end(), sample.timestamp()); |
| 85 | return Sample(impl); |
| 86 | } |
| 87 | |
| 88 | size SignalConditioner::filterSpikes(span<const i16> pcm, vector<bool>& validMask) { |
| 89 | const size count = pcm.size(); |