Class: MIDIEvents::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/midi-events/context.rb

Overview

DSL for creating MIDI messages with shared context

Provides a convenient way to create multiple MIDI messages that share common parameters (like channel and velocity) without repeating them for each message.

Examples:

Basic context usage

MIDIEvents.with(channel: 0, velocity: 100) do
  note_on("C4")   # Creates NoteOn with channel 0, velocity 100
  note_off("C4")  # Creates NoteOff with channel 0, velocity 100
end

Override context parameters

MIDIEvents.with(channel: 0, velocity: 100) do
  note_on("C4")                  # Uses context: channel 0, velocity 100
  note_on("E4", velocity: 127)   # Overrides velocity to 127
end

Control changes in context

MIDIEvents.with(channel: 0) do
  control_change("Modulation Wheel", 64)
  program_change("Acoustic Grand Piano")
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Context

Returns a new instance of Context.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)
  • :velocity (Fixnum)


49
50
51
52
# File 'lib/midi-events/context.rb', line 49

def initialize(options = {})
  @channel = options[:channel]
  @velocity = options[:velocity]
end

Instance Attribute Details

#channelInteger?

Returns The MIDI channel (0-15) for messages created in this context.

Returns:

  • (Integer, nil)

    The MIDI channel (0-15) for messages created in this context



30
31
32
# File 'lib/midi-events/context.rb', line 30

def channel
  @channel
end

#velocityInteger?

Returns The velocity (0-127) for note messages created in this context.

Returns:

  • (Integer, nil)

    The velocity (0-127) for note messages created in this context



33
34
35
# File 'lib/midi-events/context.rb', line 33

def velocity
  @velocity
end

Class Method Details

.with(options = {}, &block) ⇒ Object

Create and execute a context with the given parameters

Parameters:

  • options (Hash) (defaults to: {})

    Context parameters

  • block (Proc)

    The block to execute within this context

Options Hash (options):

  • :channel (Integer)

    MIDI channel (0-15)

  • :velocity (Integer)

    Note velocity (0-127)

Returns:

  • (Object)

    The result of evaluating the block



42
43
44
# File 'lib/midi-events/context.rb', line 42

def self.with(options = {}, &block)
  new(options, &block).instance_eval(&block)
end

Instance Method Details

#channel_aftertouch(value, options = {}) ⇒ Object Also known as: ChannelAftertouch, ChannelPressure, channel_pressure

A channel pressure message

Parameters:

  • value (Fixnum)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)


153
154
155
156
157
158
# File 'lib/midi-events/context.rb', line 153

def channel_aftertouch(value, options = {})
  channel = options[:channel] || @channel
  raise 'channel_aftertouch requires a channel' if channel.nil?

  ChannelAftertouch.new(channel, value, options)
end

#control_change(index, value, options = {}) ⇒ Object Also known as: ControlChange, Controller, controller

A control change message

Parameters:

  • index (Fixnum, String)
  • value (Fixnum)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)
  • :velocity (Fixnum)


112
113
114
115
116
117
118
119
120
121
# File 'lib/midi-events/context.rb', line 112

def control_change(index, value, options = {})
  channel = options[:channel] || @channel
  raise 'control_change requires channel' if channel.nil?

  if index.is_a?(String)
    ControlChange[index].new(channel, value, options)
  else
    ControlChange.new(channel, index, value, options)
  end
end

#note_off(note, options = {}) ⇒ Object Also known as: NoteOff

A note off message

Parameters:

  • note (Fixnum, String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)
  • :velocity (Fixnum)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/midi-events/context.rb', line 59

def note_off(note, options = {})
  channel = options[:channel] || @channel
  velocity = options[:velocity] || @velocity
  raise 'note_off requires both channel and velocity' if channel.nil? || velocity.nil?
  
  if note.is_a?(String)
    NoteOff[note].new(channel, velocity, options)
  else
    NoteOff.new(channel, note, velocity, options)
  end
end

#note_on(note, options = {}) ⇒ Object Also known as: NoteOn

A note on message

Parameters:

  • note (Fixnum, String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)
  • :velocity (Fixnum)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/midi-events/context.rb', line 77

def note_on(note, options = {})
  channel = options[:channel] || @channel
  velocity = options[:velocity] || @velocity
  raise 'note_on requires both channel and velocity' if channel.nil? || velocity.nil?

  if note.is_a?(String)
    NoteOn[note].new(channel, velocity, options)
  else
    NoteOn.new(channel, note, velocity, options)
  end
end

#pitch_bend(low, high, options = {}) ⇒ Object Also known as: PitchBend

A poly pressure message

Parameters:

  • low (Fixnum)
  • high (Fixnum)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)


168
169
170
171
172
173
# File 'lib/midi-events/context.rb', line 168

def pitch_bend(low, high, options = {})
  channel = options[:channel] || @channel
  raise 'channel_aftertouch requires a channel' if channel.nil?

  PitchBend.new(channel, low, high, options)
end

#polyphonic_aftertouch(note, value, options = {}) ⇒ Object Also known as: PolyphonicAftertouch, PolyAftertouch, PolyphonicPressure, PolyPressure, poly_aftertouch, poly_pressure

A poly pressure message

Parameters:

  • note (Fixnum, String)
  • value (Fixnum)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)


131
132
133
134
135
136
137
138
139
140
# File 'lib/midi-events/context.rb', line 131

def polyphonic_aftertouch(note, value, options = {})
  channel = options[:channel] || @channel
  raise 'channel_aftertouch requires a channel' if channel.nil?

  if note.is_a?(String)
    PolyphonicAftertouch[note].new(channel, value, options)
  else
    PolyphonicAftertouch.new(channel, note, value, options)
  end
end

#program_change(program, options = {}) ⇒ Object Also known as: ProgramChange

A program change message

Parameters:

  • program (Fixnum, String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)


94
95
96
97
98
99
100
101
102
103
# File 'lib/midi-events/context.rb', line 94

def program_change(program, options = {})
  channel = options[:channel] || @channel
  raise 'program_change requires channel' if channel.nil?

  if program.is_a?(String)
    ProgramChange[program].new(channel, options)
  else
    ProgramChange.new(channel, program, options)
  end
end