Class: Ramekin::Instrument
Overview
TODO: customization options for instrument
Instance Attribute Summary collapse
Attributes inherited from Element
#channel, #components, #macro_stack, #start_tick
Instance Method Summary
collapse
Methods inherited from Element
#fin, make, #start
Constructor Details
#initialize(pack, directive, name, path, extensions) ⇒ Instrument
Returns a new instance of Instrument.
271
272
273
274
275
276
277
|
# File 'lib/ramekin/meta.rb', line 271
def initialize(pack, directive, name, path, extensions)
@pack = pack
@directive = directive
@name = name
@path = path
@extensions = extensions
end
|
Instance Attribute Details
#directive ⇒ Object
Returns the value of attribute directive.
270
271
272
|
# File 'lib/ramekin/meta.rb', line 270
def directive
@directive
end
|
#name ⇒ Object
Returns the value of attribute name.
270
271
272
|
# File 'lib/ramekin/meta.rb', line 270
def name
@name
end
|
#pack ⇒ Object
Returns the value of attribute pack.
270
271
272
|
# File 'lib/ramekin/meta.rb', line 270
def pack
@pack
end
|
#path ⇒ Object
Returns the value of attribute path.
270
271
272
|
# File 'lib/ramekin/meta.rb', line 270
def path
@path
end
|
Instance Method Details
#adsr ⇒ Object
320
321
322
|
# File 'lib/ramekin/meta.rb', line 320
def adsr
@adsr ||= ext_adsr || pack_adsr || [0, 7, 7, 0x1f]
end
|
#default_error_location ⇒ Object
281
282
283
|
# File 'lib/ramekin/meta.rb', line 281
def default_error_location
@directive
end
|
#ext_adsr ⇒ Object
315
316
317
318
|
# File 'lib/ramekin/meta.rb', line 315
def ext_adsr
adsr = @extensions.select { |e| e.type == :adsr }.last
adsr && Util.adsr_value(adsr)
end
|
#ext_gain ⇒ Object
349
350
351
352
|
# File 'lib/ramekin/meta.rb', line 349
def ext_gain
gain = @extensions.select { |e| e.type == :gain }.last
gain && to_hex(gain.value.to_i(16))
end
|
#ext_tuning ⇒ Object
334
335
336
337
|
# File 'lib/ramekin/meta.rb', line 334
def ext_tuning
tuning = @extensions.select { |e| e.type == :tuning }.last
tuning && tuning.value.scan(/../)
end
|
#gain ⇒ Object
354
355
356
|
# File 'lib/ramekin/meta.rb', line 354
def gain
@gain ||= ext_gain || pack_gain || 0x8F
end
|
#hexes ⇒ Object
358
359
360
361
362
363
364
365
366
367
368
369
|
# File 'lib/ramekin/meta.rb', line 358
def hexes
a, d, s, r = self.adsr
t1, t2 = self.tuning
g = self.gain
return [] unless a && d && s && r && t1 && t2 && g
adsr1 = ((7 - d)*16 | 0x80) + (15 - a)
adsr2 = (s*32 + (31-r))
[adsr1, adsr2, g, t1, t2].map(&method(:to_hex))
end
|
#inspect ⇒ Object
285
286
287
|
# File 'lib/ramekin/meta.rb', line 285
def inspect
"inst:@#{name.value} #{@extensions.inspect}"
end
|
#octave ⇒ Object
386
387
388
389
390
391
392
|
# File 'lib/ramekin/meta.rb', line 386
def octave
@extensions.reverse_each do |ext|
return ext.value.to_i if Token === ext && ext.type == :o
end
4
end
|
#pack_adsr ⇒ Object
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/ramekin/meta.rb', line 297
def pack_adsr
return if pack_hexes.nil?
hex1, hex2, _ = pack_hexes
h1 = hex1.to_i(16)
h2 = hex2.to_i(16)
h1 &= ~0x80
decay = (7 - (h1 >> 4))
attack = (15 - (h1 & 0b1111))
sustain = h2 >> 5
release = (31 - (h2 & 0b11111))
[attack, decay, sustain, release]
end
|
#pack_gain ⇒ Object
344
345
346
347
|
# File 'lib/ramekin/meta.rb', line 344
def pack_gain
_, _, g, _, _ = pack_hexes
return g
end
|
#pack_hexes ⇒ Object
339
340
341
342
|
# File 'lib/ramekin/meta.rb', line 339
def pack_hexes
@pack_hexes ||= @pack.tunings_for(@path.value).first
end
|
#pack_tuning ⇒ Object
328
329
330
331
332
|
# File 'lib/ramekin/meta.rb', line 328
def pack_tuning
_, _, _, d, e = pack_hexes
return nil unless d && e
[d, e]
end
|
#sample_name ⇒ Object
289
290
291
|
# File 'lib/ramekin/meta.rb', line 289
def sample_name
@sample_name ||= File.basename(sample_path)
end
|
#sample_path ⇒ Object
293
294
295
|
# File 'lib/ramekin/meta.rb', line 293
def sample_path
@pack.find(@path.value)
end
|
#to_amk ⇒ Object
377
378
379
380
381
382
383
384
|
# File 'lib/ramekin/meta.rb', line 377
def to_amk
unless @pack.has?(@path.value)
error! "no sample named #{path.value.inspect} in pack #{pack.name.inspect}", el: @path
return ''
end
"#{File.basename(sample_name).inspect} #{hexes.map { |h| "$#{h}" }.join(' ')}"
end
|
#to_hex(x) ⇒ Object
371
372
373
374
375
|
# File 'lib/ramekin/meta.rb', line 371
def to_hex(x)
return x if x.is_a?(String)
sprintf("%02x", x)
end
|
#tuning ⇒ Object
324
325
326
|
# File 'lib/ramekin/meta.rb', line 324
def tuning
@tuning ||= ext_tuning || pack_tuning || [4, 0]
end
|