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 ,' \t emp.wav' ));
54
+ tmpfile = strcat(lame ,' \t emp.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 );
0 commit comments