Class: Musicality::MidiUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/musicality/performance/midi/midi_util.rb

Constant Summary collapse

QUARTER =
Rational(1,4)
PITCH_TO_NOTENUM =
{}
NOTENUM_TO_PITCH =
{}

Class Method Summary collapse

Class Method Details

.delta(duration, ppqn) ⇒ Object

Number of pulses equivalent to the given duration



6
7
8
9
# File 'lib/musicality/performance/midi/midi_util.rb', line 6

def self.delta duration, ppqn
  pulses = (duration / QUARTER) * ppqn
  return pulses.round
end

.dynamic_to_volume(dynamic) ⇒ Object



29
30
31
# File 'lib/musicality/performance/midi/midi_util.rb', line 29

def self.dynamic_to_volume dynamic
  (dynamic * 127).round
end

.note_velocity(attack) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/musicality/performance/midi/midi_util.rb', line 33

def self.note_velocity(attack)
  case attack
  when Attack::NORMAL, Attack::NONE then 70
  when Attack::TENUTO then 90
  when Attack::ACCENT then 112
  else
    raise ArgumentError
  end
end

.notenum_to_pitch(notenum) ⇒ Object



25
26
27
# File 'lib/musicality/performance/midi/midi_util.rb', line 25

def self.notenum_to_pitch notenum
  NOTENUM_TO_PITCH.fetch(notenum)
end

.pitch_to_notenum(pitch) ⇒ Object



21
22
23
# File 'lib/musicality/performance/midi/midi_util.rb', line 21

def self.pitch_to_notenum pitch
  PITCH_TO_NOTENUM.fetch(pitch.round)
end