#include "AudioSynth.h"
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
#include <limits>
#include <iostream>

AudioSynth::AudioSynth()
{

}

AudioSamplePtr AudioSynth::GetConstant(unsigned amplitude, unsigned duration)
{
    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)
{
    auto sample = AudioSample::Create();
    const auto sample_rate = sample->GetSampleRate();
    const auto num_samples = sample_rate*duration;

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

    double tick_duration = 1.0/sample_rate;
    double pi_2 = 2.0 * M_PI;
    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*std::sin(pi_2*freq*t);
    }
    sample->SetChannelData(data, 0);
    return sample;
}
