Class: Cyclotone::Backends::MIDIBackend

Inherits:
Object
  • Object
show all
Includes:
MIDIMessageSupport
Defined in:
lib/cyclotone/backends/midi_backend.rb

Constant Summary collapse

UNIMIDI_AVAILABLE =
begin
  require "unimidi"
  true
rescue LoadError
  false
end

Constants included from MIDIMessageSupport

Cyclotone::Backends::MIDIMessageSupport::FRACTIONAL_NOTE_POLICIES, Cyclotone::Backends::MIDIMessageSupport::SUPPORTED_CC_CONTROLS, Cyclotone::Backends::MIDIMessageSupport::SUPPORTED_NOTE_CONTROLS, Cyclotone::Backends::MIDIMessageSupport::UNSUPPORTED_CONTROL_POLICIES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MIDIMessageSupport

#messages_for

Constructor Details

#initialize(device_name: nil, channel: 0, output: nil, schedule: false, strict_output: false, unsupported_controls: :ignore, fractional_notes: :floor) ⇒ MIDIBackend

Returns a new instance of MIDIBackend.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cyclotone/backends/midi_backend.rb', line 19

def initialize(
  device_name: nil,
  channel: 0,
  output: nil,
  schedule: false,
  strict_output: false,
  unsupported_controls: :ignore,
  fractional_notes: :floor
)
  @channel = channel.to_i
  @output = output || detect_output(device_name)
  @schedule = schedule
  @strict_output = strict_output
  @unsupported_controls = normalize_unsupported_control_policy(unsupported_controls)
  @fractional_notes = normalize_fractional_note_policy(fractional_notes)
  @queue_mutex = Mutex.new
  @queue_cv = ConditionVariable.new
  @scheduled_messages = []
  @closed = false
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



17
18
19
# File 'lib/cyclotone/backends/midi_backend.rb', line 17

def channel
  @channel
end

Class Method Details

.available_outputsObject



41
42
43
44
45
46
47
# File 'lib/cyclotone/backends/midi_backend.rb', line 41

def available_outputs
  return [] unless UNIMIDI_AVAILABLE || defined?(UniMIDI::Output)

  UniMIDI::Output.all
rescue StandardError
  []
end

Instance Method Details

#closeObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cyclotone/backends/midi_backend.rb', line 71

def close
  thread = nil
  @queue_mutex.synchronize do
    @closed = true
    @scheduled_messages.clear
    @queue_cv.broadcast
    thread = @scheduler_thread
  end

  thread&.join(0.5)
  self
end

#flushObject



62
63
64
65
66
67
68
69
# File 'lib/cyclotone/backends/midi_backend.rb', line 62

def flush
  @queue_mutex.synchronize do
    @scheduled_messages.clear
    @queue_cv.signal
  end

  self
end

#panicObject



84
85
86
87
88
89
90
91
# File 'lib/cyclotone/backends/midi_backend.rb', line 84

def panic
  (0..15).each do |panic_channel|
    emit(type: :cc, channel: panic_channel, controller: 123, value: 0, at: Time.now.to_f)
    emit(type: :cc, channel: panic_channel, controller: 120, value: 0, at: Time.now.to_f)
  end

  self
end

#send_event(event, at: Time.now.to_f, cps: nil, **_options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cyclotone/backends/midi_backend.rb', line 50

def send_event(event, at: Time.now.to_f, cps: nil, **_options)
  ensure_output!

  if @schedule
    schedule_messages(messages_for(event, cps: cps), at: at)
  else
    messages_for(event, cps: cps).each { |message| emit(message.merge(at: at)) }
  end
rescue StandardError => error
  raise ConnectionError, error.message
end