2020-08-18 00:56:51 +02:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <module.h>
|
|
|
|
#include <watcher.h>
|
|
|
|
#include <wav.h>
|
|
|
|
#include <dsp/types.h>
|
|
|
|
#include <dsp/stream.h>
|
|
|
|
#include <thread>
|
|
|
|
#include <ctime>
|
2020-10-01 13:46:12 +02:00
|
|
|
#include <gui/gui.h>
|
2020-10-22 12:53:46 +02:00
|
|
|
#include <signal_path/signal_path.h>
|
|
|
|
#include <config.h>
|
2020-08-18 00:56:51 +02:00
|
|
|
|
|
|
|
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
// TODO: Fix this and finish module
|
2020-08-18 00:56:51 +02:00
|
|
|
|
|
|
|
std::string genFileName(std::string prefix) {
|
|
|
|
time_t now = time(0);
|
|
|
|
tm *ltm = localtime(&now);
|
|
|
|
char buf[1024];
|
|
|
|
sprintf(buf, "%02d-%02d-%02d_%02d-%02d-%02d.wav", ltm->tm_hour, ltm->tm_min, ltm->tm_sec, ltm->tm_mday, ltm->tm_mon + 1, ltm->tm_year + 1900);
|
|
|
|
return prefix + buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamRemovedHandler(void* ctx) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-20 00:32:17 +02:00
|
|
|
void sampleRateChanged(void* ctx, double sampleRate, int blockSize) {
|
2020-08-18 00:56:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
class RecorderModule {
|
|
|
|
public:
|
|
|
|
RecorderModule(std::string name) {
|
|
|
|
this->name = name;
|
|
|
|
recording = false;
|
|
|
|
selectedStreamName = "";
|
|
|
|
selectedStreamId = 0;
|
|
|
|
lastNameList = "";
|
|
|
|
gui::menu.registerEntry(name, menuHandler, this);
|
|
|
|
}
|
2020-08-18 00:56:51 +02:00
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
~RecorderModule() {
|
2020-08-18 00:56:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
private:
|
|
|
|
static void menuHandler(void* ctx) {
|
|
|
|
RecorderModule* _this = (RecorderModule*)ctx;
|
|
|
|
float menuColumnWidth = ImGui::GetContentRegionAvailWidth();
|
2020-08-20 18:29:23 +02:00
|
|
|
|
2020-11-30 05:51:33 +01:00
|
|
|
std::vector<std::string> streamNames = sigpath::sinkManager.getStreamNames();
|
2020-10-01 13:46:12 +02:00
|
|
|
std::string nameList;
|
|
|
|
for (std::string name : streamNames) {
|
|
|
|
nameList += name;
|
|
|
|
nameList += '\0';
|
2020-08-20 18:29:23 +02:00
|
|
|
}
|
2020-10-01 13:46:12 +02:00
|
|
|
|
|
|
|
if (nameList == "") {
|
|
|
|
ImGui::Text("No audio stream available");
|
|
|
|
return;
|
2020-08-20 18:29:23 +02:00
|
|
|
}
|
2020-08-18 00:56:51 +02:00
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
if (_this->lastNameList != nameList) {
|
|
|
|
_this->lastNameList = nameList;
|
|
|
|
auto _nameIt = std::find(streamNames.begin(), streamNames.end(), _this->selectedStreamName);
|
|
|
|
if (_nameIt == streamNames.end()) {
|
|
|
|
// TODO: verify if there even is a stream
|
|
|
|
_this->selectedStreamId = 0;
|
|
|
|
_this->selectedStreamName = streamNames[_this->selectedStreamId];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_this->selectedStreamId = std::distance(streamNames.begin(), _nameIt);
|
|
|
|
_this->selectedStreamName = streamNames[_this->selectedStreamId];
|
|
|
|
}
|
2020-08-20 18:29:23 +02:00
|
|
|
}
|
2020-10-01 13:46:12 +02:00
|
|
|
|
2020-10-22 12:53:46 +02:00
|
|
|
ImGui::BeginGroup();
|
|
|
|
|
|
|
|
// TODO: Change VFO ref in signal path
|
2020-11-30 16:30:45 +01:00
|
|
|
// TODO: Add VFO record
|
|
|
|
ImGui::Columns(2, CONCAT("RecordModeColumns##_", _this->name), false);
|
2020-10-22 12:53:46 +02:00
|
|
|
if (ImGui::RadioButton(CONCAT("Baseband##_", _this->name), _this->recMode == 0) && _this->recMode != 0) {
|
|
|
|
_this->recMode = 0;
|
|
|
|
}
|
|
|
|
ImGui::NextColumn();
|
|
|
|
if (ImGui::RadioButton(CONCAT("Audio##_", _this->name), _this->recMode == 1) && _this->recMode != 1) {
|
|
|
|
_this->recMode = 1;
|
2020-10-01 13:46:12 +02:00
|
|
|
}
|
2020-10-22 12:53:46 +02:00
|
|
|
ImGui::Columns(1, CONCAT("EndRecordModeColumns##_", _this->name), false);
|
|
|
|
|
|
|
|
ImGui::EndGroup();
|
|
|
|
|
|
|
|
if (_this->recMode == 0) {
|
|
|
|
ImGui::PushItemWidth(menuColumnWidth);
|
|
|
|
if (!_this->recording) {
|
|
|
|
if (ImGui::Button("Record", ImVec2(menuColumnWidth, 0))) {
|
|
|
|
_this->samplesWritten = 0;
|
|
|
|
_this->sampleRate = sigpath::signalPath.getSampleRate();
|
|
|
|
_this->writer = new WavWriter(ROOT_DIR "/recordings/" + genFileName("baseband_"), 16, 2, _this->sampleRate);
|
2020-11-02 03:57:44 +01:00
|
|
|
_this->iqStream = new dsp::stream<dsp::complex_t>;
|
2020-10-22 12:53:46 +02:00
|
|
|
sigpath::signalPath.bindIQStream(_this->iqStream);
|
|
|
|
_this->workerThread = std::thread(_iqWriteWorker, _this);
|
|
|
|
_this->recording = true;
|
|
|
|
_this->startTime = time(0);
|
|
|
|
}
|
|
|
|
ImGui::TextColored(ImGui::GetStyleColorVec4(ImGuiCol_Text), "Idle --:--:--");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ImGui::Button("Stop", ImVec2(menuColumnWidth, 0))) {
|
|
|
|
_this->iqStream->stopReader();
|
|
|
|
_this->workerThread.join();
|
|
|
|
_this->iqStream->clearReadStop();
|
|
|
|
sigpath::signalPath.unbindIQStream(_this->iqStream);
|
|
|
|
_this->writer->close();
|
|
|
|
delete _this->writer;
|
|
|
|
_this->recording = false;
|
|
|
|
}
|
|
|
|
uint64_t seconds = _this->samplesWritten / (uint64_t)_this->sampleRate;
|
|
|
|
time_t diff = seconds;
|
|
|
|
tm *dtm = gmtime(&diff);
|
|
|
|
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Recording %02d:%02d:%02d", dtm->tm_hour, dtm->tm_min, dtm->tm_sec);
|
2020-10-01 13:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-22 12:53:46 +02:00
|
|
|
else if (_this->recMode == 1) {
|
|
|
|
ImGui::PushItemWidth(menuColumnWidth);
|
|
|
|
if (!_this->recording) {
|
|
|
|
if (ImGui::Combo(CONCAT("##_strea_select_", _this->name), &_this->selectedStreamId, nameList.c_str())) {
|
|
|
|
_this->selectedStreamName = streamNames[_this->selectedStreamId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.44f, 0.44f, 0.44f, 0.15f));
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0.20f, 0.21f, 0.22f, 0.30f));
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.00f, 1.00f, 1.00f, 0.65f));
|
|
|
|
ImGui::Combo(CONCAT("##_strea_select_", _this->name), &_this->selectedStreamId, nameList.c_str());
|
|
|
|
ImGui::PopItemFlag();
|
|
|
|
ImGui::PopStyleColor(3);
|
|
|
|
}
|
|
|
|
if (!_this->recording) {
|
|
|
|
if (ImGui::Button("Record", ImVec2(menuColumnWidth, 0))) {
|
|
|
|
_this->samplesWritten = 0;
|
2020-11-30 05:51:33 +01:00
|
|
|
_this->sampleRate = sigpath::sinkManager.getStreamSampleRate(_this->selectedStreamName);
|
|
|
|
_this->writer = new WavWriter(ROOT_DIR "/recordings/" + genFileName("audio_"), 16, 2, _this->sampleRate);
|
|
|
|
_this->audioStream = sigpath::sinkManager.bindStream(_this->selectedStreamName);
|
2020-10-22 12:53:46 +02:00
|
|
|
_this->workerThread = std::thread(_audioWriteWorker, _this);
|
|
|
|
_this->recording = true;
|
|
|
|
_this->startTime = time(0);
|
|
|
|
}
|
|
|
|
ImGui::TextColored(ImGui::GetStyleColorVec4(ImGuiCol_Text), "Idle --:--:--");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ImGui::Button("Stop", ImVec2(menuColumnWidth, 0))) {
|
|
|
|
_this->audioStream->stopReader();
|
|
|
|
_this->workerThread.join();
|
|
|
|
_this->audioStream->clearReadStop();
|
2020-11-30 05:51:33 +01:00
|
|
|
sigpath::sinkManager.unbindStream(_this->selectedStreamName, _this->audioStream);
|
2020-10-22 12:53:46 +02:00
|
|
|
_this->writer->close();
|
|
|
|
delete _this->writer;
|
|
|
|
_this->recording = false;
|
|
|
|
}
|
|
|
|
uint64_t seconds = _this->samplesWritten / (uint64_t)_this->sampleRate;
|
|
|
|
time_t diff = seconds;
|
|
|
|
tm *dtm = gmtime(&diff);
|
|
|
|
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Recording %02d:%02d:%02d", dtm->tm_hour, dtm->tm_min, dtm->tm_sec);
|
2020-10-01 13:46:12 +02:00
|
|
|
}
|
2020-08-18 00:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-01 13:46:12 +02:00
|
|
|
|
2020-10-22 12:53:46 +02:00
|
|
|
static void _audioWriteWorker(RecorderModule* _this) {
|
2020-11-02 03:57:44 +01:00
|
|
|
int16_t* sampleBuf = new int16_t[STREAM_BUFFER_SIZE * 2];
|
2020-10-01 13:46:12 +02:00
|
|
|
while (true) {
|
2020-11-02 03:57:44 +01:00
|
|
|
int count = _this->audioStream->read();
|
|
|
|
if (count < 0) { break; }
|
2020-11-12 00:53:38 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2020-11-02 03:57:44 +01:00
|
|
|
sampleBuf[(i * 2) + 0] = _this->audioStream->data[i].l * 0x7FFF;
|
|
|
|
sampleBuf[(i * 2) + 1] = _this->audioStream->data[i].r * 0x7FFF;
|
2020-10-01 13:46:12 +02:00
|
|
|
}
|
2020-11-02 03:57:44 +01:00
|
|
|
_this->audioStream->flush();
|
2020-11-12 00:53:38 +01:00
|
|
|
_this->samplesWritten += count;
|
|
|
|
_this->writer->writeSamples(sampleBuf, count * sizeof(int16_t) * 2);
|
2020-08-18 00:56:51 +02:00
|
|
|
}
|
2020-10-01 13:46:12 +02:00
|
|
|
delete[] sampleBuf;
|
2020-08-18 00:56:51 +02:00
|
|
|
}
|
2020-10-01 13:46:12 +02:00
|
|
|
|
2020-10-22 12:53:46 +02:00
|
|
|
static void _iqWriteWorker(RecorderModule* _this) {
|
2020-11-12 00:53:38 +01:00
|
|
|
int16_t* sampleBuf = new int16_t[STREAM_BUFFER_SIZE];
|
2020-10-22 12:53:46 +02:00
|
|
|
while (true) {
|
2020-11-02 03:57:44 +01:00
|
|
|
int count = _this->iqStream->read();
|
|
|
|
if (count < 0) { break; }
|
2020-11-12 00:53:38 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2020-11-02 03:57:44 +01:00
|
|
|
sampleBuf[(i * 2) + 0] = _this->iqStream->data[i].q * 0x7FFF;
|
|
|
|
sampleBuf[(i * 2) + 1] = _this->iqStream->data[i].i * 0x7FFF;
|
2020-10-22 12:53:46 +02:00
|
|
|
}
|
2020-11-02 03:57:44 +01:00
|
|
|
_this->iqStream->flush();
|
2020-11-12 00:53:38 +01:00
|
|
|
_this->samplesWritten += count;
|
|
|
|
_this->writer->writeSamples(sampleBuf, count * sizeof(int16_t) * 2);
|
2020-10-22 12:53:46 +02:00
|
|
|
}
|
|
|
|
delete[] sampleBuf;
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
std::string name;
|
2020-11-02 03:57:44 +01:00
|
|
|
dsp::stream<dsp::stereo_t>* audioStream;
|
2020-10-22 12:53:46 +02:00
|
|
|
dsp::stream<dsp::complex_t>* iqStream;
|
2020-10-01 13:46:12 +02:00
|
|
|
WavWriter* writer;
|
2020-11-30 16:30:45 +01:00
|
|
|
VFOManager::VFO* vfo;
|
2020-10-01 13:46:12 +02:00
|
|
|
std::thread workerThread;
|
|
|
|
bool recording;
|
|
|
|
time_t startTime;
|
|
|
|
std::string lastNameList;
|
|
|
|
std::string selectedStreamName;
|
|
|
|
int selectedStreamId;
|
|
|
|
uint64_t samplesWritten;
|
|
|
|
float sampleRate;
|
2020-11-30 16:30:45 +01:00
|
|
|
float vfoSampleRate = 200000;
|
2020-10-22 12:53:46 +02:00
|
|
|
int recMode = 0;
|
2020-10-01 13:46:12 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RecorderContext_t {
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOD_EXPORT void _INIT_() {
|
|
|
|
// Nothing here
|
2020-08-18 00:56:51 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 13:46:12 +02:00
|
|
|
MOD_EXPORT void* _CREATE_INSTANCE_(std::string name) {
|
|
|
|
RecorderModule* instance = new RecorderModule(name);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _DELETE_INSTANCE_() {
|
|
|
|
|
2020-08-18 00:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _STOP_(RecorderContext_t* ctx) {
|
|
|
|
|
|
|
|
}
|