Audacity 3.2.0
WavMirAudioReader.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 WaveMirAudioReader.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
11#include "WavMirAudioReader.h"
12#include "AudioFileIO.h"
13#include "AudioFileInfo.h"
14
15#include <cassert>
16#include <exception>
17#include <stdexcept>
18
19namespace MIR
20{
22 const std::string& filename, std::optional<double> timeLimit)
23{
24 AudioFileInfo info;
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);
28
29 const_cast<double&>(mSampleRate) = info.sampleRate;
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;
39 else
41 samples[0].begin(), samples[0].begin() + numFrames,
42 mutableSamples.begin());
43}
44
46{
47 return mSampleRate;
48}
49
51{
52 return mSamples.size();
53}
54
56 float* buffer, long long start, size_t numFrames) const
57{
58 assert(start >= 0);
59 assert(start + numFrames <= mSamples.size());
61 mSamples.begin() + start, mSamples.begin() + start + numFrames, buffer);
62}
63} // namespace MIR
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
Definition: StringUtils.h:101
void copy(const T *src, T *dst, int32_t n)
Definition: VectorOps.h:40