function oggwrite(varargin)
%OGGWRITE Write OGG (".OGG") sound file.
% OGGWRITE(Y,FS,NBITS,OGGFILE,ENCODING) writes data Y to a OGG Vorbis
% file specified by the file name OGGFILE, with a sample rate
% of FS Hz and with NBITS number of bits. Stereo data should
% be specified as a matrix with two columns.
% ENCODING must be specified as an integer number from 1 to 4
%
% 1 = Variable bit rate 256 kbs encoding.
% 2 = Variable bit rate 128 kbs encoding.
% 3 = Variable bit rate 64 kbs encoding.
% 4 = Variable bit rate 32 kbs encoding.
%
% Y,FS and NBITS are mandatory fields. If OGGFILE is not defined the file
% name will be 'Default_name.ogg'. If ENCODING is not defined encoding
% type '2' will be used by deault.
%
% See also OGGREAD, WAVREAD, WAVWRITE.
if length(varargin) < 3 | length(varargin) > 5
error('Unsopported number of argument inputs')
end
Y = varargin{1};
FS = varargin{2};
NBITS = varargin{3};
if NBITS~=8 & NBITS~=16 & NBITS~=24 & NBITS~=32
error('Unsopported bit depth')
end
if length(varargin) >= 4
OGGFILE = varargin{4};
if ischar(OGGFILE) ~= 1
error('File name is not a string')
end
else
OGGFILE = 'Default_name.ogg';
disp('File name = Default_name.ogg')
end
if isempty(findstr(OGGFILE,'.ogg'))
OGGFILE = strcat(OGGFILE,'.ogg');
end
if length(varargin) == 5
ENCODING = varargin{5};
else
ENCODING = '2';
disp('Variable bit rate, joint-stereo, 128 kb/s encoding')
end
app_dir = which('oggwrite.m');
app_dir_base = findstr('oggwrite.m',app_dir);
encoder_dir = app_dir(1:app_dir_base-2);
%%%%% Temporary File %%%%%
[data,channels] = size(Y);
wavwrite(Y,FS,NBITS,strcat(encoder_dir,'\temp.wav'));
tmpfile = strcat(encoder_dir,'\temp.wav');
OGGFILE = strcat(pwd,'\',OGGFILE);
ENCODING = num2str(ENCODING);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% Data Encoding using "oggenc.exe"%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch ENCODING
case {'1'}
[stat,info_out] = dos([encoder_dir,'\oggenc.exe', ' ', tmpfile, ' ', ' -b 256 ',' -B' ,num2str(NBITS),' -C' ,num2str(channels), ' -R' ,num2str(FS), ' -o ',OGGFILE]);
case {'2'}
[stat,info_out] = dos([encoder_dir,'\oggenc.exe', ' ', tmpfile, ' ', ' -b 128 ',' -B' ,num2str(NBITS),' -C' ,num2str(channels), ' -R' ,num2str(FS),' -o ',OGGFILE]);
case {'3'}
[stat,info_out] = dos([encoder_dir,'\oggenc.exe', ' ', tmpfile, ' ', ' -b 64 ',' -B' ,num2str(NBITS),' -C' ,num2str(channels), ' -R' ,num2str(FS),' -o ',OGGFILE]);
case {'4'}
[stat,info_out] = dos([encoder_dir,'\oggenc.exe', ' ', tmpfile, ' ', ' -b 32 ',' -B' ,num2str(NBITS),' -C' ,num2str(channels), ' -R' ,num2str(FS),' -o ',OGGFILE]);
otherwise
error('Encoding parameters not suported')
stat = 2;
end
if stat == 1
delete(OGGFILE);
delete(tmpfile);
error('Error while encoding')
end
% Delete temporary file
delete(tmpfile);