Browse Source

BufferingAudioReader: Fixed an infinite read bug

v6.1.6
ed 4 years ago
parent
commit
b3bdfdb34c
2 changed files with 22 additions and 22 deletions
  1. +7
    -8
      modules/juce_audio_formats/format/juce_BufferingAudioFormatReader.cpp
  2. +15
    -14
      modules/juce_audio_formats/format/juce_BufferingAudioFormatReader.h

+ 7
- 8
modules/juce_audio_formats/format/juce_BufferingAudioFormatReader.cpp View File

@@ -75,7 +75,7 @@ bool BufferingAudioReader::readSamples (int** destSamples, int numDestChannels,
for (int j = 0; j < numDestChannels; ++j)
{
if (auto dest = (float*) destSamples[j])
if (auto* dest = (float*) destSamples[j])
{
dest += startOffsetInDestBuffer;
@@ -95,7 +95,7 @@ bool BufferingAudioReader::readSamples (int** destSamples, int numDestChannels,
if (timeoutMs >= 0 && Time::getMillisecondCounter() >= startTime + (uint32) timeoutMs)
{
for (int j = 0; j < numDestChannels; ++j)
if (auto dest = (float*) destSamples[j])
if (auto* dest = (float*) destSamples[j])
FloatVectorOperations::clear (dest + startOffsetInDestBuffer, numSamples);
break;
@@ -135,14 +135,13 @@ int BufferingAudioReader::useTimeSlice()
bool BufferingAudioReader::readNextBufferChunk()
{
auto pos = nextReadPosition.load();
auto startPos = ((pos - 1024) / samplesPerBlock) * samplesPerBlock;
auto endPos = startPos + numBlocks * samplesPerBlock;
auto endPos = pos + numBlocks * samplesPerBlock;
OwnedArray<BufferedBlock> newBlocks;
for (int i = blocks.size(); --i >= 0;)
if (blocks.getUnchecked(i)->range.intersects (Range<int64> (startPos, endPos)))
newBlocks.add (blocks.getUnchecked(i));
if (blocks.getUnchecked (i)->range.intersects (Range<int64> (pos, endPos)))
newBlocks.add (blocks.getUnchecked (i));
if (newBlocks.size() == numBlocks)
{
@@ -150,7 +149,7 @@ bool BufferingAudioReader::readNextBufferChunk()
return false;
}
for (auto p = startPos; p < endPos; p += samplesPerBlock)
for (auto p = pos; p < endPos; p += samplesPerBlock)
{
if (getBlockContaining (p) == nullptr)
{
@@ -165,7 +164,7 @@ bool BufferingAudioReader::readNextBufferChunk()
}
for (int i = blocks.size(); --i >= 0;)
newBlocks.removeObject (blocks.getUnchecked(i), false);
newBlocks.removeObject (blocks.getUnchecked (i), false);
return true;
}


+ 15
- 14
modules/juce_audio_formats/format/juce_BufferingAudioFormatReader.h View File

@@ -57,23 +57,16 @@ public:
/** Sets a number of milliseconds that the reader can block for in its readSamples()
method before giving up and returning silence.
A value of less that 0 means "wait forever".
The default timeout is 0.
A value of less that 0 means "wait forever". The default timeout is 0.
*/
void setReadTimeout (int timeoutMilliseconds) noexcept;
//==============================================================================
bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
int64 startSampleInFile, int numSamples) override;
private:
std::unique_ptr<AudioFormatReader> source;
TimeSliceThread& thread;
std::atomic<int64> nextReadPosition { 0 };
const int numBlocks;
int timeoutMs = 0;
enum { samplesPerBlock = 32768 };
struct BufferedBlock
{
BufferedBlock (AudioFormatReader& reader, int64 pos, int numSamples);
@@ -82,13 +75,21 @@ private:
AudioBuffer<float> buffer;
};
CriticalSection lock;
OwnedArray<BufferedBlock> blocks;
BufferedBlock* getBlockContaining (int64 pos) const noexcept;
int useTimeSlice() override;
BufferedBlock* getBlockContaining (int64 pos) const noexcept;
bool readNextBufferChunk();
static constexpr int samplesPerBlock = 32768;
std::unique_ptr<AudioFormatReader> source;
TimeSliceThread& thread;
std::atomic<int64> nextReadPosition { 0 };
const int numBlocks;
int timeoutMs = 0;
CriticalSection lock;
OwnedArray<BufferedBlock> blocks;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioReader)
};


Loading…
Cancel
Save