Loading...
Searching...
No Matches
juce_AudioFormatReader.h
/*
==============================================================================
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.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
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
{
class AudioFormat;
//==============================================================================
class JUCE_API AudioFormatReader
{
protected:
//==============================================================================
AudioFormatReader (InputStream* sourceStream,
const String& formatName);
public:
virtual ~AudioFormatReader();
//==============================================================================
const String& getFormatName() const noexcept { return formatName; }
//==============================================================================
bool read (float* const* destChannels, int numDestChannels,
int64 startSampleInSource, int numSamplesToRead);
bool read (int* const* destChannels,
int numDestChannels,
int64 startSampleInSource,
int numSamplesToRead,
bool fillLeftoverChannelsWithCopies);
bool read (AudioBuffer<float>* buffer,
int startSampleInDestBuffer,
int numSamples,
int64 readerStartSample,
bool useReaderLeftChan,
bool useReaderRightChan);
virtual void readMaxLevels (int64 startSample, int64 numSamples,
Range<float>* results, int numChannelsToRead);
virtual void readMaxLevels (int64 startSample, int64 numSamples,
float& lowestLeft, float& highestLeft,
float& lowestRight, float& highestRight);
int64 searchForLevel (int64 startSample,
int64 numSamplesToSearch,
double magnitudeRangeMinimum,
double magnitudeRangeMaximum,
int minimumConsecutiveSamples);
//==============================================================================
double sampleRate = 0;
unsigned int bitsPerSample = 0;
int64 lengthInSamples = 0;
unsigned int numChannels = 0;
bool usesFloatingPointData = false;
StringPairArray metadataValues;
InputStream* input;
//==============================================================================
virtual AudioChannelSet getChannelLayout();
//==============================================================================
virtual bool readSamples (int* const* destChannels,
int numDestChannels,
int startOffsetInDestBuffer,
int64 startSampleInFile,
int numSamples) = 0;
protected:
//==============================================================================
template <class DestSampleType, class SourceSampleType, class SourceEndianness>
struct ReadHelper
{
using DestType = AudioData::Pointer<DestSampleType, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::NonConst>;
using SourceType = AudioData::Pointer<SourceSampleType, SourceEndianness, AudioData::Interleaved, AudioData::Const>;
template <typename TargetType>
static void read (TargetType* const* destData, int destOffset, int numDestChannels,
const void* sourceData, int numSourceChannels, int numSamples) noexcept
{
for (int i = 0; i < numDestChannels; ++i)
{
if (void* targetChan = destData[i])
{
DestType dest (targetChan);
dest += destOffset;
if (i < numSourceChannels)
dest.convertSamples (SourceType (addBytesToPointer (sourceData, i * SourceType::getBytesPerSample()), numSourceChannels), numSamples);
else
dest.clearSamples (numSamples);
}
}
}
};
static void clearSamplesBeyondAvailableLength (int* const* destChannels, int numDestChannels,
int startOffsetInDestBuffer, int64 startSampleInFile,
int& numSamples, int64 fileLengthInSamples)
{
if (destChannels == nullptr)
{
jassertfalse;
return;
}
const int64 samplesAvailable = fileLengthInSamples - startSampleInFile;
if (samplesAvailable < numSamples)
{
for (int i = numDestChannels; --i >= 0;)
if (destChannels[i] != nullptr)
zeromem (destChannels[i] + startOffsetInDestBuffer, (size_t) numSamples * sizeof (int));
numSamples = (int) samplesAvailable;
}
}
private:
String formatName;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioFormatReader)
};
} // namespace juce
juce::AudioBuffer
Definition juce_AudioSampleBuffer.h:34
juce::AudioChannelSet
Definition juce_AudioChannelSet.h:47
juce::AudioData::Pointer
Definition juce_AudioDataConverters.h:355
juce::AudioData::Pointer::clearSamples
void clearSamples(int numSamples) const noexcept
Definition juce_AudioDataConverters.h:489
juce::AudioData::Pointer< SourceSampleType, SourceEndianness, AudioData::Interleaved, AudioData::Const >::getBytesPerSample
static int getBytesPerSample() noexcept
Definition juce_AudioDataConverters.h:553
juce::AudioData::Pointer::convertSamples
void convertSamples(Pointer source, int numSamples) const noexcept
Definition juce_AudioDataConverters.h:445
juce::AudioFormatReader
Definition juce_AudioFormatReader.h:44
juce::AudioFormatReader::input
InputStream * input
Definition juce_AudioFormatReader.h:248
juce::AudioFormatReader::usesFloatingPointData
bool usesFloatingPointData
Definition juce_AudioFormatReader.h:237
juce::AudioFormatReader::metadataValues
StringPairArray metadataValues
Definition juce_AudioFormatReader.h:245
juce::AudioFormatReader::getFormatName
const String & getFormatName() const noexcept
Definition juce_AudioFormatReader.h:68
juce::AudioFormatReader::clearSamplesBeyondAvailableLength
static void clearSamplesBeyondAvailableLength(int *const *destChannels, int numDestChannels, int startOffsetInDestBuffer, int64 startSampleInFile, int &numSamples, int64 fileLengthInSamples)
Definition juce_AudioFormatReader.h:309
juce::AudioFormatReader::lengthInSamples
int64 lengthInSamples
Definition juce_AudioFormatReader.h:231
juce::AudioFormatReader::sampleRate
double sampleRate
Definition juce_AudioFormatReader.h:225
juce::AudioFormatReader::getChannelLayout
virtual AudioChannelSet getChannelLayout()
Definition juce_AudioFormatReader.cpp:398
juce::AudioFormatReader::AudioFormatReader
AudioFormatReader(InputStream *sourceStream, const String &formatName)
Definition juce_AudioFormatReader.cpp:29
juce::AudioFormatReader::readSamples
virtual bool readSamples(int *const *destChannels, int numDestChannels, int startOffsetInDestBuffer, int64 startSampleInFile, int numSamples)=0
juce::AudioFormatReader::bitsPerSample
unsigned int bitsPerSample
Definition juce_AudioFormatReader.h:228
juce::AudioFormatReader::numChannels
unsigned int numChannels
Definition juce_AudioFormatReader.h:234
juce::AudioFormat
Definition juce_AudioFormat.h:39
juce::InputStream
Definition juce_InputStream.h:37
juce::Range
Definition juce_Range.h:40
juce::StringPairArray
Definition juce_StringPairArray.h:35
juce::String
Definition juce_String.h:53
juce::AudioFormatReader::ReadHelper
Definition juce_AudioFormatReader.h:282