Initial commit - basic sine generator
This commit is contained in:
28
src/SineSynth.cpp
Normal file
28
src/SineSynth.cpp
Normal 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
23
src/main.cpp
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user