9#include "signals/hart_signal.hpp"
22template<
typename SampleType>
41 m_filePath (filePath),
46 drwav_uint64 numFrames;
47 unsigned int numChannels;
48 unsigned int wavSampleRateHz;
50 float* pcmFrames = drwav_open_file_and_read_pcm_frames_f32 (
58 if (pcmFrames ==
nullptr)
61 m_wavFrames = std::make_shared<
AudioBuffer<
float>> (numChannels, numFrames);
63 for (size_t frame = 0; frame < numFrames; ++frame)
64 for (size_t channel = 0; channel < numChannels; ++channel)
65 (*m_wavFrames)
[channel
][frame] = pcmFrames[frame * numChannels + channel];
67 drwav_free (pcmFrames,
nullptr);
69 m_wavSampleRateHz =
static_cast<
double> (wavSampleRateHz);
70 m_wavNumChannels =
static_cast<size_t> (numChannels);
79 return numChannels <= m_wavNumChannels;
82 void prepare (
double sampleRateHz, size_t numOutputChannels, size_t )
override
85 if (numOutputChannels != m_wavNumChannels)
96 const size_t numFrames = output.getNumFrames();
97 size_t frameInOutputBuffer = 0;
98 size_t frameInWavBuffer = m_wavOffsetFrames;
100 while (m_wavOffsetFrames < m_wavFrames->
getNumFrames() && frameInOutputBuffer < numFrames)
102 for (size_t channel = 0; channel < m_wavNumChannels; ++channel)
103 output[channel][frameInOutputBuffer] = (*m_wavFrames)
[channel
][frameInWavBuffer];
105 ++frameInOutputBuffer;
109 if (m_loop ==
Loop::yes)
113 while (frameInOutputBuffer < numFrames)
117 for (size_t channel = 0; channel < m_wavNumChannels; ++channel)
118 output[channel][frameInOutputBuffer] = (SampleType) 0;
120 ++frameInOutputBuffer;
126 m_wavOffsetFrames = 0;
131 stream <<
"WavFile (\"" << m_filePath << (m_loop ==
Loop::yes ?
"\", Loop::yes)" :
"\", Loop::no)");
137 const std::string m_filePath;
139 size_t m_wavNumChannels;
140 double m_wavSampleRateHz;
141 size_t m_wavOffsetFrames = 0;
SampleType * operator[](size_t channel)
size_t getNumFrames() const
Produces audio from a wav file.
void renderNextBlock(AudioBuffer< SampleType > &output) override
Renders next block audio for the signal.
void prepare(double sampleRateHz, size_t numOutputChannels, size_t) override
Prepare the signal for rendering.
void represent(std::ostream &stream) const override
Makes a text representation of this Signal for test failure outputs.
bool supportsNumChannels(size_t numChannels) const override
Tells the host whether this Signal is capable of generating audio for a certain amount of cchannels.
WavFile(const std::string &filePath, Loop loop=Loop::no)
Creates a Signal that produces audio from a wav file.
void reset() override
Resets the Signal to initial state.
#define HART_SIGNAL_DEFINE_COPY_AND_MOVE(ClassName)
Defines hart::Signal::copy() and hart::Signal::move() methods.
static SampleType floatsNotEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
static std::string toAbsolutePath(const std::string &path)
Converts path to absolute, if it's relative @deials Relative paths are resolved based on a provided -...
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
#define hassert(condition)
#define HART_SIGNAL_DECLARE_ALIASES_FOR(ClassName)