SPREAD Read HTK (".SP") sound file. Y=SPREAD(FILE) reads a SP file specified by the string FILE, returning the sampled data in Y. The ".SP" extension is appended if no extension is given. Amplitude values are in the range [-1,+1]. [Y,FS,NBITS,SAMPLEKIND]=SPREAD(FILE) returns the sample rate (FS) in Hertz, the number of bits per sample (NBITS) used to encode the data in the file, and the code indicating the sample kind. [...]=SPREAD(FILE,N) returns only the first N samples in the file. [...]=SPREAD(FILE,[N1 N2]) returns only samples N1 through N2 in the file. SIZ=SPREAD(FILE,'size') returns the size of the audio data contained in the file in place of the actual audio data, returning the SIZ=samples.
0001 function [y,Fs,bits,parmKind] = spread(file,ext) 0002 %SPREAD Read HTK (".SP") sound file. 0003 % Y=SPREAD(FILE) reads a SP file specified by the string FILE, 0004 % returning the sampled data in Y. The ".SP" extension is appended 0005 % if no extension is given. Amplitude values are in the range [-1,+1]. 0006 % 0007 % [Y,FS,NBITS,SAMPLEKIND]=SPREAD(FILE) returns the sample rate 0008 % (FS) in Hertz, the number of bits per sample (NBITS) used to 0009 % encode the data in the file, and the code indicating the sample kind. 0010 % 0011 % [...]=SPREAD(FILE,N) returns only the first N samples in the file. 0012 % [...]=SPREAD(FILE,[N1 N2]) returns only samples N1 through N2 in the file. 0013 % SIZ=SPREAD(FILE,'size') returns the size of the audio data contained 0014 % in the file in place of the actual audio data, returning the 0015 % SIZ=samples. 0016 0017 % Parse input arguments: 0018 nargchk(1,2,nargin); 0019 if nargin<2, ext=[]; end % Default - read all samples 0020 exts = prod(size(ext)); % length of extent info 0021 if ~strncmp(lower(ext),'size',exts) & (exts > 2), 0022 error('Index range must be specified as a scalar or 2-element vector.'); 0023 end 0024 if ~ischar(ext) & exts==1, 0025 if ext==0, 0026 ext='size'; % synonym for size 0027 else 0028 ext=[1 ext]; % Prepend start sample index 0029 end 0030 end 0031 0032 % Open SP file. 0033 [fid,msg] = open_sp(file); 0034 error(msg); 0035 0036 % ------------------------------------------------------------------------ 0037 % Parse structure info for return to user: 0038 % ------------------------------------------------------------------------ 0039 0040 % The number of samples in file (4 bytes interger). 0041 nSamples = fread(fid,1,'int32'); 0042 0043 % The sample period in 100ns units (4 bytes interger). 0044 sampPeriod = fread(fid,1,'int32'); 0045 0046 % The number of bytes per sample (2 bytes interger). 0047 sampSize = fread(fid,1,'int16'); 0048 0049 % A code indicating the sample kind (2 bytes interger). 0050 parmKind = fread(fid,1,'int16'); 0051 0052 % Determine # bytes/sample. 0053 if (sampSize == 1), 0054 dtype='uchar'; % unsigned 8-bit 0055 elseif (sampSize == 2), 0056 dtype='short'; % signed 16-bit 0057 else 0058 error('Cannot read SP file formats with more than 16 bits per sample.'); 0059 end 0060 0061 % Return the sampled data. 0062 [y_tmp,cnt] = fread(fid,dtype); 0063 0064 % Return the sample rate in Hertz. 0065 Fs = 1/(sampPeriod/10000000); 0066 0067 % Return the number of bits per sample. 0068 % used to encode the data in the file. 0069 bits = sampSize*8; 0070 0071 % Check whether the file is SP file or not. 0072 if (nSamples ~= cnt), 0073 error('Not a SP file.'); 0074 end 0075 0076 if strncmp(lower(ext),'size',exts) | (~isempty(ext) & all(ext==0)), 0077 % Caller doesn't want data - just data size: 0078 y = nSamples; 0079 0080 else 0081 % Determine sample range to read: 0082 if isempty(ext), 0083 y = normal(y_tmp,sampSize); % Return all samples. 0084 else 0085 if prod(size(ext))~=2, 0086 error('Sample limit vector must have 2 elements.'); 0087 end 0088 if ext(1)<1 | ext(2)>nSamples, 0089 error('Sample limits out of range.'); 0090 end 0091 if ext(1)>ext(2), 0092 error('Sample limits must be given in ascending order.'); 0093 end 0094 y = normal(y_tmp(ext(1):ext(2)),sampSize); 0095 end 0096 0097 end 0098 0099 % ------------------------------------------------------------------------ 0100 % Private functions: 0101 % ------------------------------------------------------------------------ 0102 0103 % --------------------------------------------- 0104 % OPEN_SP: Open a sp file for reading 0105 % --------------------------------------------- 0106 function [fid,msg] = open_sp(file) 0107 % Append .sp extension if it's missing: 0108 [pat,nam,ext] = fileparts(file); 0109 if isempty(ext), 0110 file = [file '.sp']; 0111 end 0112 [fid,msg] = fopen(file,'rb','l'); % Little-endian 0113 if fid == -1, 0114 msg = 'Cannot open file.'; 0115 end 0116 return 0117 0118 % ---------------------------------------------- 0119 % NORMAL: Normalize data range. Min will hit -1, 0120 % max will not quite hit +1. 0121 % ---------------------------------------------- 0122 function [newData] = normal(Data,BytesPerSample) 0123 if (BytesPerSample == 1), 0124 newData = (Data-128)/128; % [-1,1) 0125 else 0126 newData = Data/32768; % [-1,1) 0127 end 0128 return