Class: Diarize::Audio

Inherits:
Object
  • Object
show all
Includes:
ToRdf
Defined in:
lib/diarize/audio.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_url_or_file_name) ⇒ Audio

Returns a new instance of Audio.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/diarize/audio.rb', line 5

def initialize(uri_url_or_file_name)
  if uri_url_or_file_name.is_a?(URI)
    @uri = uri_url_or_file_name
  elsif uri_url_or_file_name.is_a?(String)
    # url or file name
    @uri = URI.parse(uri_url_or_file_name)
    if @uri.scheme && @uri.scheme.match(/^(http|https|file)$/)
      # url or file:/// uri, do nothing
    else
      @uri = URI.join('file:///', File.join(File.expand_path(uri_url_or_file_name)))
    end
  end

  if @uri.scheme == 'file'
    @path = uri.path
  else
    # remote file, we download it locally
    @path = '/tmp/' + Digest::MD5.hexdigest(@uri.to_s)
    File.open(@path, "wb") {|f| f << open(@uri, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read}
  end

  raise "Unable to locate '#{@path}' from '#{@uri.inspect}'." unless File.exist?(@path)

  @file = File.new(@path)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/diarize/audio.rb', line 3

def file
  @file
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/diarize/audio.rb', line 3

def path
  @path
end

#uriObject

Returns the value of attribute uri.



3
4
5
# File 'lib/diarize/audio.rb', line 3

def uri
  @uri
end

Instance Method Details

#analyze!(train_speaker_models = true) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/diarize/audio.rb', line 31

def analyze!(train_speaker_models = true)
  # parameter = fr.lium.spkDiarization.parameter.Parameter.new
  parameter = Rjb::import('fr.lium.spkDiarization.parameter.Parameter').new
  parameter.show = show
  # 12 MFCC + Energy
  # 1: static coefficients are present in the file
  # 1: energy coefficient is present in the file
  # 0: delta coefficients are not present in the file
  # 0: delta energy coefficient is not present in the file
  # 0: delta delta coefficients are not present in the file
  # 0: delta delta energy coefficient is not present in the file
  # 13: total size of a feature vector in the mfcc file
  # 0:0:0: no feature normalization
  parameter.parameterInputFeature.setFeaturesDescription('audio2sphinx,1:1:0:0:0:0,13,0:0:0:0')
  #parameter.parameterDiarization.cEClustering = true # We use CE clustering by default
  parameter.parameterInputFeature.setFeatureMask(@path)
  @clusters = ester2(parameter)
  @segments = Segmentation.from_clusters(self, @clusters).sort_by(&:start)
  train_speaker_gmms if train_speaker_models
end

#base_uriObject



97
98
99
100
101
102
# File 'lib/diarize/audio.rb', line 97

def base_uri
  # Remove the fragment if there is one
  base = uri.clone
  base.fragment = nil
  base
end

#clean!Object



52
53
54
55
# File 'lib/diarize/audio.rb', line 52

def clean!
  return if @uri.scheme == 'file' # Don't delete local file if initialised from local URI
  File.delete(@path)
end

#duration_by_speaker(speaker) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/diarize/audio.rb', line 71

def duration_by_speaker(speaker)
  return unless speaker
  segments = segments_by_speaker(speaker)
  duration = 0.0
  segments.each {|segment| duration += segment.duration}
  duration
end

#namespacesObject



85
86
87
# File 'lib/diarize/audio.rb', line 85

def namespaces
  super.merge 'ws' => 'http://wsarchive.prototype0.net/ontology/', 'mo' => 'http://purl.org/ontology/mo/'
end

#rdf_mappingObject



112
113
114
# File 'lib/diarize/audio.rb', line 112

def rdf_mapping
  { 'ws:segment' => segments }
end

#segmentsObject

Raises:

  • (RuntimeError)


57
58
59
60
# File 'lib/diarize/audio.rb', line 57

def segments
  raise RuntimeError, "You need to run analyze! before being able to access the analysis results" unless @segments
  @segments
end

#segments_by_speaker(speaker) ⇒ Object



67
68
69
# File 'lib/diarize/audio.rb', line 67

def segments_by_speaker(speaker)
  segments.select {|segment| segment.speaker == speaker}
end

#showObject



116
117
118
119
# File 'lib/diarize/audio.rb', line 116

def show
  # The LIUM show name will be the file name, without extension or directory
  File.expand_path(@path).split('/')[-1].split('.')[0]
end

#speakersObject



62
63
64
65
# File 'lib/diarize/audio.rb', line 62

def speakers
  return @speakers if @speakers
  @speakers = segments.map {|segment| segment.speaker}.uniq
end

#top_speakersObject



79
80
81
# File 'lib/diarize/audio.rb', line 79

def top_speakers
  speakers.sort {|s1, s2| duration_by_speaker(s1) <=> duration_by_speaker(s2)}.reverse
end

#type_uriObject



104
105
106
# File 'lib/diarize/audio.rb', line 104

def type_uri
  @type_uri || 'mo:AudioFile'
end

#type_uri=(type_uri) ⇒ Object



108
109
110
# File 'lib/diarize/audio.rb', line 108

def type_uri=(type_uri)
  @type_uri = type_uri
end