Files
DrumMachine/synthisizer.cpp

27 lines
770 B
C++
Raw Permalink Normal View History

2020-04-27 22:59:30 +02:00
#include "synthisizer.h"
#include <cmath>
2022-12-27 21:19:21 +01:00
#include "audioformat.h"
2020-04-27 22:59:30 +02:00
constexpr double pi = std::acos(-1);
void Synthisizer::writeSamples(frame_t *begin, frame_t *end)
{
2022-12-27 10:15:13 +01:00
const auto volume = m_volume;
2020-04-29 19:48:41 +02:00
const auto frequency = m_frequency;
2022-12-27 10:15:13 +01:00
2020-04-29 19:48:41 +02:00
if (frequency)
2020-04-27 22:59:30 +02:00
std::transform(begin, end, begin, [&](frame_t frame){
std::transform(std::cbegin(frame), std::cend(frame), std::begin(frame),
2022-12-27 10:15:13 +01:00
[value=std::sin(m_phase),&volume](const sample_t &sample) { return (sample + value) * volume; });
2020-04-27 22:59:30 +02:00
2020-04-29 19:48:41 +02:00
m_phase += pi*2./frameRate*m_actualFrequency;
2020-04-27 22:59:30 +02:00
if (m_phase >= pi*2.)
m_phase -= pi*2.;
return frame;
});
2020-04-29 19:48:41 +02:00
m_actualFrequency = float(m_actualFrequency+m_frequency)/2.f;
2020-04-27 22:59:30 +02:00
}