annotate aim-mat/tools/getaiffs.m @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents 20ada0af3d7d
children
rev   line source
tomwalters@0 1 % tool
tomwalters@0 2 %
tomwalters@0 3 % INPUT VALUES:
tomwalters@0 4 %
tomwalters@0 5 % RETURN VALUE:
tomwalters@0 6 %
tomwalters@0 7 %
bleeck@3 8 % (c) 2011, University of Southampton
bleeck@3 9 % Maintained by Stefan Bleeck (bleeck@gmail.com)
bleeck@3 10 % download of current version is on the soundsoftware site:
bleeck@3 11 % http://code.soundsoftware.ac.uk/projects/aimmat
bleeck@3 12 % documentation and everything is on http://www.acousticscale.org
tomwalters@0 13
tomwalters@0 14 function aiffs=getaiffs(varargin)
tomwalters@0 15 %usage: aiffs=getaiff(varargin)
tomwalters@0 16 %
tomwalters@0 17 % otherwise parameter must come in pairs: 'param','value'
tomwalters@0 18 % allowed parameters are:
tomwalters@0 19 % modelfile - the spf file that specifies the version of AMS/AIM (required)
tomwalters@0 20 % soundcommand - a wave or raw file with the sound in (required)
tomwalters@0 21 % framespersecond (default: 12)
tomwalters@0 22 % output_normalization - the norm_mode parameter in out_file very useful for automatic scaling
tomwalters@0 23 % sound_sample_rate - the sample rate of the sound file
tomwalters@0 24 % sound_endian - endian of the sound file (l or b)(PC's are little endian Suns are big endian)
tomwalters@0 25 % writeaiffile - optional: a file to which the output aif file will be written
tomwalters@0 26 % echo - on (default) or off - if output is written on the screen
tomwalters@0 27 %
tomwalters@0 28 % returns an array of the class "frame" for each frame read
tomwalters@0 29
tomwalters@0 30 defined_input_gain=70;
tomwalters@0 31 temp_sound_file_name='temp_sound.wav';
tomwalters@0 32 temp_aiff_file_name='temp_aiff.aif';
tomwalters@0 33
tomwalters@0 34 if nargin<2 % only one parameter -> read file
tomwalters@0 35 if size(varargin)==1
tomwalters@0 36 makefilename=varargin{1};
tomwalters@0 37 else
tomwalters@0 38 makefilename='lastrun.genmovie';
tomwalters@0 39 end
tomwalters@0 40 % fprintf('movie is produced from file %s from aifffile "makemovie_temp.aif"\n!',makefilename);
tomwalters@0 41 % fprintf('aiff-file is produced according to file ''%s''\n',makefilename);
tomwalters@0 42 else
tomwalters@0 43 makefilename='lastrun.genmovie';
tomwalters@0 44 generateparameterfile(makefilename,varargin);
tomwalters@0 45 end
tomwalters@0 46
tomwalters@0 47 arguments=readparameterfile(makefilename);
tomwalters@0 48
tomwalters@0 49
tomwalters@0 50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 51 % make a string from each parameter
tomwalters@0 52 str_model=getargument(arguments,'modelfile');
tomwalters@0 53 str_soundcommand=getargument(arguments,'soundfile');
tomwalters@0 54 str_framespersecond=getargument(arguments,'framespersecond');
tomwalters@0 55 str_output_normalization=getargument(arguments,'output_normalization');
tomwalters@0 56 str_sound_sample_rate=getargument(arguments,'sound_sample_rate');
tomwalters@0 57 str_sound_endian=getargument(arguments,'sound_endian');
tomwalters@0 58 str_writeaiffile=getargument (arguments,'writeaiffile');
tomwalters@0 59 str_echo=getargument(arguments,'echo');
tomwalters@0 60 str_setvalue=getargument(arguments,'setvalue');
tomwalters@0 61 str_setvalueto=getargument(arguments,'setvalueto');
tomwalters@0 62 str_movie_duration=getargument(arguments,'movie_duration');
tomwalters@0 63 str_movie_start_time=getargument(arguments,'movie_start_time');
tomwalters@0 64
tomwalters@0 65
tomwalters@0 66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 67 % default values for all parameters
tomwalters@0 68
tomwalters@0 69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 70 % sound-file. can be a command or a file
tomwalters@0 71 if isempty(str_soundcommand) %
tomwalters@0 72 error('soundcommand must be given');
tomwalters@0 73 else
tomwalters@0 74 soundcommand=str_soundcommand;
tomwalters@0 75 end
tomwalters@0 76
tomwalters@0 77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 78 % spf-file.
tomwalters@0 79 if isempty(str_model) %
tomwalters@0 80 error('modelfile must be given');
tomwalters@0 81 else
tomwalters@0 82 % check for extension
tomwalters@0 83 [dumy_path,dummy_name,ext,versn] = fileparts(str_model);
tomwalters@0 84 if strcmp(ext,'')
tomwalters@0 85 model=sprintf('%s.spf',str_model);
tomwalters@0 86 else
tomwalters@0 87 model=str_model;
tomwalters@0 88 end
tomwalters@0 89 end
tomwalters@0 90
tomwalters@0 91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 92 % output normalization cares for the correct scaling in the aiff-files
tomwalters@0 93 % to prevent negativ numbers
tomwalters@0 94 if ~isempty(str_output_normalization) % how much the spf file should be normalized by setting the norm_mode parameter in out_file
tomwalters@0 95 eval(sprintf('scale_factor_output=%s;',str_output_normalization));
tomwalters@0 96 end
tomwalters@0 97
tomwalters@0 98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 99 % frames per second
tomwalters@0 100 if isempty(str_framespersecond) % default frames per second
tomwalters@0 101 framespersecond=12;
tomwalters@0 102 else
tomwalters@0 103 eval(sprintf('framespersecond=%s;',str_framespersecond));
tomwalters@0 104 end
tomwalters@0 105
tomwalters@0 106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 107 % echo on screen
tomwalters@0 108 if isempty(str_echo) % default frames per second
tomwalters@0 109 echo=1;
tomwalters@0 110 else
tomwalters@0 111 if strcmp(str_echo,'off')
tomwalters@0 112 echo=0;
tomwalters@0 113 else
tomwalters@0 114 echo=1;
tomwalters@0 115 end
tomwalters@0 116 end
tomwalters@0 117
tomwalters@0 118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 119 % read the sound command and transfere the data to the buffer
tomwalters@0 120 if ~isempty(str_movie_duration) % default frames per second
tomwalters@0 121 eval(sprintf('movie_start_time=%s;',str_movie_start_time));
tomwalters@0 122 eval(sprintf('movie_duration=%s;',str_movie_duration));
tomwalters@0 123 % [sounddata,samplerate,bits,endian]=producesounddata(soundcommand,temp_sound_file_name,str_sound_sample_rate,str_sound_endian,movie_start_time,movie_duration);
tomwalters@0 124 [sounddata,samplerate,bits,endian]=producesounddata(soundcommand,temp_sound_file_name,str_sound_sample_rate,str_sound_endian);
tomwalters@0 125 else
tomwalters@0 126 movie_start_time=0;
tomwalters@0 127 [sounddata,samplerate,bits,endian]=producesounddata(soundcommand,temp_sound_file_name,str_sound_sample_rate,str_sound_endian);
tomwalters@0 128 movie_duration=length(sounddata)/samplerate;
tomwalters@0 129 end
tomwalters@0 130
tomwalters@0 131 disp(sprintf('producing sound from command: %s',soundcommand));
tomwalters@0 132 videolength=size(sounddata,1)/samplerate;
tomwalters@0 133
tomwalters@0 134 % so long is one picture:
tomwalters@0 135 duration=1/framespersecond;
tomwalters@0 136 if duration > movie_duration
tomwalters@0 137 duration = movie_duration;
tomwalters@0 138 end
tomwalters@0 139
tomwalters@0 140 % so many frames is the video size in the end:
tomwalters@0 141 nr_frames=round(videolength*framespersecond);
tomwalters@0 142 starttime=0;
tomwalters@0 143
tomwalters@0 144 % open Modelfile for read in some parameters like cfs etc
tomwalters@0 145 id=fopen(model,'rt');
tomwalters@0 146 if id<=0
tomwalters@0 147 error(sprintf('Model-file %s not found',model));
tomwalters@0 148 end
tomwalters@0 149 fclose(id);
tomwalters@0 150 % open the soundfile to find out about sr and length
tomwalters@0 151 id=fopen(soundcommand,'rt');
tomwalters@0 152 if id<=0
tomwalters@0 153 error(sprintf('Sound-file %s not found',soundcommand));
tomwalters@0 154 end
tomwalters@0 155 fclose(id);
tomwalters@0 156
tomwalters@0 157 % put everything important in the options to be called with ams_ng
tomwalters@0 158 options=[];
tomwalters@0 159 somuchruns=sprintf('%2.0f',nr_frames);
tomwalters@0 160 options=[options ' NUM_RUNS.ams ' somuchruns];
tomwalters@0 161 options=[options ' SEGMENT_MODE.ams ' 'ON'];
tomwalters@0 162 options=[options ' FILELOCKING_MODE.ams ' 'ON'];
tomwalters@0 163 options=[options ' CHANNELS.DataFile_In ' '1'];
tomwalters@0 164
tomwalters@0 165 %% user defined changes to values in the spf-file (eg for setting random seed)
tomwalters@0 166 if ~isempty(str_setvalue) %
tomwalters@0 167 options=[options ' ' str_setvalue ' ' str_setvalueto ' '];
tomwalters@0 168 end
tomwalters@0 169
tomwalters@0 170 % build the correct dsam-output my-input aif-file:
tomwalters@0 171 options=[options ' FILENAME.DataFile_Out ' temp_aiff_file_name];
tomwalters@0 172
tomwalters@0 173 % make the correct soundcommand
tomwalters@0 174
tomwalters@0 175 if strfind(str_soundcommand,'wav')
tomwalters@0 176 options=[options ' FILENAME.DataFile_In ' str_soundcommand];% always the same name: makemovie_temp.wav
tomwalters@0 177 else
tomwalters@0 178 options=[options ' FILENAME.DataFile_In ' temp_sound_file_name];% always the same name: makemovie_temp.wav
tomwalters@0 179 end
tomwalters@0 180
tomwalters@0 181 % bring it to a format, that dsam understands:
tomwalters@0 182 wordsize=bits/8;
tomwalters@0 183 words=sprintf('%d',wordsize);
tomwalters@0 184 options=[options ' WORDSIZE.DataFile_In ' words];
tomwalters@0 185 srat=sprintf('%f',samplerate);
tomwalters@0 186 options=[options ' SAMPLERATE.DataFile_In ' srat];
tomwalters@0 187 dur=sprintf('%f',duration);
tomwalters@0 188 options=[options ' DURATION.DataFile_In ' dur];
tomwalters@0 189 star=sprintf('%f',starttime);
tomwalters@0 190 options=[options ' STARTTIME.DataFile_In ' star];
tomwalters@0 191 gain=sprintf('%f',defined_input_gain);
tomwalters@0 192 options=[options ' GAIN.DataFile_In ' gain];
tomwalters@0 193
tomwalters@0 194
tomwalters@0 195 % scaling must be performed in the input file to prevent too high numbers in the output
tomwalters@0 196 % !!!
tomwalters@0 197 % % the user has to set the correct scale value in the original spf-file
tomwalters@0 198 % this is best done by watching a whole film in ams and searching for the biggest
tomwalters@0 199 % value. Set this value as parameter 'NORM_MODE.DataFile_Out'
tomwalters@0 200 % it is NOT A GOOD IDEA to set this value to -1 (auto) because, this value is calculated
tomwalters@0 201 % from the first frame and this ist not the highest. As result, strange negative values occur!
tomwalters@0 202 if ~isempty(str_output_normalization) % how much the spf file should be normalized by setting the norm_mode parameter in out_file
tomwalters@0 203 options=[options ' NORM_MODE.DataFile_Out ' str_output_normalization];
tomwalters@0 204 end
tomwalters@0 205
tomwalters@0 206 % find out abput the times used in ms to display numbers
tomwalters@0 207 content_spfmodel=loadtextfile(model);
tomwalters@0 208 t_minusstr=DSAMFindParameter(content_spfmodel,'NWIDTH.Ana_SAI');
tomwalters@0 209 t_minus=sscanf(t_minusstr,'%f')*1000;
tomwalters@0 210 t_plusstr=DSAMFindParameter(content_spfmodel,'PWIDTH.Ana_SAI');
tomwalters@0 211 t_plus=sscanf(t_plusstr,'%f')*1000;
tomwalters@0 212
tomwalters@0 213 % do I need later:
tomwalters@0 214 str_scale_factor_output=DSAMFindParameter(content_spfmodel,'NORM_MODE.DataFile_Out');
tomwalters@0 215 scale_factor_output=sscanf(str_scale_factor_output,'%f');
tomwalters@0 216
tomwalters@0 217 % find out about the frequency axis and make it nice
tomwalters@0 218 cf=DSAMGetCFs(content_spfmodel);
tomwalters@0 219 nr_freq=length(cf);
tomwalters@0 220
tomwalters@0 221
tomwalters@0 222 % delete the remains from last run
tomwalters@0 223 if fexist(temp_aiff_file_name)
tomwalters@0 224 try
tomwalters@0 225 delete(temp_aiff_file_name);
tomwalters@0 226 catch
tomwalters@0 227 error(sprintf('ReadAiff: Error: Could not delete existing file %s',temp_aiff_file_name));
tomwalters@0 228 end
tomwalters@0 229 end
tomwalters@0 230
tomwalters@0 231 % the command line, that starts the process:
tomwalters@0 232 % ams must be in Path or this line must point to its directory:
tomwalters@0 233 %cd('C:\Program Files\DSAM\AMS');
tomwalters@0 234 % mas_ng starts a single ams process without grafik (very fast)
tomwalters@0 235 % -d turns off the debug messages
tomwalters@0 236 % -s calls the file makemovie_temp.spf
tomwalters@0 237 % -r gives the number of runs (somuchruns)
tomwalters@0 238 % segment on tells dsam to run in segmented mode
tomwalters@0 239 if ispc
tomwalters@0 240 % str=sprintf('! ams_ng.exe -doff -smakemovie_temp.spf -r%s segment on',somuchruns);
tomwalters@0 241 str=sprintf('! ams_ng.exe -doff -s%s -r%s segment on %s',model,somuchruns,options);
tomwalters@0 242 else
tomwalters@0 243 % str=sprintf('!/cbu/cnbh/dsam/bin/ams_ng -doff -smakemovie_temp.spf -r%s segment on',somuchruns);
tomwalters@0 244 str=sprintf('! /cbu/cnbh/dsam/bin/ams_ng -doff -s%s -r%s segment on %s',model,somuchruns,options);
tomwalters@0 245 end
tomwalters@0 246
tomwalters@0 247 % this one works with a window.
tomwalters@0 248 % str=sprintf('! AMS.exe -S -s makemovie_temp.spf -r %s &',somuchruns);
tomwalters@0 249
tomwalters@0 250 % delete the remains of some buggy run:
tomwalters@0 251 if fexist('.ams_LCK')
tomwalters@0 252 try
tomwalters@0 253 delete('.ams_LCK');
tomwalters@0 254 catch
tomwalters@0 255 error('Could not delete existing file .ams_LCK. Please restart matlab');
tomwalters@0 256 end
tomwalters@0 257 end
tomwalters@0 258
tomwalters@0 259 if echo disp('Ams simulation started...');end
tomwalters@0 260 eval([str]); % do ams!
tomwalters@0 261
tomwalters@0 262 t0 = clock;
tomwalters@0 263 while etime(clock,t0)<0.1 %wait a little while
tomwalters@0 264 end
tomwalters@0 265
tomwalters@0 266 % and look for the lock file
tomwalters@0 267 while fexist('.ams_LCK')
tomwalters@0 268 end % continue, when its _not_ there
tomwalters@0 269
tomwalters@0 270
tomwalters@0 271
tomwalters@0 272 FID = fopen(temp_aiff_file_name,'r');
tomwalters@0 273 if FID==-1
tomwalters@0 274 error('error in simulation: aif-file was not generated!');
tomwalters@0 275 end
tomwalters@0 276 fclose(FID);
tomwalters@0 277
tomwalters@0 278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 279 % all finished :) now read in the aiff-file
tomwalters@0 280 allframes=SBReadAiff(temp_aiff_file_name,echo); % returns all info in a struct
tomwalters@0 281
tomwalters@0 282 if ~isempty(str_writeaiffile)
tomwalters@0 283 % for jen: make a copy of the aiff file to something else
tomwalters@0 284 copyfile(temp_aiff_file_name, str_writeaiffile);
tomwalters@0 285 end
tomwalters@0 286
tomwalters@0 287 % find out about the structure
tomwalters@0 288 sample_frame=allframes(1);
tomwalters@0 289 nr_channels=getnrchannels(sample_frame);
tomwalters@0 290 nr_points=getnrpoints(sample_frame);
tomwalters@0 291 nr_frames=size(allframes,2);
tomwalters@0 292
tomwalters@0 293 % put the information about the center frequencies in the frames
tomwalters@0 294 if ~is_current_var('cf',who) % if read from aif-file, no such info exist
tomwalters@0 295 for i=1:nr_channels
tomwalters@0 296 cf(i)=i*1000;
tomwalters@0 297 end
tomwalters@0 298 t_plus=getmaximumtime(sample_frame)*1000;
tomwalters@0 299 t_minus=getminimumtime(sample_frame)*1000;
tomwalters@0 300 end
tomwalters@0 301 % set the frequency information in each frame
tomwalters@0 302 for i=1:nr_frames
tomwalters@0 303 allframes(i)=setcf(allframes(i),cf);
tomwalters@0 304 end
tomwalters@0 305 % and make a nice name for it:
tomwalters@0 306 for i=1:nr_frames
tomwalters@0 307 allframes(i)=setname(allframes(i),sprintf('Frame #%d of %d from sound %s',i,nr_frames,str_soundcommand));
tomwalters@0 308 start_times=(i-1)*duration+movie_start_time;
tomwalters@0 309 allframes(i)=setcurrentframestarttime(allframes(i),start_times);
tomwalters@0 310 end
tomwalters@0 311
tomwalters@0 312 % values for scaling the actual sum of all channels
tomwalters@0 313 scale_summe=0;% this value is used to scale the picture of the sum later
tomwalters@0 314 for i=1:nr_frames
tomwalters@0 315 vals=getvalues(allframes(i));
tomwalters@0 316 maxsc=getamplitudemaxvalue(sample_frame);
tomwalters@0 317 if maxsc~=0
tomwalters@0 318 vals=vals/getamplitudemaxvalue(sample_frame);
tomwalters@0 319 end
tomwalters@0 320 su=sum(vals);
tomwalters@0 321 ma=max(su);
tomwalters@0 322 if ma > scale_summe scale_summe=ma; end
tomwalters@0 323 end
tomwalters@0 324
tomwalters@0 325
tomwalters@0 326 low=getamplitudeminvalue(sample_frame);
tomwalters@0 327
tomwalters@0 328
tomwalters@0 329 if low < 0
tomwalters@0 330 disp('Achtung: Negative values in the AIFF-File! ');
tomwalters@0 331 disp('This probably means, that the value in ');
tomwalters@0 332 disp('NORM_MODE in DataFile_Out was not high enough!');
tomwalters@0 333 disp(sprintf('current value: %5.1f',scale_factor_output));
tomwalters@0 334 fprintf('\n[a] double it to %5.1f, or \n(b) continue anyway \nor type in the new value\n',scale_factor_output*2);
tomwalters@0 335
tomwalters@0 336 reply = input(': ','s');
tomwalters@0 337
tomwalters@0 338 if isempty(reply) % default
tomwalters@0 339 reply ='a';
tomwalters@0 340 end
tomwalters@0 341
tomwalters@0 342 valgiven=0;
tomwalters@0 343 if reply~='a' & reply~='b'
tomwalters@0 344 eval(sprintf('val=%s;',reply));
tomwalters@0 345 else
tomwalters@0 346 if reply=='a'
tomwalters@0 347 val=2 * scale_factor_output; % double it!
tomwalters@0 348 end
tomwalters@0 349 if reply=='b'
tomwalters@0 350 val=scale_factor_output; % no change
tomwalters@0 351 end
tomwalters@0 352 end
tomwalters@0 353 if isnan(val) | val<=0
tomwalters@0 354 error('sorry, no valid input');
tomwalters@0 355 end
tomwalters@0 356
tomwalters@0 357 if val~=scale_factor_output % only, if there is a change!
tomwalters@0 358 scale_factor_output=val;
tomwalters@0 359 % try it again!
tomwalters@0 360 aiffs=getaiffs(...
tomwalters@0 361 'modelfile',str_model,...
tomwalters@0 362 'soundcommand',str_soundcommand,...
tomwalters@0 363 'framespersecond',str_framespersecond,...
tomwalters@0 364 'output_normalization',scale_factor_output,... %thats the new one
tomwalters@0 365 'sound_sample_rate',str_sound_sample_rate,...
tomwalters@0 366 'writeaiffile',str_writeaiffile,...
tomwalters@0 367 'echo',str_echo,...
tomwalters@0 368 'sound_endian',str_sound_endian,...
tomwalters@0 369 'setvalue',str_setvalue,...
tomwalters@0 370 'setvalueto',str_setvalueto...
tomwalters@0 371 );
tomwalters@0 372 return;
tomwalters@0 373
tomwalters@0 374 end
tomwalters@0 375 end
tomwalters@0 376
tomwalters@0 377 aiffs=allframes;
tomwalters@0 378