Module: Praat

Defined in:
lib/praat_lexer.rex.rb,
lib/praat.rb,
lib/praat_lexer.rb,
lib/praat_pitch.rb,
lib/praat_parser.rb,
lib/praat_formant.rb,
lib/praat_textgrid.rb

Overview

– This file is automatically generated. Do not modify it. Generated by: oedipus_lex version 2.4.0. Source: lib/praat_lexer.rex ++

Defined Under Namespace

Modules: FormantMethods Classes: Intervals, Item, Lexer, MetaCollection, MetaObject, Parser, Root

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.dominant_frame(item) ⇒ Object



2
3
4
5
6
7
8
# 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) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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) ⇒ Object

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



31
32
33
34
35
36
37
# 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 = {}) ⇒ Object



27
28
29
30
31
32
33
# 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') ⇒ Object

Parses a file given a specified encoding, returning an object containing nested objects



21
22
23
24
# 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) ⇒ Object



20
21
22
23
24
25
# 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