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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 #include <stdlib.h>
00098 #include <stdio.h>
00099 #include <stdarg.h>
00100 #include <assert.h>
00101
00102 #include "uti.h"
00103 #include "xalloc.h"
00104
00105 #include "caudiox.hpp"
00106
00107 #ifdef __OS_MSDOS__
00108 #include <io.h>
00109 #include <fcntl.h>
00110 #endif
00111
00112
00113
00114 CAudioFile::CAudioFile( VOID )
00115 {
00116 myFile=NULL;
00117 afFormat=NULL;
00118
00119 txhUpdatable=FALSE;
00120 updatable=FALSE;
00121 trackMarks=TRUE;
00122 };
00123
00124
00125
00126 BOOL CAudioFile::OK( VOID ) const
00127 {
00128 if (!myOpts.OK()) return FALSE;
00129 if (!myInfo.OK()) return FALSE;
00130 if (!myMarks.OK()) return FALSE;
00131 if (!mySamp.OK()) return FALSE;
00132 if (afFormat) if (!afFormat->OK()) return FALSE;
00133 return TRUE;
00134 }
00135
00136
00137
00138 VOID CAudioFile::setSRate( DOUBLE srate, BOOL adjustMarks )
00139 {
00140 mySRate=srate;
00141 if (adjustMarks) myMarks.adjustSRate(srate);
00142 else myMarks.setSRate(srate);
00143 }
00144
00145
00146
00147 VOID CAudioFile::open( const CHAR* fName, const CAudioFile &fa,
00148 const CHAR* options, ... )
00149 {
00150 va_list argptr;
00151 va_start(argptr, options);
00152 open(fName,fa,options,argptr);
00153 va_end(argptr);
00154 }
00155
00156
00157
00158 VOID CAudioFile::open( const CHAR* fName, const CAudioFile &fa,
00159 const CHAR* options, va_list argptr )
00160 {
00161 KVStrList tmp(options,"=", argptr);
00162
00163 open(fName,fa,tmp);
00164 }
00165
00166
00167
00168 VOID CAudioFile::open( const CHAR* fName, const CAudioFile &fa,
00169 const KVStrList &pr )
00170 {
00171 KVStrList p(pr);
00172
00173 p.add(CAUDIO_FFORMAT,fa.getFFormat());
00174 p.add(CAUDIO_SRATE,fa.getSRate());
00175 p.add(CAUDIO_SAMPTYPE,fa.getSampType_a());
00176 p.add(CAUDIO_NCHAN,(LONG)fa.getNChan());
00177 p.add(CAUDIO_BIGENDIAN,fa.getBigEndian());
00178
00179 open(fName,"w",p);
00180 }
00181
00182
00183
00184 VOID CAudioFile::open( const CHAR* fName, const CHAR* mode,
00185 const CHAR* options, ... )
00186 {
00187 va_list argptr;
00188 va_start(argptr, options);
00189 open(fName,mode,options,argptr);
00190 va_end(argptr);
00191 }
00192
00193
00194
00195 VOID CAudioFile::open( const CHAR* fName, const CHAR* mode,
00196 const CHAR* options, va_list argptr )
00197 {
00198 KVStrList tmp(options,"=", argptr);
00199
00200 open(fName,mode,tmp);
00201 }
00202
00203
00204
00205 VOID CAudioFile::open( const CHAR* fName, const CHAR* mode, const KVStrList &pr)
00206 {
00207 INT txhPos;
00208 BOOL stdinout=FALSE;
00209
00210 if (myFile!=NULL)
00211 die_beep("CAudioFile: close file %s before opening another one",
00212 (const char *)myOpts.val(CAUDIO_FNAME));
00213 cdie_beep(!mode,"CAudioFile: invalid open() mode (NULL)");
00214
00215 myOpts.clear();
00216
00217 { KVStrList tmp(CAUDIO_OPTS_DEFAULT); myOpts.add_mv(tmp,TRUE); }
00218
00219 char *s=getenv(CAUDIO_ENVOPTS);
00220 if (s) { KVStrList tmp(s); myOpts.add_mv(tmp,TRUE); }
00221
00222 myOpts.add(pr);
00223
00224 if (!strcmp(fName,"-")) {
00225 myOpts.add(CAUDIO_FFORMAT,"Raw");
00226 stdinout=TRUE;
00227 }
00228
00229 SampType_a2i(myOpts.val(CAUDIO_SAMPTYPE,SAMPTYPE_STR_PCM16));
00230 myOpts.dval(CAUDIO_SRATE,8000);
00231 myOpts.lval(CAUDIO_NCHAN,1);
00232
00233
00234 myOpts.add(CAUDIO_FNAME,fName);
00235
00236 const String &format=myOpts.val(CAUDIO_FFORMAT);
00237
00238 txhUpdatable=FALSE;
00239 txhPos=0;
00240 if (format.contains("Txh")) {
00241 txhGetName(fName);
00242 txhLoad(mode);
00243
00244 txhPos = format.contains("Txh",0) ? -1 : 1;
00245 }
00246 afFormat = getAFFormatObject(fName,mode);
00247
00248 if (txhPos==1) myOpts.add(CAUDIO_FFORMAT, String(afFormat->fFormat())+"-Txh");
00249 else if (txhPos==-1) myOpts.add(CAUDIO_FFORMAT, "Txh-"+String(afFormat->fFormat()));
00250 else myOpts.add(CAUDIO_FFORMAT, afFormat->fFormat());
00251
00252 updatable = TRUE;
00253 BOOL override = (txhPos>0);
00254 BOOL write=FALSE;
00255 LONG nSamp=0;
00256
00257 CHAR xmode= *mode;
00258 if (xmode=='M') {
00259 myFile = fopen(fName,"r+b");
00260 if (myFile) { fclose(myFile); xmode='m'; myFile=NULL; }
00261 else xmode='r';
00262 }
00263
00264
00265 switch (xmode) {
00266 case 'r':
00267 updatable = FALSE;
00268 if (stdinout) {
00269 myFile=stdin;
00270 #ifdef __OS_MSDOS__
00271 setmode(fileno(stdin), O_BINARY);
00272 #endif
00273 }
00274 else myFile = xfopen(fName,"rb");
00275 nSamp=afFormat->HdrR(myFile,myOpts,override);
00276 break;
00277 case 'w':
00278 if (stdinout) {
00279 myFile=stdout;
00280 updatable=FALSE;
00281 #ifdef __OS_MSDOS__
00282 setmode(fileno(stdout), O_BINARY);
00283 #endif
00284 }
00285 else myFile = xfopen(fName,"w+b");
00286 afFormat->HdrG(myOpts,override);
00287 write=TRUE;
00288 break;
00289 case 'm':
00290 cdie_beep(stdinout,"CAudioFile: can't open stdin/stdout in modify (m) mode");
00291 myFile = xfopen(fName,"r+b");
00292 nSamp=afFormat->HdrR(myFile,myOpts,override);
00293 break;
00294 case 'a':
00295 cdie_beep(stdinout,"CAudioFile: can't open stdin/stdout in append (a) mode");
00296 myFile = fopen(fName,"r+b");
00297 if (myFile) nSamp=afFormat->HdrR(myFile,myOpts,override);
00298 else {
00299 myFile = xfopen(fName,"w+b");
00300 afFormat->HdrG(myOpts,override);
00301 write=TRUE;
00302 }
00303 break;
00304 default:
00305 die_beep("CAudioFile: invalid open() mode (%c)",xmode);
00306 break;
00307 }
00308
00309 mySampType = SampType_a2i(myInfo.val(CAUDIO_SAMPTYPE,
00310 myOpts.val(CAUDIO_SAMPTYPE,SAMPTYPE_STR_PCM16)));
00311 myNChan = (UINT)myInfo.lval(CAUDIO_NCHAN,myOpts.lval(CAUDIO_NCHAN,1));
00312 mySRate = myInfo.dval(CAUDIO_SRATE,myOpts.dval(CAUDIO_SRATE,8000));
00313 myMarks.setSRate(mySRate);
00314
00315 myBigEndian = myInfo.bval(CAUDIO_BIGENDIAN,myOpts.bval(CAUDIO_BIGENDIAN,CAUDIO_BIGENDIAN_DEFAULT));
00316
00317 myOpts.erase(CAUDIO_SAMPTYPE);
00318 myOpts.erase(CAUDIO_SRATE);
00319 myOpts.erase(CAUDIO_NCHAN);
00320 myOpts.erase(CAUDIO_NSAMPLES);
00321 myOpts.erase(CAUDIO_BIGENDIAN);
00322
00323
00324
00325
00326
00327
00328
00329 if (write) afFormat->HdrW(myFile,nSamp);
00330 mySamp.attach(myFile, mySampType, myNChan, stdinout?-2:nSamp);
00331 mySamp.setendian(myBigEndian?-1:1);
00332
00333 if (myInfo.contains(CAUDIO_NSAMPLES))
00334 myInfo.add(CAUDIO_NSAMPLES,getNSamples());
00335 }
00336
00337
00338
00339 VOID CAudioFile::set_updatable( BOOL update )
00340 {
00341 updatable=update;
00342 }
00343
00344
00345
00346 BOOL CAudioFile::update( VOID )
00347 {
00348 if (!myFile) return FALSE;
00349
00350 if (myInfo.contains(CAUDIO_SAMPTYPE))
00351 myInfo.add(CAUDIO_SAMPTYPE,SampType_i2a(getSampType()));
00352 if (myInfo.contains(CAUDIO_SRATE)) myInfo.add(CAUDIO_SRATE,getSRate());
00353 if (myInfo.contains(CAUDIO_BIGENDIAN)) myInfo.add(CAUDIO_BIGENDIAN,getBigEndian());
00354 if (myInfo.contains(CAUDIO_NCHAN)) myInfo.add(CAUDIO_NCHAN,(LONG)getNChan());
00355 if (myInfo.contains(CAUDIO_NSAMPLES)) myInfo.add(CAUDIO_NSAMPLES,getNSamples());
00356
00357 if (updatable) {
00358 LONG pos = mySamp.getPos();
00359 LONG n = mySamp.getN();
00360 afFormat->HdrW(myFile,n);
00361 mySamp.attach(myFile, mySamp.getSampType(), mySamp.getNChan(), n);
00362 mySamp.setPos(pos);
00363 }
00364
00365 if (txhUpdatable) { txhWrite(); return TRUE; }
00366 if (updatable) return TRUE;
00367 return FALSE;
00368 }
00369
00370
00371
00372 VOID CAudioFile::close( VOID )
00373 {
00374 if (!myFile) return;
00375
00376 update();
00377 mySamp.attach(NULL,0);
00378 if (myFile) {
00379 if ((myFile!=stdin)&&(myFile!=stdout))
00380 xfclose(myFile);
00381 myFile = NULL;
00382 }
00383 if (afFormat) { delete(afFormat); afFormat = NULL; }
00384
00385 myMarks.clear();
00386 myInfo.clear();
00387 myOpts.clear();
00388 updatable=txhUpdatable=FALSE;
00389 }
00390
00391
00392
00393 VOID CAudioFile::sndCopy( CAudioFile &fromf, LONG n )
00394 {
00395 #define BUFLEN 1024
00396 if ((fromf.getSampType()==getSampType())&&
00397 (fromf.getSampType()==SAMPTYPE_PCM16)&&
00398 (fromf.getNChan()==getNChan())) {
00399 while (n>=BUFLEN) {
00400 INT16 *v=fromf.getChAll_i16(BUFLEN);
00401 setBlkChAll(v,BUFLEN);
00402 n-=BUFLEN;
00403 }
00404 if (n) {
00405 INT16 *v=fromf.getChAll_i16((UINT)n);
00406 setBlkChAll(v,(UINT)n);
00407 }
00408 }
00409 else {
00410 UINT nch=getNChan();
00411 if (nch) {
00412 nch--;
00413 while (n>=BUFLEN) {
00414 DOUBLE *v=fromf.getChAll_d(BUFLEN);
00415 setBlkChRange(v,BUFLEN,0,nch);
00416 n-=BUFLEN;
00417 }
00418 if (n) {
00419 DOUBLE *v=fromf.getChAll_d((UINT)n);
00420 setBlkChRange(v,(UINT)n,0,nch);
00421 }
00422 }
00423 }
00424 }
00425
00426