2020-06-22 16:45:57 +02:00
|
|
|
#pragma once
|
2020-11-02 03:57:44 +01:00
|
|
|
#include <dsp/block.h>
|
2020-06-22 16:45:57 +02:00
|
|
|
|
|
|
|
namespace dsp {
|
2020-11-02 03:57:44 +01:00
|
|
|
class SineSource : public generic_block<SineSource> {
|
2020-06-22 16:45:57 +02:00
|
|
|
public:
|
2020-11-02 03:57:44 +01:00
|
|
|
SineSource() {}
|
|
|
|
|
|
|
|
SineSource(int blockSize, float sampleRate, float freq) { init(blockSize, sampleRate, freq); }
|
|
|
|
|
|
|
|
~SineSource() { generic_block<SineSource>::stop(); }
|
2020-06-22 16:45:57 +02:00
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
void init(int blockSize, float sampleRate, float freq) {
|
2020-06-22 16:45:57 +02:00
|
|
|
_blockSize = blockSize;
|
|
|
|
_sampleRate = sampleRate;
|
2020-11-02 03:57:44 +01:00
|
|
|
_freq = freq;
|
|
|
|
zeroPhase = (lv_32fc_t*)volk_malloc(STREAM_BUFFER_SIZE * sizeof(lv_32fc_t), volk_get_alignment());
|
|
|
|
for (int i = 0; i < STREAM_BUFFER_SIZE; i++) {
|
|
|
|
zeroPhase[i] = lv_cmake(1.0f, 0.0f);
|
|
|
|
}
|
|
|
|
phase = lv_cmake(1.0f, 0.0f);
|
|
|
|
phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
|
|
|
|
generic_block<SineSource>::registerOutput(&out);
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
void setBlockSize(int blockSize) {
|
|
|
|
std::lock_guard<std::mutex> lck(generic_block<SineSource>::ctrlMtx);
|
|
|
|
generic_block<SineSource>::tempStop();
|
2020-06-22 16:45:57 +02:00
|
|
|
_blockSize = blockSize;
|
2020-11-02 03:57:44 +01:00
|
|
|
generic_block<SineSource>::tempStart();
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
int getBlockSize() {
|
|
|
|
return _blockSize;
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
void setSampleRate(float sampleRate) {
|
|
|
|
// No need to restart
|
|
|
|
_sampleRate = sampleRate;
|
|
|
|
phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
float getSampleRate() {
|
|
|
|
return _sampleRate;
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
void setFrequency(float freq) {
|
|
|
|
// No need to restart
|
|
|
|
_freq = freq;
|
|
|
|
phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
|
2020-07-19 15:59:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
float getFrequency() {
|
|
|
|
return _freq;
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
int run() {
|
2020-12-25 16:58:07 +01:00
|
|
|
volk_32fc_s32fc_x2_rotator_32fc((lv_32fc_t*)out.writeBuf, zeroPhase, phaseDelta, &phase, _blockSize);
|
|
|
|
if(!out.swap(_blockSize)) { return -1; }
|
2020-11-02 03:57:44 +01:00
|
|
|
return _blockSize;
|
2020-06-22 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 03:57:44 +01:00
|
|
|
stream<complex_t> out;
|
|
|
|
|
|
|
|
private:
|
2020-06-22 16:45:57 +02:00
|
|
|
int _blockSize;
|
2020-11-02 03:57:44 +01:00
|
|
|
float _sampleRate;
|
|
|
|
float _freq;
|
|
|
|
lv_32fc_t phaseDelta;
|
|
|
|
lv_32fc_t phase;
|
|
|
|
lv_32fc_t* zeroPhase;
|
|
|
|
|
2020-06-22 16:45:57 +02:00
|
|
|
};
|
2020-11-02 03:57:44 +01:00
|
|
|
}
|