-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathease_parser.cpp
More file actions
110 lines (90 loc) · 3.01 KB
/
ease_parser.cpp
File metadata and controls
110 lines (90 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#include "ease_parser.hpp"
#include "ease_model.hpp"
#include <boost/regex.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/core/parse.hpp>
#include <format>
namespace x3 = boost::spirit::x3;
//////////////////////////////
// Parser
//////////////////////////////
// clang-format off
namespace spatparse::ease
{
x3::rule<class file_header_parser, file_header_t> const file_header_parser
= "file_header_parser";
const auto text_character = ((x3::print - '\"') | x3::char_(' '));
const auto file_header_parser_def
= "\"FileType\",\"" >> +text_character >> '"'
>> "\"Format\"," >> x3::double_
>> "\"LengthUnit\",\"" >> +text_character >> '"';
x3::rule<class loudspeaker_parser, loudspeaker_t> const loudspeaker_parser
= "loudspeaker_parser";
const auto loudspeaker_parser_def
= "\"Label\",\"" >> *text_character >> x3::lit("\"")
>> "\"Position\"," >> x3::double_ >> ',' >> x3::double_ >> ',' >> x3::double_
>> "\"Ver/Hor/Rot\"," >> x3::double_ >> ',' >> x3::double_ >> ',' >> x3::double_
>> "\"Speaker\",\"" >> *text_character >> x3::lit("\"")
>> "\"Delay/Align\"," >> x3::int_ >> ',' >> x3::int_
>> "\"dB 1m\"," >> *(x3::int_ % ',')
>> "\"Watts\"," >> x3::int_
>> "\"Phase\"," >> x3::int_;
BOOST_SPIRIT_DEFINE(file_header_parser, loudspeaker_parser);
// clang-format on
std::optional<file> parse(std::string_view input)
{
file res;
std::string cleaned{input};
// Remove the comments
const boost::regex rx(";([[:print:]]| )*\n");
cleaned = boost::regex_replace(cleaned, rx, "");
auto begin = cleaned.begin();
auto end = cleaned.end();
bool success = boost::spirit::x3::phrase_parse(
begin, end, file_header_parser >> *loudspeaker_parser, x3::ascii::space, res);
if(!success)
{
return std::nullopt;
}
return res;
}
std::string to_string(const spatparse::ease::file& f)
{
std::string ease_string;
ease_string.reserve(f.loudspeakers.size() * 256);
ease_string += std::format(
"; EASE Focus 3 - Generated by spatparse\n"
"; (c) Société des Arts Technologiques\n"
";\n"
";==============================================================\n"
"\"FileType\",\"{}\"\n"
"\"Format\",{:.1f}\n"
"\"LengthUnit\",\"{}\"\n"
";\n",
f.header.file_type, f.header.format, f.header.length_unit);
for(const auto& spk : f.loudspeakers)
{
ease_string += std::format(
"\"Label\",\"{}\"\n"
"\"Position\",{:.4f},{:.4f},{:.4f}\n"
"\"Ver/Hor/Rot\",{:.1f},{:.1f},{:.1f}\n"
"\"Speaker\",\"{}\"\n"
"\"Delay/Align\",{},{}\n",
spk.label, spk.x, spk.y, spk.z, spk.ver, spk.hor, spk.rot, spk.speaker,
spk.delay, spk.align);
ease_string += "\"dB 1m\"";
for(const auto& db : spk.db_1m)
{
ease_string += std::format(",{}", db);
}
ease_string += "\n";
ease_string += std::format(
"\"Watts\",{}\n"
"\"Phase\",{}\n"
";\n",
spk.watts, spk.phase);
}
return ease_string;
}
}