muzgen/lib/Sine.cpp

45 lines
1006 B
C++
Raw Normal View History

#include "Sine.hpp"
namespace muz
{
/*explicit*/ Sine::Sine(AudioConf const& conf,
std::unique_ptr<Signal> freq)
: m_conf { conf }
, m_out_freq {std::vector<float> (m_conf.channels(), 0.0f)}
, m_freq { std::move(freq) }
{
for (size_t i=0; i<static_cast<size_t>(m_conf.channels()); i++)
{
m_phases.push_back(0.0f);
}
}
/*virtual*/ Sine::~Sine()
{
}
void Sine::next(std::vector<float>& out) /*override*/
{
assert(m_freq);
m_freq->next(m_out_freq);
if (m_out_freq.size() != m_phases.size())
{
throw signal_error {"cannot generate sine: channel number mismatch"};
}
for (size_t i=0; i<static_cast<size_t>(m_conf.channels()); i++)
{
float const value = std::sin(m_phases[i]);
m_phases[i] += 2 * M_PI * m_out_freq[i]
/ static_cast<float>(m_conf.samplerate());
2024-02-01 07:11:02 +00:00
m_phases[i] = std::fmod(m_phases[i], 2.0f * M_PI);
out[i] = value;
}
}
}