6#include "dependencies/choc/platform/choc_DisableAllWarnings.h"
7#include "dependencies/dr_libs/dr_wav.h"
8#include "dependencies/choc/platform/choc_ReenableAllWarnings.h"
11#include "signals/hart_signal.hpp"
23template<
typename SampleType>
36 m_filePath (filePath),
41 drwav_uint64 numFrames;
42 unsigned int numChannels;
43 unsigned int wavSampleRateHz;
45 float* pcmFrames = drwav_open_file_and_read_pcm_frames_f32 (
53 if (pcmFrames ==
nullptr)
56 m_wavFrames = std::make_shared<
AudioBuffer<
float>> (numChannels, numFrames);
58 for (size_t frame = 0; frame < numFrames; ++frame)
59 for (size_t channel = 0; channel < numChannels; ++channel)
60 (*m_wavFrames)
[channel
][frame] = pcmFrames[frame * numChannels + channel];
62 drwav_free (pcmFrames,
nullptr);
64 m_wavSampleRateHz =
static_cast<
double> (wavSampleRateHz);
65 m_wavNumChannels =
static_cast<size_t> (numChannels);
74 return numChannels <= m_wavNumChannels;
77 void prepare (
double sampleRateHz, size_t numOutputChannels, size_t )
override
80 if (numOutputChannels != m_wavNumChannels)
91 const size_t numFrames = output.getNumFrames();
92 size_t frameInOutputBuffer = 0;
93 size_t frameInWavBuffer = m_wavOffsetFrames;
95 while (m_wavOffsetFrames < m_wavFrames->
getNumFrames() && frameInOutputBuffer < numFrames)
97 for (size_t channel = 0; channel < m_wavNumChannels; ++channel)
98 output[channel][frameInOutputBuffer] = (*m_wavFrames)
[channel
][frameInWavBuffer];
100 ++frameInOutputBuffer;
108 while (frameInOutputBuffer < numFrames)
112 for (size_t channel = 0; channel < m_wavNumChannels; ++channel)
113 output[channel][frameInOutputBuffer] = (SampleType) 0;
115 ++frameInOutputBuffer;
121 m_wavOffsetFrames = 0;
126 stream <<
"WavFile (\"" << m_filePath << (m_loop ==
Loop::yes ?
"\", Loop::yes)" :
"\", Loop::no)");
130 const std::string m_filePath;
132 size_t m_wavNumChannels;
133 double m_wavSampleRateHz;
134 size_t m_wavOffsetFrames = 0;
Container for audio data.
SampleType * operator[](size_t channel)
Get a raw pointer to a specific channel's mutable audio data.
size_t getNumFrames() const
Get number of frames (samples)
Thrown when a numbers of channels is mismatched.
Thrown when some I/O operation fails.
Thrown when some parameter has an unsupported value.
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 channels.
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_THROW_OR_RETURN_VOID(ExceptionType, message)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message and returns otherwise.
#define hassert(condition)
Triggers a HartAssertException if the condition is false
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.
Loop
Helper values for something that could loop, like a Signal.
#define HART_SIGNAL_DECLARE_ALIASES_FOR(ClassName)