Skip to content

Commit bb9d6cf

Browse files
committed
Add the preprocessor tool for sfz files
1 parent 60cfd84 commit bb9d6cf

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

devtools/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ if(JACK_FOUND AND TARGET Qt5::Widgets)
1313
target_link_libraries(sfizz_capture_eg PRIVATE sfizz-sndfile Qt5::Widgets ${JACK_LIBRARIES})
1414
set_target_properties(sfizz_capture_eg PROPERTIES AUTOUIC ON)
1515
endif()
16+
17+
add_executable(sfizz_preprocessor Preprocessor.cpp)
18+
target_link_libraries(sfizz_preprocessor sfizz_parser)

devtools/Preprocessor.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
3+
// This code is part of the sfizz library and is licensed under a BSD 2-clause
4+
// license. You should have receive a LICENSE.md file along with the code.
5+
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
6+
7+
/*
8+
This program reads a SFZ file, and outputs it back into a single file
9+
with all the includes and definitions processed.
10+
11+
It can serve to facilitate identifying problems, whether these are related to
12+
the parser or complicated instrument structures.
13+
*/
14+
15+
#include "parser/Parser.h"
16+
#include <iostream>
17+
18+
class MyParserListener : public sfz::Parser::Listener {
19+
public:
20+
explicit MyParserListener(sfz::Parser& parser)
21+
: _parser(parser)
22+
{
23+
}
24+
25+
protected:
26+
void onParseFullBlock(const std::string& header, const std::vector<sfz::Opcode>& opcodes) override
27+
{
28+
std::cout << '\n';
29+
std::cout << '<' << header << '>' << '\n';
30+
for (const sfz::Opcode& opc : opcodes)
31+
std::cout << opc.opcode << '=' << opc.value << '\n';
32+
}
33+
34+
void onParseError(const sfz::SourceRange& range, const std::string& message) override
35+
{
36+
const auto relativePath = range.start.filePath->lexically_relative(_parser.originalDirectory());
37+
std::cerr << "Parse error in " << relativePath << " at line " << range.start.lineNumber + 1 << ": " << message << '\n';
38+
}
39+
40+
void onParseWarning(const sfz::SourceRange& range, const std::string& message) override
41+
{
42+
const auto relativePath = range.start.filePath->lexically_relative(_parser.originalDirectory());
43+
std::cerr << "Parse warning in " << relativePath << " at line " << range.start.lineNumber + 1 << ": " << message << '\n';
44+
}
45+
46+
private:
47+
sfz::Parser& _parser;
48+
};
49+
50+
int main(int argc, char *argv[])
51+
{
52+
if (argc != 2) {
53+
std::cerr << "Please indicate the SFZ file path.\n";
54+
return 1;
55+
}
56+
57+
const fs::path sfzFilePath { argv[1] };
58+
59+
sfz::Parser parser;
60+
MyParserListener listener(parser);
61+
62+
parser.setListener(&listener);
63+
parser.parseFile(argv[1]);
64+
65+
if (parser.getErrorCount() > 0)
66+
return 1;
67+
68+
return 0;
69+
}

0 commit comments

Comments
 (0)