Loading a AudioBuffer<double> as IR to Convolution class

Hi,
maybe this is a stupid question, but I’m trying to get my head around C++ and the JUCE Framework for the first time and there so much going on with types, pointers and other stuff that I maybe missing some basics here. I hope you can help me.

I’m loading a sofa file with this particular library (GitHub - superkittens/libBasicSOFA: Basic library for spatial audio SOFA files) which is working fine and I’m getting back a pointer to a memory location where the HRIR sits.
Now I’m trying to get this HRIR into a AudioBuffer which I can feed into the Convolution.loadImpulseResponse() method. The problem is, that the pointer points to double. When I create a AudioBuffer I can’t get this buffer into the loadImpulseResponse() method. Is it possible to simply cast the double to float or is there a better way to do this?
Thank you for the help!

 bool success = sofa.readSOFAFile("PATH_TO_FILE");
    if (!success)
        return;

    auto samplingFrequency = sofa.getFs();
    auto numSourcePositions = sofa.getM();

    auto theta = 150.0;
    auto phi = 0.0;
    auto radius = 0.0;
    auto channel = 0;
    auto size = sofa.getN();

    auto hrirBuffer = AudioBuffer<double>(sofa.getHRIR(channel, theta, phi, radius), 1, sofa.getN());

    conv.loadImpulseResponse(hrirBuffer,
            getSampleRate(),
            dsp::Convolution::Stereo::no,
            dsp::Convolution::Trim::yes,
            dsp::Convolution::Normalise::no
    );
1 Like

The dsp::Convolution class only allows you loading float buffers. So what you need to do is convert the double data into float, this can be done by simply iterating over the channel array and cast each sample to float. Furthermore, I assume that you want a stereo IR to generate some sensible binaural synthesis. Something like this might work (untested)

constexpr size_t numChannels = 2;
std::array<double*, numChannels> srcChannels { sofa.getHRIR (0, theta, phi, radius), sofa.getHRIR (1, theta, phi, radius) };

jassert (srcChannels[0] != nullptr);
jassert (srcChannels[1] != nullptr);

// Why does this library return the sample count as double...?
const auto numSamples = static_cast<int> (sofa.getN());
const auto sampleRate = sofa.getFs();

// Create an empty pre-allocated audio buffer
juce::AudioBuffer<float> hrirBuffer (numChannels, numSamples);

// Copy over the samples
for (auto ch = 0; ch < numChannels; ++ch)
{
    auto* srcChannel = srcChannels[ch];
    auto* dstChannel = hrirBuffer.getWritePointer (ch);
    
    for (auto smp = 0; smp < numSamples; ++smp)
        dstChannel[smp] = static_cast<float> (srcChannel[smp]);
}

// Finally we can move the filled buffer into the convolution 
conv.loadImpulseResponse (std::move (hrirBuffer),
                          sampleRate,
                          juce::dsp::Convolution::Stereo::yes,
                          juce::dsp::Convolution::Trim::no,
                          juce::dsp::Convolution::Normalise::no);