Code covered by the BSD License  

Highlights from
Nero AAC Codec Wrapper

Nero AAC Codec Wrapper

by

 

MATLAB wrapper functions for Nero AAC Codec (v1.0)

neroaacread(aacfile,varargin)
function varargout = neroaacread(aacfile,varargin)
%NEROAACREAD Read Advanced Audio Coding sound file.
%   NEROAACREAD is a MATLAB wrapper function for Nero AAC Decoder, which is
%   downloadable for free at http://www.nero.com/eng/technologies-aac-codec.html
%
%   Y = NEROAACREAD(FILE) reads a AAC file specified by the string FILE,
%   returning the sampled data in Y. The ".m4a" extension is appended if no
%   extension is given in FILE.
%
%   [Y,FS,NBITS] = NEROAACREAD(FILE) returns the sample rate (FS) in Hertz
%   and the number of bits per sample (NBITS) used to encode the data in
%   the file.
%
%   [...] = NEROAACREAD(FILE,N) returns only the first N samples from each
%   channel in the file.
%
%   [...] = NEROAACREAD(FILE,[N1 N2]) returns only samples N1 through N2
%   from each channel in the file.
%
%   [Y,...] = NEROAACREAD(...,FMT) specifies the data type format of Y used
%   to represent samples read from the file. If FMT='double', Y contains
%   double-precision normalized samples. If FMT='native', Y contains
%   samples in the native data type found in the file. Interpretation of
%   FMT is case-insensitive, and partial matching is supported.  If
%   omitted, FMT='double'.
%
%   SIZ = NEROAACREAD(FILE,'size') returns the size of the audio data
%   contained in the file in place of the actual audio data, returning the
%   2-element vector SIZ=[samples channels].
%
%   [Y,FS,NBITS,OPTS] = NEROAACREAD(...) returns a structure OPTS of
%   additional information contained in the WAV file.  The content of this
%   structure differs from file to file.  Typical structure fields include
%   '.fmt' (audio format information) and '.info' (text which may describe
%   title, author, etc.)
%
%   Output Scaling The range of values in Y depends on the data format FMT
%   specified. Some examples of output scaling based on typical bit-widths
%   found in a WAV file are given below for both 'double' and 'native'
%   formats. 
%
%   FMT='native'
%      #Bits   MATLAB data type          Data range
%      -----   ------------------------- -------------------
%        8     uint8  (unsigned integer)      0 <= Y <= 255
%       16     int16  (signed integer)   -32768 <= Y <= +32767
%       24     int32  (signed integer)    -2^23 <= Y <= 2^23-1
%       32     single (floating point)     -1.0 <= Y <= +1.0
%
%   FMT='double'
%      #Bits   MATLAB data type          Data range
%      -----   ------------------------- -------------------
%       N<32   double                     -1.0 <= Y <  +1.0
%       N=32   double                     -1.0 <= Y <= +1.0
%      Note: Values in y might exceed -1.0 or +1.0 for the case of
%            N=32 bit data samples stored in the AAC file.
%
%   See also NEROAACWRITE, NEROAACTAG, WAVREAD.

% Copyright 2012 Takeshi Ikuma
% History:
% rev. - : (10-03-2012) original release

% Check input arguments
error(nargchk(1,inf,nargin)); %#ok

if ~ischar(aacfile) || size(aacfile,1)~=1
   error('FILE must be a string of characters.');
end

aacfile = which(aacfile); % expand to full path
if isempty(aacfile) % if file does not exist, try appending .wav extension
   if isempty(regexp(aacfile,'.m4a$','once'))
      aacfile = which(strcat(aacfile,'.m4a'));
      if isempty(aacfile)
         error('Invalid FILE: Cannot open file.');
      end
   end
end

% Retrieve the path
[exefile,msg] = getexepath();
error(msg);

% generate temporary wave file name
wavfile = strcat(tempname,'.wav'); % to be deleted

%Run the decoder and output to wavefile
[status,result] = system(sprintf('"%s" -if "%s" -of "%s"',exefile,aacfile,wavfile));
% Usage: D:\Users\tikuma\Documents\MATLAB\toolbox\neroaac\neroAacDec.exe -if <input file> -of <output file> [-chapter <number>] 
if status==1 % if decoding failed, show the message
   delete(wavefile);
   error(result);
end

% read wave file content
try
   varargout = cell(1,max(1,nargout));
   [varargout{:}] = wavread(wavfile,varargin{:});
   delete(wavfile); % Delete temporary wave file
catch ME
   delete(wavfile);
   rethrow(ME);
end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [exefile,msg] = getexepath()

msg = '';
if ispref('neroaac','decpath')
   exefile = getpref('neroaac','decpath');
   if ~(ischar(exefile) && exist(exefile,'file')) % check to make sure the file exists
      exefile = '';
   end
else
   exefile = '';
end

if isempty(exefile)
   [filename,pathname] = uigetfile({'neroaacdec.exe','Nero AAC Decoder EXE file'},...
      'Locate Nero AAC Encoder EXE file',which('neroaacdec.exe'));
   if (isnumeric(filename) && filename==0) || (isnumeric(pathname) && pathname==0)
      msg = 'User canceled to specify the Nero AAC Decoder executable.';
      return;
   end
   
   exefile = [pathname filename];
   
   setpref('neroaac','decpath',exefile);
end

end

Contact us