module Praat
Constants
- HAS_NMATRIX
- VERSION
Public Class Methods
dominant_frame(item)
click to toggle source
# File lib/praat_formant.rb, line 2 def self.dominant_frame item dominant_frame = item.frames.max {|a, b| a.intensity <=> b.intensity } item.add_property :dominant_frame, dominant_frame item end
find_dominant_pitch(item)
click to toggle source
# File lib/praat_pitch.rb, line 2 def self.find_dominant_pitch item item.frames.map! do |frame| top = frame.candidates.max do |a, b| a.strength <=> b.strength end frame.add_property "freq", top.frequency # Filter out the unvoiced candidates if frame.freq > item.ceiling frame.freq = nil end frame end item end
hz_to_midi(hz, base_hz = 440.0, base_midi = 69)
click to toggle source
Turns a hz reading into a midi value.
hz - The input value in hz base_hz - The frequency for tuning (i.e., A=440) base_midi - The midi key that the tuning pitch corresponds to
# File lib/praat.rb, line 31 def self.hz_to_midi hz, base_hz = 440.0, base_midi = 69 if hz && hz > 0.0 (Math.log2(hz.to_f / base_hz) * 12.0) + base_midi else 0.0 end end
normalize(nm, options = {})
click to toggle source
# File lib/praat_pitch.rb, line 27 def self.normalize nm, options = {} min = options[:min] || nm.min max = options[:max] || nm.max nm -= NMatrix.new(nm.shape, [min[0]], dtype: nm.dtype) nm /= NMatrix.new(nm.shape, [max[0] - min[0]], dtype: nm.dtype) nm end
parse_file(filename, encoding = 'utf-8')
click to toggle source
Parses a file given a specified encoding, returning an object containing nested objects
# File lib/praat.rb, line 21 def self.parse_file filename, encoding = 'utf-8' f = File.open(filename, "rb", {encoding: "#{encoding}:utf-8"}) Praat::Parser.new.parse(Praat::Lexer.new.parse(f.read)) end
pitch_vector(item)
click to toggle source
# File lib/praat_pitch.rb, line 20 def self.pitch_vector item pv = self.find_dominant_pitch(Marshal.load Marshal.dump item.dup) pv.frames.map!(&:freq) pv.frames.select! {|f| f > 0} pv.frames.to_nm unless pv.frames.empty? end