1 |
schoenebeck |
2581 |
/* |
2 |
|
|
* Copyright (c) 2014 Christian Schoenebeck |
3 |
|
|
* |
4 |
|
|
* http://www.linuxsampler.org |
5 |
|
|
* |
6 |
|
|
* This program is part of LinuxSampler and released under the same terms. |
7 |
|
|
* See README file for details. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "common/global.h" |
11 |
|
|
#include "scriptvm/ScriptVM.h" |
12 |
|
|
#include "shell/CFmt.h" |
13 |
schoenebeck |
2611 |
#include "engines/common/InstrumentScriptVM.h" |
14 |
|
|
#include "engines/gig/InstrumentScriptVM.h" |
15 |
|
|
#include <iostream> |
16 |
schoenebeck |
2581 |
|
17 |
|
|
/* |
18 |
|
|
This command line tool is currently merely for development and testing |
19 |
|
|
purposes, regarding the real-time instrument script feature of the sampler. |
20 |
|
|
You can use this command line application like this: |
21 |
|
|
|
22 |
schoenebeck |
2611 |
ls_instr_script core < src/scriptvm/examples/helloworld.txt |
23 |
schoenebeck |
2581 |
|
24 |
|
|
Which will peform 3 things: |
25 |
|
|
|
26 |
|
|
1. Parses the given instrument script and prints any parser errors or |
27 |
|
|
warnings. |
28 |
|
|
2. It dumps the parsed VM tree (only interesting for LS developers). |
29 |
|
|
3. If there were not parser errors, it will run each event handler defined in |
30 |
|
|
the script. |
31 |
|
|
*/ |
32 |
|
|
|
33 |
|
|
using namespace LinuxSampler; |
34 |
schoenebeck |
2611 |
using namespace std; |
35 |
schoenebeck |
2581 |
|
36 |
schoenebeck |
2611 |
static void printUsage() { |
37 |
|
|
cout << "ls_instr_script - Parse real-time instrument script from stdin." << endl; |
38 |
|
|
cout << endl; |
39 |
|
|
cout << "Usage: ls_instr_script ENGINE" << endl; |
40 |
|
|
cout << endl; |
41 |
|
|
cout << " ENGINE\n"; |
42 |
|
|
cout << " Either \"core\", \"gig\", \"sf2\" or \"sfz\"." << endl; |
43 |
|
|
cout << endl; |
44 |
|
|
cout << "If you pass \"core\" as argument, only the core language built-in" << endl; |
45 |
|
|
cout << "variables and functions are available. However in this particular" << endl; |
46 |
|
|
cout << "mode the program will not just parse the given script, but also" << endl; |
47 |
|
|
cout << "execute the event handlers. All other arguments for ENGINE provide" << endl; |
48 |
|
|
cout << "the sampler engine / sampler format specific additional built-in" << endl; |
49 |
|
|
cout << "variables and functions, however they wil not be executed by this" << endl; |
50 |
|
|
cout << "program." << endl; |
51 |
|
|
cout << endl; |
52 |
|
|
} |
53 |
schoenebeck |
2581 |
|
54 |
schoenebeck |
2611 |
int main(int argc, char *argv[]) { |
55 |
|
|
if (argc < 2) { |
56 |
|
|
printUsage(); |
57 |
|
|
return -1; |
58 |
|
|
} |
59 |
|
|
String engine = argv[1]; |
60 |
|
|
bool runScript = false; |
61 |
|
|
|
62 |
|
|
ScriptVM* vm; |
63 |
|
|
if (engine == "core") { |
64 |
|
|
vm = new ScriptVM; |
65 |
|
|
runScript = true; |
66 |
|
|
} else if (engine == "sf2" || engine == "sfz") { |
67 |
|
|
vm = new InstrumentScriptVM; |
68 |
|
|
} else if (engine == "gig") { |
69 |
|
|
vm = new gig::InstrumentScriptVM; |
70 |
|
|
} else { |
71 |
|
|
std::cerr << "Unknown ENGINE '" << engine << "'\n\n"; |
72 |
|
|
printUsage(); |
73 |
|
|
return -1; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
VMParserContext* parserContext = vm->loadScript(&std::cin); |
77 |
|
|
|
78 |
schoenebeck |
2588 |
std::vector<ParserIssue> errors = parserContext->errors(); |
79 |
|
|
std::vector<ParserIssue> warnings = parserContext->warnings(); |
80 |
|
|
std::vector<ParserIssue> issues = parserContext->issues(); |
81 |
schoenebeck |
2581 |
if (warnings.empty() && errors.empty()) { |
82 |
|
|
CFmt fmt; fmt.green(); |
83 |
|
|
printf("EOF. Script parse completed successfully (no errors, no warnings).\n"); |
84 |
|
|
} else if (!errors.empty()) { |
85 |
|
|
CFmt fmt; fmt.red(); |
86 |
|
|
printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n", |
87 |
|
|
errors.size(), warnings.size()); |
88 |
|
|
} else { |
89 |
|
|
CFmt fmt; fmt.yellow(); |
90 |
|
|
printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n", |
91 |
|
|
errors.size(), warnings.size()); |
92 |
|
|
} |
93 |
|
|
for (int i = 0; i < issues.size(); ++i) { |
94 |
|
|
CFmt fmt; |
95 |
|
|
if (issues[i].isWrn()) fmt.yellow(); |
96 |
|
|
else if (issues[i].isErr()) fmt.red(); |
97 |
|
|
issues[i].dump(); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
printf("[Dumping parsed VM tree]\n"); |
101 |
schoenebeck |
2611 |
vm->dumpParsedScript(parserContext); |
102 |
schoenebeck |
2581 |
printf("[End of parsed VM tree]\n"); |
103 |
|
|
|
104 |
schoenebeck |
2588 |
if (!errors.empty()) { |
105 |
|
|
if (parserContext) delete parserContext; |
106 |
|
|
return -1; |
107 |
|
|
} |
108 |
schoenebeck |
2581 |
|
109 |
schoenebeck |
2611 |
if (!runScript) { |
110 |
|
|
return 0; |
111 |
|
|
} |
112 |
|
|
|
113 |
schoenebeck |
2588 |
if (!parserContext->eventHandler(0)) { |
114 |
schoenebeck |
2581 |
printf("No event handler exists. So nothing to execute.\n"); |
115 |
schoenebeck |
2588 |
if (parserContext) delete parserContext; |
116 |
schoenebeck |
2581 |
return 0; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
printf("Preparing execution of script.\n"); |
120 |
schoenebeck |
2611 |
VMExecContext* execContext = vm->createExecContext(parserContext); |
121 |
schoenebeck |
2588 |
for (int i = 0; parserContext->eventHandler(i); ++i) { |
122 |
|
|
VMEventHandler* handler = parserContext->eventHandler(i); |
123 |
schoenebeck |
2581 |
printf("[Running event handler '%s']\n", handler->eventHandlerName().c_str()); |
124 |
schoenebeck |
2611 |
VMExecStatus_t result = vm->exec(parserContext, execContext, handler); |
125 |
schoenebeck |
2581 |
CFmt fmt; |
126 |
|
|
if (result & VM_EXEC_ERROR) { |
127 |
|
|
fmt.red(); |
128 |
|
|
printf("[Event handler '%s' finished with ERROR status]\n", handler->eventHandlerName().c_str()); |
129 |
|
|
} else if (result & VM_EXEC_SUSPENDED) { |
130 |
|
|
fmt.yellow(); |
131 |
|
|
printf("[Event handler '%s' returned with SUSPENDED status: %d microseconds]\n", |
132 |
schoenebeck |
2588 |
handler->eventHandlerName().c_str(), execContext->suspensionTimeMicroseconds()); |
133 |
schoenebeck |
2581 |
} else if (!(result & VM_EXEC_RUNNING)) { |
134 |
|
|
fmt.green(); |
135 |
|
|
printf("[Event handler '%s' finished with SUCCESS status]\n", handler->eventHandlerName().c_str()); |
136 |
|
|
} else if (result & VM_EXEC_RUNNING) { |
137 |
|
|
fmt.cyan(); |
138 |
|
|
printf("[Event handler '%s' finished with RUNNING status]\n", handler->eventHandlerName().c_str()); |
139 |
|
|
} else { |
140 |
|
|
fmt.red(); |
141 |
|
|
printf("[Event handler '%s' finished with UNKNOWN status]\n", handler->eventHandlerName().c_str()); |
142 |
|
|
} |
143 |
|
|
} |
144 |
schoenebeck |
2611 |
|
145 |
schoenebeck |
2588 |
if (parserContext) delete parserContext; |
146 |
|
|
if (execContext) delete execContext; |
147 |
schoenebeck |
2611 |
if (vm) delete vm; |
148 |
schoenebeck |
2581 |
|
149 |
|
|
return 0; |
150 |
|
|
} |