2020-10-01 13:46:12 +02:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <module.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <signal_path/signal_path.h>
|
2020-10-04 02:56:02 +02:00
|
|
|
#include <wavreader.h>
|
|
|
|
#include <core.h>
|
2021-04-29 22:04:20 +02:00
|
|
|
#include <options.h>
|
2021-02-07 23:47:17 +01:00
|
|
|
#include <gui/widgets/file_select.h>
|
2020-10-01 13:46:12 +02:00
|
|
|
|
|
|
|
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
|
|
|
|
2021-02-07 23:47:17 +01:00
|
|
|
SDRPP_MOD_INFO {
|
|
|
|
/* Name: */ "file_source",
|
|
|
|
/* Description: */ "Wav file source module for SDR++",
|
|
|
|
/* Author: */ "Ryzerth",
|
|
|
|
/* Version: */ 0, 1, 1,
|
|
|
|
/* Max instances */ 1
|
2020-10-01 13:46:12 +02:00
|
|
|
};
|
|
|
|
|
2021-04-29 22:04:20 +02:00
|
|
|
ConfigManager config;
|
2021-02-07 23:47:17 +01:00
|
|
|
|
|
|
|
class FileSourceModule : public ModuleManager::Instance {
|
2020-10-01 13:46:12 +02:00
|
|
|
public:
|
2021-04-29 20:43:17 +02:00
|
|
|
FileSourceModule(std::string name) : fileSelect("", {"Wav IQ Files (*.wav)", "*.wav", "All Files", "*"}) {
|
2020-10-01 13:46:12 +02:00
|
|
|
this->name = name;
|
|
|
|
|
2021-04-29 22:04:20 +02:00
|
|
|
config.aquire();
|
|
|
|
fileSelect.setPath(config.conf["path"], true);
|
|
|
|
config.release();
|
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
handler.ctx = this;
|
|
|
|
handler.selectHandler = menuSelected;
|
|
|
|
handler.deselectHandler = menuDeselected;
|
|
|
|
handler.menuHandler = menuHandler;
|
|
|
|
handler.startHandler = start;
|
|
|
|
handler.stopHandler = stop;
|
|
|
|
handler.tuneHandler = tune;
|
|
|
|
handler.stream = &stream;
|
|
|
|
sigpath::sourceManager.registerSource("File", &handler);
|
2020-10-04 02:56:02 +02:00
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
spdlog::info("FileSourceModule '{0}': Instance created!", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
~FileSourceModule() {
|
2021-05-05 04:31:37 +02:00
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
spdlog::info("FileSourceModule '{0}': Instance deleted!", name);
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:47:17 +01:00
|
|
|
void enable() {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable() {
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
private:
|
|
|
|
static void menuSelected(void* ctx) {
|
|
|
|
FileSourceModule* _this = (FileSourceModule*)ctx;
|
2021-02-07 23:47:17 +01:00
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
2020-10-01 13:46:12 +02:00
|
|
|
spdlog::info("FileSourceModule '{0}': Menu Select!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void menuDeselected(void* ctx) {
|
|
|
|
FileSourceModule* _this = (FileSourceModule*)ctx;
|
|
|
|
spdlog::info("FileSourceModule '{0}': Menu Deselect!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start(void* ctx) {
|
|
|
|
FileSourceModule* _this = (FileSourceModule*)ctx;
|
2021-02-07 23:47:17 +01:00
|
|
|
if (_this->running) { return; }
|
|
|
|
if (_this->reader == NULL) { return; }
|
|
|
|
_this->running = true;
|
2021-02-26 16:52:54 +01:00
|
|
|
_this->workerThread = _this->float32Mode ? std::thread(floatWorker, _this) : std::thread(worker, _this);
|
2020-10-01 13:46:12 +02:00
|
|
|
spdlog::info("FileSourceModule '{0}': Start!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stop(void* ctx) {
|
|
|
|
FileSourceModule* _this = (FileSourceModule*)ctx;
|
2021-02-07 23:47:17 +01:00
|
|
|
if (!_this->running) { return; }
|
|
|
|
if (_this->reader == NULL) { return; }
|
2020-10-04 02:56:02 +02:00
|
|
|
_this->stream.stopWriter();
|
|
|
|
_this->workerThread.join();
|
|
|
|
_this->stream.clearWriteStop();
|
2021-02-07 23:47:17 +01:00
|
|
|
_this->running = false;
|
|
|
|
_this->reader->rewind();
|
2020-10-01 13:46:12 +02:00
|
|
|
spdlog::info("FileSourceModule '{0}': Stop!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tune(double freq, void* ctx) {
|
|
|
|
FileSourceModule* _this = (FileSourceModule*)ctx;
|
|
|
|
spdlog::info("FileSourceModule '{0}': Tune: {1}!", _this->name, freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void menuHandler(void* ctx) {
|
|
|
|
FileSourceModule* _this = (FileSourceModule*)ctx;
|
2021-02-07 23:47:17 +01:00
|
|
|
|
|
|
|
if (_this->fileSelect.render("##file_source_" + _this->name)) {
|
|
|
|
if (_this->fileSelect.pathIsValid()) {
|
|
|
|
if (_this->reader != NULL) {
|
|
|
|
_this->reader->close();
|
|
|
|
delete _this->reader;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
_this->reader = new WavReader(_this->fileSelect.path);
|
2021-02-26 16:52:54 +01:00
|
|
|
_this->sampleRate = _this->reader->getSampleRate();
|
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
2021-02-07 23:47:17 +01:00
|
|
|
}
|
|
|
|
catch (std::exception e) {}
|
2021-04-29 22:04:20 +02:00
|
|
|
config.aquire();
|
|
|
|
config.conf["path"] = _this->fileSelect.path;
|
|
|
|
config.release(true);
|
2021-02-07 23:47:17 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-26 16:52:54 +01:00
|
|
|
|
|
|
|
ImGui::Checkbox("Float32 Mode##_file_source", &_this->float32Mode);
|
2020-10-01 13:46:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 02:56:02 +02:00
|
|
|
static void worker(void* ctx) {
|
|
|
|
FileSourceModule* _this = (FileSourceModule*)ctx;
|
|
|
|
double sampleRate = _this->reader->getSampleRate();
|
2020-12-31 14:26:12 +01:00
|
|
|
int blockSize = sampleRate / 200.0f;
|
2020-10-04 02:56:02 +02:00
|
|
|
int16_t* inBuf = new int16_t[blockSize * 2];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
_this->reader->readSamples(inBuf, blockSize * 2 * sizeof(int16_t));
|
2021-05-03 04:20:48 +02:00
|
|
|
volk_16i_s32f_convert_32f((float*)_this->stream.writeBuf, inBuf, 32768.0f, blockSize * 2);
|
2021-02-07 23:47:17 +01:00
|
|
|
if (!_this->stream.swap(blockSize)) { break; };
|
2020-10-04 02:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] inBuf;
|
|
|
|
}
|
|
|
|
|
2021-02-26 16:52:54 +01:00
|
|
|
static void floatWorker(void* ctx) {
|
|
|
|
FileSourceModule* _this = (FileSourceModule*)ctx;
|
|
|
|
double sampleRate = _this->reader->getSampleRate();
|
|
|
|
int blockSize = sampleRate / 200.0f;
|
|
|
|
dsp::complex_t* inBuf = new dsp::complex_t[blockSize];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
_this->reader->readSamples(_this->stream.writeBuf, blockSize * sizeof(dsp::complex_t));
|
|
|
|
if (!_this->stream.swap(blockSize)) { break; };
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] inBuf;
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:47:17 +01:00
|
|
|
FileSelect fileSelect;
|
2020-10-01 13:46:12 +02:00
|
|
|
std::string name;
|
|
|
|
dsp::stream<dsp::complex_t> stream;
|
|
|
|
SourceManager::SourceHandler handler;
|
2021-02-07 23:47:17 +01:00
|
|
|
WavReader* reader = NULL;
|
|
|
|
bool running = false;
|
|
|
|
bool enabled = true;
|
|
|
|
float sampleRate = 48000;
|
2020-10-04 02:56:02 +02:00
|
|
|
std::thread workerThread;
|
2021-02-26 16:52:54 +01:00
|
|
|
|
|
|
|
bool float32Mode = false;
|
2020-10-01 13:46:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
MOD_EXPORT void _INIT_() {
|
2021-04-29 22:04:20 +02:00
|
|
|
json def = json({});
|
|
|
|
def["path"] = "";
|
|
|
|
config.setPath(options::opts.root + "/file_source_config.json");
|
|
|
|
config.load(def);
|
|
|
|
config.enableAutoSave();
|
2020-10-01 13:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void* _CREATE_INSTANCE_(std::string name) {
|
|
|
|
return new FileSourceModule(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _DELETE_INSTANCE_(void* instance) {
|
|
|
|
delete (FileSourceModule*)instance;
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:47:17 +01:00
|
|
|
MOD_EXPORT void _END_() {
|
2021-04-29 22:04:20 +02:00
|
|
|
config.disableAutoSave();
|
|
|
|
config.save();
|
2020-10-01 13:46:12 +02:00
|
|
|
}
|