/* * Copyright (C) 2015, Mike Walters * Copyright (C) 2015, Jared Boone * * This file is part of inspectrum. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "inputsource.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class ComplexF32SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(std::complex); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast*>(src); std::copy(&s[start], &s[start + length], dest); } }; class ComplexF64SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(std::complex); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast*>(src); std::transform(&s[start], &s[start + length], dest, [](const std::complex& v) -> std::complex { return { static_cast(v.real()) , static_cast(v.imag()) }; } ); } }; class ComplexS32SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(std::complex); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast*>(src); std::transform(&s[start], &s[start + length], dest, [](const std::complex& v) -> std::complex { const float k = 1.0f / 2147483648.0f; return { v.real() * k, v.imag() * k }; } ); } }; class ComplexS16SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(std::complex); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast*>(src); std::transform(&s[start], &s[start + length], dest, [](const std::complex& v) -> std::complex { const float k = 1.0f / 32768.0f; return { v.real() * k, v.imag() * k }; } ); } }; class ComplexS8SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(std::complex); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast*>(src); std::transform(&s[start], &s[start + length], dest, [](const std::complex& v) -> std::complex { const float k = 1.0f / 128.0f; return { v.real() * k, v.imag() * k }; } ); } }; class ComplexU8SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(std::complex); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast*>(src); std::transform(&s[start], &s[start + length], dest, [](const std::complex& v) -> std::complex { const float k = 1.0f / 128.0f; return { (v.real() - 127.4f) * k, (v.imag() - 127.4f) * k }; } ); } }; class RealF32SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(float); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast(src); std::transform(&s[start], &s[start + length], dest, [](const float& v) -> std::complex { return {v, 0.0f}; } ); } }; class RealF64SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(double); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast(src); std::transform(&s[start], &s[start + length], dest, [](const double& v) -> std::complex { return {static_cast(v), 0.0f}; } ); } }; class RealS16SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(int16_t); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast(src); std::transform(&s[start], &s[start + length], dest, [](const int16_t& v) -> std::complex { const float k = 1.0f / 32768.0f; return { v * k, 0.0f }; } ); } }; class RealS8SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(int8_t); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast(src); std::transform(&s[start], &s[start + length], dest, [](const int8_t& v) -> std::complex { const float k = 1.0f / 128.0f; return { v * k, 0.0f }; } ); } }; class RealU8SampleAdapter : public SampleAdapter { public: size_t sampleSize() override { return sizeof(uint8_t); } void copyRange(const void* const src, size_t start, size_t length, std::complex* const dest) override { auto s = reinterpret_cast(src); std::transform(&s[start], &s[start + length], dest, [](const uint8_t& v) -> std::complex { const float k = 1.0f / 128.0f; return { (v - 127.4f) * k, 0 }; } ); } }; InputSource::InputSource() { } InputSource::~InputSource() { cleanup(); } void InputSource::cleanup() { if (mmapData != nullptr) { inputFile->unmap(mmapData); mmapData = nullptr; } if (inputFile != nullptr) { delete inputFile; inputFile = nullptr; } } QJsonObject InputSource::readMetaData(const QString &filename) { QFile datafile(filename); if (!datafile.open(QFile::ReadOnly | QIODevice::Text)) { throw std::runtime_error("Error while opening meta data file: " + datafile.errorString().toStdString()); } QJsonDocument d = QJsonDocument::fromJson(datafile.readAll()); datafile.close(); auto root = d.object(); if (!root.contains("global") || !root["global"].isObject()) { throw std::runtime_error("SigMF meta data is invalid (no global object found)"); } auto global = root["global"].toObject(); if (!global.contains("core:datatype") || !global["core:datatype"].isString()) { throw std::runtime_error("SigMF meta data does not specify a valid datatype"); } auto datatype = global["core:datatype"].toString(); if (datatype.compare("cf32_le") == 0) { sampleAdapter = std::make_unique(); } else if (datatype.compare("ci32_le") == 0) { sampleAdapter = std::make_unique(); } else if (datatype.compare("ci16_le") == 0) { sampleAdapter = std::make_unique(); } else if (datatype.compare("ci8") == 0) { sampleAdapter = std::make_unique(); } else if (datatype.compare("cu8") == 0) { sampleAdapter = std::make_unique(); } else if (datatype.compare("rf32_le") == 0) { sampleAdapter = std::make_unique(); _realSignal = true; } else if (datatype.compare("ri16_le") == 0) { sampleAdapter = std::make_unique(); _realSignal = true; } else if (datatype.compare("ri8") == 0) { sampleAdapter = std::make_unique(); _realSignal = true; } else if (datatype.compare("ru8") == 0) { sampleAdapter = std::make_unique(); _realSignal = true; } else { throw std::runtime_error("SigMF meta data specifies unsupported datatype"); } if (global.contains("core:sample_rate") && global["core:sample_rate"].isDouble()) { setSampleRate(global["core:sample_rate"].toDouble()); } if (root.contains("captures") && root["captures"].isArray()) { auto captures = root["captures"].toArray(); for (auto capture_ref : captures) { if (capture_ref.isObject()) { auto capture = capture_ref.toObject(); if (capture.contains("core:frequency") && capture["core:frequency"].isDouble()) { frequency = capture["core:frequency"].toDouble(); } } else { throw std::runtime_error("SigMF meta data is invalid (invalid capture object)"); } } } if(root.contains("annotations") && root["annotations"].isArray()) { size_t offset = 0; if (global.contains("core:offset")) { offset = global["offset"].toDouble(); } auto annotations = root["annotations"].toArray(); for (auto annotation_ref : annotations) { if (annotation_ref.isObject()) { auto sigmf_annotation = annotation_ref.toObject(); const size_t sample_start = sigmf_annotation["core:sample_start"].toDouble(); if (sample_start < offset) continue; const size_t rel_sample_start = sample_start - offset; const size_t sample_count = sigmf_annotation["core:sample_count"].toDouble(); auto sampleRange = range_t{rel_sample_start, rel_sample_start + sample_count - 1}; const double freq_lower_edge = sigmf_annotation["core:freq_lower_edge"].toDouble(); const double freq_upper_edge = sigmf_annotation["core:freq_upper_edge"].toDouble(); auto frequencyRange = range_t{freq_lower_edge, freq_upper_edge}; auto label = sigmf_annotation["core:label"].toString(); if (label.isEmpty()) { label = sigmf_annotation["core:description"].toString(); } auto comment = sigmf_annotation["core:comment"].toString(); annotationList.emplace_back(sampleRange, frequencyRange, label, comment); } } } return root; } void InputSource::openFile(const char *filename) { QFileInfo fileInfo(filename); std::string suffix = std::string(fileInfo.suffix().toLower().toUtf8().constData()); if (_fmt != "") { suffix = _fmt; } // allow fmt override if ((suffix == "cfile") || (suffix == "cf32") || (suffix == "fc32")) { sampleAdapter = std::make_unique(); } else if ((suffix == "cf64") || (suffix == "fc64")) { sampleAdapter = std::make_unique(); } else if ((suffix == "cs32") || (suffix == "sc32") || (suffix == "c32")) { sampleAdapter = std::make_unique(); } else if ((suffix == "cs16") || (suffix == "sc16") || (suffix == "c16")) { sampleAdapter = std::make_unique(); } else if ((suffix == "cs8") || (suffix == "sc8") || (suffix == "c8")) { sampleAdapter = std::make_unique(); } else if ((suffix == "cu8") || (suffix == "uc8")) { sampleAdapter = std::make_unique(); } else if (suffix == "f32") { sampleAdapter = std::make_unique(); _realSignal = true; } else if (suffix == "f64") { sampleAdapter = std::make_unique(); _realSignal = true; } else if (suffix == "s16") { sampleAdapter = std::make_unique(); _realSignal = true; } else if (suffix == "s8") { sampleAdapter = std::make_unique(); _realSignal = true; } else if (suffix == "u8") { sampleAdapter = std::make_unique(); _realSignal = true; } else { sampleAdapter = std::make_unique(); } QString dataFilename; annotationList.clear(); QString metaFilename; if (suffix == "sigmf-meta" || suffix == "sigmf-data" || suffix == "sigmf-") { dataFilename = fileInfo.path() + "/" + fileInfo.completeBaseName() + ".sigmf-data"; metaFilename = fileInfo.path() + "/" + fileInfo.completeBaseName() + ".sigmf-meta"; auto metaData = readMetaData(metaFilename); QFile datafile(dataFilename); if (!datafile.open(QFile::ReadOnly | QIODevice::Text)) { auto global = metaData["global"].toObject(); if (global.contains("core:dataset")) { auto datasetfilename = global["core:dataset"].toString(); if(QFileInfo(datasetfilename).isAbsolute()){ dataFilename = datasetfilename; } else{ dataFilename = fileInfo.path() + "/" + datasetfilename; } } } } else if (suffix == "sigmf") { throw std::runtime_error("SigMF archives are not supported. Consider extracting a recording."); } else { dataFilename = filename; } auto file = std::make_unique(dataFilename); if (!file->open(QFile::ReadOnly)) { throw std::runtime_error(file->errorString().toStdString()); } auto size = file->size(); sampleCount = size / sampleAdapter->sampleSize(); auto data = file->map(0, size); if (data == nullptr) throw std::runtime_error("Error mmapping file"); cleanup(); inputFile = file.release(); mmapData = data; invalidate(); } void InputSource::setSampleRate(double rate) { sampleRate = rate; invalidate(); } double InputSource::rate() { return sampleRate; } std::unique_ptr[]> InputSource::getSamples(size_t start, size_t length) { if (inputFile == nullptr) return nullptr; if (mmapData == nullptr) return nullptr; if(start < 0 || length < 0) return nullptr; if (start + length > sampleCount) return nullptr; auto dest = std::make_unique[]>(length); sampleAdapter->copyRange(mmapData, start, length, dest.get()); return dest; } void InputSource::setFormat(std::string fmt){ _fmt = fmt; }