PCM2AIFF.C
上传用户:njqiyou
上传日期:2007-01-08
资源大小:574k
文件大小:21k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. /**********************************************************************
  2.  * ISO MPEG Audio Subgroup Software Simulation Group (1996)
  3.  * Audio Interchange File Format (AIFF) / PCM file conversion.
  4.  *
  5.  * $Id: pcm2aiff.c,v 1.2 1996/04/18 06:15:42 rowlands Exp $
  6.  *
  7.  * $Log: pcm2aiff.c,v $
  8.  * Revision 1.2  1996/04/18 06:15:42  rowlands
  9.  * Added multilingual file name format facility.
  10.  *
  11.  * Revision 1.1  1996/04/18 03:39:59  rowlands
  12.  * Initial revision
  13.  *
  14.  * Revision 1.0  1995/06/13  fukuchi
  15.  * UNIX version.
  16.  *
  17.  * Revision 1.1  1995/10/03  09:35:00 fukuchi 
  18.  * Ported new code for MSDOS .
  19.  *
  20.  **********************************************************************/
  21. /**********************************************************************
  22.  * VERSION 1.1                                                        *
  23.  *   changes made since last update:                                  *
  24.  *   date   programmers                comment                        *
  25.  * 6/13/95  Hiroyuki Fukuchi    start of version 1.0 records          *
  26.  * 10/03/95  Hiroyuki Fukuchi   Ported new code for MSDOS.            *
  27.  **********************************************************************/
  28. /***********************************************************************
  29. *
  30. *  Global Include Files
  31. *
  32. ***********************************************************************/
  33. #include        "common.h"
  34. #include        "decoder.h"
  35. /***************************************************************************/
  36. #define MaxNumberOfChannels 7
  37. char * multichannelPCMFilenameSuffix[] = {
  38. "l", "r", "c", "ls", "rs", NULL
  39. };
  40. char * multilingualPCMFilenameSuffix[] = {
  41. "m1", "m2", "m3", "m4", "m5", "m6", "m7", NULL
  42. };
  43. char ** pcmFilenameSuffix = multichannelPCMFilenameSuffix;
  44. #ifdef __MSDOS__
  45. char * multichannelAIFFFilenameSuffix = ".aif";
  46. char * multilingualAIFFFilenameSuffix = ".aif";
  47. char * aiffFilenameSuffix = ".aif";
  48. #else
  49. char * multichannelAIFFFilenameSuffix = ".aiff";
  50. char * multilingualAIFFFilenameSuffix = ".aiff.ml";
  51. char * aiffFilenameSuffix = ".aiff";
  52. #endif
  53. /***************************************************************************/
  54. main(int argc, char *argv[])
  55. {
  56. char    file_name[50];
  57. int     i,j,c,disp,arg_count,config,aiff_mode;
  58. IFF_AIFF        aiff,*aiff_ptr;
  59. void    exit();
  60. int     pcm2aiff(),aiff2pcm();        
  61. aiff_ptr=&aiff;
  62. if(argc==1){
  63.     printf("usage : pcm2aiff <input file> [-a],[-c[nx]],[-r[nx]],[-m]n");
  64.     printf("            pcm <-> aiff file conversionn");
  65.     printf("         [option]n");
  66.     printf("        a : aiff->pcm (default: pcm->aiff)n");
  67.     printf("        c[nx]: channel configuration for pcm->aiffn");
  68.     printf("            nx (default: 1)n");
  69.     printf("            0: L n");
  70.     printf("            1: L Rn"); 
  71.     printf("            2: L R Cn");
  72.     printf("            3: L R C LSn");
  73.     printf("            4: L R C LS RSn");
  74.     printf("        m : multilingual filename format (default multichannel)n");
  75.     printf("        r[nx]: sampling frequencyn");
  76.     printf("            nx (default: 0)n");
  77.     printf("            0: 44.1 kHz n");
  78.     printf("            1: 48 kHzn"); 
  79.     printf("            2: 32 KHzn");
  80.     printf("        L  ch file is <[input file]_l.pcm>n");
  81.     printf("        R  ch file is <[input file]_r.pcm>n");
  82.     printf("        LS ch file is <[input file]_ls.pcm>n");
  83.     printf("        RS ch file is <[input file]_rs.pcm>n");
  84.     printf("        C  ch file is <[input file]_c.pcm>n");
  85.     printf("        aiff  file is <[input file].aiff> or <[input file].aiff.ml>n");
  86.     exit();
  87. }
  88. for(i=0;i<50;i++)
  89.     file_name[i]=0;
  90. i=0;
  91. file_name[i]=(*++argv)[0];
  92. i++;
  93. while(file_name[i]=*++argv[0])
  94.     i++;
  95. config=1;
  96. aiff_mode=0;
  97. arg_count=argc;
  98. aiff_ptr->sampleRate=44100.00;
  99. while(arg_count>=3){
  100.     arg_count--;
  101.     if((*++argv)[0]=='-'){
  102. while(c=*++argv[0]){
  103.     switch(c){
  104.     case        'r':
  105. c=*++argv[0];
  106. switch(c){
  107. case    '0':
  108.     aiff_ptr->sampleRate=44100.00;
  109.     break;
  110. case    '1':
  111.     aiff_ptr->sampleRate=48000.00;
  112.     break;
  113. case    '2':
  114.     aiff_ptr->sampleRate=32000.00;
  115.     break;
  116. default :
  117.     aiff_ptr->sampleRate=44100.00;
  118.     break;
  119. };
  120. break;
  121.     case 'm':
  122. pcmFilenameSuffix = multilingualPCMFilenameSuffix;
  123. aiffFilenameSuffix = multilingualAIFFFilenameSuffix;
  124. break;
  125.     case        'c':
  126. config=*++argv[0]-'0';
  127. break;
  128.     case        'a':
  129. aiff_mode=1;
  130. break;
  131.     default:
  132. printf(" ????n");
  133. exit();
  134.     }
  135. }
  136.     }
  137. }
  138. if(aiff_mode){
  139.     aiff2pcm(file_name,aiff_ptr);
  140. }else{
  141.     pcm2aiff(file_name,aiff_ptr,config);
  142. }
  143. };
  144. /**********************************************************************
  145. *
  146. *  PCM to Audio Interchange File Format(AIFF) conversion.
  147. *
  148. **********************************************************************/
  149. int pcm2aiff(char *fname,IFF_AIFF *aiff_ptr,int config)
  150. {
  151. register char   i;
  152. register long   seek_offset;
  153. int             index,lflag,rflag,cflag,lsflag,rsflag;
  154. int             size;
  155. int             BUF_SIZE=384;
  156. unsigned long   frame;
  157. char            temp_sampleRate[10];
  158. static char     tmp_file_name[200];
  159. char            holder;
  160. FILE            *file_ptr_pcm[MaxNumberOfChannels];
  161. FILE            *file_ptr_aiff;
  162. static char     buf[MaxNumberOfChannels][1152*2];
  163. char            buf0[2]={0,0};
  164. Chunk           FormChunk;
  165. CommonChunk     CommChunk;
  166. SoundDataChunk  SndDChunk;
  167. void            exit();
  168. #ifdef __MSDOS__
  169. long            lholder;
  170. void            swab();
  171. #endif
  172. printf("        pcm to aiff conversionn");
  173. aiff_ptr->numChannels = config + 1;
  174. for (i = 0; i < aiff_ptr->numChannels; i++) {
  175.     sprintf(tmp_file_name, "%s_%s.pcm", fname, pcmFilenameSuffix[i]);
  176.     if(NULL==(file_ptr_pcm[i]=fopen(tmp_file_name,"rb"))){
  177. printf("        read file open errorn");
  178. exit(1);
  179.     }
  180. }
  181. strcpy( tmp_file_name,fname); 
  182. strcat( tmp_file_name,aiffFilenameSuffix); 
  183. if(NULL==(file_ptr_aiff=fopen(tmp_file_name,"wb"))){
  184. printf("        write file open errorn");
  185. exit();
  186. }
  187. aiff_ptr->sampleSize=16;
  188. aiff_ptr->blkAlgn.offset=0;
  189. aiff_ptr->blkAlgn.blockSize=0;
  190. strcpy(aiff_ptr->sampleType,IFF_ID_SSND);
  191. double_to_extended(&aiff_ptr->sampleRate, temp_sampleRate);
  192. strcpy( FormChunk.ckID, IFF_ID_FORM);
  193. strcpy( FormChunk.formType, IFF_ID_AIFF);
  194. strcpy( CommChunk.ckID, IFF_ID_COMM); 
  195. for (i = 0; i < sizeof(char[10]); i++)
  196.     CommChunk.sampleRate[i] = temp_sampleRate[i];
  197. CommChunk.numChannels             = aiff_ptr->numChannels;
  198. CommChunk.sampleSize              = aiff_ptr->sampleSize;
  199. SndDChunk.offset                  = aiff_ptr->blkAlgn.offset;
  200. SndDChunk.blockSize               = aiff_ptr->blkAlgn.blockSize;
  201. strncpy( SndDChunk.ckID, aiff_ptr->sampleType,4);
  202. CommChunk.ckSize = sizeof(CommChunk.numChannels) +
  203. sizeof(CommChunk.numSampleFrames)+sizeof(CommChunk.sampleSize)+
  204. sizeof(CommChunk.sampleRate);
  205. SndDChunk.ckSize = sizeof(SoundDataChunk)-sizeof(ChunkHeader)+
  206. (CommChunk.sampleSize + BITS_IN_A_BYTE - 1) / BITS_IN_A_BYTE *
  207. CommChunk.numChannels * CommChunk.numSampleFrames;
  208. FormChunk.ckSize=sizeof(Chunk)+SndDChunk.ckSize+sizeof(ChunkHeader)+
  209.     CommChunk.ckSize;
  210. if(fwrite(&FormChunk, sizeof(Chunk), 1, file_ptr_aiff) != 1)
  211.     return(-1);
  212. if(fwrite(CommChunk.ckID, sizeof(ID),1,file_ptr_aiff) != 1)
  213.     return(-1);
  214. if(fwrite(&CommChunk.ckSize,sizeof(long),1,file_ptr_aiff)!=1)
  215.     return(-1);
  216. if(fwrite(&CommChunk.numChannels,sizeof(short),1,file_ptr_aiff)!=1)
  217.     return(-1);
  218. if(fwrite(&CommChunk.numSampleFrames,sizeof(unsigned long),1,
  219.       file_ptr_aiff) != 1)
  220.     return(-1);
  221. if(fwrite(&CommChunk.sampleSize,sizeof(short),1,file_ptr_aiff)!=1)
  222.     return(-1);
  223. if(fwrite(CommChunk.sampleRate,sizeof(char[10]),1,file_ptr_aiff)!=1)
  224.     return(-1);
  225. if(fwrite(&SndDChunk,sizeof(SoundDataChunk),1,file_ptr_aiff)!=1)
  226.     return(-1);
  227. frame=0;
  228. while(1){
  229.     for (i = 0; i < aiff_ptr->numChannels; i++) {
  230.     size=fread(&buf[i],2,BUF_SIZE,file_ptr_pcm[i]);
  231.     if(size==0) break;
  232.     }
  233.     for(index=0;index<size;index++){
  234. for (i = 0; i < aiff_ptr->numChannels; i++) {
  235. fwrite(&buf[i][2*index],2,1,file_ptr_aiff);
  236. }
  237.     }
  238.     frame++;
  239.     if(size<BUF_SIZE){
  240. for(index=size;index<BUF_SIZE;index++){
  241.     for (i = 0; i < aiff_ptr->numChannels; i++) {
  242. fwrite(&buf0[i],2,1,file_ptr_aiff);
  243.     }
  244. }
  245. break;
  246.     }
  247. }
  248. printf("        number of samples in frame / channel = %ldn",BUF_SIZE*frame-size);
  249. aiff_ptr->numSampleFrames=BUF_SIZE*frame;
  250. CommChunk.numSampleFrames         = aiff_ptr->numSampleFrames;
  251. CommChunk.ckSize = sizeof(CommChunk.numChannels)+
  252. sizeof(CommChunk.numSampleFrames)+sizeof(CommChunk.sampleSize)+
  253. sizeof(CommChunk.sampleRate);
  254. SndDChunk.ckSize =sizeof(SoundDataChunk)-sizeof(ChunkHeader)+
  255. (CommChunk.sampleSize + BITS_IN_A_BYTE - 1) / BITS_IN_A_BYTE *
  256. CommChunk.numChannels * CommChunk.numSampleFrames;
  257. FormChunk.ckSize = sizeof(Chunk)+SndDChunk.ckSize+sizeof(ChunkHeader)+
  258.     CommChunk.ckSize;
  259. if (fseek(file_ptr_aiff, 0, SEEK_SET) != 0)
  260.     return(-1);
  261. #ifdef __MSDOS__
  262. lholder=FormChunk.ckSize;
  263. swab(&lholder,&FormChunk.ckSize,4);
  264. lholder=FormChunk.ckSize&0xffff;
  265. FormChunk.ckSize=(((FormChunk.ckSize>>16)&0xffff)+(lholder<<16))&0xffffffff;
  266. #endif
  267. if (fwrite(&FormChunk, sizeof(Chunk), 1, file_ptr_aiff) != 1)
  268.     return(-1);
  269. #ifdef __MSDOS__
  270. lholder=CommChunk.ckSize;
  271. swab(&lholder,&CommChunk.ckSize,4);
  272. lholder=CommChunk.ckSize&0xffff;
  273. CommChunk.ckSize=(((CommChunk.ckSize>>16)&0xffff)+(lholder<<16))&0xffffffff;
  274. lholder=CommChunk.sampleSize;
  275. swab(&lholder,&CommChunk.sampleSize,2);
  276. lholder=CommChunk.numChannels;
  277. swab(&lholder,&CommChunk.numChannels,2);
  278. lholder=CommChunk.numSampleFrames;
  279. swab(&lholder,&CommChunk.numSampleFrames,4);
  280. lholder=CommChunk.numSampleFrames&0xffff;
  281. CommChunk.numSampleFrames=(((CommChunk.numSampleFrames>>16)&0xffff)+(lholder<<16))&0xffffffff;
  282. #endif
  283. if (fwrite(CommChunk.ckID,sizeof(ID),1,file_ptr_aiff)!=1)
  284.     return(-1);
  285. if (fwrite(&CommChunk.ckSize,sizeof(long),1,file_ptr_aiff)!=1)
  286.     return(-1);
  287. if (fwrite(&CommChunk.numChannels,sizeof(short),1,file_ptr_aiff)!=1)
  288.     return(-1);
  289. if (fwrite(&CommChunk.numSampleFrames,sizeof(unsigned long),1,
  290.       file_ptr_aiff)!=1)
  291.     return(-1);
  292. if (fwrite(&CommChunk.sampleSize, sizeof(short),1,file_ptr_aiff)!=1)
  293.     return(-1);
  294. if (fwrite(CommChunk.sampleRate,sizeof(char[10]),1,file_ptr_aiff)!=1)
  295.     return(-1);
  296. #ifdef __MSDOS__
  297. lholder=SndDChunk.ckSize;
  298. swab(&lholder,&SndDChunk.ckSize,4);
  299. lholder=SndDChunk.ckSize&0xffff;
  300. SndDChunk.ckSize=(((SndDChunk.ckSize>>16)&0xffff)+(lholder<<16))&0xffffffff;
  301. lholder=SndDChunk.blockSize;
  302. swab(&lholder,&SndDChunk.blockSize,4);
  303. lholder=SndDChunk.blockSize&0xffff;
  304. SndDChunk.blockSize=(((SndDChunk.blockSize>>16)&0xffff)+(lholder<<16))&0xffffffff;
  305. lholder=SndDChunk.offset;
  306. swab(&lholder,&SndDChunk.offset,4);
  307. lholder=SndDChunk.offset&0xffff;
  308. SndDChunk.offset=(((SndDChunk.offset>>16)&0xffff)+(lholder<<16))&0xffffffff;
  309. #endif
  310. if(fwrite(&SndDChunk,sizeof(SoundDataChunk),1,file_ptr_aiff)!=1)
  311.     return(-1);
  312. for (i = 0; i < aiff_ptr->numChannels; i++) {
  313. fclose(file_ptr_pcm[i]);
  314. }
  315. fclose(file_ptr_aiff);
  316. return(0);
  317. }
  318. /**********************************************************************
  319. *
  320. *  Audio Interchange File Format(AIFF) to PCM conversion.
  321. *
  322. **********************************************************************/
  323. int aiff2pcm(char *fname,IFF_AIFF *aiff_ptr)
  324. {
  325. int             *byte_per_sample;
  326. FILE            *file_ptr,*write_file_ptr[MaxNumberOfChannels];
  327. int             ch,numChan,diff_numChan,sample_size;
  328. register char   i;
  329. long            index;
  330. register long   seek_offset;
  331. char            temp_sampleRate[10];
  332. char            tmpBuf[10];
  333. int             *dummy,*dummy1;
  334. char            holder;
  335. static char     tmp_file_name[200];
  336. Chunk           FormChunk;
  337. CommonChunk     CommChunk;
  338. SoundDataChunk  SndDChunk;
  339. identifier      ident;
  340. void            exit();
  341. IEEE_DBL        *p_dbl,dbl;
  342. #ifdef  __MSDOS__
  343. unsigned long   lholder;
  344. void            swab();
  345. #endif
  346. p_dbl=&dbl;
  347. printf("        aiff to pcm conversionn");
  348. strcpy( tmp_file_name,fname); 
  349. strcat( tmp_file_name,aiffFilenameSuffix); 
  350. if(NULL==(file_ptr=fopen(tmp_file_name,"rb"))){
  351.     printf("    read file open errorn");
  352.     exit();
  353. }
  354. if (fseek(file_ptr, 0, SEEK_SET) != 0)
  355.     return(-1);
  356. if (fread(&FormChunk, sizeof(Chunk), 1, file_ptr) != 1)
  357.     return(-1);
  358. if (strncmp(FormChunk.ckID,IFF_ID_FORM,4) != 0 ||
  359.       strncmp(FormChunk.formType,IFF_ID_AIFF,4) != 0)
  360.     return(-1);
  361. if (fread(&ident,sizeof(identifier), 1, file_ptr) != 1)
  362.     return(-1);
  363. #ifdef __MSDOS__
  364. lholder=ident.ck_length;
  365. swab(&lholder,&ident.ck_length,4);
  366. lholder=ident.ck_length&0xffff;
  367. ident.ck_length=(((ident.ck_length>>16)&0xffff)+(lholder<<16))&0xffffffff;
  368. #endif
  369. while(strncmp(ident.name,IFF_ID_COMM,4) != 0)
  370. {
  371.     if(NULL==(dummy = (int *)calloc( ident.ck_length, sizeof(char)))){
  372. printf("        NULL pointer 1 n");
  373.     }
  374.     if(fread( dummy, ident.ck_length, 1, file_ptr) != 1)
  375. return(-1);
  376.     free(dummy);
  377.     if(fread( &ident, sizeof(identifier),1, file_ptr) != 1)
  378. return(-1);
  379. #ifdef __MSDOS__
  380.     lholder=ident.ck_length;
  381.     swab(&lholder,&ident.ck_length,4);
  382.     lholder=ident.ck_length&0xffff;
  383.     ident.ck_length=(((ident.ck_length>>16)&0xffff)+(lholder<<16))&0xffffffff;
  384. #endif
  385. }
  386. for( i = 0; i < 4; ++i){
  387. CommChunk.ckID[i] = ident.name[i];
  388. }
  389. CommChunk.ckSize = ident.ck_length;
  390. if (fread(&CommChunk.numChannels, sizeof(short), 1, file_ptr) != 1)
  391.     return(-1);
  392. if (fread(&CommChunk.numSampleFrames, sizeof(unsigned long), 1,
  393.       file_ptr) != 1)
  394.     return(-1);
  395. if (fread(&CommChunk.sampleSize, sizeof(short), 1, file_ptr) != 1)
  396.     return(-1);
  397. if (fread(CommChunk.sampleRate, sizeof(char[10]), 1, file_ptr) != 1)
  398.     return(-1);
  399. #ifdef __MSDOS__
  400. lholder=CommChunk.numChannels;
  401. swab(&lholder,&CommChunk.numChannels,2);
  402. lholder=CommChunk.numSampleFrames;
  403. swab(&lholder,&CommChunk.numSampleFrames,4);
  404. lholder=CommChunk.numSampleFrames&0xffff;
  405. CommChunk.numSampleFrames=((CommChunk.numSampleFrames>>16)+(lholder<<16))&0xffffffff;
  406. lholder=CommChunk.sampleSize;
  407. swab(&lholder,&CommChunk.sampleSize,2);
  408. #endif
  409. for (i = 0; i < sizeof(char[10]); i++)
  410.     temp_sampleRate[i] = CommChunk.sampleRate[i];
  411. extended_to_double(temp_sampleRate, &aiff_ptr->sampleRate);
  412. fseek (file_ptr, sizeof(Chunk), SEEK_SET);
  413. if (fread(&ident, sizeof(identifier), 1, file_ptr) != 1)
  414.     return(-1);
  415. #ifdef __MSDOS__
  416. lholder=ident.ck_length;
  417. swab(&lholder,&ident.ck_length,4);
  418. lholder=ident.ck_length&0xffff;
  419. ident.ck_length=(((ident.ck_length>>16)&0xffff)+(lholder<<16))&0xffffffff;
  420. #endif
  421. while(strncmp(ident.name,IFF_ID_SSND,4) != 0)
  422. {
  423.     if(NULL==(dummy1 = (int *)calloc( ident.ck_length, sizeof(char)))){
  424. printf("        NULL pointer2 n");
  425.     }
  426.     if(fread( dummy1, ident.ck_length, 1, file_ptr) != 1)
  427. return(-1);
  428.     free(dummy1);
  429.     if(fread( &ident, sizeof(identifier),1, file_ptr) != 1)
  430. return (-1);
  431. #ifdef __MSDOS__
  432.     lholder=ident.ck_length;
  433.     swab(&lholder,&ident.ck_length,4);
  434.     lholder=ident.ck_length&0xffff;
  435.     ident.ck_length=(((ident.ck_length>>16)&0xffff)+(lholder<<16))&0xffffffff;
  436. #endif
  437. }
  438. for(i = 0; i < 4; ++i)
  439.     SndDChunk.ckID[i] = ident.name[i];
  440. if (fread(&SndDChunk.offset, sizeof(unsigned long), 1, file_ptr)!=1)
  441.     return(-1);
  442. #ifdef __MSDOS__
  443. lholder=SndDChunk.offset;
  444. swab(&lholder,&SndDChunk.offset,4);
  445. lholder=SndDChunk.offset&0xffff;
  446. SndDChunk.offset=(((SndDChunk.offset>>16)&0xffff)+(lholder<<16))&0xffffffff;
  447. #endif
  448. if (fread(&SndDChunk.blockSize, sizeof(unsigned long), 1,
  449.      file_ptr) != 1)
  450.     return(-1);
  451. #ifdef __MSDOS__
  452. lholder=SndDChunk.blockSize;
  453. swab(&lholder,&SndDChunk.blockSize,4);
  454. lholder=SndDChunk.blockSize&0xffff;
  455. SndDChunk.blockSize=(((SndDChunk.blockSize>>16)&0xffff)+(lholder<<16))&0xffffffff;
  456. #endif
  457. numChan = CommChunk.numChannels;
  458. ch=0;
  459. for (i = 0; i < numChan; i++) {
  460.     sprintf(tmp_file_name, "%s_%s.pcm", fname, pcmFilenameSuffix[i]);
  461.     if(NULL==(write_file_ptr[ch]=fopen(tmp_file_name,"wb"))){
  462. printf("        write file open errorn");
  463. exit(1);
  464.     }
  465.     ch++;
  466. }
  467. diff_numChan=numChan-CommChunk.numChannels;
  468. if(diff_numChan>0){
  469. }
  470. printf("        number of samples in frame / channel = %ldn",CommChunk.numSampleFrames);
  471. sample_size=(CommChunk.sampleSize+BITS_IN_A_BYTE-1)/BITS_IN_A_BYTE;
  472. for(index = 0; index < CommChunk.numSampleFrames; index++){
  473.     for(ch = 0; ch < CommChunk.numChannels; ch++){
  474. if (fread(&tmpBuf, sample_size, 1,file_ptr) != 1)
  475.     return(-1);
  476. if (fwrite(&tmpBuf, sample_size, 1,write_file_ptr[ch]) != 1)
  477.     return(-1);
  478.     }
  479. }
  480. aiff_ptr->numChannels       = CommChunk.numChannels;
  481. aiff_ptr->numSampleFrames   = CommChunk.numSampleFrames;
  482. aiff_ptr->sampleSize        = CommChunk.sampleSize;
  483. aiff_ptr->blkAlgn.offset    = SndDChunk.offset;
  484. aiff_ptr->blkAlgn.blockSize = SndDChunk.blockSize;
  485. strncpy(aiff_ptr->sampleType, SndDChunk.ckID, 4);
  486. for (i = 0; i < numChan; i++) {
  487. fclose(write_file_ptr[i]);
  488. }
  489. fclose(file_ptr);
  490. return(0);
  491. }
  492. /**********************************************************************
  493. *
  494. *  Seek past some Audio Interchange File Format (AIFF) headers to sound data.
  495. *
  496. **********************************************************************/
  497. int aiff_seek_to_sound_data(FILE *file_ptr)
  498. {
  499. if (fseek(file_ptr,sizeof(Chunk)+sizeof(SoundDataChunk),SEEK_SET)!=0)
  500.     return(-1);
  501. else
  502.     return(0);
  503. }
  504. /**********************************************************************
  505. *
  506. *  double_to_extended()
  507. *
  508. *  Purpose:     Convert from IEEE double precision format to SANE extended
  509. *               format.
  510. *
  511. *  Passed:      Pointer to the double precision number and a pointer to what
  512. *               will hold the Apple SANE extended format value.
  513. *
  514. *  Outputs:     The SANE extended format pointer will be filled with the
  515. *               converted value.
  516. *
  517. *  Returned:    Nothing.
  518. *
  519. *********************************************************************/
  520. void    double_to_extended(double *pd, char ps[10])
  521. {
  522. #ifdef  MACINTOSH
  523. x96tox80(pd, (extended *) ps);
  524. #else
  525. register unsigned long  top2bits;
  526. register unsigned short *ps2;
  527. register IEEE_DBL       *p_dbl;
  528. register SANE_EXT       *p_ext;
  529. SANE_EXT ext_align;
  530. char *c_align;
  531. int i;
  532. #ifdef  __MSDOS__
  533. unsigned long  holder;
  534. void    swab();
  535. #endif
  536. p_dbl = (IEEE_DBL *) pd;
  537. #ifdef  __MSDOS__
  538. holder = p_dbl->lo;
  539. p_dbl->lo = p_dbl->hi;
  540. p_dbl->hi = holder;
  541. #endif
  542. p_ext=&ext_align;
  543. top2bits = p_dbl->hi & 0xc0000000L;
  544. p_ext->l1 = ((p_dbl->hi >> 4) & 0x3ff0000L) | top2bits;
  545. p_ext->l1 |= ((p_dbl->hi >> 5) & 0x7fffL) | 0x8000L;
  546. p_ext->l2 = (p_dbl->hi << 27) & 0xf8000000L;
  547. p_ext->l2 |= ((p_dbl->lo >> 5) & 0x07ffffffL);
  548. #ifdef  __MSDOS__
  549. holder = p_ext->l1 ;
  550. swab(&holder,&p_ext->l1,4);
  551. holder=p_ext->l1&0xffff;
  552. p_ext->l1=((p_ext->l1>>16)+(holder<<16))&0xffffffff;
  553. holder = p_ext->l2 ;
  554. swab(&holder,&p_ext->l2,4);
  555. holder=p_ext->l2&0xffff;
  556. p_ext->l2=((p_ext->l2>>16)+(holder<<16))&0xffffffff;
  557. #endif
  558. ps2 = (unsigned short *) & (p_dbl->lo);
  559. ps2++;
  560. ext_align.s1 = (*ps2 << 11) & 0xf800;
  561. c_align = (char *) p_ext;
  562. for (i=0;i<10;i++)
  563.     ps[i] = c_align[i];
  564. #endif
  565. }
  566. /**********************************************************************
  567. *
  568. *  extended_to_double()
  569. *
  570. *  Purpose:     Convert from SANE extended format to IEEE double precision
  571. *               format.
  572. *
  573. *  Passed:      Pointer to the Apple SANE extended format value and a pointer
  574. *               to what will hold the the IEEE double precision number.
  575. *
  576. *  Outputs:     The IEEE double precision format pointer will be filled with
  577. *               the converted value.
  578. *
  579. *  Returned:    Nothing.
  580. *
  581. **********************************************************************/
  582. void    extended_to_double(char ps[10], double *pd)
  583. {
  584. #ifdef  MACINTOSH
  585.    x80tox96((extended *) ps, pd);
  586. #else
  587. register unsigned long  top2bits;
  588. register IEEE_DBL       *p_dbl;
  589. register SANE_EXT       *p_ext;
  590. SANE_EXT ext_align;
  591. char *c_align;
  592. int i;
  593. #ifdef  __MSDOS__
  594. unsigned long  holder;
  595. void    swab();
  596. #endif
  597. p_dbl = (IEEE_DBL *) pd;
  598. p_ext = &ext_align;
  599. c_align = (char *) p_ext;
  600. for (i=0;i<10;i++)
  601.    c_align[i] = ps[i];
  602. #ifdef  __MSDOS__
  603. holder = p_ext->l1;
  604. swab(&holder,&p_ext->l1,4);
  605. holder = p_ext->l1&0xffff;
  606. p_ext->l1 = (((p_ext->l1>>16)&0xffff)+(holder<<16))&0xffffffff;
  607. holder = p_ext->l2;
  608. swab(&holder,&p_ext->l2,4);
  609. holder = p_ext->l2&0xffff;
  610. p_ext->l2 = (((p_ext->l2>>16)&0xffff)+(holder<<16))&0xffffffff;
  611. holder = p_ext->s1;
  612. swab(&holder,&p_ext->s1,2);
  613. #endif
  614. top2bits = p_ext->l1 & 0xc0000000L;
  615. p_dbl->hi = ((p_ext->l1 << 4) & 0x3ff00000L) | top2bits;
  616. p_dbl->hi |= (p_ext->l1 << 5) & 0xffff0L;
  617. p_dbl->hi |= (p_ext->l2 >> 27) & 0x1f;
  618. p_dbl->lo = (p_ext->l2 << 5) & 0xffffffe0L;
  619. p_dbl->lo |= (unsigned long) ((p_ext->s1 >> 11) & 0x1f);
  620. #ifdef  __MSDOS__
  621. holder = p_dbl->lo;
  622. p_dbl->lo = p_dbl->hi;
  623. p_dbl->hi = holder;
  624. #endif
  625. #endif
  626. };