0001 function [Y,FS,NBITS,OPTS] = mp3read(FILE,N,MONO,DOWNSAMP)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 if nargin < 2
0026 N = 0;
0027 else
0028 if isempty(N)
0029 N = 0;
0030 elseif length(N) == 1
0031
0032 N = [1 N];
0033 end
0034 end
0035 if nargin < 3
0036 forcemono = 0;
0037 else
0038 forcemono = (MONO ~= 0);
0039 end
0040 if nargin < 4
0041 downsamp = 1;
0042 else
0043 downsamp = DOWNSAMP;
0044 end
0045 if downsamp ~= 1 && downsamp ~= 2 && downsamp ~= 4
0046 error('DOWNSAMP can only be 1, 2, or 4');
0047 end
0048
0049
0050
0051
0052
0053
0054
0055
0056 arch = computer('arch');
0057 if strcmp(arch,'win32')
0058 fpath = fileparts(which(mfilename));
0059 mp3_toolbox_path = fullfile(fpath,'mp3_toolbox');
0060 if exist(mp3_toolbox_path)
0061 mpg123 = fullfile(mp3_toolbox_path,'mpg123');
0062 mp3info = fullfile(mp3_toolbox_path,'mp3info');
0063 else
0064 error('Cannot locate mpg123 and mp3info!')
0065 end
0066 else
0067 mpg123 = '/usr/local/bin/mpg123';
0068 mp3info = '/usr/local/bin/mp3info';
0069 end
0070
0071
0072 NBITS=16;
0073
0074
0075 cmd = [mp3info, ' -r m -p "%Q %u %r %v * %C %e %E %L %O %o %p" "', FILE,'"'];
0076
0077
0078 w = mysystem(cmd);
0079 if isempty(w) && exist(FILE,'file')
0080 trylim = 20;
0081 tries = 0;
0082 pauseval = 2;
0083 fprintf(1,'mp3info error, retrying %d times\n',trylim);
0084 while isempty(w)
0085 tries = tries+1;
0086 pause(pauseval);
0087 fprintf(1,'attempt %d after %0.2fs pause\n',tries,pauseval);
0088 w = mysystem(cmd);
0089 if tries == trylim
0090 error('%s found, but %s is not loading it',FILE,mp3info)
0091 end
0092 end
0093 end
0094
0095
0096 starpos = strfind(w,'*');
0097 nums = str2num(w(1:(starpos - 2)));
0098 strs = tokenize(w((starpos+2):end));
0099
0100 SR = nums(1);
0101 nframes = nums(2);
0102 nchans = 2 - strcmp(strs{6}, 'mono');
0103 layer = length(strs{4});
0104 bitrate = nums(3)*1000;
0105 mpgv = nums(4);
0106
0107
0108 if layer == 1
0109 smpspfrm = 384;
0110 elseif SR < 32000 && layer ==3
0111 smpspfrm = 576;
0112 if mpgv == 1
0113 error('SR < 32000 but mpeg version = 1');
0114 end
0115 else
0116 smpspfrm = 1152;
0117 end
0118
0119 OPTS.fmt.mpgBitrate = bitrate;
0120 OPTS.fmt.mpgVersion = mpgv;
0121
0122 OPTS.fmt.nAvgBytesPerSec = bitrate/8;
0123 OPTS.fmt.nSamplesPerSec = SR;
0124 OPTS.fmt.nChannels = nchans;
0125 OPTS.fmt.nBlockAlign = smpspfrm/SR*bitrate/8;
0126 OPTS.fmt.nBitsPerSample = NBITS;
0127 OPTS.fmt.mpgNFrames = nframes;
0128 OPTS.fmt.mpgCopyright = strs{1};
0129 OPTS.fmt.mpgEmphasis = strs{2};
0130 OPTS.fmt.mpgCRC = strs{3};
0131 OPTS.fmt.mpgLayer = strs{4};
0132 OPTS.fmt.mpgOriginal = strs{5};
0133 OPTS.fmt.mpgChanmode = strs{6};
0134 OPTS.fmt.mpgPad = strs{7};
0135 OPTS.fmt.mpgSampsPerFrame = smpspfrm;
0136
0137 if SR == 16000 && downsamp == 4
0138 error('mpg123 will not downsample 16 kHz files by 4 (only 2)');
0139 end
0140
0141
0142
0143
0144
0145 if downsamp == 1
0146 downsampstr = '';
0147 else
0148 downsampstr = [' -',num2str(downsamp)];
0149 end
0150 FS = SR/downsamp;
0151
0152 if forcemono == 1
0153 nchans = 1;
0154 chansstr = ' -m';
0155 else
0156 chansstr = '';
0157 end
0158
0159
0160 if strcmp(N,'size') == 1
0161 Y = [floor(smpspfrm*nframes/downsamp), nchans];
0162 else
0163
0164
0165 tmpstub = sprintf('tmp%s.wav',num2str(round(1000*rand(1))));
0166 if strcmp(computer,'PCWIN')
0167 tmpdir = fullfile(FILE(1:regexp(FILE,filesep)-1),'TMP');
0168 if ~exist(tmpdir,'dir')
0169 mkdir(tmpdir)
0170 end
0171 tmpfile = fullfile(tmpdir,tmpstub);
0172 else
0173 tmpfile = fullfile('/tmp',tmpstub);
0174 end
0175
0176 skipx = 0;
0177 skipblks = 0;
0178 skipstr = '';
0179 sttfrm = N(1)-1;
0180 if sttfrm > 0
0181 skipblks = floor(sttfrm*downsamp/smpspfrm);
0182 skipx = sttfrm - (skipblks*smpspfrm/downsamp);
0183 skipstr = [' -k ', num2str(skipblks)];
0184 end
0185
0186 lenstr = '';
0187 endfrm = -1;
0188 if length(N) > 1
0189 endfrm = N(2);
0190 if endfrm > sttfrm
0191 decblk = ceil(endfrm*downsamp/smpspfrm) - skipblks + 1;
0192 lenstr = [' -n ', num2str(decblk)];
0193 end
0194 end
0195
0196
0197 cmd=[mpg123, downsampstr, chansstr, skipstr, lenstr, ' -q -w ', tmpfile, ' ', '"',FILE,'"'];
0198
0199 w = mysystem(cmd);
0200
0201
0202 [Y,SR] = wavread(tmpfile);
0203
0204
0205 if strcmp(computer,'PCWIN')
0206 delete(tmpfile);
0207 else
0208 mysystem(['rm ', tmpfile]);
0209 end
0210
0211
0212 if endfrm > sttfrm
0213 Y = Y(skipx+[1:(endfrm-sttfrm)],:);
0214 elseif skipx > 0
0215 Y = Y((skipx+1):end,:);
0216 end
0217
0218 end
0219
0220
0221 function w = mysystem(cmd)
0222
0223 [s,w] = system(cmd);
0224 if s ~= 0
0225 error(['unable to execute ',cmd]);
0226 end
0227
0228 w = w((1+max([0,findstr(w,10)])):end);
0229
0230
0231
0232
0233 function a = tokenize(s)
0234
0235
0236 a = [];
0237 p = 1;
0238 n = 1;
0239 l = length(s);
0240 nss = findstr([s(p:end),' '],' ');
0241 for ns = nss
0242
0243 if ns == p
0244 p = p+1;
0245 else
0246 if p <= l
0247 a{n} = s(p:(ns-1));
0248 n = n+1;
0249 p = ns+1;
0250 end
0251 end
0252 end
0253