# v_readcnx

## PURPOSE

V_READCNX Read a .CNX format sound file [Y,FS,H]=(FILENAME)

## SYNOPSIS

function [y,fs,h]=v_readcnx(filename,mode)

## DESCRIPTION

    V_READCNX  Read a .CNX format sound file [Y,FS,H]=(FILENAME)

     Inputs:
               filename : character string containing filename (default extension .cnx)
               mode:    't' = trim leading and trailing silences
                        'h' = read header only
                 'd'    Look in data directory: v_voicebox('dir_data')

     Outputs:
               y  : column vector containing waveform
               fs : sample frequency
               h  : parameter array:
                    h(1) = number of samples in file
                    h(2) = status: 0=good, 1=bad
                    h(3) = start sample number
                    h(4) = ending sample number
                    h(5) = speaker identification number
                    h(6) = speaker age group
                    h(7) = speaker sex: 0=male, 1=female
                    h(8) = ascii character
                    h(9) = repetition number

     This is the format of the BT Connex-S1 alphabet database
     Note: the decoding is not particularly robust and assumes
           that all headers contain the same sequence of fields

## CROSS-REFERENCE INFORMATION

This function calls:

- v_voicebox V_VOICEBOX set global parameters for Voicebox functions Y=(FIELD,VAL)

This function is called by:

## SOURCE CODE

    0001 function [y,fs,h]=v_readcnx(filename,mode)
    0002 %V_READCNX  Read a .CNX format sound file [Y,FS,H]=(FILENAME)
    0003 %
    0004 % Inputs:
    0005 %           filename : character string containing filename (default extension .cnx)
    0006 %           mode:    't' = trim leading and trailing silences
    0007 %                    'h' = read header only
    0008 %             'd'    Look in data directory: v_voicebox('dir_data')
    0009 %
    0010 % Outputs:
    0011 %           y  : column vector containing waveform
    0012 %           fs : sample frequency
    0013 %           h  : parameter array:
    0014 %                h(1) = number of samples in file
    0015 %                h(2) = status: 0=good, 1=bad
    0016 %                h(3) = start sample number
    0017 %                h(4) = ending sample number
    0018 %                h(5) = speaker identification number
    0019 %                h(6) = speaker age group
    0020 %                h(7) = speaker sex: 0=male, 1=female
    0021 %                h(8) = ascii character
    0022 %                h(9) = repetition number
    0023 %
    0024 % This is the format of the BT Connex-S1 alphabet database
    0025 % Note: the decoding is not particularly robust and assumes
    0026 %       that all headers contain the same sequence of fields
    0027
    0028 %       Copyright (C) Mike Brookes 1998
    0029 %      Version: $Id: v_readcnx.m 10865 2018-09-21 17:22:45Z dmb $
    0030 %
    0031 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0032 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0033 %
    0034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0035 %   This program is free software; you can redistribute it and/or modify
    0036 %   it under the terms of the GNU Lesser General Public License as published by
    0037 %   the Free Software Foundation; either version 3 of the License, or
    0038 %   (at your option) any later version.
    0039 %
    0040 %   This program is distributed in the hope that it will be useful,
    0041 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0042 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0043 %   GNU Lesser General Public License for more details.
    0044 %
    0045 %   You can obtain a copy of the GNU Lesser General Public License from
    0046 %   https://www.gnu.org/licenses/ .
    0047 %
    0048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0049
    0050 ix=[17 71; 18 0; 19 0; 10 0; 12 0; 13 77; 15 -1; 16 0; ];
    0051
    0052 if nargin<2  mode='0'; end
    0053 if any(mode=='d')
    0054     filename=fullfile(v_voicebox('dir_data'),filename);
    0055 end
    0056 fid=fopen(filename,'rb','l');
    0057 if fid == -1
    0058     fn=[filename,'.cnx'];
    0059     fid=fopen(fn,'rb','l');
    0060     if fid ~= -1 filename=fn; end
    0061 end
    0062 if fid == -1 error(sprintf('Can''t open %s for input',filename)); end
    0063 [hdr,n]=fread(fid,512,'uchar');
    0064 if n ~= 512 error(sprintf('Can''t read header from connex file %s',filename)); end
    0065 del=find(hdr(5:end)=='|')';
    0066 fs=sscanf(char(hdr(17:del(1)+3)),'%f');
    0067 h=zeros(size(ix,1),1);
    0068 for i=1:length(h)
    0069     e=find(hdr(del(ix(i)-1)+5:del(ix(i))+3)=='=');
    0070     if ix(i,2)
    0071         h(i)=hdr(del(ix(i)-1)+e+5);
    0072         if ix(i,2)>0
    0073             h(i)=1-(h(i)==ix(i,2));
    0074         end
    0075     else
    0076         h(i)=sscanf(char(hdr(del(ix(i)-1)+e+5:del(ix(i))+3)),'%d');
    0077     end
    0078 end
    0079 if any(mode =='h')
    0080     y=[];
    0081 elseif any(mode =='t')
    0082     fseek(fid,2*h(2),0);
    0083     [y,n]=fread(fid,h(3)-h(2)+1,'short');
    0084     if n ~= h(3)-h(2)+1 error(sprintf('Error reading data from connex file %s',filename)); end
    0085 else
    0086     y=fread(fid,inf,'short');
    0087 end
    0088 fseek(fid,0,1);
    0089 h=[ftell(fid)/2-256; h];
    0090 fclose(fid);