Class: Muze::Feature::Context
- Inherits:
-
Object
- Object
- Muze::Feature::Context
- Defined in:
- lib/muze/feature/context.rb
Overview
Lightweight cache for feature extractors that share the same STFT.
Constant Summary collapse
- DEFAULT_FEATURES =
%i[ melspectrogram chroma_stft spectral_centroid spectral_bandwidth spectral_rolloff spectral_flatness rms zero_crossing_rate ].freeze
Instance Attribute Summary collapse
-
#center ⇒ Object
readonly
Returns the value of attribute center.
-
#hop_length ⇒ Object
readonly
Returns the value of attribute hop_length.
-
#n_fft ⇒ Object
readonly
Returns the value of attribute n_fft.
-
#pad_mode ⇒ Object
readonly
Returns the value of attribute pad_mode.
-
#sr ⇒ Object
readonly
Returns the value of attribute sr.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #extract(features: DEFAULT_FEATURES) ⇒ Object
- #fetch(feature) ⇒ Object
-
#initialize(y:, sr: 22_050, n_fft: 2048, hop_length: 512, center: true, pad_mode: :reflect) ⇒ Context
constructor
A new instance of Context.
- #magnitude ⇒ Object
- #power ⇒ Object
- #stft ⇒ Object
Constructor Details
#initialize(y:, sr: 22_050, n_fft: 2048, hop_length: 512, center: true, pad_mode: :reflect) ⇒ Context
Returns a new instance of Context.
20 21 22 23 24 25 26 27 28 |
# File 'lib/muze/feature/context.rb', line 20 def initialize(y:, sr: 22_050, n_fft: 2048, hop_length: 512, center: true, pad_mode: :reflect) @y = y @sr = sr @n_fft = n_fft @hop_length = hop_length @center = center @pad_mode = pad_mode @cache = {} end |
Instance Attribute Details
#center ⇒ Object (readonly)
Returns the value of attribute center.
18 19 20 |
# File 'lib/muze/feature/context.rb', line 18 def center @center end |
#hop_length ⇒ Object (readonly)
Returns the value of attribute hop_length.
18 19 20 |
# File 'lib/muze/feature/context.rb', line 18 def hop_length @hop_length end |
#n_fft ⇒ Object (readonly)
Returns the value of attribute n_fft.
18 19 20 |
# File 'lib/muze/feature/context.rb', line 18 def n_fft @n_fft end |
#pad_mode ⇒ Object (readonly)
Returns the value of attribute pad_mode.
18 19 20 |
# File 'lib/muze/feature/context.rb', line 18 def pad_mode @pad_mode end |
#sr ⇒ Object (readonly)
Returns the value of attribute sr.
18 19 20 |
# File 'lib/muze/feature/context.rb', line 18 def sr @sr end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
18 19 20 |
# File 'lib/muze/feature/context.rb', line 18 def y @y end |
Instance Method Details
#extract(features: DEFAULT_FEATURES) ⇒ Object
42 43 44 45 46 |
# File 'lib/muze/feature/context.rb', line 42 def extract(features: DEFAULT_FEATURES) features.each_with_object({}) do |feature, results| results[feature] = fetch(feature) end end |
#fetch(feature) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/muze/feature/context.rb', line 48 def fetch(feature) @cache[feature] ||= case feature when :melspectrogram then Muze::Feature.melspectrogram(sr:, s: power, n_fft:, hop_length:) when :chroma_stft then Muze::Feature.chroma_stft(sr:, s: magnitude, n_fft:, hop_length:) when :spectral_centroid then Muze::Feature.spectral_centroid(s: magnitude, sr:, n_fft:, hop_length:) when :spectral_bandwidth then Muze::Feature.spectral_bandwidth(s: magnitude, sr:, n_fft:, hop_length:) when :spectral_rolloff then Muze::Feature.spectral_rolloff(s: magnitude, sr:, n_fft:, hop_length:) when :spectral_flatness then Muze::Feature.spectral_flatness(s: magnitude, n_fft:, hop_length:) when :spectral_flux then Muze::Feature.spectral_flux(s: magnitude, sr:, n_fft:, hop_length:) when :spectral_entropy then Muze::Feature.spectral_entropy(s: magnitude, sr:, n_fft:, hop_length:) when :spectral_crest then Muze::Feature.spectral_crest(s: magnitude, sr:, n_fft:, hop_length:) when :spectral_slope then Muze::Feature.spectral_slope(s: magnitude, sr:, n_fft:, hop_length:) when :spectral_decrease then Muze::Feature.spectral_decrease(s: magnitude, sr:, n_fft:, hop_length:) when :poly_features then Muze::Feature.poly_features(s: magnitude, sr:, n_fft:, hop_length:) when :tonnetz then Muze::Feature.tonnetz(chroma: fetch(:chroma_stft), sr:, n_fft:, hop_length:) when :rms then Muze::Feature.rms(s: magnitude) when :zero_crossing_rate then Muze::Feature.zero_crossing_rate(y, frame_length: n_fft, hop_length:) else raise Muze::ParameterError, "Unsupported feature: #{feature}" end end |
#magnitude ⇒ Object
34 35 36 |
# File 'lib/muze/feature/context.rb', line 34 def magnitude @cache[:magnitude] ||= Muze.magphase(stft).first end |
#power ⇒ Object
38 39 40 |
# File 'lib/muze/feature/context.rb', line 38 def power @cache[:power] ||= (magnitude**2).cast_to(Numo::SFloat) end |