Class: MIDICommunicationsMacOS::Destination

Inherits:
Object
  • Object
show all
Includes:
Endpoint
Defined in:
lib/midi-communications-macos/destination.rb

Overview

MIDI output endpoint for sending MIDI messages.

A Destination represents a MIDI output that can send messages to external MIDI devices or software. Messages can be sent as numeric bytes, hex strings, or arrays.

Examples:

Send a Note On/Off sequence

output = MIDICommunicationsMacOS::Destination.first
output.open
output.puts(0x90, 60, 100)  # Note On, middle C, velocity 100
sleep(0.5)
output.puts(0x80, 60, 0)    # Note Off

Send as hex string

output.puts_s("903C64")     # Note On
output.puts_s("803C00")     # Note Off

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledBoolean (readonly) Also known as: enabled? Originally defined in module Endpoint

Returns whether the endpoint has been initialized.

Returns:

  • (Boolean)

    whether the endpoint has been initialized

#entityObject (readonly)



26
27
28
# File 'lib/midi-communications-macos/destination.rb', line 26

def entity
  @entity
end

#entityEntity (readonly) Originally defined in module Endpoint

Returns the parent entity.

Returns:

  • (Entity)

    the parent entity

#idInteger Originally defined in module Endpoint

Returns unique local numeric ID of the endpoint.

Returns:

  • (Integer)

    unique local numeric ID of the endpoint

#resource_idInteger (readonly) Originally defined in module Endpoint

Returns Core MIDI resource identifier.

Returns:

  • (Integer)

    Core MIDI resource identifier

#typeObject (readonly) Originally defined in module Endpoint

Class Method Details

.allArray<Destination>

Returns all available output endpoints.

Examples:

outputs = MIDICommunicationsMacOS::Destination.all
outputs.each { |o| puts o.display_name }

Returns:



152
153
154
# File 'lib/midi-communications-macos/destination.rb', line 152

def self.all
  Endpoint.all_by_type[:destination]
end

.firstDestination

Returns the first available output endpoint.

Examples:

output = MIDICommunicationsMacOS::Destination.first

Returns:



134
135
136
# File 'lib/midi-communications-macos/destination.rb', line 134

def self.first
  Endpoint.first(:destination)
end

.lastDestination

Returns the last available output endpoint.

Returns:



141
142
143
# File 'lib/midi-communications-macos/destination.rb', line 141

def self.last
  Endpoint.last(:destination)
end

Instance Method Details

#closeBoolean

Closes this output.

Returns:

  • (Boolean)

    true if closed, false if already closed



31
32
33
34
35
36
37
38
# File 'lib/midi-communications-macos/destination.rb', line 31

def close
  if @enabled
    @enabled = false
    true
  else
    false
  end
end

#display_nameString Originally defined in module Endpoint

Returns formatted display name (delegated to entity).

Returns:

  • (String)

    formatted display name (delegated to entity)

#enable {|destination| ... } ⇒ Destination Also known as: open, start

Opens this output for use.

When a block is given, the output is automatically closed when the block exits.

Examples:

Open with automatic close

output.open do |o|
  o.puts(0x90, 60, 100)
end

Yields:

  • (destination)

    optional block to execute with the open output

Yield Parameters:

Returns:



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/midi-communications-macos/destination.rb', line 114

def enable
  @enabled ||= true
  if block_given?
    begin
      yield(self)
    ensure
      close
    end
  end
  self
end

#initialize(resource_id, entity) ⇒ Object Originally defined in module Endpoint

Parameters:

  • resource_id (Integer)
  • entity (Entity)

#manufacturerString Originally defined in module Endpoint

Returns device manufacturer name (delegated to entity).

Returns:

  • (String)

    device manufacturer name (delegated to entity)

#modelString Originally defined in module Endpoint

Returns device model name (delegated to entity).

Returns:

  • (String)

    device model name (delegated to entity)

#nameString Originally defined in module Endpoint

Returns endpoint name (delegated to entity).

Returns:

  • (String)

    endpoint name (delegated to entity)

#online?Boolean Originally defined in module Endpoint

Is this endpoint online?

Returns:

  • (Boolean)

#puts(*args) ⇒ Boolean Also known as: write

Sends a MIDI message in any supported format.

Accepts multiple formats:

  • Numeric bytes: puts(0x90, 0x40, 0x40)
  • Array of bytes: puts([0x90, 0x40, 0x40])
  • Hex string: puts("904040")

Examples:

Send as bytes

output.puts(0x90, 60, 100)

Send as array

output.puts([0x90, 60, 100])

Send as hex string

output.puts("903C64")

Parameters:

  • args (Array<Integer>, Array<String>, Integer, String)

    MIDI data

Returns:

  • (Boolean)

    true on success



92
93
94
95
96
97
98
# File 'lib/midi-communications-macos/destination.rb', line 92

def puts(*args)
  case args.first
  when Array then args.each { |arg| puts(*arg) }
  when Integer then puts_bytes(*args)
  when String then puts_bytestr(*args)
  end
end

#puts_bytes(*data) ⇒ Boolean

Sends a MIDI message as numeric bytes.

Examples:

output.puts_bytes(0x90, 0x40, 0x40)  # Note On

Parameters:

  • data (Integer)

    numeric bytes (e.g., 0x90, 0x40, 0x40)

Returns:

  • (Boolean)

    true on success



67
68
69
70
71
72
# File 'lib/midi-communications-macos/destination.rb', line 67

def puts_bytes(*data)
  type = sysex?(data) ? :sysex : :small
  bytes = API.get_midi_packet(data)
  send("puts_#{type.to_s}", bytes, data.size)
  true
end

#puts_s(data) ⇒ Boolean Also known as: puts_bytestr, puts_hex

Sends a MIDI message as a hex string.

Examples:

output.puts_s("904060")  # Note On
output.puts_s("804060")  # Note Off

Parameters:

  • data (String)

    hex string (e.g., "904040" for Note On)

Returns:

  • (Boolean)

    true on success



48
49
50
51
52
53
54
55
56
# File 'lib/midi-communications-macos/destination.rb', line 48

def puts_s(data)
  data = data.dup
  bytes = []
  until (str = data.slice!(0, 2)).eql?('')
    bytes << str.hex
  end
  puts_bytes(*bytes)
  true
end