| 72 | } |
| 73 | |
| 74 | void Reconstructor::reconstruct(int numIRs, |
| 75 | const EnergyField* const* energyFields, |
| 76 | const float* const* distanceAttenuationCorrectionCurves, |
| 77 | const AirAbsorptionModel* airAbsorptionModels, |
| 78 | ImpulseResponse* const* impulseResponses, |
| 79 | ReconstructionType type, |
| 80 | float duration, |
| 81 | int order) |
| 82 | { |
| 83 | PROFILE_FUNCTION(); |
| 84 | |
| 85 | for (auto i = 0; i < numIRs; ++i) |
| 86 | { |
| 87 | const auto& energyField = *energyFields[i]; |
| 88 | auto& impulseResponse = *impulseResponses[i]; |
| 89 | |
| 90 | auto numChannels = SphericalHarmonics::numCoeffsForOrder(order); |
| 91 | auto numSamples = static_cast<int>(ceilf(duration * mSamplingRate)); |
| 92 | |
| 93 | numChannels = std::min({ energyField.numChannels(), impulseResponse.numChannels(), numChannels }); |
| 94 | auto numSamplesPerBin = static_cast<int>(ceilf(EnergyField::kBinDuration * mSamplingRate)); |
| 95 | numSamples = std::min({ impulseResponse.numSamples(), numSamples }); |
| 96 | auto numBins = std::min({ energyField.numBins(), static_cast<int>(ceilf(static_cast<float>(numSamples) / static_cast<float>(numSamplesPerBin))) }); |
| 97 | |
| 98 | impulseResponses[i]->reset(); |
| 99 | |
| 100 | for (auto iChannel = 0; iChannel < numChannels; ++iChannel) |
| 101 | { |
| 102 | for (auto iBand = 0; iBand < Bands::kNumBands; ++iBand) |
| 103 | { |
| 104 | for (auto iBin = 0; iBin < numBins; ++iBin) |
| 105 | { |
| 106 | auto numBinSamples = std::min(numSamplesPerBin, numSamples - iBin * numSamplesPerBin); |
| 107 | auto normalization = 1.0f; |
| 108 | |
| 109 | if (type == ReconstructionType::Linear) |
| 110 | { |
| 111 | auto energy = 0.0f; |
| 112 | if (fabsf(energyField[iChannel][iBand][iBin]) >= kEnergyThreshold && fabsf(energyField[0][iBand][iBin]) >= kEnergyThreshold) |
| 113 | { |
| 114 | energy = energyField[iChannel][iBand][iBin] / sqrtf(energyField[0][iBand][iBin] * sqrtf(4.0f * Math::kPi)); |
| 115 | } |
| 116 | |
| 117 | auto prevEnergy = 0.0f; |
| 118 | if (iBin == 0) |
| 119 | { |
| 120 | prevEnergy = energy; |
| 121 | } |
| 122 | else if (fabsf(energyField[iChannel][iBand][iBin - 1]) >= kEnergyThreshold && fabsf(energyField[0][iBand][iBin - 1]) >= kEnergyThreshold) |
| 123 | { |
| 124 | prevEnergy = energyField[iChannel][iBand][iBin - 1] / sqrtf(energyField[0][iBand][iBin - 1] * sqrtf(4.0f * Math::kPi)); |
| 125 | } |
| 126 | |
| 127 | for (auto iBinSample = 0, iSample = iBin * numSamplesPerBin; iBinSample < numBinSamples; ++iBinSample, ++iSample) |
| 128 | { |
| 129 | auto weight = static_cast<float>(iBinSample) / static_cast<float>(numSamplesPerBin); |
| 130 | auto sampleEnergy = (1.0f - weight) * prevEnergy + weight * energy; |
| 131 | |