#include "hw/gtl_hw_player_file.h" #include "qjsonarray.h" #include namespace gtl { namespace hw { player_file::player_file(QObject *parent, QString path) : QObject(parent) , _path(path) , _stream(NULL) , _is_ok(false) , _time(0) , _channels(0) { _file = new QFile(path); if (_file->open(QIODevice::ReadOnly)) { _stream = new QDataStream(_file); _stream->setByteOrder(QDataStream::LittleEndian); _stream->setFloatingPointPrecision(QDataStream::FloatingPointPrecision::SinglePrecision); _is_ok = true; } QFile info_file(path + ".info"); if(info_file.open(QFile::ReadOnly)) { QByteArray data = info_file.readAll(); _data = QJsonDocument::fromJson(data).object(); info_file.close(); } } player_file::~player_file() { if (_stream) delete _stream; _file->close(); delete _file; } QString player_file::channel(int idx) const { QString name = _data["hw"]["channels"].toArray().at(idx).toObject().value("name").toString(); if(name.isEmpty()) name = "input " + QString::number(idx); return name; } QString player_file::unit(int idx) const { return _data["hw"]["channels"].toArray().at(idx).toObject().value("unit").toString(); } int player_file::color(int idx) const { return _data["hw"]["channels"].toArray().at(idx).toObject().value("color").toInt(); } QJsonObject player_file::channel_info(int idx) const { return _data["hw"]["channels"].toArray().at(idx).toObject(); } qreal player_file::reference(int idx) const { qreal reference = get_root(idx).value("reference").toDouble(); if(reference == 0) reference = 1; return reference; } qreal player_file::sensitivity(int idx) const { qreal sensitivity = get_root(idx).value("sensitivity").toDouble(); if(sensitivity == 0) sensitivity = 1; return sensitivity; } qreal player_file::gain(int idx) const { qreal gain = get_root(idx).value("gain").toDouble(); if(gain == 0) gain = 1; return gain; } qreal player_file::offset(int idx) const { return get_root(idx).value("offset").toDouble(); } QString player_file::device() const { return _data["hw"]["name"].toString(); } QString player_file::comment() const { return _data["comment"].toString(); } const QJsonObject &player_file::data() const { return _data; } void player_file::seek_to_start() { _file->seek(_pos_data); } void player_file::get_ranges_indices(std::back_insert_iterator>> ranges) const { QJsonObject data_object; QFile info_file(_path + ".info"); if(info_file.open(QFile::ReadOnly)) { QByteArray data = info_file.readAll(); data_object = QJsonDocument::fromJson(data).object(); QJsonArray ranges_array = data_object["ranges"].toArray(); if(ranges_array.empty()) { ranges = std::pair(0, samples()); ranges++; } for(auto it = ranges_array.begin(); it != ranges_array.end(); it++, ranges++) ranges = std::pair(qRound(it->toObject()["left"].toDouble()*_rate), qRound(it->toObject()["right"].toDouble()*_rate)); info_file.close(); } } void player_file::set_ranges(std::vector>::iterator begin, std::vector>::iterator end) { std::sort(begin, end); QJsonArray ranges; for(auto it = begin; it!= end; it++) { QJsonObject range; range.insert("left", it->first); range.insert("right", it->second); ranges.append(range); } _data["ranges"] = ranges; QFile info_file(_path + ".info"); if(info_file.open(QFile::WriteOnly)) { info_file.write(QJsonDocument(_data).toJson()); info_file.close(); } } void player_file::get_ranges(std::back_insert_iterator>> ranges) const { QJsonArray ranges_array = _data["ranges"].toArray(); for(auto it = ranges_array.begin(); it != ranges_array.end(); it++, ranges++) ranges = std::pair(it->toObject()["left"].toDouble(), it->toObject()["right"].toDouble()); } QJsonObject player_file::get_root(int idx) const { QJsonObject root = _data["hw"]["channels"].toArray().at(idx).toObject(); while(!root.isEmpty()) { if(root.value("node").toString().contains("analog_input")) break; root = root.value("parent").toObject(); } return root; } } }