Loading...
Searching...
No Matches
juce_AudioFormatWriter.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 JUCE_API AudioFormatWriter
{
protected:
//==============================================================================
AudioFormatWriter (OutputStream* destStream,
const String& formatName,
double sampleRate,
unsigned int numberOfChannels,
unsigned int bitsPerSample);
//==============================================================================
AudioFormatWriter (OutputStream* destStream,
const String& formatName,
double sampleRate,
const AudioChannelSet& audioChannelLayout,
unsigned int bitsPerSample);
public:
virtual ~AudioFormatWriter();
//==============================================================================
const String& getFormatName() const noexcept { return formatName; }
//==============================================================================
virtual bool write (const int** samplesToWrite, int numSamples) = 0;
virtual bool flush();
//==============================================================================
bool writeFromAudioReader (AudioFormatReader& reader,
int64 startSample,
int64 numSamplesToRead);
bool writeFromAudioSource (AudioSource& source,
int numSamplesToRead,
int samplesPerBlock = 2048);
bool writeFromAudioSampleBuffer (const AudioBuffer<float>& source,
int startSample, int numSamples);
bool writeFromFloatArrays (const float* const* channels, int numChannels, int numSamples);
//==============================================================================
double getSampleRate() const noexcept { return sampleRate; }
int getNumChannels() const noexcept { return (int) numChannels; }
int getBitsPerSample() const noexcept { return (int) bitsPerSample; }
bool isFloatingPoint() const noexcept { return usesFloatingPointData; }
//==============================================================================
class ThreadedWriter
{
public:
ThreadedWriter (AudioFormatWriter* writer,
TimeSliceThread& backgroundThread,
int numSamplesToBuffer);
~ThreadedWriter();
bool write (const float* const* data, int numSamples);
class JUCE_API IncomingDataReceiver
{
public:
IncomingDataReceiver() = default;
virtual ~IncomingDataReceiver() = default;
virtual void reset (int numChannels, double sampleRate, int64 totalSamplesInSource) = 0;
virtual void addBlock (int64 sampleNumberInSource, const AudioBuffer<float>& newData,
int startOffsetInBuffer, int numSamples) = 0;
};
void setDataReceiver (IncomingDataReceiver*);
void setFlushInterval (int numSamplesPerFlush) noexcept;
private:
class Buffer;
std::unique_ptr<Buffer> buffer;
};
protected:
//==============================================================================
double sampleRate;
unsigned int numChannels;
unsigned int bitsPerSample;
bool usesFloatingPointData;
AudioChannelSet channelLayout;
OutputStream* output;
template <class DestSampleType, class SourceSampleType, class DestEndianness>
struct WriteHelper
{
using DestType = AudioData::Pointer <DestSampleType, DestEndianness, AudioData::Interleaved, AudioData::NonConst>;
using SourceType = AudioData::Pointer <SourceSampleType, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::Const>;
static void write (void* destData, int numDestChannels, const int* const* source,
int numSamples, const int sourceOffset = 0) noexcept
{
for (int i = 0; i < numDestChannels; ++i)
{
const DestType dest (addBytesToPointer (destData, i * DestType::getBytesPerSample()), numDestChannels);
if (*source != nullptr)
{
dest.convertSamples (SourceType (*source + sourceOffset), numSamples);
++source;
}
else
{
dest.clearSamples (numSamples);
}
}
}
};
private:
String formatName;
friend class ThreadedWriter;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioFormatWriter)
};
} // namespace juce
juce::AudioBuffer
Definition juce_AudioSampleBuffer.h:34
juce::AudioChannelSet
Definition juce_AudioChannelSet.h:47
juce::AudioData::Pointer::clearSamples
void clearSamples(int numSamples) const noexcept
Definition juce_AudioDataConverters.h:489
juce::AudioData::Pointer< DestSampleType, DestEndianness, AudioData::Interleaved, AudioData::NonConst >::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::AudioFormatWriter::ThreadedWriter::IncomingDataReceiver
Definition juce_AudioFormatWriter.h:216
juce::AudioFormatWriter::ThreadedWriter
Definition juce_AudioFormatWriter.h:184
juce::AudioFormatWriter::ThreadedWriter::write
bool write(const float *const *data, int numSamples)
Definition juce_AudioFormatWriter.cpp:347
juce::AudioFormatWriter::ThreadedWriter::setDataReceiver
void setDataReceiver(IncomingDataReceiver *)
Definition juce_AudioFormatWriter.cpp:352
juce::AudioFormatWriter::ThreadedWriter::ThreadedWriter
ThreadedWriter(AudioFormatWriter *writer, TimeSliceThread &backgroundThread, int numSamplesToBuffer)
Definition juce_AudioFormatWriter.cpp:338
juce::AudioFormatWriter::ThreadedWriter::setFlushInterval
void setFlushInterval(int numSamplesPerFlush) noexcept
Definition juce_AudioFormatWriter.cpp:357
juce::AudioFormatWriter::writeFromAudioReader
bool writeFromAudioReader(AudioFormatReader &reader, int64 startSample, int64 numSamplesToRead)
Definition juce_AudioFormatWriter.cpp:81
juce::AudioFormatWriter::numChannels
unsigned int numChannels
Definition juce_AudioFormatWriter.h:251
juce::AudioFormatWriter::writeFromFloatArrays
bool writeFromFloatArrays(const float *const *channels, int numChannels, int numSamples)
Definition juce_AudioFormatWriter.cpp:152
juce::AudioFormatWriter::writeFromAudioSource
bool writeFromAudioSource(AudioSource &source, int numSamplesToRead, int samplesPerBlock=2048)
Definition juce_AudioFormatWriter.cpp:130
juce::AudioFormatWriter::sampleRate
double sampleRate
Definition juce_AudioFormatWriter.h:248
juce::AudioFormatWriter::usesFloatingPointData
bool usesFloatingPointData
Definition juce_AudioFormatWriter.h:257
juce::AudioFormatWriter::flush
virtual bool flush()
Definition juce_AudioFormatWriter.cpp:208
juce::AudioFormatWriter::getFormatName
const String & getFormatName() const noexcept
Definition juce_AudioFormatWriter.h:95
juce::AudioFormatWriter::bitsPerSample
unsigned int bitsPerSample
Definition juce_AudioFormatWriter.h:254
juce::AudioFormatWriter::write
virtual bool write(const int **samplesToWrite, int numSamples)=0
juce::AudioFormatWriter::getNumChannels
int getNumChannels() const noexcept
Definition juce_AudioFormatWriter.h:170
juce::AudioFormatWriter::AudioFormatWriter
AudioFormatWriter(OutputStream *destStream, const String &formatName, double sampleRate, unsigned int numberOfChannels, unsigned int bitsPerSample)
Definition juce_AudioFormatWriter.cpp:29
juce::AudioFormatWriter::writeFromAudioSampleBuffer
bool writeFromAudioSampleBuffer(const AudioBuffer< float > &source, int startSample, int numSamples)
Definition juce_AudioFormatWriter.cpp:189
juce::AudioFormatWriter::getSampleRate
double getSampleRate() const noexcept
Definition juce_AudioFormatWriter.h:167
juce::AudioFormatWriter::isFloatingPoint
bool isFloatingPoint() const noexcept
Definition juce_AudioFormatWriter.h:176
juce::AudioFormatWriter::channelLayout
AudioChannelSet channelLayout
Definition juce_AudioFormatWriter.h:260
juce::AudioFormatWriter::getBitsPerSample
int getBitsPerSample() const noexcept
Definition juce_AudioFormatWriter.h:173
juce::AudioFormatWriter::output
OutputStream * output
Definition juce_AudioFormatWriter.h:263
juce::AudioSource
Definition juce_AudioSource.h:110
juce::OutputStream
Definition juce_OutputStream.h:38
juce::String
Definition juce_String.h:53
juce::TimeSliceThread
Definition juce_TimeSliceThread.h:83
juce::AudioFormatWriter::WriteHelper
Definition juce_AudioFormatWriter.h:268