aboutsummaryrefslogblamecommitdiffstats
path: root/plugins/LV2/makeplugin
blob: be152b16945da78d36fdb153ce01bae4d0bd706b (plain) (tree)
1
2
3
4
5
6
7
8
9

                   



                     

                           
                                                  


                   
                           


                                              
                          




                        

                          
                         
                                                                          
























                                        
                  
                                        










                                                








                                                
                                          










                                                   






















                                                                                                                






                                    

                








                                                                                                
                                        
                                  
                                                














                                             


                                                                             
                                                                












                                                       
#!/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