Class: Xi::Stream

Inherits:
Object show all
Defined in:
lib/xi/stream.rb

Direct Known Subclasses

Xi::Supercollider::Stream

Constant Summary collapse

DEFAULT_PARAMS =
{
  degree: 0,
  octave: 5,
  root:   0,
  scale:  Xi::Scale.major,
  steps_per_octave: 12,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, clock, **opts) ⇒ Stream

Returns a new instance of Stream.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xi/stream.rb', line 16

def initialize(name, clock, **opts)
  Array(opts.delete(:include)).each { |m| include_mixin(m) }

  @name = name.to_sym
  @opts = opts

  @mutex = Mutex.new
  @playing = false
  @last_sound_object_id = 0
  @state = {}
  @changed_params = [].to_set
  @playing_sound_objects = {}
  @prev_ts = {}
  @prev_delta = {}

  self.clock = clock
end

Instance Attribute Details

#clockObject

Returns the value of attribute clock.



6
7
8
# File 'lib/xi/stream.rb', line 6

def clock
  @clock
end

#deltaObject

Returns the value of attribute delta.



6
7
8
# File 'lib/xi/stream.rb', line 6

def delta
  @delta
end

#gateObject

Returns the value of attribute gate.



6
7
8
# File 'lib/xi/stream.rb', line 6

def gate
  @gate
end

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/xi/stream.rb', line 6

def opts
  @opts
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/xi/stream.rb', line 6

def source
  @source
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/xi/stream.rb', line 6

def state
  @state
end

Instance Method Details

#inspectObject



97
98
99
100
101
102
103
# File 'lib/xi/stream.rb', line 97

def inspect
  "#<#{self.class.name} :#{@name} " \
    "#{playing? ? :playing : :stopped} at #{@clock.cps}cps" \
    "#{" #{@opts}" if @opts.any?}>"
rescue => err
  error(err)
end

#notify(now, cps) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/xi/stream.rb', line 105

def notify(now, cps)
  return unless playing? && @source

  @mutex.synchronize do
    @changed_params.clear

    update_all_state if @reset

    gate_off = gate_off_old_sound_objects(now)
    gate_on = play_enums(now, cps)

    # Call hooks
    do_gate_off_change(gate_off) unless gate_off.empty?
    do_state_change if state_changed?
    do_gate_on_change(gate_on) unless gate_on.empty?
  end
end

#playObject Also known as: start, pause



76
77
78
79
80
81
82
# File 'lib/xi/stream.rb', line 76

def play
  @mutex.synchronize do
    @playing = true
    @clock.subscribe(self)
  end
  self
end

#playing?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/xi/stream.rb', line 68

def playing?
  @mutex.synchronize { @playing }
end

#set(delta: nil, gate: nil, **source) ⇒ Object Also known as: call



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xi/stream.rb', line 34

def set(delta: nil, gate: nil, **source)
  @mutex.synchronize do
    remove_parameters_from_prev_source(source)
    @source = source
    @gate = gate || parameter_with_smallest_delta(source)
    @delta = delta if delta
    @reset = true unless @playing
    update_internal_structures
  end
  play
  self
end

#stopObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/xi/stream.rb', line 85

def stop
  @mutex.synchronize do
    @playing = false
    @state.clear
    @prev_ts.clear
    @prev_delta.clear
    @clock.unsubscribe(self)
  end
  self
end

#stopped?Boolean

Returns:

  • (Boolean)


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

def stopped?
  !playing?
end