Loading...
Searching...
No Matches
juce_ChannelRemappingAudioSource.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
ChannelRemappingAudioSource::ChannelRemappingAudioSource (AudioSource* const source_,
const bool deleteSourceWhenDeleted)
: source (source_, deleteSourceWhenDeleted),
requiredNumberOfChannels (2)
{
remappedInfo.buffer = &buffer;
remappedInfo.startSample = 0;
}
ChannelRemappingAudioSource::~ChannelRemappingAudioSource() {}
//==============================================================================
void ChannelRemappingAudioSource::setNumberOfChannelsToProduce (const int requiredNumberOfChannels_)
{
const ScopedLock sl (lock);
requiredNumberOfChannels = requiredNumberOfChannels_;
}
void ChannelRemappingAudioSource::clearAllMappings()
{
const ScopedLock sl (lock);
remappedInputs.clear();
remappedOutputs.clear();
}
void ChannelRemappingAudioSource::setInputChannelMapping (const int destIndex, const int sourceIndex)
{
const ScopedLock sl (lock);
while (remappedInputs.size() < destIndex)
remappedInputs.add (-1);
remappedInputs.set (destIndex, sourceIndex);
}
void ChannelRemappingAudioSource::setOutputChannelMapping (const int sourceIndex, const int destIndex)
{
const ScopedLock sl (lock);
while (remappedOutputs.size() < sourceIndex)
remappedOutputs.add (-1);
remappedOutputs.set (sourceIndex, destIndex);
}
int ChannelRemappingAudioSource::getRemappedInputChannel (const int inputChannelIndex) const
{
const ScopedLock sl (lock);
if (inputChannelIndex >= 0 && inputChannelIndex < remappedInputs.size())
return remappedInputs.getUnchecked (inputChannelIndex);
return -1;
}
int ChannelRemappingAudioSource::getRemappedOutputChannel (const int outputChannelIndex) const
{
const ScopedLock sl (lock);
if (outputChannelIndex >= 0 && outputChannelIndex < remappedOutputs.size())
return remappedOutputs .getUnchecked (outputChannelIndex);
return -1;
}
//==============================================================================
void ChannelRemappingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
source->prepareToPlay (samplesPerBlockExpected, sampleRate);
}
void ChannelRemappingAudioSource::releaseResources()
{
source->releaseResources();
}
void ChannelRemappingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
{
const ScopedLock sl (lock);
buffer.setSize (requiredNumberOfChannels, bufferToFill.numSamples, false, false, true);
const int numChans = bufferToFill.buffer->getNumChannels();
for (int i = 0; i < buffer.getNumChannels(); ++i)
{
const int remappedChan = getRemappedInputChannel (i);
if (remappedChan >= 0 && remappedChan < numChans)
{
buffer.copyFrom (i, 0, *bufferToFill.buffer,
remappedChan,
bufferToFill.startSample,
bufferToFill.numSamples);
}
else
{
buffer.clear (i, 0, bufferToFill.numSamples);
}
}
remappedInfo.numSamples = bufferToFill.numSamples;
source->getNextAudioBlock (remappedInfo);
bufferToFill.clearActiveBufferRegion();
for (int i = 0; i < requiredNumberOfChannels; ++i)
{
const int remappedChan = getRemappedOutputChannel (i);
if (remappedChan >= 0 && remappedChan < numChans)
{
bufferToFill.buffer->addFrom (remappedChan, bufferToFill.startSample,
buffer, i, 0, bufferToFill.numSamples);
}
}
}
//==============================================================================
std::unique_ptr<XmlElement> ChannelRemappingAudioSource::createXml() const
{
auto e = std::make_unique<XmlElement> ("MAPPINGS");
String ins, outs;
const ScopedLock sl (lock);
for (int i = 0; i < remappedInputs.size(); ++i)
ins << remappedInputs.getUnchecked (i) << ' ';
for (int i = 0; i < remappedOutputs.size(); ++i)
outs << remappedOutputs.getUnchecked (i) << ' ';
e->setAttribute ("inputs", ins.trimEnd());
e->setAttribute ("outputs", outs.trimEnd());
return e;
}
void ChannelRemappingAudioSource::restoreFromXml (const XmlElement& e)
{
if (e.hasTagName ("MAPPINGS"))
{
const ScopedLock sl (lock);
clearAllMappings();
StringArray ins, outs;
ins.addTokens (e.getStringAttribute ("inputs"), false);
outs.addTokens (e.getStringAttribute ("outputs"), false);
for (int i = 0; i < ins.size(); ++i)
remappedInputs.add (ins[i].getIntValue());
for (int i = 0; i < outs.size(); ++i)
remappedOutputs.add (outs[i].getIntValue());
}
}
} // namespace juce
juce::AudioBuffer::getNumChannels
int getNumChannels() const noexcept
Definition juce_AudioSampleBuffer.h:236
juce::AudioBuffer::addFrom
void addFrom(int destChannel, int destStartSample, const AudioBuffer &source, int sourceChannel, int sourceStartSample, int numSamples, Type gainToApplyToSource=Type(1)) noexcept
Definition juce_AudioSampleBuffer.h:784
juce::AudioSource::AudioSource
AudioSource()=default
juce::ChannelRemappingAudioSource::setOutputChannelMapping
void setOutputChannelMapping(int sourceChannelIndex, int destChannelIndex)
Definition juce_ChannelRemappingAudioSource.cpp:62
juce::ChannelRemappingAudioSource::getNextAudioBlock
void getNextAudioBlock(const AudioSourceChannelInfo &) override
Definition juce_ChannelRemappingAudioSource.cpp:103
juce::ChannelRemappingAudioSource::~ChannelRemappingAudioSource
~ChannelRemappingAudioSource() override
Definition juce_ChannelRemappingAudioSource.cpp:35
juce::ChannelRemappingAudioSource::prepareToPlay
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Definition juce_ChannelRemappingAudioSource.cpp:93
juce::ChannelRemappingAudioSource::setNumberOfChannelsToProduce
void setNumberOfChannelsToProduce(int requiredNumberOfChannels)
Definition juce_ChannelRemappingAudioSource.cpp:38
juce::ChannelRemappingAudioSource::restoreFromXml
void restoreFromXml(const XmlElement &)
Definition juce_ChannelRemappingAudioSource.cpp:167
juce::ChannelRemappingAudioSource::clearAllMappings
void clearAllMappings()
Definition juce_ChannelRemappingAudioSource.cpp:44
juce::ChannelRemappingAudioSource::releaseResources
void releaseResources() override
Definition juce_ChannelRemappingAudioSource.cpp:98
juce::ChannelRemappingAudioSource::getRemappedInputChannel
int getRemappedInputChannel(int inputChannelIndex) const
Definition juce_ChannelRemappingAudioSource.cpp:72
juce::ChannelRemappingAudioSource::getRemappedOutputChannel
int getRemappedOutputChannel(int outputChannelIndex) const
Definition juce_ChannelRemappingAudioSource.cpp:82
juce::ChannelRemappingAudioSource::ChannelRemappingAudioSource
ChannelRemappingAudioSource(AudioSource *source, bool deleteSourceWhenDeleted)
Definition juce_ChannelRemappingAudioSource.cpp:26
juce::ChannelRemappingAudioSource::createXml
std::unique_ptr< XmlElement > createXml() const
Definition juce_ChannelRemappingAudioSource.cpp:148
juce::ChannelRemappingAudioSource::setInputChannelMapping
void setInputChannelMapping(int destChannelIndex, int sourceChannelIndex)
Definition juce_ChannelRemappingAudioSource.cpp:52
juce::StringArray
Definition juce_StringArray.h:35
juce::StringArray::size
int size() const noexcept
Definition juce_StringArray.h:136
juce::StringArray::addTokens
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Definition juce_StringArray.cpp:329
juce::String
Definition juce_String.h:53
juce::String::trimEnd
String trimEnd() const
Definition juce_String.cpp:1687
juce::XmlElement
Definition juce_XmlElement.h:83
juce::XmlElement::hasTagName
bool hasTagName(StringRef possibleTagName) const noexcept
Definition juce_XmlElement.cpp:470
juce::XmlElement::getStringAttribute
const String & getStringAttribute(StringRef attributeName) const noexcept
Definition juce_XmlElement.cpp:555
juce::AudioSourceChannelInfo
Definition juce_AudioSource.h:33
juce::AudioSourceChannelInfo::numSamples
int numSamples
Definition juce_AudioSource.h:81
juce::AudioSourceChannelInfo::clearActiveBufferRegion
void clearActiveBufferRegion() const
Definition juce_AudioSource.h:84
juce::AudioSourceChannelInfo::buffer
AudioBuffer< float > * buffer
Definition juce_AudioSource.h:73
juce::AudioSourceChannelInfo::startSample
int startSample
Definition juce_AudioSource.h:77