Mercurial > hg > simscene-py
comparison simScene.m @ 14:b1901e8d8f5f
initial commit
author | Mathieu Lagrange <mathieu.lagrange@cnrs.fr> |
---|---|
date | Tue, 17 Mar 2015 09:34:13 +0100 |
parents | |
children | 69410ac2a50d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 14:b1901e8d8f5f |
---|---|
1 function [] = simScene(inputPath,outputPath,score,varargin) | |
2 % [] = simScene(inputPath,outputPath,score,[opts ... ]) | |
3 % | |
4 % Generate a simulated auditory scene by mixing randomly chosen samples. | |
5 % The samples and their mixing properties are chosen partially randomly | |
6 % following user-provided guidelines. | |
7 % | |
8 % Required input variables are: | |
9 % | |
10 % - inputPath: a dataset of isolated sound events and backgrounds | |
11 % respectively located in inputPat/event/ and | |
12 % inputPat/background | |
13 % | |
14 % - outputPath: the repository where simscene will write the simulated scene | |
15 % as well as the annotations | |
16 % | |
17 % - score: a structure describing the scene to simulate | |
18 % | |
19 % score.sceneDuration: length of the simulated scene in second | |
20 % | |
21 % score.backgrounds: Backgrounds are continuous sounds that keep going | |
22 % over the whole duration of the scene. Several background samples | |
23 % may be mixed together. If 'sceneDuration' is more than the | |
24 % natural length of one of the selected bg sounds, they will be | |
25 % looped as necessary -- that may not sound great, depending on the | |
26 % sound... The format of the 'background' field controlling their | |
27 % selection and use is: | |
28 % | |
29 % {{'<bg 1 label>','<bg 1 sampleId>', 1}, | |
30 % {'<bg 2 label>','<bg 2 sampleId>', <bg 2 snr>}...} | |
31 % | |
32 % The value of 'snr' is ignored for the first bg sound. For the | |
33 % following ones, it will be evaluated against the sum of the | |
34 % previous ones. The value of 'Label' is the label of the | |
35 % background track. Simscene will use the value 'sampleId' to load | |
36 % the background sample(s) located in inputpath/background. | |
37 % Simscene will only use background sample(s) having a name | |
38 % containing the value 'sampleId'. | |
39 % | |
40 % score.events: The 'events' is an array of cell arrays containg data use | |
41 % to simulated the scenes. Event contains the following data: | |
42 % | |
43 % {{'<event 1 label>', | |
44 % '<event 1 sampleId>', | |
45 % <event 1 ebr>, | |
46 % <event 1 ebr_stddev>, | |
47 % <event 1 mean_time_between_instances>, | |
48 % <event 1 time_between_instances_stddev>, | |
49 % <event 1 start_time>, | |
50 % <event 1 end_time>, | |
51 % <event 1 fade_in_time>, | |
52 % <event 1 fade_out_time>}, | |
53 % {'<event 2 ...> ...}, | |
54 % {'<event 3 ...> ...} | |
55 % ...} | |
56 % | |
57 % If <mean_time_between_instances> is 0, then it will be set to the | |
58 % sample length (so if <time_between_instances_stddev> is also 0, | |
59 % the sample will be looped). | |
60 % If <mean_time_between_instances> is -1, then the sound will be | |
61 % played just once. The considered EBR value is the maximum EBR | |
62 % of the fg sample relative to the bg on windows of 1000 samples. | |
63 % The considered bg for this is the mix of all specified bgs. | |
64 % | |
65 % This version of simscene allows a lot of options to be controlled, as | |
66 % optional 'name', value pairs from the 4th argument. Here is a brief | |
67 % description of the optional parameters: | |
68 % | |
69 % - timeMode: control a time spacing between events. | |
70 % 'generate' (default): values must be set for each track using the | |
71 % score control structure | |
72 % 'abstract': values are computed from an abstract representation of | |
73 % an existing acoustic scene given as parameter | |
74 % 'replicate': values are replicated from an existing acoustic scene | |
75 % given as parameter | |
76 % | |
77 % - ebrMode: control the Event to Background power level Ratio (EBR) of events. | |
78 % Possible options are: 'generate' (default), 'abstract', 'replicate', see above. | |
79 % | |
80 % If ebrMode is set to 'replicate' or 'abstract', and score.events exist, | |
81 % simScene will use the values <ebr> and <ebr_stddev> as offsets for | |
82 % the class <label>. | |
83 % If timeMode is set to 'abstract' and score.events exist, simScene will use | |
84 % the values <mean_time_between_instances> and <time_between_instances_stddev>. | |
85 % as offsets for the class <label>. | |
86 % If one of the mode is not set to 'generate' and score.events exist, SimScene | |
87 % will also use the values <start_time>, <end_time>, <fade_in_time>, <fade_out_time> | |
88 % and '<sampleId>' of score.events, provided they are not equal to 0 or | |
89 % empty. | |
90 % | |
91 % - instanceAnnotFile: if timeMode and / or ebrMode are set to 'abstract' | |
92 % or 'replicate', this parameter is the path to the | |
93 % annotation of the reference scene. Annotation must | |
94 % be provided as a Comma Separated Value (CSV) file | |
95 % with one event per line with class identifier as a | |
96 % string, onset and offset times in second as floating | |
97 % point value. | |
98 % | |
99 % - instanceAudioFile: if timeMode and / or ebrMode are set to 'abstract' | |
100 % or 'replicate', this parameter is the path to the wav | |
101 % file of the reference scene sampled at 44100 Hz. | |
102 % | |
103 % - displayFigures: Boolean (default 0), save pictures (piano roll, time | |
104 % domain stacked visualization, overlaid spectrogram) | |
105 % | |
106 % - outputFileName: name of generated files with no extension | |
107 % | |
108 % - sampleChoice: method for choosing sample to schedule within a track | |
109 % 'random': unconstrained sampling | |
110 % 'close': choose among the sampling dataset the sample that | |
111 % has the closest duration to the reference one. | |
112 % This mode is available only for the replicate timeMode. | |
113 % | |
114 % - minSpace: floating point value (default -1: overlapping between events allowed): | |
115 % minimal spacing in seconds between successive events | |
116 % | |
117 % - endCut: Boolean (default 0): adjust the duration of the last sample of | |
118 % each track if the latter ends after the track duration. | |
119 % If 1, cut the last sample to the track duration. If | |
120 % 0, the last sample is removed. | |
121 % | |
122 % SceneSchedule and SceneObject: | |
123 % | |
124 % - sceneObjects: This vector will contain detailed information concerning | |
125 % all the samples selected to build this scene. | |
126 % | |
127 % - sceneSchedule: This vector will contain all scheduling information | |
128 % (i.e. what sample goes where, with what scaling factor). | |
129 % The sample indices stored in it refer to the position | |
130 % in the previous vector. | |
131 % | |
132 % SceneSchedule, SceneObject, as well as score are saved in the .mat | |
133 % annotation file. | |
134 | |
135 % This program was written by Mathias Rossignol & Grégoire Lafay | |
136 % is Copyright (C) 2015 IRCAM <http://www.ircam.fr> | |
137 % | |
138 % This program is free software: you can redistribute it and/or modify it | |
139 % under the terms of the GNU General Public License as published by the Free | |
140 % Software Foundation, either version 3 of the License, or (at your option) | |
141 % any later version. | |
142 % | |
143 % This program is distributed in the hope that it will be useful, but | |
144 % WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
145 % or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
146 % for more details. | |
147 % | |
148 % You should have received a copy of the GNU General Public License along | |
149 % with this program. If not, see <http://www.gnu.org/licenses/>. | |
150 % | |
151 % Uses Mark Paskin's process_options.m from KPMtools | |
152 | |
153 addpath(genpath('nonExposed')); | |
154 | |
155 [timeMode,ebrMode,instanceAnnotFile,instanceAudioFile,displayFigures,sampleChoice,outputFileName,minSpace,endCut]=process_options(varargin,'timeMode','generate','ebrMode','generate','instanceAnnotFile','none','instanceAudioFile','none','displayFigures','1','sampleChoice','random','outputFileName','simulatedScene','minSpace',-1,'endCut',0); | |
156 | |
157 check_options(timeMode,ebrMode,score) | |
158 | |
159 %% Get score (only if at least one of the modes is not set to generate). | |
160 % Else use score inpu variable | |
161 if (~strcmp(timeMode,'generate') || ~strcmp(ebrMode,'generate')) | |
162 template=getTemplate(instanceAnnotFile,instanceAudioFile); | |
163 if isfield(score,'events') | |
164 score.events=addUserOffsets(template.class,score.events,timeMode,ebrMode); | |
165 else | |
166 warning('score.event is empty: score.event will be created from the scene to replicate'); | |
167 score.events=template.class; | |
168 end | |
169 if ~isfield(score,'sceneDuration') | |
170 score.sceneDuration=template.sceneDuration; | |
171 end | |
172 end | |
173 | |
174 %% Get sceneSchedule and sceneObjects | |
175 [sceneSchedule,sceneObjects]=getBackground(inputPath,score); | |
176 [sceneSchedule,sceneObjects]=getEvent(sceneSchedule,sceneObjects,inputPath,score,timeMode,ebrMode,strcmp(sampleChoice,'random')); | |
177 | |
178 %% Manage overlapping | |
179 if minSpace>=0 | |
180 [sceneSchedule] = manageOverlapping(sceneSchedule,minSpace); | |
181 end | |
182 | |
183 | |
184 %% Generate Scene | |
185 if (~exist(outputPath, 'dir')) | |
186 mkdir(outputPath) | |
187 end | |
188 if (~exist([outputPath,'annotation'], 'dir')) | |
189 mkdir([outputPath,'sound']) | |
190 end | |
191 if (~exist([outputPath,'annotation'], 'dir')) | |
192 mkdir([outputPath,'annotation']) | |
193 end | |
194 | |
195 [simsSceneWav,sceneSchedule] = generateScene(sceneSchedule,sceneObjects,score,inputPath,outputPath,outputFileName,displayFigures,timeMode,endCut); | |
196 | |
197 %% Check simulated scene | |
198 | |
199 checkClassPresence(sceneSchedule,sceneObjects); | |
200 | |
201 if minSpace>=0 | |
202 checkOverlapping(sceneSchedule,score.sceneDuration) | |
203 end | |
204 | |
205 %% Generate Sound | |
206 wavwrite(simsSceneWav,44100,[outputPath,'sound/',outputFileName,'.wav']); | |
207 | |
208 end |