mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-10-12 17:00:04 +02:00
new dsp
This commit is contained in:
@@ -1,92 +1,75 @@
|
||||
#pragma once
|
||||
#include <thread>
|
||||
#include <dsp/stream.h>
|
||||
#include <dsp/types.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <dsp/block.h>
|
||||
|
||||
namespace dsp {
|
||||
class SineSource {
|
||||
class SineSource : public generic_block<SineSource> {
|
||||
public:
|
||||
SineSource() {
|
||||
|
||||
}
|
||||
SineSource() {}
|
||||
|
||||
SineSource(float frequency, long sampleRate, int blockSize) : output(blockSize * 2) {
|
||||
SineSource(int blockSize, float sampleRate, float freq) { init(blockSize, sampleRate, freq); }
|
||||
|
||||
~SineSource() { generic_block<SineSource>::stop(); }
|
||||
|
||||
void init(int blockSize, float sampleRate, float freq) {
|
||||
_blockSize = blockSize;
|
||||
_sampleRate = sampleRate;
|
||||
_frequency = frequency;
|
||||
_phasorSpeed = (2 * 3.1415926535 * frequency) / sampleRate;
|
||||
_phase = 0;
|
||||
}
|
||||
|
||||
void init(float frequency, long sampleRate, int blockSize) {
|
||||
output.init(blockSize * 2);
|
||||
_sampleRate = sampleRate;
|
||||
_blockSize = blockSize;
|
||||
_frequency = frequency;
|
||||
_phasorSpeed = (2 * 3.1415926535 * frequency) / sampleRate;
|
||||
_phase = 0;
|
||||
}
|
||||
|
||||
void start() {
|
||||
if (running) {
|
||||
return;
|
||||
_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);
|
||||
}
|
||||
_workerThread = std::thread(_worker, this);
|
||||
running = true;
|
||||
}
|
||||
|
||||
void stop() {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
output.stopWriter();
|
||||
_workerThread.join();
|
||||
output.clearWriteStop();
|
||||
running = false;
|
||||
}
|
||||
|
||||
void setFrequency(float frequency) {
|
||||
_frequency = frequency;
|
||||
_phasorSpeed = (2 * 3.1415926535 * frequency) / _sampleRate;
|
||||
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);
|
||||
}
|
||||
|
||||
void setBlockSize(int blockSize) {
|
||||
if (running) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lck(generic_block<SineSource>::ctrlMtx);
|
||||
generic_block<SineSource>::tempStop();
|
||||
_blockSize = blockSize;
|
||||
output.setMaxLatency(blockSize * 2);
|
||||
generic_block<SineSource>::tempStart();
|
||||
}
|
||||
|
||||
int getBlockSize() {
|
||||
return _blockSize;
|
||||
}
|
||||
|
||||
void setSampleRate(float sampleRate) {
|
||||
// No need to restart
|
||||
_sampleRate = sampleRate;
|
||||
_phasorSpeed = (2 * 3.1415926535 * _frequency) / sampleRate;
|
||||
phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
|
||||
}
|
||||
|
||||
stream<complex_t> output;
|
||||
float getSampleRate() {
|
||||
return _sampleRate;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
float getFrequency() {
|
||||
return _freq;
|
||||
}
|
||||
|
||||
int run() {
|
||||
if (out.aquire() < 0) { return -1; }
|
||||
volk_32fc_s32fc_x2_rotator_32fc((lv_32fc_t*)out.data, zeroPhase, phaseDelta, &phase, _blockSize);
|
||||
out.write(_blockSize);
|
||||
return _blockSize;
|
||||
}
|
||||
|
||||
stream<complex_t> out;
|
||||
|
||||
private:
|
||||
static void _worker(SineSource* _this) {
|
||||
complex_t* outBuf = new complex_t[_this->_blockSize];
|
||||
while (true) {
|
||||
for (int i = 0; i < _this->_blockSize; i++) {
|
||||
_this->_phase += _this->_phasorSpeed;
|
||||
outBuf[i].i = sin(_this->_phase);
|
||||
outBuf[i].q = cos(_this->_phase);
|
||||
_this->_phase = fmodf(_this->_phase, 2.0f * 3.1415926535); // TODO: Get a more efficient generator
|
||||
}
|
||||
if (_this->output.write(outBuf, _this->_blockSize) < 0) { break; };
|
||||
}
|
||||
delete[] outBuf;
|
||||
}
|
||||
|
||||
int _blockSize;
|
||||
float _phasorSpeed;
|
||||
float _phase;
|
||||
long _sampleRate;
|
||||
float _frequency;
|
||||
std::thread _workerThread;
|
||||
bool running = false;
|
||||
float _sampleRate;
|
||||
float _freq;
|
||||
lv_32fc_t phaseDelta;
|
||||
lv_32fc_t phase;
|
||||
lv_32fc_t* zeroPhase;
|
||||
|
||||
};
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user