12#include "AudioFileIO.h"
13#include "AudioFileInfo.h"
22 const std::string& filename, std::optional<double> timeLimit)
25 std::vector<std::vector<float>> samples;
26 if (!AudioFileIO::Read(filename, samples, info))
27 throw std::runtime_error(std::string {
"Failed to read " } + filename);
30 const auto limit = timeLimit.has_value() ?
31 static_cast<long long>(*timeLimit * info.sampleRate) :
32 std::numeric_limits<long long>::max();
33 auto& mutableSamples =
const_cast<std::vector<float>&
>(
mSamples);
34 const auto numFrames = std::min<long long>(info.numFrames, limit);
35 mutableSamples.resize(numFrames);
36 if (info.numChannels == 2)
37 for (
size_t i = 0; i < numFrames; ++i)
38 mutableSamples[i] = (samples[0][i] + samples[1][i]) / 2.f;
41 samples[0].
begin(), samples[0].
begin() + numFrames,
42 mutableSamples.begin());
56 float* buffer,
long long start,
size_t numFrames)
const
59 assert(start + numFrames <=
mSamples.size());
long long GetNumSamples() const override
WavMirAudioReader(const std::string &filename, std::optional< double > timeLimit={})
const std::vector< float > mSamples
double GetSampleRate() const override
void ReadFloats(float *buffer, long long start, size_t numFrames) const override
const char * begin(const char *str) noexcept
void copy(const T *src, T *dst, int32_t n)