Class: Ramekin::Instrument

Inherits:
Element
  • Object
show all
Includes:
Error::Helpers
Defined in:
lib/ramekin/meta.rb

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

#directiveObject (readonly)

Returns the value of attribute directive.



270
271
272
# File 'lib/ramekin/meta.rb', line 270

def directive
  @directive
end

#nameObject (readonly)

Returns the value of attribute name.



270
271
272
# File 'lib/ramekin/meta.rb', line 270

def name
  @name
end

#packObject (readonly)

Returns the value of attribute pack.



270
271
272
# File 'lib/ramekin/meta.rb', line 270

def pack
  @pack
end

#pathObject (readonly)

Returns the value of attribute path.



270
271
272
# File 'lib/ramekin/meta.rb', line 270

def path
  @path
end

Instance Method Details

#adsrObject



320
321
322
# File 'lib/ramekin/meta.rb', line 320

def adsr
  @adsr ||= ext_adsr || pack_adsr || [0, 7, 7, 0x1f]
end

#default_error_locationObject



281
282
283
# File 'lib/ramekin/meta.rb', line 281

def default_error_location
  @directive
end

#ext_adsrObject



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_gainObject



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_tuningObject



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

#gainObject



354
355
356
# File 'lib/ramekin/meta.rb', line 354

def gain
  @gain ||= ext_gain || pack_gain || 0x8F
end

#hexesObject



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

#inspectObject



285
286
287
# File 'lib/ramekin/meta.rb', line 285

def inspect
  "inst:@#{name.value} #{@extensions.inspect}"
end

#octaveObject



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_adsrObject



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)

  # unset the first bit, as it is unused
  h1 &= ~0x80

  decay = (7 - (h1 >> 4))
  attack = (15 - (h1 & 0b1111))
  sustain = h2 >> 5
  release = (31 - (h2 & 0b11111))

  [attack, decay, sustain, release]
end

#pack_gainObject



344
345
346
347
# File 'lib/ramekin/meta.rb', line 344

def pack_gain
  _, _, g, _, _ = pack_hexes
  return g
end

#pack_hexesObject



339
340
341
342
# File 'lib/ramekin/meta.rb', line 339

def pack_hexes
  # TODO: alts
  @pack_hexes ||= @pack.tunings_for(@path.value).first
end

#pack_tuningObject



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_nameObject



289
290
291
# File 'lib/ramekin/meta.rb', line 289

def sample_name
  @sample_name ||= File.basename(sample_path)
end

#sample_pathObject



293
294
295
# File 'lib/ramekin/meta.rb', line 293

def sample_path
  @pack.find(@path.value)
end

#to_amkObject



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

#tuningObject



324
325
326
# File 'lib/ramekin/meta.rb', line 324

def tuning
  @tuning ||= ext_tuning || pack_tuning || [4, 0]
end