Home > bml > utils > bml_praat.m

bml_praat

PURPOSE ^

BML_PRAAT opens FT_DATATYPE_RAWs in praat

SYNOPSIS ^

function bml_praat(varargin)

DESCRIPTION ^

 BML_PRAAT opens FT_DATATYPE_RAWs in praat

 Use as
   bml_praat(raw1, raw2)
   bml_praat('name1', raw1, 'name2', raw2)

 where raw1, raw2 ... rawN are the FT_DATATYPE_RAWs you wanto to open 

 If no name is provided, the praat objects will be named r<R>t<T>_<channel_name>, where <R> is the
 index of the FT_DATATYPE_RAW in the call to bml_praat, <T> is the trial 
 number and <channel_name> is the name of the label of the channel.

 If a name is provided, the praat objects will be named <name>_t<T>_<channel_name>. 

 Opens a new praat window

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function bml_praat(varargin)
0002 
0003 % BML_PRAAT opens FT_DATATYPE_RAWs in praat
0004 %
0005 % Use as
0006 %   bml_praat(raw1, raw2)
0007 %   bml_praat('name1', raw1, 'name2', raw2)
0008 %
0009 % where raw1, raw2 ... rawN are the FT_DATATYPE_RAWs you wanto to open
0010 %
0011 % If no name is provided, the praat objects will be named r<R>t<T>_<channel_name>, where <R> is the
0012 % index of the FT_DATATYPE_RAW in the call to bml_praat, <T> is the trial
0013 % number and <channel_name> is the name of the label of the channel.
0014 %
0015 % If a name is provided, the praat objects will be named <name>_t<T>_<channel_name>.
0016 %
0017 % Opens a new praat window
0018 %
0019 
0020 %saving files to temp directory
0021 recycle('off');
0022 tdir=[tempdir 'bml_praat'];
0023 
0024 %deleting previous wavs if any
0025 if(exist(tdir,'dir')==7) %7 -> folder
0026   wavs = dir(fullfile(tdir, '*.wav'));
0027   for k = 1 : length(wavs)
0028     delete(fullfile(tdir, wavs(k).name));
0029   end
0030 else
0031   mkdir(tdir);
0032 end
0033 
0034 %sorting raws and names from varargin
0035 names={};
0036 raws={};
0037 waslastname=false;
0038 for i=1:numel(varargin)
0039   if isa(varargin{i},'string') || isa(varargin{i},'char') 
0040     assert(~waslastname,'two consecutive names in argument %i', i);
0041     names = [names {strcat(varargin{i},'_')}];
0042     waslastname=true;  
0043   elseif iscellstr(varargin{i})
0044     assert(numel(varargin{i})==1,'invalid name parameter %i',i);
0045     assert(~waslastname,'two consecutive names in argument %i', i);
0046     names = [names {strcat(varargin{i}{1},'_')}];
0047     waslastname=true; 
0048   elseif isstruct(varargin{i})
0049     if ~waslastname; names = {names{:} ''}; end
0050     raws = [raws {varargin{i}}];   
0051     waslastname=false;
0052   else
0053     error('unknown type for argument %i',i);
0054   end
0055 end
0056 
0057 names = strrep(deblank(names),' ','_');
0058 
0059 %wavs={};
0060 rf=['%0' num2str(ceil(log10(numel(raws)))+1) 'd']; 
0061 cmd ='';
0062 for i=1:numel(raws)
0063   tf=['%0' num2str(ceil(log10(numel(raws{i}.trial)))+1) 'd']; 
0064   for t=1:numel(raws{i}.trial)
0065     for c=1:numel(raws{i}.label)
0066       wfn=char(strrep(raws{i}.label{c}," ","_"));
0067       if isempty(char(names{i}))
0068         wfn=[tdir filesep 'r' num2str(i,rf) 't' num2str(t,tf) '_' wfn '.wav'];
0069       else
0070         wfn=[tdir filesep char(names{i}) 't' num2str(t,tf) '_' wfn '.wav'];
0071       end
0072       v=raws{i}.trial{t}(c,:);
0073       
0074       if ismember({'fsample'},fields(raws{i}))
0075         Fs = round(raws{i}.fsample,0);
0076       else
0077         Fs = round(1/mean(diff(raws{i}.time{1})),0);
0078       end
0079       
0080       audiowrite(wfn,v./max(abs(v)),Fs);
0081       %wavs=[wavs,wfn];
0082       cmd = [cmd wfn ' '];
0083     end
0084   end
0085 end
0086 
0087 
0088 %os specific determination of praat path
0089 if ismac()
0090   %using open command in mac
0091   cmd = ['open -a praat ' cmd];
0092 elseif isunix()
0093   search_dirs={'/usr/local/bin'};
0094   [~,cmdout]=system('which praat');
0095   if isempty(cmdout)  
0096     %cheking for praat in seach_dirs
0097     indx=find(cellfun(@(x)exist([x filesep 'praat'],'file')==2,search_dirs),1);
0098     if isempty(indx)
0099       warning('make sure praat is in your path')    
0100     else
0101       setenv('PATH', [search_dirs{indx} ':' getenv('PATH')]);   
0102     end
0103   end
0104   cmd = ['praat --open ' cmd '&'];
0105 else
0106   cmd = ['praat --open ' cmd '&'];  
0107   warning('make sure praat is in your path. !echo $PATH')
0108 end
0109 
0110 system(cmd);
0111 
0112

Generated on Tue 25-Sep-2018 10:08:19 by m2html © 2005