class TEF::Sequencing::AudacityReader

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.

Public Class Methods

new(file) click to toggle source

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.

@param [String] file The file to read.

Should be a tab-separated list of start and end time and label name.
# 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

Public Instance Methods

[](name) click to toggle source

@return [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.
# File lib/tef/Sequencing/AudacityLabelReader.rb, line 45
def [](name)
        return [] if @tracks[name].nil?

        return @tracks[name]
end