OpenShot Library | OpenShotAudio  0.2.1
juce::AudioFormatWriter Class Referenceabstract

Writes samples to an audio file stream. More...

#include <juce_AudioFormatWriter.h>

+ Inheritance diagram for juce::AudioFormatWriter:

Classes

class  ThreadedWriter
 Provides a FIFO for an AudioFormatWriter, allowing you to push incoming data into a buffer which will be flushed to disk by a background thread. More...
 
struct  WriteHelper
 Used by AudioFormatWriter subclasses to copy data to different formats. More...
 

Public Member Functions

virtual ~AudioFormatWriter ()
 Destructor. More...
 
const StringgetFormatName () const noexcept
 Returns a description of what type of format this is. More...
 
virtual bool write (const int **samplesToWrite, int numSamples)=0
 Writes a set of samples to the audio stream. More...
 
virtual bool flush ()
 Some formats may support a flush operation that makes sure the file is in a valid state before carrying on. More...
 
bool writeFromAudioReader (AudioFormatReader &reader, int64 startSample, int64 numSamplesToRead)
 Reads a section of samples from an AudioFormatReader, and writes these to the output. More...
 
bool writeFromAudioSource (AudioSource &source, int numSamplesToRead, int samplesPerBlock=2048)
 Reads some samples from an AudioSource, and writes these to the output. More...
 
bool writeFromAudioSampleBuffer (const AudioBuffer< float > &source, int startSample, int numSamples)
 Writes some samples from an AudioBuffer. More...
 
bool writeFromFloatArrays (const float *const *channels, int numChannels, int numSamples)
 Writes some samples from a set of float data channels. More...
 
double getSampleRate () const noexcept
 Returns the sample rate being used. More...
 
int getNumChannels () const noexcept
 Returns the number of channels being written. More...
 
int getBitsPerSample () const noexcept
 Returns the bit-depth of the data being written. More...
 
bool isFloatingPoint () const noexcept
 Returns true if it's a floating-point format, false if it's fixed-point. More...
 

Protected Member Functions

 AudioFormatWriter (OutputStream *destStream, const String &formatName, double sampleRate, unsigned int numberOfChannels, unsigned int bitsPerSample)
 Creates an AudioFormatWriter object. More...
 
 AudioFormatWriter (OutputStream *destStream, const String &formatName, double sampleRate, const AudioChannelSet &audioChannelLayout, unsigned int bitsPerSample)
 Creates an AudioFormatWriter object. More...
 

Protected Attributes

double sampleRate
 The sample rate of the stream. More...
 
unsigned int numChannels
 The number of channels being written to the stream. More...
 
unsigned int bitsPerSample
 The bit depth of the file. More...
 
bool usesFloatingPointData
 True if it's a floating-point format, false if it's fixed-point. More...
 
AudioChannelSet channelLayout
 The audio channel layout that the writer should use.
 
OutputStreamoutput
 The output stream for use by subclasses. More...
 

Friends

class ThreadedWriter
 

Detailed Description

Writes samples to an audio file stream.

A subclass that writes a specific type of audio format will be created by an AudioFormat object.

After creating one of these with the AudioFormat::createWriterFor() method you can call its write() method to store the samples, and then delete it.

See also
AudioFormat, AudioFormatReader

Definition at line 48 of file juce_AudioFormatWriter.h.

Constructor & Destructor Documentation

◆ AudioFormatWriter() [1/2]

juce::AudioFormatWriter::AudioFormatWriter ( OutputStream destStream,
const String formatName,
double  sampleRate,
unsigned int  numberOfChannels,
unsigned int  bitsPerSample 
)
protected

Creates an AudioFormatWriter object.

Parameters
destStreamthe stream to write to - this will be deleted by this object when it is no longer needed
formatNamethe description that will be returned by the getFormatName() method
sampleRatethe sample rate to use - the base class just stores this value, it doesn't do anything with it
numberOfChannelsthe number of channels to write - the base class just stores this value, it doesn't do anything with it
bitsPerSamplethe bit depth of the stream - the base class just stores this value, it doesn't do anything with it

Definition at line 30 of file juce_AudioFormatWriter.cpp.

◆ AudioFormatWriter() [2/2]

juce::AudioFormatWriter::AudioFormatWriter ( OutputStream destStream,
const String formatName,
double  sampleRate,
const AudioChannelSet audioChannelLayout,
unsigned int  bitsPerSample 
)
protected

Creates an AudioFormatWriter object.

Parameters
destStreamthe stream to write to - this will be deleted by this object when it is no longer needed
formatNamethe description that will be returned by the getFormatName() method
sampleRatethe sample rate to use - the base class just stores this value, it doesn't do anything with it
audioChannelLayoutthe channel layout to use for the writer - the base class just stores this value, it doesn't do anything with it
bitsPerSamplethe bit depth of the stream - the base class just stores this value, it doesn't do anything with it

Definition at line 45 of file juce_AudioFormatWriter.cpp.

◆ ~AudioFormatWriter()

juce::AudioFormatWriter::~AudioFormatWriter ( )
virtual

Destructor.

Definition at line 60 of file juce_AudioFormatWriter.cpp.

References output.

Member Function Documentation

◆ getFormatName()

const String& juce::AudioFormatWriter::getFormatName ( ) const
inlinenoexcept

Returns a description of what type of format this is.

E.g. "AIFF file"

Definition at line 100 of file juce_AudioFormatWriter.h.

◆ write()

virtual bool juce::AudioFormatWriter::write ( const int **  samplesToWrite,
int  numSamples 
)
pure virtual

Writes a set of samples to the audio stream.

Note that if you're trying to write the contents of an AudioBuffer, you can use writeFromAudioSampleBuffer().

