1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/*
==============================================================================
This file is part of the IEM plug-in suite.
Author: Daniel Rudrich
Copyright (c) 2017 - Institute of Electronic Music and Acoustics (IEM)
https://iem.at
The IEM plug-in suite is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The IEM plug-in suite is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software. If not, see <https://www.gnu.org/licenses/>.
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
#include <EQData.h>
#include <IRData.h>
const juce::StringArray BinauralDecoderAudioProcessor::headphoneEQs =
juce::StringArray ("AKG-K141MK2",
"AKG-K240DF",
"AKG-K240MK2",
"AKG-K271MK2",
"AKG-K271STUDIO",
"AKG-K601",
"AKG-K701",
"AKG-K702",
"AKG-K1000-Closed",
"AKG-K1000-Open",
"AudioTechnica-ATH-M50",
"Beyerdynamic-DT250",
"Beyerdynamic-DT770PRO-250Ohms",
"Beyerdynamic-DT880",
"Beyerdynamic-DT990PRO",
"Presonus-HD7",
"Sennheiser-HD430",
"Sennheiser-HD480",
"Sennheiser-HD560ovationII",
"Sennheiser-HD565ovation",
"Sennheiser-HD600",
"Sennheiser-HD650",
"SHURE-SRH940");
//==============================================================================
BinauralDecoderAudioProcessor::BinauralDecoderAudioProcessor() :
AudioProcessorBase (
#ifndef JucePlugin_PreferredChannelConfigurations
BusesProperties()
#if ! JucePlugin_IsMidiEffect
#if ! JucePlugin_IsSynth
.withInput ("Input",
((juce::PluginHostType::getPluginLoadedAs()
== juce::AudioProcessor::wrapperType_VST3)
? juce::AudioChannelSet::ambisonic (1)
: juce::AudioChannelSet::ambisonic (7)),
true)
#endif
.withOutput ("Output", juce::AudioChannelSet::stereo(), true)
#endif
,
#endif
createParameterLayout())
{
// get pointers to the parameters
inputOrderSetting = parameters.getRawParameterValue ("inputOrderSetting");
useSN3D = parameters.getRawParameterValue ("useSN3D");
applyHeadphoneEq = parameters.getRawParameterValue ("applyHeadphoneEq");
// add listeners to parameter changes
parameters.addParameterListener ("inputOrderSetting", this);
parameters.addParameterListener ("applyHeadphoneEq", this);
// load IRs
juce::AudioFormatManager formatManager;
formatManager.registerBasicFormats();
juce::WavAudioFormat wavFormat;
juce::MemoryInputStream* mis[7];
mis[0] = new juce::MemoryInputStream (IRData::irsOrd1_wav, IRData::irsOrd1_wavSize, false);
mis[1] = new juce::MemoryInputStream (IRData::irsOrd2_wav, IRData::irsOrd2_wavSize, false);
mis[2] = new juce::MemoryInputStream (IRData::irsOrd3_wav, IRData::irsOrd3_wavSize, false);
mis[3] = new juce::MemoryInputStream (IRData::irsOrd4_wav, IRData::irsOrd4_wavSize, false);
mis[4] = new juce::MemoryInputStream (IRData::irsOrd5_wav, IRData::irsOrd5_wavSize, false);
mis[5] = new juce::MemoryInputStream (IRData::irsOrd6_wav, IRData::irsOrd6_wavSize, false);
mis[6] = new juce::MemoryInputStream (IRData::irsOrd7_wav, IRData::irsOrd7_wavSize, false);
for (int i = 0; i < 7; ++i)
{
irs[i].setSize (juce::square (i + 2), irLength);
std::unique_ptr<juce::AudioFormatReader> reader (wavFormat.createReaderFor (mis[i], true));
reader->read (&irs[i], 0, irLength, 0, true, false);
irs[i].applyGain (0.3f);
}
}
BinauralDecoderAudioProcessor::~BinauralDecoderAudioProcessor()
{
}
int BinauralDecoderAudioProcessor::getNumPrograms()
{
return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs,
// so this should be at least 1, even if you're not really implementing programs.
}
int BinauralDecoderAudioProcessor::getCurrentProgram()
{
return 0;
}
void BinauralDecoderAudioProcessor::setCurrentProgram (int index)
{
}
const juce::String BinauralDecoderAudioProcessor::getProgramName (int index)
{
return {};
}
void BinauralDecoderAudioProcessor::changeProgramName (int index, const juce::String& newName)
{
}
//==============================================================================
void BinauralDecoderAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
checkInputAndOutput (this, *inputOrderSetting, 0, true);
juce::dsp::ProcessSpec convSpec;
convSpec.sampleRate = sampleRate;
convSpec.maximumBlockSize = samplesPerBlock;
convSpec.numChannels =
2; // convolve two channels (which actually point two one and the same input channel)
EQ.prepare (convSpec);
}
void BinauralDecoderAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
}
void BinauralDecoderAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer,
juce::MidiBuffer& midiMessages)
{
checkInputAndOutput (this, *inputOrderSetting, 0, false);
juce::ScopedNoDenormals noDenormals;
if (buffer.getNumChannels() < 2)
{
buffer.clear();
return;
}
const int nCh = juce::jmin (buffer.getNumChannels(), input.getNumberOfChannels());
const int L = buffer.getNumSamples();
const int ergL = overlapBuffer.getNumSamples();
const int overlap = irLengthMinusOne;
const int copyL = juce::jmin (L, overlap); // copy max L samples of the overlap data
if (*useSN3D >= 0.5f)
for (int ch = 1; ch < nCh; ++ch)
buffer.applyGain (ch, 0, buffer.getNumSamples(), sn3d2n3d[ch]);
// clear accumulation buffers
juce::FloatVectorOperations::clear (reinterpret_cast<float*> (accumMid.data()), fftLength + 2);
juce::FloatVectorOperations::clear (reinterpret_cast<float*> (accumSide.data()), fftLength + 2);
const int nZeros = fftLength - L;
//compute mid signal in frequency domain
for (int midix = 0; midix < nMidCh; ++midix)
{
const int ch = mix2cix[midix];
juce::FloatVectorOperations::copy (reinterpret_cast<float*> (fftBuffer.data()),
buffer.getReadPointer (ch),
L);
juce::FloatVectorOperations::clear (reinterpret_cast<float*> (fftBuffer.data()) + L,
nZeros);
fft->performRealOnlyForwardTransform (reinterpret_cast<float*> (fftBuffer.data()));
const auto tfMid =
reinterpret_cast<const std::complex<float>*> (irsFrequencyDomain.getReadPointer (ch));
for (int i = 0; i < fftLength / 2 + 1; ++i)
accumMid[i] += fftBuffer[i] * tfMid[i];
}
//compute side signal in frequency domain
for (int sidix = 0; sidix < nSideCh; ++sidix)
{
const int ch = six2cix[sidix];
juce::FloatVectorOperations::copy (reinterpret_cast<float*> (fftBuffer.data()),
buffer.getReadPointer (ch),
L);
juce::FloatVectorOperations::clear (reinterpret_cast<float*> (fftBuffer.data()) + L,
nZeros);
fft->performRealOnlyForwardTransform (reinterpret_cast<float*> (fftBuffer.data()));
const auto tfSide =
reinterpret_cast<const std::complex<float>*> (irsFrequencyDomain.getReadPointer (ch));
for (int i = 0; i < fftLength / 2 + 1; ++i)
accumSide[i] += fftBuffer[i] * tfSide[i];
}
fft->performRealOnlyInverseTransform (reinterpret_cast<float*> (accumMid.data()));
fft->performRealOnlyInverseTransform (reinterpret_cast<float*> (accumSide.data()));
///* MS -> LR */
juce::FloatVectorOperations::copy (buffer.getWritePointer (0),
reinterpret_cast<float*> (accumMid.data()),
L);
juce::FloatVectorOperations::copy (buffer.getWritePointer (1),
reinterpret_cast<float*> (accumMid.data()),
L);
juce::FloatVectorOperations::add (buffer.getWritePointer (0),
reinterpret_cast<float*> (accumSide.data()),
L);
juce::FloatVectorOperations::subtract (buffer.getWritePointer (1),
reinterpret_cast<float*> (accumSide.data()),
L);
juce::FloatVectorOperations::add (buffer.getWritePointer (0),
overlapBuffer.getWritePointer (0),
copyL);
juce::FloatVectorOperations::add (buffer.getWritePointer (1),
overlapBuffer.getWritePointer (1),
copyL);
if (copyL < overlap) // there is some overlap left, want some?
{
const int howManyAreLeft = overlap - L;
//shift the overlap buffer to the left
juce::FloatVectorOperations::copy (overlapBuffer.getWritePointer (0),
overlapBuffer.getReadPointer (0, L),
howManyAreLeft);
juce::FloatVectorOperations::copy (overlapBuffer.getWritePointer (1),
overlapBuffer.getReadPointer (1, L),
howManyAreLeft);
//clear the tail
juce::FloatVectorOperations::clear (overlapBuffer.getWritePointer (0, howManyAreLeft),
ergL - howManyAreLeft);
juce::FloatVectorOperations::clear (overlapBuffer.getWritePointer (1, howManyAreLeft),
ergL - howManyAreLeft);
/* MS -> LR */
juce::FloatVectorOperations::add (overlapBuffer.getWritePointer (0),
reinterpret_cast<float*> (accumMid.data()) + L,
irLengthMinusOne);
juce::FloatVectorOperations::add (overlapBuffer.getWritePointer (1),
reinterpret_cast<float*> (accumMid.data()) + L,
irLengthMinusOne);
juce::FloatVectorOperations::add (overlapBuffer.getWritePointer (0),
reinterpret_cast<float*> (accumSide.data()) + L,
irLengthMinusOne);
juce::FloatVectorOperations::subtract (overlapBuffer.getWritePointer (1),
reinterpret_cast<float*> (accumSide.data()) + L,
irLengthMinusOne);
}
else
{
/* MS -> LR */
juce::FloatVectorOperations::copy (overlapBuffer.getWritePointer (0),
reinterpret_cast<float*> (accumMid.data()) + L,
irLengthMinusOne);
juce::FloatVectorOperations::copy (overlapBuffer.getWritePointer (1),
reinterpret_cast<float*> (accumMid.data()) + L,
irLengthMinusOne);
juce::FloatVectorOperations::add (overlapBuffer.getWritePointer (0),
reinterpret_cast<float*> (accumSide.data()) + L,
irLengthMinusOne);
juce::FloatVectorOperations::subtract (overlapBuffer.getWritePointer (1),
reinterpret_cast<float*> (accumSide.data()) + L,
irLengthMinusOne);
}
if (*applyHeadphoneEq >= 0.5f)
{
float* channelData[2] = { buffer.getWritePointer (0), buffer.getWritePointer (1) };
juce::dsp::AudioBlock<float> sumBlock (channelData, 2, L);
juce::dsp::ProcessContextReplacing<float> eqContext (sumBlock);
EQ.process (eqContext);
}
for (int ch = 2; ch < buffer.getNumChannels(); ++ch)
buffer.clear (ch, 0, buffer.getNumSamples());
}
//==============================================================================
bool BinauralDecoderAudioProcessor::hasEditor() const
{
return true; // (change this to false if you choose to not supply an editor)
}
juce::AudioProcessorEditor* BinauralDecoderAudioProcessor::createEditor()
{
return new BinauralDecoderAudioProcessorEditor (*this, parameters);
}
//==============================================================================
void BinauralDecoderAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
auto state = parameters.copyState();
auto oscConfig = state.getOrCreateChildWithName ("OSCConfig", nullptr);
oscConfig.copyPropertiesFrom (oscParameterInterface.getConfig(), nullptr);
std::unique_ptr<juce::XmlElement> xml (state.createXml());
copyXmlToBinary (*xml, destData);
}
void BinauralDecoderAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
std::unique_ptr<juce::XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
if (xmlState.get() != nullptr)
if (xmlState->hasTagName (parameters.state.getType()))
{
parameters.replaceState (juce::ValueTree::fromXml (*xmlState));
if (parameters.state.hasProperty ("OSCPort")) // legacy
{
oscParameterInterface.getOSCReceiver().connect (
parameters.state.getProperty ("OSCPort", juce::var (-1)));
parameters.state.removeProperty ("OSCPort", nullptr);
}
auto oscConfig = parameters.state.getChildWithName ("OSCConfig");
if (oscConfig.isValid())
oscParameterInterface.setConfig (oscConfig);
}
}
//==============================================================================
void BinauralDecoderAudioProcessor::parameterChanged (const juce::String& parameterID,
float newValue)
{
if (parameterID == "inputOrderSetting")
userChangedIOSettings = true;
else if (parameterID == "applyHeadphoneEq")
{
const int sel (juce::roundToInt (newValue));
if (sel > 0)
{
int sourceDataSize;
juce::String name = headphoneEQs[sel - 1].replace ("-", "") + "_wav";
auto* sourceData = EQData::getNamedResource (name.toUTF8(), sourceDataSize);
if (sourceData == nullptr)
DBG ("error");
EQ.loadImpulseResponse (sourceData,
sourceDataSize,
juce::dsp::Convolution::Stereo::yes,
juce::dsp::Convolution::Trim::no,
2048,
juce::dsp::Convolution::Normalise::no);
}
}
}
void BinauralDecoderAudioProcessor::updateBuffers()
{
DBG ("IOHelper: input size: " << input.getSize());
DBG ("IOHelper: output size: " << output.getSize());
const double sampleRate = getSampleRate();
const int blockSize = getBlockSize();
int order = juce::jmax (input.getOrder(), 1);
const int nCh =
input
.getNumberOfChannels(); // why not juce::jmin(buffer.....)? Is updateBuffers called before the first processBlock?
DBG ("order: " << order);
DBG ("nCh: " << nCh);
int tmpOrder = sqrt (nCh) - 1;
if (tmpOrder < order)
{
order = tmpOrder;
}
//get number of mid- and side-channels
nSideCh = order * (order + 1) / 2;
nMidCh = juce::square (order + 1)
- nSideCh; //nMidCh = nCh - nSideCh; //nCh should be equalt to (order+1)^2
if (order < 1)
order = 1; // just use first order filters
juce::AudioBuffer<float> resampledIRs;
bool useResampled = false;
irLength = 236;
if (sampleRate != irsSampleRate && order != 0) // do resampling!
{
useResampled = true;
double factorReading = irsSampleRate / sampleRate;
irLength = juce::roundToInt (irLength / factorReading + 0.49);
juce::MemoryAudioSource memorySource (irs[order - 1], false);
juce::ResamplingAudioSource resamplingSource (&memorySource, false, nCh);
resamplingSource.setResamplingRatio (factorReading);
resamplingSource.prepareToPlay (irLength, sampleRate);
resampledIRs.setSize (nCh, irLength);
juce::AudioSourceChannelInfo info;
info.startSample = 0;
info.numSamples = irLength;
info.buffer = &resampledIRs;
resamplingSource.getNextAudioBlock (info);
// compensate for more (correlated) samples contributing to output signal
resampledIRs.applyGain (irsSampleRate / sampleRate);
}
irLengthMinusOne = irLength - 1;
const int prevFftLength = fftLength;
const int ergL = blockSize + irLength - 1; //max number of nonzero output samples
fftLength = juce::nextPowerOfTwo (ergL); //fftLength >= ergL
overlapBuffer.setSize (2, irLengthMinusOne);
overlapBuffer.clear();
if (prevFftLength != fftLength)
{
const int fftOrder = std::log2 (fftLength);
fft = std::make_unique<juce::dsp::FFT> (fftOrder);
fftBuffer.resize (fftLength);
accumMid.resize (fftLength);
accumSide.resize (fftLength);
}
irsFrequencyDomain.setSize (nCh, 2 * (fftLength / 2 + 1));
irsFrequencyDomain.clear();
for (int i = 0; i < nCh; ++i)
{
float* inOut = reinterpret_cast<float*> (fftBuffer.data());
const float* src =
useResampled ? resampledIRs.getReadPointer (i) : irs[order - 1].getReadPointer (i);
juce::FloatVectorOperations::copy (inOut, src, irLength);
juce::FloatVectorOperations::clear (inOut + irLength, fftLength - irLength); // zero padding
fft->performRealOnlyForwardTransform (inOut);
juce::FloatVectorOperations::copy (irsFrequencyDomain.getWritePointer (i),
inOut,
2 * (fftLength / 2 + 1));
}
}
//==============================================================================
std::vector<std::unique_ptr<juce::RangedAudioParameter>>
BinauralDecoderAudioProcessor::createParameterLayout()
{
// add your audio parameters here
std::vector<std::unique_ptr<juce::RangedAudioParameter>> params;
params.push_back (OSCParameterInterface::createParameterTheOldWay (
"inputOrderSetting",
"Input Ambisonic Order",
"",
juce::NormalisableRange<float> (0.0f, 8.0f, 1.0f),
0.0f,
[] (float value)
{
if (value >= 0.5f && value < 1.5f)
return "0th";
else if (value >= 1.5f && value < 2.5f)
return "1st";
else if (value >= 2.5f && value < 3.5f)
return "2nd";
else if (value >= 3.5f && value < 4.5f)
return "3rd";
else if (value >= 4.5f && value < 5.5f)
return "4th";
else if (value >= 5.5f && value < 6.5f)
return "5th";
else if (value >= 6.5f && value < 7.5f)
return "6th";
else if (value >= 7.5f)
return "7th";
else
return "Auto";
},
nullptr));
params.push_back (OSCParameterInterface::createParameterTheOldWay (
"useSN3D",
"Input Normalization",
"",
juce::NormalisableRange<float> (0.0f, 1.0f, 1.0f),
1.0f,
[] (float value)
{
if (value >= 0.5f)
return "SN3D";
else
return "N3D";
},
nullptr));
params.push_back (OSCParameterInterface::createParameterTheOldWay (
"applyHeadphoneEq",
"Headphone Equalization",
"",
juce::NormalisableRange<float> (0.0f, float (headphoneEQs.size()), 1.0f),
0.0f,
[this] (float value)
{
if (value < 0.5f)
return juce::String ("OFF");
else
return juce::String (this->headphoneEQs[juce::roundToInt (value) - 1]);
},
nullptr));
return params;
}
//==============================================================================
// This creates new instances of the plugin..
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new BinauralDecoderAudioProcessor();
}
|