Class: Vizcore::Analysis::FeatureReplay

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/analysis/feature_replay.rb

Overview

Replays recorded analysis features as a pipeline-compatible source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ FeatureReplay

Returns a new instance of FeatureReplay.



13
14
15
16
17
18
19
# File 'lib/vizcore/analysis/feature_replay.rb', line 13

def initialize(path:)
  @path = Pathname.new(path.to_s).expand_path
  payload = load_payload
  @metadata = deep_symbolize(payload.fetch("metadata", {}))
  @features = load_features(payload)
  @cursor = 0
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



11
12
13
# File 'lib/vizcore/analysis/feature_replay.rb', line 11

def cursor
  @cursor
end

#metadataObject (readonly)

Returns the value of attribute metadata.



11
12
13
# File 'lib/vizcore/analysis/feature_replay.rb', line 11

def 
  @metadata
end

Instance Method Details

#call(_samples = nil) ⇒ Hash<Symbol, Object>

Returns recorded audio analysis for the next frame.

Parameters:

  • _samples (Array<Float>, nil) (defaults to: nil)

    ignored; replay data already contains analyzed features

Returns:

  • (Hash<Symbol, Object>)

    recorded audio analysis for the next frame



23
24
25
26
27
# File 'lib/vizcore/analysis/feature_replay.rb', line 23

def call(_samples = nil)
  audio = @features.fetch(@cursor)
  @cursor = (@cursor + 1) % @features.length
  deep_dup(audio)
end

#frame(index) ⇒ Hash<Symbol, Object>

Read a specific feature frame without changing the replay cursor.

Parameters:

  • index (Integer)

Returns:

  • (Hash<Symbol, Object>)


57
58
59
# File 'lib/vizcore/analysis/feature_replay.rb', line 57

def frame(index)
  deep_dup(@features.fetch(normalize_index(index)))
end

#frame_countObject



29
30
31
# File 'lib/vizcore/analysis/feature_replay.rb', line 29

def frame_count
  @features.length
end

#seek(index) ⇒ Vizcore::Analysis::FeatureReplay

Move the replay cursor to a frame index.

Parameters:

  • index (Integer)

Returns:



37
38
39
40
# File 'lib/vizcore/analysis/feature_replay.rb', line 37

def seek(index)
  @cursor = normalize_index(index)
  self
end

#seek_seconds(seconds) ⇒ Vizcore::Analysis::FeatureReplay

Move the replay cursor to the frame nearest to the given timestamp.

Parameters:

  • seconds (Numeric)

Returns:

Raises:

  • (ArgumentError)


46
47
48
49
50
51
# File 'lib/vizcore/analysis/feature_replay.rb', line 46

def seek_seconds(seconds)
  fps = 
  raise ArgumentError, "feature metadata fps must be positive to seek by seconds" unless fps.positive?

  seek((numeric_seconds(seconds) * fps).floor)
end