Skip to content

Commit 7190d2c

Browse files
committed
播放mp3
1 parent e0c3335 commit 7190d2c

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

mp3_toolbox/mp3reader/mp3info.exe

70 KB
Binary file not shown.

mp3_toolbox/mp3reader/mp3read.m

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function [Y,FS,NBITS,encoding_info,tag_info,out] = mp3read(FILE)
2+
%MP3READ Read MP3 (".mp3") sound file.
3+
% Y = MP3READ(FILE) reads a MP3 file specified by the string FILE,
4+
% returning the sampled data in Y. Amplitude values are in the range [-1,+1].
5+
%
6+
% [Y,FS,NBITS,encoding_info,ID3v1_tag_info] = MP3READ(FILE) returns the sample rate (FS) in Hertz
7+
% and the number of bits per sample (NBITS) used to encode the
8+
% data in the file.
9+
%
10+
% 'encoding_info' is a string containing information about the mp3
11+
% encoding used
12+
%
13+
% 'ID3v1_tag_info' is a string containing the tag information of the file
14+
% (only ID3v1 tag supported in this version)
15+
%
16+
%
17+
% Supports two channel or mono encoded data, with up to 16 bits per sample.
18+
%
19+
% See also MP3WRITE, WAVWRITE, AUREAD, AUWRITE.
20+
a = length(FILE);
21+
if a >= 4
22+
exten = FILE(a-3:a);
23+
if exten ~= '.mp3'
24+
FILE = strcat(FILE,'.mp3');
25+
end
26+
end
27+
if a <= 3
28+
FILE = strcat(FILE,'.mp3');
29+
end
30+
if exist(FILE) ~= 2
31+
error('File not Found')
32+
end
33+
%%%%%% Location of the ".exe" Files
34+
s = which('mp3read.m');
35+
ww = findstr('mp3read.m',s);
36+
location = s(1:ww-2);
37+
%%%%Temporary file%%%%%%
38+
tmpfile = ['temp.wav'];
39+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40+
%%%%%%%%%%%%%% Data Decoding using "mpg123.exe"%%%%%%%%%%%%%%%%%%
41+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42+
[stat,raw_info] = dos([location,'\mpg123', ' -w ', tmpfile, ' ', '"',FILE,'"']);
43+
data_init = findstr(raw_info,'MPEG');
44+
blocks = findstr(raw_info,'[0:');
45+
if raw_info(blocks+3) == '0'
46+
error('Error while decoding file. File may be corrupted')
47+
end
48+
[Y,FS,NBITS] = wavread(tmpfile); % Load the data and delete temporary file
49+
delete(tmpfile);
50+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51+
tag_info_start = strfind(raw_info,'Title');
52+
tag_info_end = (strfind(raw_info,'Playing MPEG'))-1;
53+
tag_info = raw_info(tag_info_start:tag_info_end);
54+
encoding_info = raw_info(data_init(3):data_init(3)+53);

mp3_toolbox/mp3reader/mpg123.exe

136 KB
Binary file not shown.

mp3_toolbox/mp3write/lame.exe

198 KB
Binary file not shown.

mp3_toolbox/mp3write/lame_enc.dll

171 KB
Binary file not shown.

mp3_toolbox/mp3write/mp3write.m

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
function mp3write(varargin)
2+
%MP3WRITE Write MP3 (".mp3") sound file.
3+
% MP3WRITE(Y,FS,NBITS,MP3FILE,ENCODING) writes data Y to a MP3
4+
% file specified by the file name MP3FILE, with a sample rate
5+
% of FS Hz and with NBITS number of bits. Stereo data should
6+
% be specified as a matrix with two columns.
7+
% ENCODING must be specified as an integer number from 1 to 5
8+
%
9+
% 1 = Fixed bit rate 128kbs encoding.
10+
% 2 = Fixed bit rate jstereo 128kbs encoding, high quality (recommended).
11+
% 3 = Average bit rate 112kbs encoding.
12+
% 4 = Fast encode, low quality.
13+
% 5 = Variable bitrate.
14+
%
15+
% Y,FS and NBITS are mandatory fields. If MP3FILE is not defined the file
16+
% name will be 'Default_name.mp3'. If ENCODING is not defined encoding
17+
% type '2' will be used by deault.
18+
%
19+
% See also MP3READ, WAVREAD, WAVWRITE.
20+
if length(varargin) < 3 | length(varargin) > 5
21+
error('Unsopported number of argument inputs')
22+
end
23+
Y = varargin{1};
24+
FS = varargin{2};
25+
NBITS = varargin{3};
26+
if NBITS~=8 & NBITS~=16 & NBITS~=24 & NBITS~=32
27+
error('Unsopported bit depth')
28+
end
29+
if length(varargin) >= 4
30+
MP3FILE = varargin{4};
31+
if ischar(MP3FILE) ~= 1
32+
error('File name is not a string')
33+
end
34+
else
35+
MP3FILE = 'Default_name.mp3';
36+
disp('File name = Default_name.mp3')
37+
end
38+
39+
if isempty(findstr(MP3FILE,'.mp3'))
40+
MP3FILE = strcat(MP3FILE,'.mp3');
41+
end
42+
43+
if length(varargin) == 5
44+
ENCODING = varargin{5};
45+
else
46+
ENCODING = '2';
47+
disp('Fixed bit rate, joint-stereo, 128 kb/s encoding')
48+
end
49+
50+
s = which('mp3write.m');
51+
ww = findstr('mp3write.m',s);
52+
lame = s(1:ww-2);
53+
wavwrite(Y,FS,NBITS,strcat(lame,'\temp.wav'));
54+
tmpfile = strcat(lame,'\temp.wav');
55+
MP3FILE = strcat(pwd,'\',MP3FILE);
56+
ENCODING = num2str(ENCODING);
57+
switch ENCODING
58+
case {'1'}
59+
cmd = [lame,'\lame', ' --quiet', ' ', tmpfile, ' ',MP3FILE];
60+
case {'2'}
61+
cmd = [lame,'\lame', ' --quiet', ' -b 128 ', tmpfile, ' ',MP3FILE];
62+
case {'3'}
63+
cmd = [lame,'\lame', ' --quiet', ' --abr 112 ', tmpfile, ' ',MP3FILE];
64+
case {'4'}
65+
cmd = [lame,'\lame', ' --quiet', ' -f ', tmpfile, ' ',MP3FILE];
66+
case {'5'}
67+
cmd = [lame,'\lame', ' --quiet', ' -h ', ' -V ', tmpfile, ' ',MP3FILE];
68+
otherwise
69+
error('Encoding parameters not suported')
70+
end
71+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72+
%%%%%%%%%%%%%% Data Encoding using "Lame.exe"%%%%%%%%%%%%%%%%%%%%
73+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74+
dos(cmd);
75+
% Delete temporary file
76+
delete(tmpfile);

mp3_toolbox_v2.0.zip

465 KB
Binary file not shown.

0 commit comments

Comments
 (0)