Sha256: dcd08281360b638f1e105ab122094a2b15f0e7c410b5bdc74e3f0f6c1512cab8
Contents?: true
Size: 1.25 KB
Versions: 3
Compression:
Stored size: 1.25 KB
Contents
# frozen_string_literal: true # A pitch class is a set of notes separated by octaves. class HeadMusic::PitchClass attr_reader :number SHARP_SPELLINGS = %w[C C# D D# E F F# G G# A A# B].freeze FLAT_SPELLINGS = %w[C Db D Eb E F Gb G Ab A Bb B].freeze def self.get(identifier) @pitch_classes ||= {} number = HeadMusic::Spelling.get(identifier).pitch_class.to_i if HeadMusic::Spelling.matching_string(identifier) number ||= identifier.to_i % 12 @pitch_classes[number] ||= new(number) end class << self alias [] get end def initialize(pitch_class_or_midi_number) @number = pitch_class_or_midi_number.to_i % 12 end def to_i number end # Pass in the number of semitones def +(other) HeadMusic::PitchClass.get(to_i + other.to_i) end # Pass in the number of semitones def -(other) HeadMusic::PitchClass.get(to_i - other.to_i) end def ==(other) to_i == other.to_i end alias enharmonic? == def intervals_to(other) delta = other.to_i - to_i inverse = delta.positive? ? delta - 12 : delta + 12 [delta, inverse].sort_by(&:abs).map { |interval| HeadMusic::Interval.get(interval) } end def smallest_interval_to(other) intervals_to(other).first end private_class_method :new end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
head_music-0.19.1 | lib/head_music/pitch_class.rb |
head_music-0.19.0 | lib/head_music/pitch_class.rb |
head_music-0.18.0 | lib/head_music/pitch_class.rb |