1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck |
* Copyright (c) 2014-2015 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
10 |
#include "common/global.h" |
#include "common/global.h" |
11 |
#include "scriptvm/ScriptVM.h" |
#include "scriptvm/ScriptVM.h" |
12 |
#include "shell/CFmt.h" |
#include "shell/CFmt.h" |
13 |
|
#include "engines/common/InstrumentScriptVM.h" |
14 |
|
#include "engines/gig/InstrumentScriptVM.h" |
15 |
|
#include <iostream> |
16 |
|
|
17 |
/* |
/* |
18 |
This command line tool is currently merely for development and testing |
This command line tool is currently merely for development and testing |
19 |
purposes, regarding the real-time instrument script feature of the sampler. |
purposes, regarding the real-time instrument script feature of the sampler. |
20 |
You can use this command line application like this: |
You can use this command line application like this: |
21 |
|
|
22 |
ls_instr_script < src/scriptvm/examples/helloworld.txt |
ls_instr_script core < src/scriptvm/examples/helloworld.txt |
23 |
|
|
24 |
Which will peform 3 things: |
Which will peform 3 things: |
25 |
|
|
31 |
*/ |
*/ |
32 |
|
|
33 |
using namespace LinuxSampler; |
using namespace LinuxSampler; |
34 |
|
using namespace std; |
35 |
|
|
36 |
int main() { |
static void printUsage() { |
37 |
ScriptVM script; |
cout << "ls_instr_script - Parse real-time instrument script from stdin." << endl; |
38 |
script.loadScript(&std::cin); |
cout << endl; |
39 |
|
cout << "Usage: ls_instr_script ENGINE" << endl; |
40 |
std::vector<ParserIssue> errors = script.errors(); |
cout << endl; |
41 |
std::vector<ParserIssue> warnings = script.warnings(); |
cout << " ENGINE\n"; |
42 |
std::vector<ParserIssue> issues = script.issues(); |
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 |
|
|
54 |
|
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 |
|
std::vector<ParserIssue> errors = parserContext->errors(); |
79 |
|
std::vector<ParserIssue> warnings = parserContext->warnings(); |
80 |
|
std::vector<ParserIssue> issues = parserContext->issues(); |
81 |
if (warnings.empty() && errors.empty()) { |
if (warnings.empty() && errors.empty()) { |
82 |
CFmt fmt; fmt.green(); |
CFmt fmt; fmt.green(); |
83 |
printf("EOF. Script parse completed successfully (no errors, no warnings).\n"); |
printf("EOF. Script parse completed successfully (no errors, no warnings).\n"); |
84 |
} else if (!errors.empty()) { |
} else if (!errors.empty()) { |
85 |
CFmt fmt; fmt.red(); |
CFmt fmt; fmt.red(); |
86 |
printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n", |
printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n", |
87 |
errors.size(), warnings.size()); |
int(errors.size()), int(warnings.size())); |
88 |
} else { |
} else { |
89 |
CFmt fmt; fmt.yellow(); |
CFmt fmt; fmt.yellow(); |
90 |
printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n", |
printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n", |
91 |
errors.size(), warnings.size()); |
int(errors.size()), int(warnings.size())); |
92 |
} |
} |
93 |
for (int i = 0; i < issues.size(); ++i) { |
for (int i = 0; i < issues.size(); ++i) { |
94 |
CFmt fmt; |
CFmt fmt; |
98 |
} |
} |
99 |
|
|
100 |
printf("[Dumping parsed VM tree]\n"); |
printf("[Dumping parsed VM tree]\n"); |
101 |
script.dumpParsedScript(); |
vm->dumpParsedScript(parserContext); |
102 |
printf("[End of parsed VM tree]\n"); |
printf("[End of parsed VM tree]\n"); |
103 |
|
|
104 |
if (!errors.empty()) return -1; |
if (!errors.empty()) { |
105 |
|
if (parserContext) delete parserContext; |
106 |
|
return -1; |
107 |
|
} |
108 |
|
|
109 |
|
if (!runScript) { |
110 |
|
return 0; |
111 |
|
} |
112 |
|
|
113 |
if (!script.eventHandler(0)) { |
if (!parserContext->eventHandler(0)) { |
114 |
printf("No event handler exists. So nothing to execute.\n"); |
printf("No event handler exists. So nothing to execute.\n"); |
115 |
|
if (parserContext) delete parserContext; |
116 |
return 0; |
return 0; |
117 |
} |
} |
118 |
|
|
119 |
printf("Preparing execution of script.\n"); |
printf("Preparing execution of script.\n"); |
120 |
VMExecContext* ctx = script.createExecContext(); |
VMExecContext* execContext = vm->createExecContext(parserContext); |
121 |
for (int i = 0; script.eventHandler(i); ++i) { |
for (int i = 0; parserContext->eventHandler(i); ++i) { |
122 |
VMEventHandler* handler = script.eventHandler(i); |
VMEventHandler* handler = parserContext->eventHandler(i); |
123 |
printf("[Running event handler '%s']\n", handler->eventHandlerName().c_str()); |
printf("[Running event handler '%s']\n", handler->eventHandlerName().c_str()); |
124 |
VMExecStatus_t result = script.exec(handler, ctx); |
VMExecStatus_t result = vm->exec(parserContext, execContext, handler); |
125 |
CFmt fmt; |
CFmt fmt; |
126 |
if (result & VM_EXEC_ERROR) { |
if (result & VM_EXEC_ERROR) { |
127 |
fmt.red(); |
fmt.red(); |
129 |
} else if (result & VM_EXEC_SUSPENDED) { |
} else if (result & VM_EXEC_SUSPENDED) { |
130 |
fmt.yellow(); |
fmt.yellow(); |
131 |
printf("[Event handler '%s' returned with SUSPENDED status: %d microseconds]\n", |
printf("[Event handler '%s' returned with SUSPENDED status: %d microseconds]\n", |
132 |
handler->eventHandlerName().c_str(), ctx->suspensionTimeMicroseconds()); |
handler->eventHandlerName().c_str(), execContext->suspensionTimeMicroseconds()); |
133 |
} else if (!(result & VM_EXEC_RUNNING)) { |
} else if (!(result & VM_EXEC_RUNNING)) { |
134 |
fmt.green(); |
fmt.green(); |
135 |
printf("[Event handler '%s' finished with SUCCESS status]\n", handler->eventHandlerName().c_str()); |
printf("[Event handler '%s' finished with SUCCESS status]\n", handler->eventHandlerName().c_str()); |
141 |
printf("[Event handler '%s' finished with UNKNOWN status]\n", handler->eventHandlerName().c_str()); |
printf("[Event handler '%s' finished with UNKNOWN status]\n", handler->eventHandlerName().c_str()); |
142 |
} |
} |
143 |
} |
} |
144 |
if (ctx) delete ctx; |
|
145 |
|
if (parserContext) delete parserContext; |
146 |
|
if (execContext) delete execContext; |
147 |
|
if (vm) delete vm; |
148 |
|
|
149 |
return 0; |
return 0; |
150 |
} |
} |