Class: Musa::MIDIVoices::MIDIVoice

Inherits:
Object
  • Object
show all
Defined in:
lib/musa-dsl/midi/midi-voices.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequencer:, output:, channel:, name: nil, do_log: nil) ⇒ MIDIVoice

Returns a new instance of MIDIVoice.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/musa-dsl/midi/midi-voices.rb', line 51

def initialize(sequencer:, output:, channel:, name: nil, do_log: nil)
  do_log ||= false

  @sequencer = sequencer
  @output = output
  @channel = channel
  @name = name
  @do_log = do_log

  @tick_duration = Rational(1, @sequencer.ticks_per_bar)

  @controllers_control = ControllersControl.new(@output, @channel)

  @active_pitches = []
  fill_active_pitches @active_pitches

  @sequencer.logger.warn 'voice without output' unless @output

  self
end

Instance Attribute Details

#active_pitchesObject (readonly)

Returns the value of attribute active_pitches.



49
50
51
# File 'lib/musa-dsl/midi/midi-voices.rb', line 49

def active_pitches
  @active_pitches
end

#channelObject (readonly)

Returns the value of attribute channel.



49
50
51
# File 'lib/musa-dsl/midi/midi-voices.rb', line 49

def channel
  @channel
end

#do_logObject

Returns the value of attribute do_log.



48
49
50
# File 'lib/musa-dsl/midi/midi-voices.rb', line 48

def do_log
  @do_log
end

#nameObject

Returns the value of attribute name.



48
49
50
# File 'lib/musa-dsl/midi/midi-voices.rb', line 48

def name
  @name
end

#outputObject (readonly)

Returns the value of attribute output.



49
50
51
# File 'lib/musa-dsl/midi/midi-voices.rb', line 49

def output
  @output
end

#sequencerObject (readonly)

Returns the value of attribute sequencer.



49
50
51
# File 'lib/musa-dsl/midi/midi-voices.rb', line 49

def sequencer
  @sequencer
end

#tick_durationObject (readonly)

Returns the value of attribute tick_duration.



49
50
51
# File 'lib/musa-dsl/midi/midi-voices.rb', line 49

def tick_duration
  @tick_duration
end

Instance Method Details

#all_notes_offObject



113
114
115
116
117
118
# File 'lib/musa-dsl/midi/midi-voices.rb', line 113

def all_notes_off
  @active_pitches.clear
  fill_active_pitches @active_pitches

  @output.puts MIDIEvents::ChannelMessage.new(0xb, @channel, 0x7b, 0)
end

#controllerObject



101
102
103
# File 'lib/musa-dsl/midi/midi-voices.rb', line 101

def controller
  @controllers_control
end

#fast_forward=(enabled) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/musa-dsl/midi/midi-voices.rb', line 72

def fast_forward=(enabled)
  if @fast_forward && !enabled
    (0..127).each do |pitch|
      @output.puts MIDIEvents::NoteOn.new(@channel, pitch, @active_pitches[pitch][:velocity]) unless @active_pitches[pitch][:note_controls].empty?
    end
  end

  @fast_forward = enabled
end

#fast_forward?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/musa-dsl/midi/midi-voices.rb', line 82

def fast_forward?
  @fast_forward
end

#log(msg) ⇒ Object



120
121
122
# File 'lib/musa-dsl/midi/midi-voices.rb', line 120

def log(msg)
  @sequencer.logger.info('MIDIVoice') { "voice #{name || @channel}: #{msg}" } if @do_log
end

#note(pitchvalue = nil, pitch: nil, velocity: nil, duration: nil, duration_offset: nil, note_duration: nil, velocity_off: nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/musa-dsl/midi/midi-voices.rb', line 86

def note(pitchvalue = nil, pitch: nil, velocity: nil, duration: nil, duration_offset: nil, note_duration: nil, velocity_off: nil)
  pitch ||= pitchvalue

  if pitch
    velocity ||= 63

    duration_offset ||= -@tick_duration
    note_duration ||= [0, duration + duration_offset].max

    velocity_off ||= 63

    NoteControl.new(self, pitch: pitch, velocity: velocity, duration: note_duration, velocity_off: velocity_off).note_on
  end
end

#sustain_pedalObject



109
110
111
# File 'lib/musa-dsl/midi/midi-voices.rb', line 109

def sustain_pedal
  @controllers_control[:sustain_pedal]
end

#sustain_pedal=(value) ⇒ Object



105
106
107
# File 'lib/musa-dsl/midi/midi-voices.rb', line 105

def sustain_pedal=(value)
  @controllers_control[:sustain_pedal] = value
end

#to_sObject



124
125
126
# File 'lib/musa-dsl/midi/midi-voices.rb', line 124

def to_s
  "voice #{@name} output: #{@output} channel: #{@channel}"
end