| 555 | } |
| 556 | |
| 557 | inline void AudioFileData::resample (double targetSampleRate, uint64_t maxNumFrames) |
| 558 | { |
| 559 | if (targetSampleRate != 0 && std::abs (targetSampleRate - sampleRate) > 1.0) |
| 560 | { |
| 561 | static constexpr double maxRatio = 64.0; |
| 562 | |
| 563 | if (targetSampleRate > sampleRate * maxRatio || targetSampleRate < sampleRate / maxRatio) |
| 564 | throw std::runtime_error ("Resampling ratio is out-of-range"); |
| 565 | |
| 566 | auto ratio = targetSampleRate / sampleRate; |
| 567 | auto newFrameCount = static_cast<uint64_t> (ratio * static_cast<double> (frames.getNumFrames()) + 0.5); |
| 568 | |
| 569 | if (maxNumFrames != 0 && newFrameCount > maxNumFrames) |
| 570 | throw std::runtime_error ("File too long"); |
| 571 | |
| 572 | if (newFrameCount != frames.getNumFrames()) |
| 573 | { |
| 574 | using BufferType = decltype (frames); |
| 575 | BufferType resampled (frames.getNumChannels(), static_cast<uint32_t> (newFrameCount), false); |
| 576 | choc::interpolation::sincInterpolate<BufferType&, BufferType, 10> (resampled, frames); |
| 577 | frames = std::move (resampled); |
| 578 | sampleRate = targetSampleRate; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | inline AudioFileData AudioFileFormatList::loadFileContent (std::shared_ptr<std::istream> fileReader, |
| 584 | double targetSampleRate, |
no test coverage detected