aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/LV2/makeplugin
blob: be152b16945da78d36fdb153ce01bae4d0bd706b (plain) (blame)
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
#!/usr/bin/env ruby

def param_sym(n)
    ('A'.ord + n).chr
end

def make_param_assigns(num)
  (0 ... num)
    .map { |n| "#{param_sym(n)} = *param[#{n}];" }
    .join("\n    ")
end

def make_ctrl_port(n, name)
  %Q{      a lv2:InputPort , lv2:ControlPort ;
      lv2:index #{n} ;
      lv2:symbol "#{param_sym(n)}" ;
      lv2:name "#{name}" ;
      lv2:default 0.5 ;
      lv2:minimum 0.0 ;
      lv2:maximum 1.0 ;}
end

def make_lv2_port_defs(pi)
  num = pi[:n_ctrl_ports]
  ctrlports = (0 ... num)
    .map { |n| make_ctrl_port(n, pi["ctrl_port_#{param_sym(n)}".to_sym]) }
    .join("\n    ] , [\n")

  ctrlports + %Q{
    ] , [
      a lv2:InputPort , lv2:AudioPort ;
      lv2:index #{num} ;
      lv2:symbol "in_l" ;
      lv2:name "In left" ;
    ] , [
      a lv2:InputPort , lv2:AudioPort ;
      lv2:index #{num + 1} ;
      lv2:symbol "in_r" ;
      lv2:name "In right" ;
    ] , [
      a lv2:OutputPort , lv2:AudioPort ;
      lv2:index #{num + 2} ;
      lv2:symbol "out_l" ;
      lv2:name "Out left" ;
    ] , [
      a lv2:OutputPort , lv2:AudioPort ;
      lv2:index #{num + 3} ;
      lv2:symbol "out_r" ;
      lv2:name "Out right" ;}
end

if ARGV.length < 1
  puts "Usage: #{__FILE__} <PluginName>"
  exit
end

root = File.dirname(__FILE__)

plugin_name = ARGV[0]
plugin_slug = plugin_name.downcase

target_dir = File.join(root, 'src', plugin_name)
template_dir = File.join(root, 'Template')

PLUGIN_FIELDS = %w{Type Use Description Comment}

def find_plugin_info(db, plugin_name)
  pi = { name: plugin_name }
  found = false

  File.open(db) do |f|
    pat = /^\s*(\w+): \"([^"]+)\",/
    f.each_line do |line|
      break if found && line =~ /^\s*\},$/

      pat.match(line) do |m|
        if m[1] == 'Name' && m[2] == plugin_name
          found = true
        elsif found && PLUGIN_FIELDS.include?(m[1])
          pi[m[1].downcase.to_sym] = m[2]
        end
      end
    end
  end

  header_file = File.join('../WinVST', plugin_name, "#{plugin_name}.h")

  File.open(header_file) do |f|
    f.each_line do |line|
      if line =~ /kNumParameters\s*=\s*(\d+)/
          pi[:n_ctrl_ports] = $1.to_i
      end
    end
  end

  class_file = File.join('../WinVST', plugin_name, "#{plugin_name}.cpp")
  fun = ""

  File.open(class_file) do |f|
    f.each_line do |line|
      if line =~ /void #{plugin_name}::(\w+)/
        fun = $1
      elsif fun == 'getParameterName' and line =~ /case\s+kParam([A-Z])\:\s+vst_strncpy\s*\([^,]*,\s*\"(\w+)\",/
        pi["ctrl_port_#{$1}".to_sym] = $2
      end
    end
  end

  return pi
end

def get_lv2_plugin_type(plugin_info)
  case plugin_info[:type]
    when 'Saturation'
      'DistortionPlugin'
    when 'EQ'
      'EQPlugin'
    else
      'EffectsPlugin'
  end
end

puts "Reading plugin info from cheatsheet..."
plugin_info = find_plugin_info(File.join(root, '../../cheatsheet/src/database.js'), plugin_name)


puts "Creating plugin #{plugin_name}..."
puts "Type: #{plugin_info[:type]}"
puts "CTRL ports: #{plugin_info[:n_ctrl_ports]}"

Templates = %w{
    manifest.ttl.in
    Plugin.ttl
    Plugin.h
    Plugin.cpp
}

begin
  Dir.mkdir(target_dir)

  Templates.each do |t|
    tpl = IO.read(File.join(template_dir, t))
      .gsub('@Plugin@', plugin_name)
      .gsub('@plugin@', plugin_slug)
      .gsub('@NCtrlPorts@', plugin_info[:n_ctrl_ports].to_s)
      .gsub('@AssignParams@', make_param_assigns(plugin_info[:n_ctrl_ports]))
      .gsub('@TTLPortDefs@', make_lv2_port_defs(plugin_info))
      .gsub('@TTLPluginType@', get_lv2_plugin_type(plugin_info))

    fname = t.sub('Plugin', plugin_name)

    puts " - #{fname}..."
    File.open(File.join(target_dir, fname), "w") do |f|
      f.write(tpl)
    end
  end

rescue SystemCallError => e
  puts "Could not create plugin: #{e}"
  puts "Aborting!"
end