Class: Cyclotone::Stream

Inherits:
Object
  • Object
show all
Includes:
Transition
Defined in:
lib/cyclotone/stream.rb

Constant Summary collapse

NullBackend =
Backends::NullBackend

Constants included from Transition

Transition::DEFAULT_DURATION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transition

#anticipate, #clutch, #clutch_in, #fade_in, #fade_out, #interpolate, #interpolate_in, #jump, #jump_in, #xfade, #xfade_in

Constructor Details

#initialize(backend: nil, scheduler: nil) ⇒ Stream

Returns a new instance of Stream.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cyclotone/stream.rb', line 19

def initialize(backend: nil, scheduler: nil)
  @slots = {}
  @slot_options = {}
  @transitions = {}
  @muted = Set.new
  @soloed = Set.new
  @fallback_error = nil
  @scheduler = scheduler || Scheduler.new(backend: backend || default_backend)
rescue StandardError => error
  @fallback_error = error
  @scheduler = Scheduler.new(backend: Backends::NullBackend.new)
end

Instance Attribute Details

#fallback_errorObject (readonly)

Returns the value of attribute fallback_error.



15
16
17
# File 'lib/cyclotone/stream.rb', line 15

def fallback_error
  @fallback_error
end

#schedulerObject (readonly)

Returns the value of attribute scheduler.



15
16
17
# File 'lib/cyclotone/stream.rb', line 15

def scheduler
  @scheduler
end

Class Method Details

.instanceObject



10
11
12
# File 'lib/cyclotone/stream.rb', line 10

def instance
  @instance ||= new
end

Instance Method Details

#d(slot_id, pattern, cps: nil, phase: 0) ⇒ Object



32
33
34
# File 'lib/cyclotone/stream.rb', line 32

def d(slot_id, pattern, cps: nil, phase: 0)
  assign(normalize_d_slot_id(slot_id), pattern, cps: cps, phase: phase)
end

#hush(mode: :silence) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cyclotone/stream.rb', line 40

def hush(mode: :silence)
  case mode
  when :silence
    @slots.each_key { |slot_id| assign(slot_id, Pattern.silence) }
  when :mute
    @muted.merge(@slots.keys)
    sync_scheduler
  when :clear
    @slots.each_key { |slot_id| @scheduler.remove_pattern(slot_id) }
    @slots.clear
    @slot_options.clear
    @transitions.clear
    @muted.clear
    @soloed.clear
  else
    raise ArgumentError, "unknown hush mode #{mode}"
  end

  self
end

#mtrigger(period) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cyclotone/stream.rb', line 123

def mtrigger(period)
  normalized_period = period.to_i
  raise ArgumentError, "mtrigger period must be positive" unless normalized_period.positive?

  current_cycle = @scheduler.current_cycle
  next_cycle = current_cycle.ceil
  remainder = next_cycle % normalized_period
  target_cycle = remainder.zero? ? next_cycle : next_cycle + (normalized_period - remainder)

  wait_until_cycle(target_cycle)
end

#mute(id) ⇒ Object



71
72
73
74
# File 'lib/cyclotone/stream.rb', line 71

def mute(id)
  @muted << normalize_slot_reference(id)
  sync_scheduler
end

#p(name, pattern, cps: nil, phase: 0) ⇒ Object



36
37
38
# File 'lib/cyclotone/stream.rb', line 36

def p(name, pattern, cps: nil, phase: 0)
  assign(normalize_slot_reference(name), pattern, cps: cps, phase: phase)
end

#qtriggerObject



119
120
121
# File 'lib/cyclotone/stream.rb', line 119

def qtrigger
  wait_until_cycle(@scheduler.current_cycle.ceil)
end

#reset_cyclesObject



86
87
88
89
# File 'lib/cyclotone/stream.rb', line 86

def reset_cycles
  @scheduler.reset_cycles
  self
end

#running?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/cyclotone/stream.rb', line 111

def running?
  @scheduler.running?
end

#set_cycle(value) ⇒ Object



91
92
93
94
# File 'lib/cyclotone/stream.rb', line 91

def set_cycle(value)
  @scheduler.set_cycle(value)
  self
end

#setcps(value) ⇒ Object



81
82
83
84
# File 'lib/cyclotone/stream.rb', line 81

def setcps(value)
  @scheduler.setcps(value)
  self
end

#slot(slot_id) ⇒ Object



135
136
137
138
139
# File 'lib/cyclotone/stream.rb', line 135

def slot(slot_id)
  normalized = normalize_slot_reference(slot_id)
  simplify_completed_transition(normalized)
  @slots[normalized]
end

#solo(id) ⇒ Object



61
62
63
64
# File 'lib/cyclotone/stream.rb', line 61

def solo(id)
  @soloed << normalize_slot_reference(id)
  sync_scheduler
end

#startObject



101
102
103
104
# File 'lib/cyclotone/stream.rb', line 101

def start
  @scheduler.start
  self
end

#stopObject



106
107
108
109
# File 'lib/cyclotone/stream.rb', line 106

def stop
  @scheduler.stop
  self
end

#triggerObject



115
116
117
# File 'lib/cyclotone/stream.rb', line 115

def trigger
  wait_until_cycle(@scheduler.current_cycle + (@scheduler.interval * @scheduler.cps))
end

#unmute(id) ⇒ Object



76
77
78
79
# File 'lib/cyclotone/stream.rb', line 76

def unmute(id)
  @muted.delete(normalize_slot_reference(id))
  sync_scheduler
end

#unsolo(id) ⇒ Object



66
67
68
69
# File 'lib/cyclotone/stream.rb', line 66

def unsolo(id)
  @soloed.delete(normalize_slot_reference(id))
  sync_scheduler
end

#use_backend(backend) ⇒ Object



96
97
98
99
# File 'lib/cyclotone/stream.rb', line 96

def use_backend(backend)
  @scheduler.backend = backend
  sync_scheduler
end