Class: TEF::ProgramSelection::SoundCollection
- Inherits:
-
Object
- Object
- TEF::ProgramSelection::SoundCollection
- Defined in:
- lib/tef/ProgramSelection/SoundCollection.rb
Overview
Sound-File collection.
This class is meant as convenient container for sound-filenames. It will automatically scan the current directory for any file ending with ‘.mp3’, ‘.ogg’ or ‘.wav’, and will construct a unique ID for it based on the filepath. Paths can then be retrieved by using #soundmap.
Additionally, it keeps a list of silences, the #silence_maps. They are auto-generated lists of silences or loud sections of the file, useful for auto-generating programs. They can be retrieved via #silence_maps
Note that to play a sound, this should be done via Sequencing::SheetSequence#play, to ensure that the sound is killed in synch with the program.
Instance Attribute Summary collapse
-
#load_config ⇒ Hash
readonly
Config loaded from ‘./sounds/soundconfig.yml’.
-
#silence_maps ⇒ Hash<String, Hash<Numeric, Numeric>>
readonly
List of noise levels for a given file-path.
-
#soundmap ⇒ Hash<ID, String>
readonly
Map of IDs to matching file-paths.
Instance Method Summary collapse
-
#add_file(fname) ⇒ Object
Add a file to the collection of files.
-
#initialize(program_handler) ⇒ SoundCollection
constructor
Initialize a SoundCollection.
-
#silences_for(key) ⇒ Hash<Numeric, Numeric>?
The silence map for the passed ID, or nil if none was found.
Constructor Details
#initialize(program_handler) ⇒ SoundCollection
Initialize a SoundCollection
This will scan the current directory for any sound files. Found files will auto-generate a ID based on their path, and are registered with the passed TEF::ProgramSelection::Selector.
Paths are deconstructed as follows:
-
The path is split along any ‘/’ or ‘-’. Each element up to the last is taken as a group. The last element in the list is taken as title.
-
The variant is generated by taking the sequence (-d+)?.(mp3|ogg|wav) from the end. This means that variants can be specified by appending a ‘-1234’ to the title.
‘./sounds/portal/announcer-hello-4.mp3’ is registered as:
{Selector#register_ID}('hello', ['sounds', 'portal', 'announcer'], '-4.mp3');
Also note that a custom config file, #load_config, is loaded from
a YAML file './sounds/soundconfig.yml', if present.
Additionally, the #silence_maps are loaded from, and saved to,
'./sounds/silence_maps.yml'
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 61 def initialize(program_handler) @handler = program_handler @soundmap = {} @load_config = {}; @silence_maps = {} if File.file? './sounds/soundconfig.yml' @load_config = YAML.load File.read './sounds/soundconfig.yml' end if File.file? './sounds/silence_maps.yml' @silence_maps = YAML.load File.read './sounds/silence_maps.yml' end `find ./`.split("\n").each { |fn| add_file fn }; File.write('./sounds/silence_maps.yml', YAML.dump(@silence_maps)); end |
Instance Attribute Details
#load_config ⇒ Hash (readonly)
Returns Config loaded from ‘./sounds/soundconfig.yml’.
38 39 40 |
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 38 def load_config @load_config end |
#silence_maps ⇒ Hash<String, Hash<Numeric, Numeric>> (readonly)
Returns List of noise levels for a given file-path. Is in the format { timestamp (in s) => 0 to 1 } It is ensured that a 0 is inserted at the beginning and end of the sound track, and it is ensured that the hash keys are sorted.
32 33 34 |
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 32 def silence_maps @silence_maps end |
Instance Method Details
#add_file(fname) ⇒ Object
Add a file to the collection of files.
Will auto-generate silences and a matching ID as described in #initialize.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 120 def add_file(fname) rMatch = /^\.\/sounds\/(?<groups>(?:[a-z_]+[\/-])*)(?<title>[a-z_]+)(?<variant>(?:-\d+)?\.(?:ogg|mp3|wav))/.match fname; return unless rMatch; title = rMatch[:title].gsub('_', ' '); groups = rMatch[:groups].gsub('_', ' ').gsub('-','/').split('/'); groups = ["default"] if groups.empty? id = @handler.register_ID(title, groups, rMatch[:variant]) @soundmap[id] = fname generate_silences fname end |
#silences_for(key) ⇒ Hash<Numeric, Numeric>?
Returns The silence map for the passed ID, or nil if none was found.
140 141 142 |
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 140 def silences_for(key) @silence_maps[@soundmap[key]] end |