| 42 | { |
| 43 | |
| 44 | std::vector<EqualizePattern> read(const std::string &filename) |
| 45 | { |
| 46 | Json::Value root; |
| 47 | std::ifstream ifs(filename); |
| 48 | |
| 49 | // Failed to open cfg file |
| 50 | if (not ifs.is_open()) |
| 51 | throw std::runtime_error("Cannot open config file. " + filename); |
| 52 | |
| 53 | Json::CharReaderBuilder builder; |
| 54 | JSONCPP_STRING errs; |
| 55 | |
| 56 | // Failed to parse |
| 57 | if (not parseFromStream(builder, ifs, &root, &errs)) |
| 58 | throw std::runtime_error("Cannot parse config file (json format). " + errs); |
| 59 | |
| 60 | std::vector<EqualizePattern> res; |
| 61 | |
| 62 | for (auto &eq_pattern : root) |
| 63 | { |
| 64 | auto get_string = [&](const std::string &val) { |
| 65 | if (not eq_pattern.isMember(val)) |
| 66 | throw std::runtime_error(val + " is missing in " + filename); |
| 67 | if (not eq_pattern[val].isString()) |
| 68 | throw std::runtime_error(val + " is not string"); |
| 69 | |
| 70 | return eq_pattern[val].asString(); |
| 71 | }; |
| 72 | |
| 73 | auto get_fp32_array = [&](const std::string &val) { |
| 74 | if (not eq_pattern.isMember(val)) |
| 75 | throw std::runtime_error(val + " is missing in " + filename); |
| 76 | auto arr = eq_pattern[val]; |
| 77 | if (not arr.isArray()) |
| 78 | throw std::runtime_error(val + " is not array"); |
| 79 | |
| 80 | std::vector<float> res; |
| 81 | for (auto &elem : arr) |
| 82 | { |
| 83 | if (not elem.isNumeric()) |
| 84 | throw std::runtime_error(val + "'s element is not fp32"); |
| 85 | |
| 86 | res.emplace_back(elem.asFloat()); |
| 87 | } |
| 88 | |
| 89 | return res; |
| 90 | }; |
| 91 | |
| 92 | auto front = get_string("front"); |
| 93 | auto back = get_string("back"); |
| 94 | auto type = get_string("type"); |
| 95 | |
| 96 | EqualizePattern p; |
| 97 | { |
| 98 | p.front = front; |
| 99 | p.back = back; |
| 100 | p.type = eq_type(type); |
| 101 | switch (p.type) |