0001 function [sig,fs,nbits,opts] = read_audio_stim(stimulus_id,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 ensemble_globals;
0034
0035
0036
0037 using_dict_input = false;
0038
0039 if nargin > 1 && all(cellfun(@ischar, varargin(1:2:end)))
0040 using_dict_input = true;
0041 end
0042
0043
0044 mysql_conn_id = [];
0045 mono = 0;
0046 downsamp = 1;
0047 N = [];
0048
0049 if using_dict_input
0050 for iarg = 1:2:length(varargin)
0051 switch varargin{iarg}
0052 case 'conn_id'
0053 mysql_conn_id = varargin{iarg+1};
0054
0055 case 'stimulus_root'
0056 stimulus_root = varargin{iarg+1};
0057
0058 case 'mono'
0059 mono = varargin{iarg+1};
0060
0061 case 'downsamp'
0062 downsamp = varargin{iarg+1};
0063
0064 case 'N'
0065 N = varargin{iar+1};
0066 end
0067 end
0068 else
0069 switch(nargin)
0070 case 1
0071 N = [];
0072 mono = 0;
0073 downsamp = 1;
0074 case 2
0075 N = varargin{1};
0076 mono = 0;
0077 downsamp = 1;
0078 case 3
0079 N = varargin{1};
0080 mono = varargin{2};
0081 downsamp = 1;
0082 case {4,5,6}
0083 N = varargin{1};
0084 mono = varargin{2};
0085 if(isempty(varargin{3}))
0086 downsamp = 1;
0087 else
0088 downsamp = varargin{3};
0089 end
0090 end
0091 end
0092
0093 if isempty(mysql_conn_id)
0094 if(nargin < 5)
0095 mysql_conn_id = 7;
0096 else
0097 mysql_conn_id = varargin{4};
0098 end
0099 end
0100
0101 if ~using_dict_input && nargin > 5
0102 params = varargin{5};
0103 else
0104 params = [];
0105 end
0106
0107 if(nargin == 0)
0108 error('Need a stimulus_id from the stimulus table. Type ''help read_audio_stim'' for more information.');
0109 end
0110
0111 if ~exist('mysql_conn_id','var') || isempty(mysql_conn_id)
0112 error('Unspecified mysql conn_id')
0113 else
0114 mysqlFail = false;
0115 end
0116
0117
0118 maxTries = 30;
0119 numTries = 0;
0120 forceStatusCheck = true;
0121 while forceStatusCheck && ~mysqlFail && mysql(mysql_conn_id,'status') && numTries < maxTries
0122 numTries=numTries+1;
0123 pause(rand*2)
0124 fprintf('.')
0125 end
0126
0127 if forceStatusCheck && (numTries == maxTries)
0128 mysqlFail = true;
0129 end
0130
0131
0132
0133 usingTmpConnID = false;
0134 if mysqlFail && ~isempty(params) && isfield(params,'mysql')
0135 fprintf('Trying to establish alternate connection\n');
0136 for iport = setdiff(0:9,mysql_conn_id)
0137 if ~mysql(iport,'status')
0138 params.mysql.conn_id = iport;
0139 mysql_conn_id = mysql_make_conn(params.mysql);
0140 usingTmpConnID = true;
0141 mysqlFail = false;
0142 fprintf('Established alternate connection: %d\n', mysql_conn_id);
0143 break
0144 end
0145 pause(rand*5)
0146 end
0147 if ~usingTmpConnID
0148 error('Tried, but failed to get an open port: last tried: %d', iport)
0149 end
0150 end
0151
0152 if mysqlFail
0153 error('%s: Do not have a valid connection ID to read stimulus_id: %d; Tried %d times on conn_id: %d', ...
0154 mfilename, stimulus_id, numTries, mysql_conn_id);
0155 elseif numTries
0156 fprintf('Tried to reach database %d times\n', numTries);
0157 end
0158
0159 sql_choose_stim = sprintf('select location from stimulus where stimulus_id = %d',stimulus_id);
0160 stim_location = mysql(mysql_conn_id,sql_choose_stim);
0161
0162 if usingTmpConnID
0163 mysql(mysql_conn_id,'close')
0164 end
0165
0166 stim_location = char(stim_location);
0167
0168
0169
0170 [p,fstub,ext] = fileparts(stim_location);
0171
0172 switch(ext)
0173 case '.mp3'
0174 sfreader = @mp3read;
0175 case '.wav'
0176 sfreader = @wavread;
0177 otherwise
0178 error('Did not recognize sound file name extension');
0179 end
0180
0181
0182 if((downsamp ~= 1) && (strcmp(ext,'.wav')))
0183 error('This function doesn''t support downsampling of wav files. Wav files must be downsampled in the calling function.');
0184 end
0185
0186 if(isempty(N) && (downsamp == 1))
0187 [sig,fs,nbits,opts] = sfreader(fullfile(stimulus_root,stim_location));
0188 elseif(~isempty(N) && (downsamp == 1))
0189 [sig,fs,nbits,opts] = sfreader(fullfile(stimulus_root,stim_location),N);
0190 elseif(isempty(N) && (downsamp ~= 1))
0191
0192 [sig,fs,nbits,opts] = sfreader(fullfile(stimulus_root,stim_location),N,0,downsamp);
0193 elseif(~isempty(N) && (downsamp ~= 1))
0194
0195 [sig,fs,nbits,opts] = sfreader(fullfile(stimulus_root,stim_location),N,0,downsamp);
0196 end
0197
0198 if(mono)
0199 sig = mean(sig,2);
0200 end
0201