1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
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 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-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
{
AudioFormatReader::AudioFormatReader (InputStream* in, const String& name)
: input (in), formatName (name)
{
}
AudioFormatReader::~AudioFormatReader()
{
delete input;
}
static void convertFixedToFloat (int* const* channels, int numChannels, int numSamples)
{
for (int i = 0; i < numChannels; ++i)
if (auto d = channels[i])
FloatVectorOperations::convertFixedToFloat (reinterpret_cast<float*> (d), d, 1.0f / 0x7fffffff, numSamples);
}
bool AudioFormatReader::read (float* const* destChannels, int numDestChannels,
int64 startSampleInSource, int numSamplesToRead)
{
auto channelsAsInt = reinterpret_cast<int* const*> (destChannels);
if (! read (channelsAsInt, numDestChannels, startSampleInSource, numSamplesToRead, false))
return false;
if (! usesFloatingPointData)
convertFixedToFloat (channelsAsInt, numDestChannels, numSamplesToRead);
return true;
}
bool AudioFormatReader::read (int* const* destChannels,
int numDestChannels,
int64 startSampleInSource,
int numSamplesToRead,
bool fillLeftoverChannelsWithCopies)
{
jassert (numDestChannels > 0); // you have to actually give this some channels to work with!
auto originalNumSamplesToRead = (size_t) numSamplesToRead;
int startOffsetInDestBuffer = 0;
if (startSampleInSource < 0)
{
auto silence = (int) jmin (-startSampleInSource, (int64) numSamplesToRead);
for (int i = numDestChannels; --i >= 0;)
if (auto d = destChannels[i])
zeromem (d, sizeof (int) * (size_t) silence);
startOffsetInDestBuffer += silence;
numSamplesToRead -= silence;
startSampleInSource = 0;
}
if (numSamplesToRead <= 0)
return true;
if (! readSamples (const_cast<int**> (destChannels),
jmin ((int) numChannels, numDestChannels), startOffsetInDestBuffer,
startSampleInSource, numSamplesToRead))
return false;
if (numDestChannels > (int) numChannels)
{
if (fillLeftoverChannelsWithCopies)
{
auto lastFullChannel = destChannels[0];
for (int i = (int) numChannels; --i > 0;)
{
if (destChannels[i] != nullptr)
{
lastFullChannel = destChannels[i];
break;
}
}
if (lastFullChannel != nullptr)
for (int i = (int) numChannels; i < numDestChannels; ++i)
if (auto d = destChannels[i])
memcpy (d, lastFullChannel, sizeof (int) * originalNumSamplesToRead);
}
else
{
for (int i = (int) numChannels; i < numDestChannels; ++i)
if (auto d = destChannels[i])
zeromem (d, sizeof (int) * originalNumSamplesToRead);
}
}
return true;
}
static void readChannels (AudioFormatReader& reader, int** chans, AudioBuffer<float>* buffer,
int startSample, int numSamples, int64 readerStartSample, int numTargetChannels,
bool convertToFloat)
{
for (int j = 0; j < numTargetChannels; ++j)
chans[j] = reinterpret_cast<int*> (buffer->getWritePointer (j, startSample));
chans[numTargetChannels] = nullptr;
reader.read (chans, numTargetChannels, readerStartSample, numSamples, true);
if (convertToFloat)
convertFixedToFloat (chans, numTargetChannels, numSamples);
}
void AudioFormatReader::read (AudioBuffer<float>* buffer,
int startSample,
int numSamples,
int64 readerStartSample,
bool useReaderLeftChan,
bool useReaderRightChan)
{
jassert (buffer != nullptr);
jassert (startSample >= 0 && startSample + numSamples <= buffer->getNumSamples());
if (numSamples > 0)
{
auto numTargetChannels = buffer->getNumChannels();
if (numTargetChannels <= 2)
{
int* dests[2] = { reinterpret_cast<int*> (buffer->getWritePointer (0, startSample)),
reinterpret_cast<int*> (numTargetChannels > 1 ? buffer->getWritePointer (1, startSample) : nullptr) };
int* chans[3] = {};
if (useReaderLeftChan == useReaderRightChan)
{
chans[0] = dests[0];
if (numChannels > 1)
chans[1] = dests[1];
}
else if (useReaderLeftChan || (numChannels == 1))
{
chans[0] = dests[0];
}
else if (useReaderRightChan)
{
chans[1] = dests[0];
}
read (chans, 2, readerStartSample, numSamples, true);
// if the target's stereo and the source is mono, dupe the first channel..
if (numTargetChannels > 1 && (chans[0] == nullptr || chans[1] == nullptr))
memcpy (dests[1], dests[0], sizeof (float) * (size_t) numSamples);
if (! usesFloatingPointData)
convertFixedToFloat (dests, 2, numSamples);
}
else if (numTargetChannels <= 64)
{
int* chans[65];
readChannels (*this, chans, buffer, startSample, numSamples,
readerStartSample, numTargetChannels, ! usesFloatingPointData);
}
else
{
HeapBlock<int*> chans (numTargetChannels + 1);
readChannels (*this, chans, buffer, startSample, numSamples,
readerStartSample, numTargetChannels, ! usesFloatingPointData);
}
}
}
void AudioFormatReader::readMaxLevels (int64 startSampleInFile, int64 numSamples,
Range<float>* const results, const int channelsToRead)
{
jassert (channelsToRead > 0 && channelsToRead <= (int) numChannels);
if (numSamples <= 0)
{
for (int i = 0; i < channelsToRead; ++i)
results[i] = Range<float>();
return;
}
auto bufferSize = (int) jmin (numSamples, (int64) 4096);
AudioBuffer<float> tempSampleBuffer ((int) channelsToRead, bufferSize);
auto floatBuffer = tempSampleBuffer.getArrayOfWritePointers();
auto intBuffer = reinterpret_cast<int* const*> (floatBuffer);
bool isFirstBlock = true;
while (numSamples > 0)
{
auto numToDo = (int) jmin (numSamples, (int64) bufferSize);
if (! read (intBuffer, channelsToRead, startSampleInFile, numToDo, false))
break;
for (int i = 0; i < channelsToRead; ++i)
{
Range<float> r;
if (usesFloatingPointData)
{
r = FloatVectorOperations::findMinAndMax (floatBuffer[i], numToDo);
}
else
{
auto intRange = Range<int>::findMinAndMax (intBuffer[i], numToDo);
r = Range<float> (intRange.getStart() / (float) std::numeric_limits<int>::max(),
intRange.getEnd() / (float) std::numeric_limits<int>::max());
}
results[i] = isFirstBlock ? r : results[i].getUnionWith (r);
}
isFirstBlock = false;
numSamples -= numToDo;
startSampleInFile += numToDo;
}
}
void AudioFormatReader::readMaxLevels (int64 startSampleInFile, int64 numSamples,
float& lowestLeft, float& highestLeft,
float& lowestRight, float& highestRight)
{
Range<float> levels[2];
if (numChannels < 2)
{
readMaxLevels (startSampleInFile, numSamples, levels, (int) numChannels);
levels[1] = levels[0];
}
else
{
readMaxLevels (startSampleInFile, numSamples, levels, 2);
}
lowestLeft = levels[0].getStart();
highestLeft = levels[0].getEnd();
lowestRight = levels[1].getStart();
highestRight = levels[1].getEnd();
}
int64 AudioFormatReader::searchForLevel (int64 startSample,
int64 numSamplesToSearch,
double magnitudeRangeMinimum,
double magnitudeRangeMaximum,
int minimumConsecutiveSamples)
{
if (numSamplesToSearch == 0)
return -1;
const int bufferSize = 4096;
HeapBlock<int> tempSpace (bufferSize * 2 + 64);
int* tempBuffer[3] = { tempSpace.get(),
tempSpace.get() + bufferSize,
nullptr };
int consecutive = 0;
int64 firstMatchPos = -1;
jassert (magnitudeRangeMaximum > magnitudeRangeMinimum);
auto doubleMin = jlimit (0.0, (double) std::numeric_limits<int>::max(), magnitudeRangeMinimum * std::numeric_limits<int>::max());
auto doubleMax = jlimit (doubleMin, (double) std::numeric_limits<int>::max(), magnitudeRangeMaximum * std::numeric_limits<int>::max());
auto intMagnitudeRangeMinimum = roundToInt (doubleMin);
auto intMagnitudeRangeMaximum = roundToInt (doubleMax);
while (numSamplesToSearch != 0)
{
auto numThisTime = (int) jmin (std::abs (numSamplesToSearch), (int64) bufferSize);
int64 bufferStart = startSample;
if (numSamplesToSearch < 0)
bufferStart -= numThisTime;
if (bufferStart >= lengthInSamples)
break;
read (tempBuffer, 2, bufferStart, numThisTime, false);
auto num = numThisTime;
while (--num >= 0)
{
if (numSamplesToSearch < 0)
--startSample;
bool matches = false;
auto index = (int) (startSample - bufferStart);
if (usesFloatingPointData)
{
const float sample1 = std::abs (((float*) tempBuffer[0]) [index]);
if (sample1 >= magnitudeRangeMinimum
&& sample1 <= magnitudeRangeMaximum)
{
matches = true;
}
else if (numChannels > 1)
{
const float sample2 = std::abs (((float*) tempBuffer[1]) [index]);
matches = (sample2 >= magnitudeRangeMinimum
&& sample2 <= magnitudeRangeMaximum);
}
}
else
{
const int sample1 = std::abs (tempBuffer[0] [index]);
if (sample1 >= intMagnitudeRangeMinimum
&& sample1 <= intMagnitudeRangeMaximum)
{
matches = true;
}
else if (numChannels > 1)
{
const int sample2 = std::abs (tempBuffer[1][index]);
matches = (sample2 >= intMagnitudeRangeMinimum
&& sample2 <= intMagnitudeRangeMaximum);
}
}
if (matches)
{
if (firstMatchPos < 0)
firstMatchPos = startSample;
if (++consecutive >= minimumConsecutiveSamples)
{
if (firstMatchPos < 0 || firstMatchPos >= lengthInSamples)
return -1;
return firstMatchPos;
}
}
else
{
consecutive = 0;
firstMatchPos = -1;
}
if (numSamplesToSearch > 0)
++startSample;
}
if (numSamplesToSearch > 0)
numSamplesToSearch -= numThisTime;
else
numSamplesToSearch += numThisTime;
}
return -1;
}
AudioChannelSet AudioFormatReader::getChannelLayout()
{
return AudioChannelSet::canonicalChannelSet (static_cast<int> (numChannels));
}
//==============================================================================
MemoryMappedAudioFormatReader::MemoryMappedAudioFormatReader (const File& f, const AudioFormatReader& reader,
int64 start, int64 length, int frameSize)
: AudioFormatReader (nullptr, reader.getFormatName()), file (f),
dataChunkStart (start), dataLength (length), bytesPerFrame (frameSize)
{
sampleRate = reader.sampleRate;
bitsPerSample = reader.bitsPerSample;
lengthInSamples = reader.lengthInSamples;
numChannels = reader.numChannels;
metadataValues = reader.metadataValues;
usesFloatingPointData = reader.usesFloatingPointData;
}
bool MemoryMappedAudioFormatReader::mapEntireFile()
{
return mapSectionOfFile (Range<int64> (0, lengthInSamples));
}
bool MemoryMappedAudioFormatReader::mapSectionOfFile (Range<int64> samplesToMap)
{
if (map == nullptr || samplesToMap != mappedSection)
{
map.reset();
const Range<int64> fileRange (sampleToFilePos (samplesToMap.getStart()),
sampleToFilePos (samplesToMap.getEnd()));
map.reset (new MemoryMappedFile (file, fileRange, MemoryMappedFile::readOnly));
if (map->getData() == nullptr)
map.reset();
else
mappedSection = Range<int64> (jmax ((int64) 0, filePosToSample (map->getRange().getStart() + (bytesPerFrame - 1))),
jmin (lengthInSamples, filePosToSample (map->getRange().getEnd())));
}
return map != nullptr;
}
static int memoryReadDummyVariable; // used to force the compiler not to optimise-away the read operation
void MemoryMappedAudioFormatReader::touchSample (int64 sample) const noexcept
{
if (map != nullptr && mappedSection.contains (sample))
memoryReadDummyVariable += *(char*) sampleToPointer (sample);
else
jassertfalse; // you must make sure that the window contains all the samples you're going to attempt to read.
}
} // namespace juce
|