Class: TEF::ProgramSelection::SoundCollection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_configHash (readonly)

Returns Config loaded from ‘./sounds/soundconfig.yml’.

Returns:

  • (Hash)

    Config loaded from ‘./sounds/soundconfig.yml’



38
39
40
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 38

def load_config
  @load_config
end

#silence_mapsHash<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.

Returns:

  • (Hash<String, Hash<Numeric, Numeric>>)

    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.

See Also:



32
33
34
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 32

def silence_maps
  @silence_maps
end

#soundmapHash<ID, String> (readonly)

Returns Map of IDs to matching file-paths.

Returns:

  • (Hash<ID, String>)

    Map of IDs to matching file-paths



35
36
37
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 35

def soundmap
  @soundmap
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.

Returns:

  • (Hash<Numeric, Numeric>, nil)

    The silence map for the passed ID, or nil if none was found.

See Also:



140
141
142
# File 'lib/tef/ProgramSelection/SoundCollection.rb', line 140

def silences_for(key)
	@silence_maps[@soundmap[key]]
end