Class: TEF::Sequencing::AudacityReader
- Inherits:
-
Object
- Object
- TEF::Sequencing::AudacityReader
- Defined in:
- lib/tef/Sequencing/AudacityLabelReader.rb
Overview
Audacity label reader helper.
This class is meant to read in an exported list of Labels, as generated by audacity. It provides proper data conversion and a few convenience functions.
Instance Method Summary collapse
-
#[](name) ⇒ Array<{start: Numeric, stop: Numeric, text: String}] The corresponding list of labels for the given track, or an empty array if the track did not exist.
Array<Numeric, stop: Numeric, text: String] The corresponding list of labels for the given track, or an empty array if the track did not exist.
-
#initialize(file) ⇒ AudacityReader
constructor
Initialize a new reader.
Constructor Details
#initialize(file) ⇒ AudacityReader
Initialize a new reader.
This will read in the given file and process it. Note that since Audacity does not export the name of different label tracks, it simply exports all labels, each label track should begin with a label called “TRACK: Name”. This serves as a means to separate label tracks.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tef/Sequencing/AudacityLabelReader.rb', line 21 def initialize(file) @tracks = Hash.new({}); File.open(file, 'r') do |f| current_track = 'default'; @tracks['default'] = []; f.each do |line| m = /^(?<start>[\.\d]+)\s+(?<stop>[\d\.]+)\s+(?<text>\S.+)/.match line next unless m; if(m[:text] =~ /TRACK:\s*(\S.+)/) current_track = $1; @tracks[current_track] = []; else @tracks[current_track] << { start: m[:start].to_f, stop: m[:stop].to_f, text: m[:text] } end end end end |
Instance Method Details
#[](name) ⇒ Array<{start: Numeric, stop: Numeric, text: String}] The corresponding list of labels for the given track, or an empty array if the track did not exist.
Returns Array<Numeric, stop: Numeric, text: String] The corresponding list of labels for the given track, or an empty array if the track did not exist.
45 46 47 48 49 |
# File 'lib/tef/Sequencing/AudacityLabelReader.rb', line 45 def [](name) return [] if @tracks[name].nil? return @tracks[name] end |