annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mp3write.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function mp3write(D,SR,NBITS,FILE,OPTIONS)
Daniel@0 2 % MP3WRITE Write MP3 file by use of external binary
Daniel@0 3 % MP3WRITE(Y,FS,NBITS,FILE) writes waveform data Y to mp3-encoded
Daniel@0 4 % file FILE at sampling rate FS using bitdepth NBITS.
Daniel@0 5 % The syntax exactly mirrors WAVWRITE. NBITS must be 16.
Daniel@0 6 % MP3WRITE(Y,FS,FILE) assumes NBITS is 16
Daniel@0 7 % MP3WRITE(Y,FILE) further assumes FS = 8000.
Daniel@0 8 %
Daniel@0 9 % MP3WRITE(..., OPTIONS) specifies additional compression control
Daniel@0 10 % options as a string passed directly to the lame encoder
Daniel@0 11 % program; default is '--quiet -h' for high-quality model.
Daniel@0 12 %
Daniel@0 13 % Example:
Daniel@0 14 % To convert a wav file to mp3 (assuming the sample rate is
Daniel@0 15 % supported):
Daniel@0 16 % [Y,FS] = wavread('piano.wav');
Daniel@0 17 % mp3write(Y,FS,'piano.mp3');
Daniel@0 18 % To force lame to use 160 kbps (instead of default 128 kbps)
Daniel@0 19 % with the default filename extension (mp3):
Daniel@0 20 % mp3write(Y,FS,'piano','--quiet -h -b 160');
Daniel@0 21 %
Daniel@0 22 % Note: The actual mp3 encoding is done by an external binary,
Daniel@0 23 % lame, which is available for multiple platforms. Usable
Daniel@0 24 % binaries are available from:
Daniel@0 25 % http://labrosa.ee.columbia.edu/matlab/mp3read.html
Daniel@0 26 %
Daniel@0 27 % Note: MP3WRITE will use the mex file popenw, if available, to
Daniel@0 28 % open a pipe to the lame encoder. Otherwise, it will have to
Daniel@0 29 % write a large temporary file, then execute lame on that file.
Daniel@0 30 % popenw is available at:
Daniel@0 31 % http://labrosa.ee.columbia.edu/matlab/popenrw.html
Daniel@0 32 % This is a nice way to save large audio files as the
Daniel@0 33 % incremental output of your code, but you'll have to adapt the
Daniel@0 34 % central loop of this function (rather than using it directly).
Daniel@0 35 %
Daniel@0 36 % See also: mp3read, wavwrite, popenw.
Daniel@0 37
Daniel@0 38 % 2005-11-10 Original version
Daniel@0 39 % 2007-02-04 Modified to exactly match wavwrite syntax, and to
Daniel@0 40 % automatically find architecture-dependent binaries.
Daniel@0 41 % 2007-07-26 Writing of stereo files via tmp file fixed (thx Yu-ching Lin)
Daniel@0 42 %
Daniel@0 43 % $Header: /Users/dpwe/matlab/columbiafns/RCS/mp3write.m,v 1.2 2007/07/26 15:09:16 dpwe Exp $
Daniel@0 44
Daniel@0 45 % find our baseline directory
Daniel@0 46 [path] = fileparts(which('mp3write'));
Daniel@0 47
Daniel@0 48 % %%%%% Directory for temporary file (if needed)
Daniel@0 49 % % Try to read from environment, or use /tmp if it exists, or use CWD
Daniel@0 50 tmpdir = getenv('TMPDIR');
Daniel@0 51 if isempty(tmpdir) || exist(tmpdir,'file')==0
Daniel@0 52 tmpdir = '/tmp';
Daniel@0 53 end
Daniel@0 54 if exist(tmpdir,'file')==0
Daniel@0 55 tmpdir = '';
Daniel@0 56 end
Daniel@0 57 % ensure it exists
Daniel@0 58 %if length(tmpdir) > 0 && exist(tmpdir,'file')==0
Daniel@0 59 % mkdir(tmpdir);
Daniel@0 60 %end
Daniel@0 61
Daniel@0 62 %%%%%% Command to delete temporary file (if needed)
Daniel@0 63 rmcmd = 'rm';
Daniel@0 64
Daniel@0 65 %%%%%% Location of the binary - attempt to choose automatically
Daniel@0 66 %%%%%% (or edit to be hard-coded for your installation)
Daniel@0 67 ext = lower(computer);
Daniel@0 68 if ispc
Daniel@0 69 ext = 'exe';
Daniel@0 70 rmcmd = 'del';
Daniel@0 71 end
Daniel@0 72 lame = fullfile(path,['lame.',ext]);
Daniel@0 73
Daniel@0 74 %%%% Process input arguments
Daniel@0 75 % Do we have NBITS?
Daniel@0 76 mynargin = nargin;
Daniel@0 77 if ischar(NBITS)
Daniel@0 78 % NBITS is a string i.e. it's actually the filename
Daniel@0 79 if mynargin > 3
Daniel@0 80 OPTIONS = FILE;
Daniel@0 81 end
Daniel@0 82 FILE = NBITS;
Daniel@0 83 NBITS = 16;
Daniel@0 84 % it's as if NBITS had been specified...
Daniel@0 85 mynargin = mynargin + 1;
Daniel@0 86 end
Daniel@0 87
Daniel@0 88 if mynargin < 5
Daniel@0 89 OPTIONS = '--quiet -h'; % -h means high-quality psych model
Daniel@0 90 end
Daniel@0 91
Daniel@0 92 [nr, nc] = size(D);
Daniel@0 93 if nc < nr
Daniel@0 94 D = D';
Daniel@0 95 [nr, nc] = size(D);
Daniel@0 96 end
Daniel@0 97 % Now rows are channels, cols are time frames (so interleaving is right)
Daniel@0 98
Daniel@0 99 %%%%% add extension if none (like wavread)
Daniel@0 100 [path,file,ext] = fileparts(FILE);
Daniel@0 101 if isempty(ext)
Daniel@0 102 FILE = [FILE, '.mp3'];
Daniel@0 103 end
Daniel@0 104
Daniel@0 105 nchan = nr;
Daniel@0 106 nfrm = nc;
Daniel@0 107
Daniel@0 108 if nchan == 1
Daniel@0 109 monostring = ' -m m';
Daniel@0 110 else
Daniel@0 111 monostring = '';
Daniel@0 112 end
Daniel@0 113
Daniel@0 114 lameopts = [' ', OPTIONS, monostring, ' '];
Daniel@0 115
Daniel@0 116 %if exist('popenw') == 3
Daniel@0 117 if length(which('popenw')) > 0
Daniel@0 118
Daniel@0 119 % We have the writable stream process extensions
Daniel@0 120 cmd = ['"',lame,'"', lameopts, '-r -s ',num2str(SR),' - "',FILE,'"'];
Daniel@0 121
Daniel@0 122 p = popenw(cmd);
Daniel@0 123 if p < 0
Daniel@0 124 error(['Error running popen(',cmd,')']);
Daniel@0 125 end
Daniel@0 126
Daniel@0 127 % We feed the audio to the encoder in blocks of <blksize> frames.
Daniel@0 128 % By adapting this loop, you can create your own code to
Daniel@0 129 % write a single, large, MP3 file one part at a time.
Daniel@0 130
Daniel@0 131 blksiz = 10000;
Daniel@0 132
Daniel@0 133 nrem = nfrm;
Daniel@0 134 base = 0;
Daniel@0 135
Daniel@0 136 while nrem > 0
Daniel@0 137 thistime = min(nrem, blksiz);
Daniel@0 138 done = popenw(p,32767*D(:,base+(1:thistime)),'int16be');
Daniel@0 139 nrem = nrem - thistime;
Daniel@0 140 base = base + thistime;
Daniel@0 141 %disp(['done=',num2str(done)]);
Daniel@0 142 end
Daniel@0 143
Daniel@0 144 % Close pipe
Daniel@0 145 popenw(p,[]);
Daniel@0 146
Daniel@0 147 else
Daniel@0 148 disp('Warning: popenw not available, writing temporary file');
Daniel@0 149
Daniel@0 150 tmpfile = fullfile(tmpdir,['tmp',num2str(round(1000*rand(1))),'.wav']);
Daniel@0 151
Daniel@0 152 wavwrite(D',SR,tmpfile);
Daniel@0 153
Daniel@0 154 cmd = ['"',lame,'"', lameopts, '"',tmpfile, '" "', FILE, '"'];
Daniel@0 155
Daniel@0 156 mysystem(cmd);
Daniel@0 157
Daniel@0 158 % Delete tmp file
Daniel@0 159 mysystem([rmcmd, ' "', tmpfile,'"']);
Daniel@0 160
Daniel@0 161 end
Daniel@0 162
Daniel@0 163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 164 function w = mysystem(cmd)
Daniel@0 165 % Run system command; report error; strip all but last line
Daniel@0 166 [s,w] = system(cmd);
Daniel@0 167 if s ~= 0
Daniel@0 168 error(['unable to execute ',cmd,' (',w,')']);
Daniel@0 169 end
Daniel@0 170 % Keep just final line
Daniel@0 171 w = w((1+max([0,findstr(w,10)])):end);
Daniel@0 172 % Debug
Daniel@0 173 %disp([cmd,' -> ','*',w,'*']);