Class: Deftones::Core::Source

Inherits:
AudioNode
  • Object
show all
Defined in:
lib/deftones/core/source.rb

Defined Under Namespace

Classes: VolumeProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: Deftones.context) ⇒ Source

Returns a new instance of Source.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/deftones/core/source.rb', line 94

def initialize(context: Deftones.context)
  super(context: context)
  @input = nil
  @volume = VolumeProxy.new(self)
  @mute = false
  @start_time = Float::INFINITY
  @stop_time = nil
  @onstop = nil
  @stop_notified = false
  @synced = false
  @transport_event_ids = {}
  apply_volume!
end

Instance Attribute Details

#muteObject

Returns the value of attribute mute.



92
93
94
# File 'lib/deftones/core/source.rb', line 92

def mute
  @mute
end

#onstopObject

Returns the value of attribute onstop.



91
92
93
# File 'lib/deftones/core/source.rb', line 91

def onstop
  @onstop
end

#volumeObject

Returns the value of attribute volume.



90
91
92
# File 'lib/deftones/core/source.rb', line 90

def volume
  @volume
end

Instance Method Details

#active_at?(time) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
# File 'lib/deftones/core/source.rb', line 183

def active_at?(time)
  return false if time < @start_time
  return true if @stop_time.nil?

  time < @stop_time
end

#apply_volume!Object (private)



221
222
223
224
# File 'lib/deftones/core/source.rb', line 221

def apply_volume!
  @output_gain = @volume.current_gain
  self
end

#cancel_stopObject Also known as: cancelStop



157
158
159
160
161
# File 'lib/deftones/core/source.rb', line 157

def cancel_stop
  clear_transport_event(:stop)
  @stop_time = nil
  self
end

#clear_transport_event(kind) ⇒ Object (private)



252
253
254
255
256
257
258
# File 'lib/deftones/core/source.rb', line 252

def clear_transport_event(kind)
  event_id = @transport_event_ids.delete(kind)
  return self unless event_id

  Deftones.transport.clear(event_id)
  self
end

#disposeObject



206
207
208
209
# File 'lib/deftones/core/source.rb', line 206

def dispose
  unsync
  super
end

#mute?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/deftones/core/source.rb', line 121

def mute?
  @mute
end

#notify_stop_in_window(start_frame, num_frames) ⇒ Object (private)



260
261
262
263
264
265
266
267
268
269
270
# File 'lib/deftones/core/source.rb', line 260

def notify_stop_in_window(start_frame, num_frames)
  return unless @stop_time
  return if @stop_notified

  start_time = start_frame.to_f / context.sample_rate
  end_time = (start_frame + num_frames).to_f / context.sample_rate
  return unless @stop_time >= start_time && @stop_time <= end_time

  @stop_notified = true
  @onstop&.call(@stop_time)
end

#number_of_inputsObject Also known as: numberOfInputs



108
109
110
# File 'lib/deftones/core/source.rb', line 108

def number_of_inputs
  0
end

#render(num_frames, start_frame = 0, cache = {}) ⇒ Object



190
191
192
# File 'lib/deftones/core/source.rb', line 190

def render(num_frames, start_frame = 0, cache = {})
  super
end

#render_block(num_frames, start_frame = 0, cache = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/deftones/core/source.rb', line 194

def render_block(num_frames, start_frame = 0, cache = {})
  output_block = super
  volume_gains = @volume.gains(num_frames, start_frame)
  scaled = AudioBlock.from_channel_data(
    output_block.channel_data.map do |channel|
      channel.each_with_index.map { |sample, index| sample * volume_gains[index] }
    end
  )
  notify_stop_in_window(start_frame, num_frames)
  scaled
end

#resolve_time(time) ⇒ Object (private)



226
227
228
229
230
# File 'lib/deftones/core/source.rb', line 226

def resolve_time(time)
  return context.current_time if time.nil?

  Deftones::Music::Time.parse(time)
end

#resolve_transport_time(time) ⇒ Object (private)



232
233
234
235
236
# File 'lib/deftones/core/source.rb', line 232

def resolve_transport_time(time)
  return Deftones.transport.seconds if time.nil?

  time
end

#restart(time = nil) ⇒ Object



152
153
154
155
# File 'lib/deftones/core/source.rb', line 152

def restart(time = nil)
  stop(time)
  start(time)
end

#schedule_transport_event(kind, time) ⇒ Object (private)



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/deftones/core/source.rb', line 238

def schedule_transport_event(kind, time)
  clear_transport_event(kind)
  @transport_event_ids[kind] = Deftones.transport.schedule(resolve_transport_time(time)) do |scheduled_time|
    if kind == :start
      @start_time = scheduled_time
      @stop_time = nil if @stop_time && @stop_time <= @start_time
      @stop_notified = false
    else
      @stop_time = scheduled_time
    end
  end
  self
end

#source_typeObject Also known as: sourceType



125
126
127
128
129
130
131
132
133
134
# File 'lib/deftones/core/source.rb', line 125

def source_type
  class_name = self.class.name.split("::").last
  words = class_name
    .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .downcase
    .split("_")

  [words.first, *words.drop(1).map(&:capitalize)].join
end

#start(time = nil) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/deftones/core/source.rb', line 136

def start(time = nil)
  return schedule_transport_event(:start, time) if synced?

  @start_time = resolve_time(time)
  @stop_time = nil if @stop_time && @stop_time <= @start_time
  @stop_notified = false
  self
end

#state(time = context.current_time) ⇒ Object



163
164
165
# File 'lib/deftones/core/source.rb', line 163

def state(time = context.current_time)
  active_at?(resolve_time(time)) ? :started : :stopped
end

#stop(time = nil) ⇒ Object



145
146
147
148
149
150
# File 'lib/deftones/core/source.rb', line 145

def stop(time = nil)
  return schedule_transport_event(:stop, time) if synced?

  @stop_time = resolve_time(time)
  self
end

#syncObject



167
168
169
170
# File 'lib/deftones/core/source.rb', line 167

def sync
  @synced = true
  self
end

#synced?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/deftones/core/source.rb', line 179

def synced?
  @synced
end

#unsyncObject



172
173
174
175
176
177
# File 'lib/deftones/core/source.rb', line 172

def unsync
  @synced = false
  clear_transport_event(:start)
  clear_transport_event(:stop)
  self
end

#uses_legacy_render_for_block?Boolean (private)

Returns:

  • (Boolean)


217
218
219
# File 'lib/deftones/core/source.rb', line 217

def uses_legacy_render_for_block?
  false
end