Initial commit - basic sine generator

This commit is contained in:
2020-01-06 14:04:34 -08:00
commit b0a7cd9fae
4 changed files with 118 additions and 0 deletions

28
src/SineSynth.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "SineSynth.h"
#include <iostream>
Generator::Generator(uint32_t _sampleRate, float _frequency, float _amplitude) : sampleRate(_sampleRate), frequency(_frequency), amplitude(_amplitude)
{
}
Generator::~Generator()
{
}
SineGenerator::SineGenerator(uint32_t _sampleRate, float _frequency, float _amplitude = 1) : Generator(_sampleRate, _frequency, _amplitude), tick(0) {
phaseIncr = 2 * M_PI * frequency / sampleRate;
std::cout << "Constructed SineGenerator with freq " << frequency << " phaseIncr " << phaseIncr << std::endl;
}
SineGenerator::~SineGenerator() {
}
void SineGenerator::getSamples(uint16_t nSamples, float *buf) {
for (;nSamples>0;nSamples--) {
std::cout << "Generating tick " << tick << std::endl;
*buf++ = cos(phaseIncr * tick++) * amplitude;
}
}

23
src/main.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <iostream>
#include <pulse/simple.h>
#include "SineSynth.h"
int main(int argc, char *argv[]) {
SineGenerator sg(44100, 440.0f, 0.1);
static const pa_sample_spec ss = {
.format = PA_SAMPLE_FLOAT32,
.rate = 44100,
.channels = 1
};
pa_simple *s = NULL;
int error;
s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error);
float buf[1024];
for (;;) {
sg.getSamples(1024, buf);
pa_simple_write(s, buf, sizeof(buf), &error);
}
}