tomwalters@0: % tool tomwalters@0: % tomwalters@0: % INPUT VALUES: tomwalters@0: % tomwalters@0: % RETURN VALUE: tomwalters@0: % tomwalters@0: % bleeck@3: % (c) 2011, University of Southampton bleeck@3: % Maintained by Stefan Bleeck (bleeck@gmail.com) bleeck@3: % download of current version is on the soundsoftware site: bleeck@3: % http://code.soundsoftware.ac.uk/projects/aimmat bleeck@3: % documentation and everything is on http://www.acousticscale.org tomwalters@0: tomwalters@0: function aiffs=getaiffs(varargin) tomwalters@0: %usage: aiffs=getaiff(varargin) tomwalters@0: % tomwalters@0: % otherwise parameter must come in pairs: 'param','value' tomwalters@0: % allowed parameters are: tomwalters@0: % modelfile - the spf file that specifies the version of AMS/AIM (required) tomwalters@0: % soundcommand - a wave or raw file with the sound in (required) tomwalters@0: % framespersecond (default: 12) tomwalters@0: % output_normalization - the norm_mode parameter in out_file very useful for automatic scaling tomwalters@0: % sound_sample_rate - the sample rate of the sound file tomwalters@0: % sound_endian - endian of the sound file (l or b)(PC's are little endian Suns are big endian) tomwalters@0: % writeaiffile - optional: a file to which the output aif file will be written tomwalters@0: % echo - on (default) or off - if output is written on the screen tomwalters@0: % tomwalters@0: % returns an array of the class "frame" for each frame read tomwalters@0: tomwalters@0: defined_input_gain=70; tomwalters@0: temp_sound_file_name='temp_sound.wav'; tomwalters@0: temp_aiff_file_name='temp_aiff.aif'; tomwalters@0: tomwalters@0: if nargin<2 % only one parameter -> read file tomwalters@0: if size(varargin)==1 tomwalters@0: makefilename=varargin{1}; tomwalters@0: else tomwalters@0: makefilename='lastrun.genmovie'; tomwalters@0: end tomwalters@0: % fprintf('movie is produced from file %s from aifffile "makemovie_temp.aif"\n!',makefilename); tomwalters@0: % fprintf('aiff-file is produced according to file ''%s''\n',makefilename); tomwalters@0: else tomwalters@0: makefilename='lastrun.genmovie'; tomwalters@0: generateparameterfile(makefilename,varargin); tomwalters@0: end tomwalters@0: tomwalters@0: arguments=readparameterfile(makefilename); tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % make a string from each parameter tomwalters@0: str_model=getargument(arguments,'modelfile'); tomwalters@0: str_soundcommand=getargument(arguments,'soundfile'); tomwalters@0: str_framespersecond=getargument(arguments,'framespersecond'); tomwalters@0: str_output_normalization=getargument(arguments,'output_normalization'); tomwalters@0: str_sound_sample_rate=getargument(arguments,'sound_sample_rate'); tomwalters@0: str_sound_endian=getargument(arguments,'sound_endian'); tomwalters@0: str_writeaiffile=getargument (arguments,'writeaiffile'); tomwalters@0: str_echo=getargument(arguments,'echo'); tomwalters@0: str_setvalue=getargument(arguments,'setvalue'); tomwalters@0: str_setvalueto=getargument(arguments,'setvalueto'); tomwalters@0: str_movie_duration=getargument(arguments,'movie_duration'); tomwalters@0: str_movie_start_time=getargument(arguments,'movie_start_time'); tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % default values for all parameters tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % sound-file. can be a command or a file tomwalters@0: if isempty(str_soundcommand) % tomwalters@0: error('soundcommand must be given'); tomwalters@0: else tomwalters@0: soundcommand=str_soundcommand; tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % spf-file. tomwalters@0: if isempty(str_model) % tomwalters@0: error('modelfile must be given'); tomwalters@0: else tomwalters@0: % check for extension tomwalters@0: [dumy_path,dummy_name,ext,versn] = fileparts(str_model); tomwalters@0: if strcmp(ext,'') tomwalters@0: model=sprintf('%s.spf',str_model); tomwalters@0: else tomwalters@0: model=str_model; tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % output normalization cares for the correct scaling in the aiff-files tomwalters@0: % to prevent negativ numbers tomwalters@0: if ~isempty(str_output_normalization) % how much the spf file should be normalized by setting the norm_mode parameter in out_file tomwalters@0: eval(sprintf('scale_factor_output=%s;',str_output_normalization)); tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % frames per second tomwalters@0: if isempty(str_framespersecond) % default frames per second tomwalters@0: framespersecond=12; tomwalters@0: else tomwalters@0: eval(sprintf('framespersecond=%s;',str_framespersecond)); tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % echo on screen tomwalters@0: if isempty(str_echo) % default frames per second tomwalters@0: echo=1; tomwalters@0: else tomwalters@0: if strcmp(str_echo,'off') tomwalters@0: echo=0; tomwalters@0: else tomwalters@0: echo=1; tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % read the sound command and transfere the data to the buffer tomwalters@0: if ~isempty(str_movie_duration) % default frames per second tomwalters@0: eval(sprintf('movie_start_time=%s;',str_movie_start_time)); tomwalters@0: eval(sprintf('movie_duration=%s;',str_movie_duration)); tomwalters@0: % [sounddata,samplerate,bits,endian]=producesounddata(soundcommand,temp_sound_file_name,str_sound_sample_rate,str_sound_endian,movie_start_time,movie_duration); tomwalters@0: [sounddata,samplerate,bits,endian]=producesounddata(soundcommand,temp_sound_file_name,str_sound_sample_rate,str_sound_endian); tomwalters@0: else tomwalters@0: movie_start_time=0; tomwalters@0: [sounddata,samplerate,bits,endian]=producesounddata(soundcommand,temp_sound_file_name,str_sound_sample_rate,str_sound_endian); tomwalters@0: movie_duration=length(sounddata)/samplerate; tomwalters@0: end tomwalters@0: tomwalters@0: disp(sprintf('producing sound from command: %s',soundcommand)); tomwalters@0: videolength=size(sounddata,1)/samplerate; tomwalters@0: tomwalters@0: % so long is one picture: tomwalters@0: duration=1/framespersecond; tomwalters@0: if duration > movie_duration tomwalters@0: duration = movie_duration; tomwalters@0: end tomwalters@0: tomwalters@0: % so many frames is the video size in the end: tomwalters@0: nr_frames=round(videolength*framespersecond); tomwalters@0: starttime=0; tomwalters@0: tomwalters@0: % open Modelfile for read in some parameters like cfs etc tomwalters@0: id=fopen(model,'rt'); tomwalters@0: if id<=0 tomwalters@0: error(sprintf('Model-file %s not found',model)); tomwalters@0: end tomwalters@0: fclose(id); tomwalters@0: % open the soundfile to find out about sr and length tomwalters@0: id=fopen(soundcommand,'rt'); tomwalters@0: if id<=0 tomwalters@0: error(sprintf('Sound-file %s not found',soundcommand)); tomwalters@0: end tomwalters@0: fclose(id); tomwalters@0: tomwalters@0: % put everything important in the options to be called with ams_ng tomwalters@0: options=[]; tomwalters@0: somuchruns=sprintf('%2.0f',nr_frames); tomwalters@0: options=[options ' NUM_RUNS.ams ' somuchruns]; tomwalters@0: options=[options ' SEGMENT_MODE.ams ' 'ON']; tomwalters@0: options=[options ' FILELOCKING_MODE.ams ' 'ON']; tomwalters@0: options=[options ' CHANNELS.DataFile_In ' '1']; tomwalters@0: tomwalters@0: %% user defined changes to values in the spf-file (eg for setting random seed) tomwalters@0: if ~isempty(str_setvalue) % tomwalters@0: options=[options ' ' str_setvalue ' ' str_setvalueto ' ']; tomwalters@0: end tomwalters@0: tomwalters@0: % build the correct dsam-output my-input aif-file: tomwalters@0: options=[options ' FILENAME.DataFile_Out ' temp_aiff_file_name]; tomwalters@0: tomwalters@0: % make the correct soundcommand tomwalters@0: tomwalters@0: if strfind(str_soundcommand,'wav') tomwalters@0: options=[options ' FILENAME.DataFile_In ' str_soundcommand];% always the same name: makemovie_temp.wav tomwalters@0: else tomwalters@0: options=[options ' FILENAME.DataFile_In ' temp_sound_file_name];% always the same name: makemovie_temp.wav tomwalters@0: end tomwalters@0: tomwalters@0: % bring it to a format, that dsam understands: tomwalters@0: wordsize=bits/8; tomwalters@0: words=sprintf('%d',wordsize); tomwalters@0: options=[options ' WORDSIZE.DataFile_In ' words]; tomwalters@0: srat=sprintf('%f',samplerate); tomwalters@0: options=[options ' SAMPLERATE.DataFile_In ' srat]; tomwalters@0: dur=sprintf('%f',duration); tomwalters@0: options=[options ' DURATION.DataFile_In ' dur]; tomwalters@0: star=sprintf('%f',starttime); tomwalters@0: options=[options ' STARTTIME.DataFile_In ' star]; tomwalters@0: gain=sprintf('%f',defined_input_gain); tomwalters@0: options=[options ' GAIN.DataFile_In ' gain]; tomwalters@0: tomwalters@0: tomwalters@0: % scaling must be performed in the input file to prevent too high numbers in the output tomwalters@0: % !!! tomwalters@0: % % the user has to set the correct scale value in the original spf-file tomwalters@0: % this is best done by watching a whole film in ams and searching for the biggest tomwalters@0: % value. Set this value as parameter 'NORM_MODE.DataFile_Out' tomwalters@0: % it is NOT A GOOD IDEA to set this value to -1 (auto) because, this value is calculated tomwalters@0: % from the first frame and this ist not the highest. As result, strange negative values occur! tomwalters@0: if ~isempty(str_output_normalization) % how much the spf file should be normalized by setting the norm_mode parameter in out_file tomwalters@0: options=[options ' NORM_MODE.DataFile_Out ' str_output_normalization]; tomwalters@0: end tomwalters@0: tomwalters@0: % find out abput the times used in ms to display numbers tomwalters@0: content_spfmodel=loadtextfile(model); tomwalters@0: t_minusstr=DSAMFindParameter(content_spfmodel,'NWIDTH.Ana_SAI'); tomwalters@0: t_minus=sscanf(t_minusstr,'%f')*1000; tomwalters@0: t_plusstr=DSAMFindParameter(content_spfmodel,'PWIDTH.Ana_SAI'); tomwalters@0: t_plus=sscanf(t_plusstr,'%f')*1000; tomwalters@0: tomwalters@0: % do I need later: tomwalters@0: str_scale_factor_output=DSAMFindParameter(content_spfmodel,'NORM_MODE.DataFile_Out'); tomwalters@0: scale_factor_output=sscanf(str_scale_factor_output,'%f'); tomwalters@0: tomwalters@0: % find out about the frequency axis and make it nice tomwalters@0: cf=DSAMGetCFs(content_spfmodel); tomwalters@0: nr_freq=length(cf); tomwalters@0: tomwalters@0: tomwalters@0: % delete the remains from last run tomwalters@0: if fexist(temp_aiff_file_name) tomwalters@0: try tomwalters@0: delete(temp_aiff_file_name); tomwalters@0: catch tomwalters@0: error(sprintf('ReadAiff: Error: Could not delete existing file %s',temp_aiff_file_name)); tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: % the command line, that starts the process: tomwalters@0: % ams must be in Path or this line must point to its directory: tomwalters@0: %cd('C:\Program Files\DSAM\AMS'); tomwalters@0: % mas_ng starts a single ams process without grafik (very fast) tomwalters@0: % -d turns off the debug messages tomwalters@0: % -s calls the file makemovie_temp.spf tomwalters@0: % -r gives the number of runs (somuchruns) tomwalters@0: % segment on tells dsam to run in segmented mode tomwalters@0: if ispc tomwalters@0: % str=sprintf('! ams_ng.exe -doff -smakemovie_temp.spf -r%s segment on',somuchruns); tomwalters@0: str=sprintf('! ams_ng.exe -doff -s%s -r%s segment on %s',model,somuchruns,options); tomwalters@0: else tomwalters@0: % str=sprintf('!/cbu/cnbh/dsam/bin/ams_ng -doff -smakemovie_temp.spf -r%s segment on',somuchruns); tomwalters@0: str=sprintf('! /cbu/cnbh/dsam/bin/ams_ng -doff -s%s -r%s segment on %s',model,somuchruns,options); tomwalters@0: end tomwalters@0: tomwalters@0: % this one works with a window. tomwalters@0: % str=sprintf('! AMS.exe -S -s makemovie_temp.spf -r %s &',somuchruns); tomwalters@0: tomwalters@0: % delete the remains of some buggy run: tomwalters@0: if fexist('.ams_LCK') tomwalters@0: try tomwalters@0: delete('.ams_LCK'); tomwalters@0: catch tomwalters@0: error('Could not delete existing file .ams_LCK. Please restart matlab'); tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: if echo disp('Ams simulation started...');end tomwalters@0: eval([str]); % do ams! tomwalters@0: tomwalters@0: t0 = clock; tomwalters@0: while etime(clock,t0)<0.1 %wait a little while tomwalters@0: end tomwalters@0: tomwalters@0: % and look for the lock file tomwalters@0: while fexist('.ams_LCK') tomwalters@0: end % continue, when its _not_ there tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: FID = fopen(temp_aiff_file_name,'r'); tomwalters@0: if FID==-1 tomwalters@0: error('error in simulation: aif-file was not generated!'); tomwalters@0: end tomwalters@0: fclose(FID); tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % all finished :) now read in the aiff-file tomwalters@0: allframes=SBReadAiff(temp_aiff_file_name,echo); % returns all info in a struct tomwalters@0: tomwalters@0: if ~isempty(str_writeaiffile) tomwalters@0: % for jen: make a copy of the aiff file to something else tomwalters@0: copyfile(temp_aiff_file_name, str_writeaiffile); tomwalters@0: end tomwalters@0: tomwalters@0: % find out about the structure tomwalters@0: sample_frame=allframes(1); tomwalters@0: nr_channels=getnrchannels(sample_frame); tomwalters@0: nr_points=getnrpoints(sample_frame); tomwalters@0: nr_frames=size(allframes,2); tomwalters@0: tomwalters@0: % put the information about the center frequencies in the frames tomwalters@0: if ~is_current_var('cf',who) % if read from aif-file, no such info exist tomwalters@0: for i=1:nr_channels tomwalters@0: cf(i)=i*1000; tomwalters@0: end tomwalters@0: t_plus=getmaximumtime(sample_frame)*1000; tomwalters@0: t_minus=getminimumtime(sample_frame)*1000; tomwalters@0: end tomwalters@0: % set the frequency information in each frame tomwalters@0: for i=1:nr_frames tomwalters@0: allframes(i)=setcf(allframes(i),cf); tomwalters@0: end tomwalters@0: % and make a nice name for it: tomwalters@0: for i=1:nr_frames tomwalters@0: allframes(i)=setname(allframes(i),sprintf('Frame #%d of %d from sound %s',i,nr_frames,str_soundcommand)); tomwalters@0: start_times=(i-1)*duration+movie_start_time; tomwalters@0: allframes(i)=setcurrentframestarttime(allframes(i),start_times); tomwalters@0: end tomwalters@0: tomwalters@0: % values for scaling the actual sum of all channels tomwalters@0: scale_summe=0;% this value is used to scale the picture of the sum later tomwalters@0: for i=1:nr_frames tomwalters@0: vals=getvalues(allframes(i)); tomwalters@0: maxsc=getamplitudemaxvalue(sample_frame); tomwalters@0: if maxsc~=0 tomwalters@0: vals=vals/getamplitudemaxvalue(sample_frame); tomwalters@0: end tomwalters@0: su=sum(vals); tomwalters@0: ma=max(su); tomwalters@0: if ma > scale_summe scale_summe=ma; end tomwalters@0: end tomwalters@0: tomwalters@0: tomwalters@0: low=getamplitudeminvalue(sample_frame); tomwalters@0: tomwalters@0: tomwalters@0: if low < 0 tomwalters@0: disp('Achtung: Negative values in the AIFF-File! '); tomwalters@0: disp('This probably means, that the value in '); tomwalters@0: disp('NORM_MODE in DataFile_Out was not high enough!'); tomwalters@0: disp(sprintf('current value: %5.1f',scale_factor_output)); tomwalters@0: 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: tomwalters@0: reply = input(': ','s'); tomwalters@0: tomwalters@0: if isempty(reply) % default tomwalters@0: reply ='a'; tomwalters@0: end tomwalters@0: tomwalters@0: valgiven=0; tomwalters@0: if reply~='a' & reply~='b' tomwalters@0: eval(sprintf('val=%s;',reply)); tomwalters@0: else tomwalters@0: if reply=='a' tomwalters@0: val=2 * scale_factor_output; % double it! tomwalters@0: end tomwalters@0: if reply=='b' tomwalters@0: val=scale_factor_output; % no change tomwalters@0: end tomwalters@0: end tomwalters@0: if isnan(val) | val<=0 tomwalters@0: error('sorry, no valid input'); tomwalters@0: end tomwalters@0: tomwalters@0: if val~=scale_factor_output % only, if there is a change! tomwalters@0: scale_factor_output=val; tomwalters@0: % try it again! tomwalters@0: aiffs=getaiffs(... tomwalters@0: 'modelfile',str_model,... tomwalters@0: 'soundcommand',str_soundcommand,... tomwalters@0: 'framespersecond',str_framespersecond,... tomwalters@0: 'output_normalization',scale_factor_output,... %thats the new one tomwalters@0: 'sound_sample_rate',str_sound_sample_rate,... tomwalters@0: 'writeaiffile',str_writeaiffile,... tomwalters@0: 'echo',str_echo,... tomwalters@0: 'sound_endian',str_sound_endian,... tomwalters@0: 'setvalue',str_setvalue,... tomwalters@0: 'setvalueto',str_setvalueto... tomwalters@0: ); tomwalters@0: return; tomwalters@0: tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: aiffs=allframes; tomwalters@0: