1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added a method AudioFormatWriter::flush, and implemented it in the WAV format.

This commit is contained in:
jules 2014-07-04 19:06:11 +01:00
parent 4a3c45e7bd
commit 7e35b73df6
3 changed files with 45 additions and 14 deletions

View file

@ -927,12 +927,6 @@ public:
~WavAudioFormatWriter()
{
if ((bytesWritten & 1) != 0) // pad to an even length
{
++bytesWritten;
output->writeByte (0);
}
writeHeader();
}
@ -972,6 +966,20 @@ public:
return true;
}
bool flush() override
{
const int64 lastWritePos = output->getPosition();
writeHeader();
if (output->setPosition (lastWritePos))
return true;
// if this fails, you've given it an output stream that can't seek! It needs
// to be able to seek back to write the header
jassertfalse;
return false;
}
private:
MemoryBlock tempBlock, bwavChunk, axmlChunk, smplChunk, instChunk, cueChunk, listChunk;
uint64 lengthInSamples, bytesWritten;
@ -998,13 +1006,21 @@ private:
void writeHeader()
{
using namespace WavFileHelpers;
const bool seekedOk = output->setPosition (headerPosition);
(void) seekedOk;
if ((bytesWritten & 1) != 0) // pad to an even length
{
++bytesWritten;
output->writeByte (0);
}
// if this fails, you've given it an output stream that can't seek! It needs
// to be able to seek back to write the header
jassert (seekedOk);
using namespace WavFileHelpers;
if (headerPosition != output->getPosition() && ! output->setPosition (headerPosition))
{
// if this fails, you've given it an output stream that can't seek! It needs to be
// able to seek back to go back and write the header after the data has been written.
jassertfalse;
return;
}
const size_t bytesPerFrame = numChannels * bitsPerSample / 8;
uint64 audioDataSize = bytesPerFrame * lengthInSamples;

View file

@ -183,6 +183,11 @@ bool AudioFormatWriter::writeFromAudioSampleBuffer (const AudioSampleBuffer& sou
return writeFromFloatArrays (chans, numSourceChannels, numSamples);
}
bool AudioFormatWriter::flush()
{
return false;
}
//==============================================================================
class AudioFormatWriter::ThreadedWriter::Buffer : private TimeSliceClient
{

View file

@ -91,8 +91,18 @@ public:
to pass it into the method.
@param numSamples the number of samples to write
*/
virtual bool write (const int** samplesToWrite,
int numSamples) = 0;
virtual bool write (const int** samplesToWrite, int numSamples) = 0;
/** 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.
*/
virtual bool flush();
//==============================================================================
/** Reads a section of samples from an AudioFormatReader, and writes these to