Class: Vizcore::Analysis::FeatureRecorder

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

Overview

Records deterministic audio analysis features from a file source.

Constant Summary collapse

VERSION =
"vizcore.features.v1"
DEFAULT_FRAME_COUNT =
300
DEFAULT_FRAME_RATE =
30.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(audio_file:, frames: DEFAULT_FRAME_COUNT, fps: DEFAULT_FRAME_RATE, noise_gate: Pipeline::DEFAULT_NOISE_GATE, audio_normalize: nil, bpm: nil, bpm_lock: false, cache_root: nil) ⇒ FeatureRecorder

Returns a new instance of FeatureRecorder.



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

def initialize(
  audio_file:,
  frames: DEFAULT_FRAME_COUNT,
  fps: DEFAULT_FRAME_RATE,
  noise_gate: Pipeline::DEFAULT_NOISE_GATE,
  audio_normalize: nil,
  bpm: nil,
  bpm_lock: false,
  cache_root: nil
)
  @audio_file = Pathname.new(audio_file.to_s).expand_path
  @frames = normalize_frame_count(frames)
  @fps = normalize_frame_rate(fps)
  @noise_gate = Float(noise_gate)
  @audio_normalize = audio_normalize
  @bpm = bpm
  @bpm_lock = bpm_lock
  @cache_root = normalize_cache_root(cache_root)
end

Class Method Details

.audio_file_mtime(path) ⇒ Object



107
108
109
110
111
# File 'lib/vizcore/analysis/feature_recorder.rb', line 107

def self.audio_file_mtime(path)
  file_stat_value(path, :mtime).to_i
rescue StandardError
  0
end

.audio_file_size(path) ⇒ Object



103
104
105
# File 'lib/vizcore/analysis/feature_recorder.rb', line 103

def self.audio_file_size(path)
  file_stat_value(path, :size)
end

.cache_key(version:, audio_file:, frames:, fps:, noise_gate:, audio_normalize:, bpm:, bpm_lock:) ⇒ Hash

Returns recorder metadata.

Returns:

  • (Hash)

    recorder metadata



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/vizcore/analysis/feature_recorder.rb', line 83

def self.cache_key(version:, audio_file:, frames:, fps:, noise_gate:, audio_normalize:, bpm:, bpm_lock:)
  audio_path = Pathname.new(audio_file.to_s).expand_path
  Digest::SHA256.hexdigest(
    JSON.generate(
      {
        version: version,
        audio_file: audio_path.to_s,
        audio_file_size: audio_file_size(audio_path),
        audio_file_mtime: audio_file_mtime(audio_path),
        frames: frames,
        fps: fps,
        noise_gate: noise_gate,
        audio_normalize: audio_normalize,
        bpm: bpm,
        bpm_lock: bpm_lock
      }
    )
  )
end

.file_stat_value(path, name) ⇒ Object



113
114
115
116
117
118
# File 'lib/vizcore/analysis/feature_recorder.rb', line 113

def self.file_stat_value(path, name)
  stat = Pathname.new(path).stat
  stat.send(name)
rescue StandardError
  0
end

Instance Method Details

#cache_keyString

Stable hash key for this recorder configuration.

Returns:

  • (String)


69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vizcore/analysis/feature_recorder.rb', line 69

def cache_key
  self.class.cache_key(
    version: VERSION,
    audio_file: @audio_file,
    frames: @frames,
    fps: @fps,
    noise_gate: @noise_gate,
    audio_normalize: @audio_normalize,
    bpm: @bpm,
    bpm_lock: @bpm_lock
  )
end

#cache_pathPathname?

Absolute cache file path for current recorder settings.

Returns:

  • (Pathname, nil)


42
43
44
45
46
# File 'lib/vizcore/analysis/feature_recorder.rb', line 42

def cache_path
  return nil unless @cache_root

  @cache_root.join("#{cache_key}.json")
end

#write(out:) ⇒ Hash

Returns recorder metadata.

Parameters:

  • out (String, Pathname)

    JSON output path

Returns:

  • (Hash)

    recorder metadata



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vizcore/analysis/feature_recorder.rb', line 50

def write(out:)
  output_path = Pathname.new(out.to_s).expand_path
  FileUtils.mkdir_p(output_path.dirname)

  if cache_path
    cached_payload = load_cached_payload(cache_path)
    output_payload = cached_payload if cached_payload
  end

  output_payload ||= record
  output_path.write("#{JSON.pretty_generate(output_payload)}\n")
  write_cached_output(output_payload) if cache_path && cache_path != output_path

  (output_payload, path: output_path)
end