MP3READ Read MP3 (".mp3") sound file. Y=MP3READ(FILE) reads a MP3 file specified by the string FILE, returning the sampled data in Y. Amplitude values are in the range [-1,+1]. [Y,FS,NBITS]=MP3READ(FILE) returns the sample rate (FS) in Hertz and the number of bits per sample (NBITS) used to encode the data in the file. Supports two channel encoded data, with up to 16 bits per sample. See also MP3WRITE, WAVWRITE, AUREAD, AUWRITE.
0001 function [Y,FS,NBITS] = mp3read(FILE) 0002 %MP3READ Read MP3 (".mp3") sound file. 0003 % Y=MP3READ(FILE) reads a MP3 file specified by the string FILE, 0004 % returning the sampled data in Y. Amplitude values are in the range [-1,+1]. 0005 % 0006 % [Y,FS,NBITS]=MP3READ(FILE) returns the sample rate (FS) in Hertz 0007 % and the number of bits per sample (NBITS) used to encode the 0008 % data in the file. 0009 % 0010 % Supports two channel encoded data, with up to 16 bits per sample. 0011 % 0012 % See also MP3WRITE, WAVWRITE, AUREAD, AUWRITE. 0013 0014 %%%%%% Location of the ".exe" Files 0015 s = which('mp3read.m'); 0016 ww = findstr('mp3read.m',s); 0017 location = s(1:ww-2); 0018 mpg123 = location;; 0019 mp3info = location; 0020 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0021 %%%%%% Data extraction from File using "mp3info.exe"%%%%%%%%%%%%%% 0022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0023 % Q = samprate, u = #frames, r = bitrate, v = mpeg version (1/2/2.5) 0024 % C = Copyright, e = emph, E = CRC, L = layer, O = orig, o = mono, p = pad 0025 cmd = [mp3info,'\mp3info', ' -r a -p "%Q %u %r %v * %C %e %E %L %O %o %p" "', FILE,'"']; 0026 w = mysystem(cmd); 0027 % Break into numerical and ascii parts by finding the delimiter (*) 0028 starpos = findstr(w,'*'); 0029 nums = str2num(w(1:(starpos - 2))); 0030 strs = tokenize(w((starpos+2):end)); 0031 SR = nums(1); %Sampling Rate 0032 nframes = nums(2); %Number of Frames 0033 nchans = 2 - strcmp(strs{6}, 'mono'); %Number of Channels 0034 layer = length(strs{4}); %MPEG Layer 0035 bitrate = nums(3)*1000; %MPEG Bitrate 0036 mpgv = nums(4); %MPEG Version 0037 %%%%Temporary file%%%%%% 0038 tmpfile = ['temp.wav']; 0039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0040 %%%%%%%%%%%%%% Data Decoding using "mpg123.exe"%%%%%%%%%%%%%%%%%% 0041 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0042 cmd = [mpg123,'\mpg123', ' -w ', tmpfile, ' ', '"',FILE,'"']; 0043 w = mysystem(cmd); 0044 % Load the data and delete temporary file 0045 [Y,FS,NBITS] = wavread(tmpfile); 0046 delete(tmpfile); 0047 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0048 function w = mysystem(cmd) 0049 % Run system command; report error; strip all but last line 0050 [s,w] = dos(cmd); 0051 if s ~= 0 0052 error(['unable to execute ',cmd]); 0053 end 0054 % Keep just final line 0055 w = w((1+max([0,findstr(w,10)])):end); 0056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0057 function a = tokenize(s) 0058 % Break space-separated string into cell array of strings 0059 % 2004-09-18 dpwe@ee.columbia.edu 0060 a = []; 0061 p = 1; 0062 n = 1; 0063 l = length(s); 0064 nss = findstr([s(p:end),' '],' '); 0065 for ns = nss 0066 % Skip initial spaces 0067 if ns == p 0068 p = p+1; 0069 else 0070 if p <= l 0071 a{n} = s(p:(ns-1)); 0072 n = n+1; 0073 p = ns+1; 0074 end 0075 end 0076 end 0077