Class: MIDI::KeySig

Inherits:
MetaEvent show all
Defined in:
lib/midilib/event.rb

Overview

Container for key signature events

Instance Attribute Summary

Attributes inherited from MetaEvent

#data, #meta_type

Attributes inherited from Event

#delta_time, #print_channel_numbers_from_one, #print_decimal_numbers, #print_note_names, #status, #time_from_start

Instance Method Summary collapse

Methods inherited from MetaEvent

bytes_as_str, #data_as_str, str_as_bytes

Methods inherited from Event

#<=>, #channel_to_s, #number_to_s, #quantize_to

Constructor Details

#initialize(sharpflat, is_minor, delta_time = 0) ⇒ KeySig

Constructor


598
599
600
# File 'lib/midilib/event.rb', line 598

def initialize(sharpflat, is_minor, delta_time = 0)
  super(META_KEY_SIG, [sharpflat, is_minor], delta_time)
end

Instance Method Details

#data_as_bytesObject

Returns the complete event as stored in the sequence


603
604
605
606
607
608
609
610
# File 'lib/midilib/event.rb', line 603

def data_as_bytes
  data = []
  data << @status
  data << @meta_type
  data << 2
  data << @data[0]
  data << (@data[1] ? 1 : 0)
end

#major_key?Boolean

Returns true if it's a major key, false if minor key

Returns:

  • (Boolean)

618
619
620
# File 'lib/midilib/event.rb', line 618

def major_key?
  !@data[1]
end

#minor_key?Boolean

Returns true if it's a minor key, false if major key

Returns:

  • (Boolean)

613
614
615
# File 'lib/midilib/event.rb', line 613

def minor_key?
  @data[1]
end

#sharpflatObject

Returns the number of sharps/flats in the key sig. Negative for flats.


623
624
625
# File 'lib/midilib/event.rb', line 623

def sharpflat
  @data[0] > 7 ? @data[0] - 256 : @data[0]
end

#to_sObject

Returns the key signature as a text string. Example: “key sig A flat major”


629
630
631
632
633
634
635
636
# File 'lib/midilib/event.rb', line 629

def to_s
  majorkeys = ['C flat', 'G flat', 'D flat', 'A flat', 'E flat', 'B flat', 'F',
    'C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#']
  minorkeys = ['a flat', 'e flat', 'b flat', 'f', 'c', 'g', 'd',
    'a', 'e', 'b', 'f#', 'c#', 'g#', 'd#', 'a#']
  minor_key? ? "key sig #{minorkeys[sharpflat + 7]} minor" :
    "key sig #{majorkeys[sharpflat + 7]} major"
end