#include "AudioSynth.h"

#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
#include <limits>

AudioSynth::AudioSynth()
{

}

AudioSamplePtr AudioSynth::getConstant(unsigned amplitude, unsigned duration) const
{
    auto sample = AudioSample::Create();
    auto num_samples = duration * sample->getSampleRate();
    sample->setChannelData(std::vector<short>(num_samples, amplitude), 0);
    return sample;
}

AudioSamplePtr AudioSynth::getSineWave(double freq, unsigned duration) const
{
    auto sample = AudioSample::Create();
    const auto sample_rate = sample->getSampleRate();
    const auto num_samples = sample_rate*duration;

    std::vector<short> data(num_samples, 0);

    const double tick_duration = 1.0/sample_rate;
    const double pi_2 = 2.0 * M_PI;
    constexpr short max_short = std::numeric_limits<short>::max();
    for(unsigned idx=0; idx<num_samples; idx++)
    {
        const auto t = double(idx)*tick_duration;
        data[idx] = max_short*static_cast<short>(std::sin(pi_2*freq*t));
    }
    sample->setChannelData(data, 0);
    return sample;
}
