-
Notifications
You must be signed in to change notification settings - Fork 0
/
lv2spectohpp.php
195 lines (171 loc) · 4.35 KB
/
lv2spectohpp.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env php
<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
try
{
$output_buffer='';
function ob_file_callback($buffer)
{
global $output_buffer;
$output_buffer.=$buffer;
}
ob_start('ob_file_callback');
function CamelCase(string $str)
{
//TODO: Use preg_split instead...
$x=explode(' ',$str);
foreach($x as &$namepart)
{$namepart=ucfirst($namepart);}
return implode('',$x);
}
function guardname(string $scope,string $filename)
{
//TODO: Use preg_split instead...
$x=[$scope];
$x=array_merge($x,explode('.',$filename));
foreach($x as &$namepart)
{$namepart=strtoupper($namepart);}
return implode('_',$x);
}
function cxxconstant(string $name)
{
//TODO: Use preg_split instead...
$x=explode(' ',$name);
foreach($x as &$namepart)
{$namepart=strtoupper($namepart);}
return implode('_',$x);
}
function typeGet($port)
{
$ret='';
if($port->{'direction'}=='in')
{$ret.='const ';}
$typemap=array('midi'=>'LV2_Atom_Sequence*'
,'audio'=>'float*'
,'control'=>'float');
$ret.=$typemap[$port->{'type'}];
return $ret;
}
function nameGet($port)
{
$ret='';
if(isset($port->{'prefix'}))
{$ret.=$port->{'prefix'}.' ';}
$ret.=$port->{'name'};
return $ret;
}
if($argc<2)
{
error_log('No input file is specified');
exit(-1);
}
if($argc<3)
{
error_log('No target directory is specified');
exit(-1);
}
$content=json_decode(file_get_contents($argv[1]));
$plugindata=$content->{'specification'};
$name=$plugindata->{'name'};
$namespace=CamelCase($name);
$guard=guardname($namespace,$content->{'targets'}[0]->{'name'});
$now=new DateTime();
?>
// <?=$name?> descriptor include file
//
// This file has been generated from <?=$argv[1]?> <?=$now->format('Y-m-d H:i') ?> by
// <?=$argv[0]?>. Changes to this file may be overwritten
// during compilation of the project.
#ifndef <?=$guard?>
#define <?=$guard?>
#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
#include <lv2/lv2plug.in/ns/ext/atom/util.h>
namespace <?=$namespace?>
{
struct PluginDescriptor
{
<?php foreach($plugindata as $field => $value)
{
if(!is_array($value) && !is_object($value))
{
$constant=cxxconstant($field);?>
static constexpr const char* <?=strtolower($constant)?>() noexcept
{return "<?=$value?>";}
<?php }
} ?>
static constexpr unsigned int portCount()
{return <?=count($plugindata->{'ports'}) ?>;}
<?php if(isset($plugindata->{'ui'})) {?>
static constexpr const char* uiUri() noexcept
{return "<?=$plugindata->{'ui'}->{'uri'} ?>";}
<?php }?>
struct Ports
{
<?php foreach($plugindata->{'ports'} as $index=>$port) {
$name_full=nameGet($port);
?>
static constexpr unsigned int <?=cxxconstant($name_full)?>=<?=$index ?>;
<?php }?>
static constexpr const char* PORTNAMES[]={<?php foreach($plugindata->{'ports'} as $index=>$port)
{
echo '"'.$port->{'name'}.'",';
}?>nullptr};
};
template<unsigned int port_id>
struct Port
{
typedef void type;
typedef void type_logical;
};
};
<?php foreach($plugindata->{'ports'} as $index=>$port) {
$name_full=nameGet($port);
?>
template<>
struct PluginDescriptor::Port<PluginDescriptor::Ports::<?=cxxconstant($name_full)?>>
{
typedef <?=typeGet($port)?> type;
<?php if(isset($port->{'ui_hint'})){ ?>
typedef <?=$port->{'ui_hint'}?> type_logical;
<?php } else {?>
typedef type type_logical;
<?php } ?>
static constexpr unsigned int id() noexcept
{return <?=$index?>;}
static constexpr const char* name() noexcept
{return "<?=$port->{'name'}?>";}
<?php if(isset($port->{'prefix'})){ ?>
static constexpr const char* prefix() noexcept
{return "<?=$port->{'prefix'}?>";}
<?php } ?>
<?php if(isset($port->{'minimum'})){ ?>
static constexpr type minimum() noexcept
{return static_cast<type>(<?=$port->{'minimum'}?>);}
<?php } ?>
<?php if(isset($port->{'maximum'})){ ?>
static constexpr type maximum() noexcept
{return static_cast<type>(<?=$port->{'maximum'}?>);}
<?php } ?>
<?php if(isset($port->{'default'})){ ?>
static constexpr type valueDefault() noexcept
{return static_cast<type>(<?=$port->{'default'}?>);}
<?php } ?>
};
<?php }?>
}
#endif
<?php
$ob_file=fopen($argv[2].'/'.$content->{'targets'}[0]->{'name'},'w','w');
ob_flush();
fwrite($ob_file,$output_buffer);
}
catch(Exception $e)
{
error_log($e->getMessage());
exit(-1);
}
exit(0);
?>