00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <string.h>
00027 #include <stdlib.h>
00028
00029 #include "uti.h"
00030 #include "caudiox.hpp"
00031
00032
00033
00034 PRIVATE CHAR *_nist_id16 = "NIST_1A\n 1024\n";
00035
00036
00037
00038 LONG AFNist::HdrR( FILE *f, KVStrList &, BOOL override )
00039 {
00040 #define N 1024
00041 CHAR buf[N];
00042 CHAR *s;
00043 LONG nSamp;
00044
00045 if (!testFile(f))
00046 fprintf(stderr,"%s warning: probably not a NIST 1024 file!\n",fFormat());
00047 xfseek(f,16,SEEK_SET);
00048
00049 nSamp=-1;
00050
00051 while (1) {
00052 if (!xfgets(buf,N,f)) break;
00053 if (strlen(buf) && (buf[strlen(buf)-1]!='\n'))
00054 die_beep("%s error: NIST header line too long",fFormat());
00055 s = strtok(buf," \n");
00056 if (!s) continue;
00057 if (!strcmp(s,"end_head")) break;
00058 if (!strcmp(s,"channel_count")) { s=strtok(NULL," \n"); s=strtok(NULL," \n;"); if (s) ADDIFNOV(CAUDIO_NCHAN,atol(s)); };
00059 if (!strcmp(s,"sample_count")) { s=strtok(NULL," \n"); s=strtok(NULL," \n;"); if (s) nSamp=atol(s); };
00060 if (!strcmp(s,"sample_rate")) { s=strtok(NULL," \n"); s=strtok(NULL," \n;"); if (s) ADDIFNOV(CAUDIO_SRATE,atol(s)); };
00061 if (!strcmp(s,"sample_coding")) { s=strtok(NULL," \n"); s=strtok(NULL," \n;");
00062 if (s && (strcmp("pcm",s))) die_beep("%s error: sample coding not supported (%s)",fFormat(),s); }
00063 if (!strcmp(s,"sample_n_bytes")) { s=strtok(NULL," \n"); s=strtok(NULL," \n;");
00064 INT slen=atoi(s);
00065 if (slen==2) ADD(CAUDIO_SAMPTYPE,SAMPTYPE_STR_PCM16);
00066 else if (slen==1) ADD(CAUDIO_SAMPTYPE,SAMPTYPE_STR_PCM8U);
00067 else die_beep("%s error: sample size not supported (%s)",fFormat(),s);
00068 }
00069 if (!strcmp(s,"sample_byte_format")) { s=strtok(NULL," \n"); s=strtok(NULL," \n;");
00070 if (s && (!strcmp("10",s))) ADDIFNOV(CAUDIO_BIGENDIAN,"yes");
00071 else if (s && (!strcmp(s,"01"))) ADDIFNOV(CAUDIO_BIGENDIAN,"no");
00072 else if (s) die_beep("%s error: sample byte format not supported (%s)",fFormat(),s);
00073 }
00074 }
00075
00076 xfseek(f,1024,SEEK_SET);
00077
00078 return nSamp;
00079 }
00080
00081
00082
00083 VOID AFNist::HdrG( KVStrList &def, BOOL override )
00084 {
00085 ADD(CAUDIO_NSAMPLES,0);
00086 ADDIFNOV(CAUDIO_SRATE,def.dval(CAUDIO_SRATE,8000));
00087 ADDIFNOV(CAUDIO_SAMPTYPE,def.val(CAUDIO_SAMPTYPE,SAMPTYPE_STR_PCM16));
00088 ADDIFNOV(CAUDIO_NCHAN,def.lval(CAUDIO_NCHAN,1));
00089
00090
00091 ADDIFNOV(CAUDIO_BIGENDIAN,def.val(CAUDIO_BIGENDIAN,CAUDIO_BIGENDIAN_DEFAULT?"yes":"no"));
00092
00093 switch (SampType_a2i(fa->info().val(CAUDIO_SAMPTYPE))) {
00094 case SAMPTYPE_PCM16:
00095 case SAMPTYPE_PCM8U: break;
00096 default:
00097 die_beep("%s: sampType %s not supported",fFormat(),
00098 (const char *)fa->info().val(CAUDIO_SAMPTYPE));
00099 }
00100 }
00101
00102
00103
00104 VOID AFNist::HdrW( FILE *f, LONG nSamp )
00105 {
00106 xfseek(f,0,SEEK_SET);
00107 xfwrite(_nist_id16,16,1,f);
00108
00109 fprintf(f,"channel_count -i %ld\n",(long)(fa->getNChan()));
00110 fprintf(f,"sample_count -i %ld\n",(long)nSamp);
00111 fprintf(f,"sample_rate -i %ld\n",(long)(fa->getSRate()+0.5));
00112
00113 long ssize=0;
00114 switch (fa->getSampType()) {
00115 case SAMPTYPE_PCM16: ssize=2; break;
00116 case SAMPTYPE_PCM8U: ssize=1; break;
00117 default: die_beep("%s: invalid sampType (%s)",fFormat(),fa->getSampType_a());
00118 }
00119 fprintf(f,"sample_coding -s3 pcm\n");
00120 fprintf(f,"sample_n_bytes -i %ld\n",(long)ssize);
00121 fprintf(f,"sample_byte_format -s2 %s\n",fa->getBigEndian()?"10":"01");
00122
00123 fprintf(f,"end_head\n");
00124
00125
00126 long pos0 = xftell(f);
00127 cdie_beep(pos0>1024,"%s error: NIST header too long (%ld)",fFormat(),(long)pos0);
00128 int i, nn, n=(int)(1024-pos0);
00129 nn = (n>10) ? 10 : n;
00130 for (i=0; i<nn; i++) { char ch='\n'; fwrite(&ch,1,1,f); }
00131 for (i=nn; i<n; i++) { char ch=0; fwrite(&ch,1,1,f); }
00132 }
00133
00134
00135
00136 BOOL AFNist::testFile( FILE *f )
00137 {
00138 CHAR id[17];
00139
00140 memset(id,0,sizeof(id));
00141 fseek(f,0,SEEK_SET);
00142 fread(id,16,1,f);
00143 return (!strcmp(id,_nist_id16));
00144 }
00145
00146