Parameters
samplesToWritean array of arrays containing the sample data for each channel to write. This is a zero-terminated array of arrays, and can contain a different number of channels than the actual stream uses, and the writer should do its best to cope with this. If the format is fixed-point, each channel will be formatted as an array of signed integers using the full 32-bit range -0x80000000 to 0x7fffffff, regardless of the source's bit-depth. If it is a floating-point format, you should treat the arrays as arrays of floats, and just cast it to an (int**) to pass it into the method.
numSamplesthe number of samples to write

Implemented in juce::WavAudioFormatWriter, and juce::AiffAudioFormatWriter.

Referenced by writeFromAudioReader(), and writeFromFloatArrays().

◆ flush()

bool juce::AudioFormatWriter::flush ( )
virtual

Some formats may support a flush operation that makes sure the file is in a valid state before carrying on.

If supported, this means that by calling flush periodically when writing data to a large file, then it should still be left in a readable state if your program crashes. It goes without saying that this method must be called from the same thread that's calling write()! If the format supports flushing and the operation succeeds, this returns true.

Reimplemented in juce::WavAudioFormatWriter.

Definition at line 207 of file juce_AudioFormatWriter.cpp.

◆ writeFromAudioReader()

bool juce::AudioFormatWriter::writeFromAudioReader ( AudioFormatReader reader,
int64  startSample,
int64  numSamplesToRead 
)

Reads a section of samples from an AudioFormatReader, and writes these to the output.

This will take care of any floating-point conversion that's required to convert between the two formats. It won't deal with sample-rate conversion, though.

If numSamplesToRead < 0, it will write the entire length of the reader.

Returns
false if it can't read or write properly during the operation

Definition at line 82 of file juce_AudioFormatWriter.cpp.

References juce::FloatVectorOperations::convertFixedToFloat(), juce::AudioBuffer< Type >::getNumChannels(), juce::AudioBuffer< Type >::getWritePointer(), isFloatingPoint(), juce::AudioFormatReader::lengthInSamples, numChannels, juce::AudioFormatReader::read(), juce::AudioFormatReader::usesFloatingPointData, and write().

◆ writeFromAudioSource()

bool juce::AudioFormatWriter::writeFromAudioSource ( AudioSource source,
int  numSamplesToRead,
int  samplesPerBlock = 2048 
)

Reads some samples from an AudioSource, and writes these to the output.

The source must already have been initialised with the AudioSource::prepareToPlay() method

Parameters
sourcethe source to read from
numSamplesToReadtotal number of samples to read and write
samplesPerBlockthe maximum number of samples to fetch from the source
Returns
false if it can't read or write properly during the operation

Definition at line 129 of file juce_AudioFormatWriter.cpp.

References juce::AudioSourceChannelInfo::clearActiveBufferRegion(), juce::AudioSource::getNextAudioBlock(), getNumChannels(), and writeFromAudioSampleBuffer().

◆ writeFromAudioSampleBuffer()

bool juce::AudioFormatWriter::writeFromAudioSampleBuffer ( const AudioBuffer< float > &  source,
int  startSample,
int  numSamples 
)

◆ writeFromFloatArrays()

bool juce::AudioFormatWriter::writeFromFloatArrays ( const float *const *  channels,
int  numChannels,
int  numSamples 
)

Writes some samples from a set of float data channels.

Definition at line 151 of file juce_AudioFormatWriter.cpp.

References isFloatingPoint(), and write().

Referenced by writeFromAudioSampleBuffer().

◆ getSampleRate()

double juce::AudioFormatWriter::getSampleRate ( ) const
inlinenoexcept

Returns the sample rate being used.

Definition at line 172 of file juce_AudioFormatWriter.h.

◆ getNumChannels()

int juce::AudioFormatWriter::getNumChannels ( ) const
inlinenoexcept

Returns the number of channels being written.

Definition at line 175 of file juce_AudioFormatWriter.h.

Referenced by writeFromAudioSource().

◆ getBitsPerSample()

int juce::AudioFormatWriter::getBitsPerSample ( ) const
inlinenoexcept

Returns the bit-depth of the data being written.

Definition at line 178 of file juce_AudioFormatWriter.h.

◆ isFloatingPoint()

bool juce::AudioFormatWriter::isFloatingPoint ( ) const
inlinenoexcept

Returns true if it's a floating-point format, false if it's fixed-point.

Definition at line 181 of file juce_AudioFormatWriter.h.

Referenced by writeFromAudioReader(), and writeFromFloatArrays().

Member Data Documentation

◆ sampleRate

double juce::AudioFormatWriter::sampleRate
protected

The sample rate of the stream.

Definition at line 253 of file juce_AudioFormatWriter.h.

◆ numChannels

unsigned int juce::AudioFormatWriter::numChannels
protected

The number of channels being written to the stream.

Definition at line 256 of file juce_AudioFormatWriter.h.

Referenced by writeFromAudioReader(), and writeFromAudioSampleBuffer().

◆ bitsPerSample

unsigned int juce::AudioFormatWriter::bitsPerSample
protected

The bit depth of the file.

Definition at line 259 of file juce_AudioFormatWriter.h.

◆ usesFloatingPointData

bool juce::AudioFormatWriter::usesFloatingPointData
protected

True if it's a floating-point format, false if it's fixed-point.

Definition at line 262 of file juce_AudioFormatWriter.h.

◆ output

OutputStream* juce::AudioFormatWriter::output
protected

The output stream for use by subclasses.

Definition at line 268 of file juce_AudioFormatWriter.h.

Referenced by ~AudioFormatWriter().


The documentation for this class was generated from the following files: