Created
July 20, 2014 20:58
-
-
Save wrl/f9cc3c8a892e185a0f88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VstFxProgram | |
STRUCT_FORMAT = [ | |
'l>', # int32 size | |
'a4', # int32 fx magic | |
'l>', # format version | |
'a4', # vst fx unique id | |
'l>', # vst fx version | |
'l>', # nparams | |
'Z28', # program name | |
'a*' # the rest | |
] | |
attr_reader :name | |
attr_reader :for_fx | |
def self.from_file(path) | |
s = IO.read(path) | |
raise 'Invalid chunk magic' unless s.slice!(0..3) == 'CcnK' | |
size, | |
fx_magic, | |
version, | |
fx_unique_id, | |
fx_version, | |
nparams, | |
program_name, | |
data = s.unpack(STRUCT_FORMAT.join '') | |
case fx_magic | |
when 'FxCk' | |
params = data.unpack('g%d' % nparams) | |
return VstFxRegularProgram.new(program_name, fx_unique_id, params) | |
when 'FPCh' | |
size, data = data.unpack('l>a*') | |
data = data.unpack('a%d' % size) | |
return VstFxChunkProgram.new(program_name, fx_unique_id, data) | |
else | |
raise 'Invalid FX magic "%s"' % fx_magic | |
end | |
end | |
def dump | |
payload = self.dump_data | |
['CcnK', | |
payload.length + 20 + 28, | |
self.fx_magic, | |
1, | |
@for_fx, | |
1, | |
self.nparams, | |
@name, | |
payload].pack('a4' + (STRUCT_FORMAT.join '')) | |
end | |
def nparams | |
raise NotImplementedError | |
end | |
def fx_magic | |
raise NotImplementedError | |
end | |
def dump_data | |
raise NotImplementedError | |
end | |
end | |
class VstFxRegularProgram < VstFxProgram | |
attr_reader :params | |
def initialize(name, for_fx, params) | |
@name = name | |
@for_fx = for_fx | |
@params = params | |
end | |
class <<self | |
undef_method :from_file | |
end | |
def nparams | |
@params.length | |
end | |
def fx_magic | |
'FxCk' | |
end | |
def dump_data | |
self.params.pack('g*') | |
end | |
end | |
class VstFxChunkProgram < VstFxProgram | |
attr_reader :data | |
def initialize(name, for_fx, data) | |
@name = name | |
@for_fx = for_fx | |
@data = data | |
end | |
def nparams | |
0 | |
end | |
def fx_magic | |
'FPCh' | |
end | |
def dump_data | |
payload = @data + "\x00" | |
[payload.length, payload].pack('l>a*') | |
end | |
class <<self | |
undef_method :from_file | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment