PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/sox-14.4.0/src/aiff.c

#
C | 1131 lines | 817 code | 89 blank | 225 comment | 278 complexity | 0fddf4c1e9e4cdd75d7216f31743c4d0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. /* libSoX SGI/Amiga AIFF format.
  2. * Copyright 1991-2007 Guido van Rossum And Sundry Contributors
  3. *
  4. * This source code is freely redistributable and may be used for
  5. * any purpose. This copyright notice must be maintained.
  6. * Guido van Rossum And Sundry Contributors are not responsible for
  7. * the consequences of using this software.
  8. *
  9. * Used by SGI on 4D/35 and Indigo.
  10. * This is a subformat of the EA-IFF-85 format.
  11. * This is related to the IFF format used by the Amiga.
  12. * But, apparently, not the same.
  13. * Also AIFF-C format output that is defined in DAVIC 1.4 Part 9 Annex B
  14. * (usable for japanese-data-broadcasting, specified by ARIB STD-B24.)
  15. */
  16. #include "sox_i.h"
  17. #include "aiff.h"
  18. #include <time.h> /* for time stamping comments */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <limits.h>
  24. /* forward declarations */
  25. static double read_ieee_extended(sox_format_t *);
  26. static int aiffwriteheader(sox_format_t *, uint64_t);
  27. static int aifcwriteheader(sox_format_t *, uint64_t);
  28. static void write_ieee_extended(sox_format_t *, double);
  29. static double ConvertFromIeeeExtended(unsigned char*);
  30. static void ConvertToIeeeExtended(double, char *);
  31. static int textChunk(char **text, char *chunkDescription, sox_format_t * ft);
  32. static int commentChunk(char **text, char *chunkDescription, sox_format_t * ft);
  33. static void reportInstrument(sox_format_t * ft);
  34. int lsx_aiffstartread(sox_format_t * ft)
  35. {
  36. char buf[5];
  37. uint32_t totalsize;
  38. uint32_t chunksize;
  39. unsigned short channels = 0;
  40. sox_encoding_t enc = SOX_ENCODING_SIGN2;
  41. uint32_t frames;
  42. unsigned short bits = 0;
  43. double rate = 0.0;
  44. uint32_t offset = 0;
  45. uint32_t blocksize = 0;
  46. int foundcomm = 0, foundmark = 0, foundinstr = 0, is_sowt = 0;
  47. struct mark {
  48. unsigned short id;
  49. uint32_t position;
  50. char name[40];
  51. } marks[32];
  52. unsigned short looptype;
  53. int i, j;
  54. unsigned short nmarks = 0;
  55. unsigned short sustainLoopBegin = 0, sustainLoopEnd = 0,
  56. releaseLoopBegin = 0, releaseLoopEnd = 0;
  57. off_t seekto = 0;
  58. size_t ssndsize = 0;
  59. char *annotation;
  60. char *author;
  61. char *comment = NULL;
  62. char *copyright;
  63. char *nametext;
  64. uint8_t trash8;
  65. uint16_t trash16;
  66. uint32_t trash32;
  67. int rc;
  68. /* FORM chunk */
  69. if (lsx_reads(ft, buf, (size_t)4) == SOX_EOF || strncmp(buf, "FORM", (size_t)4) != 0) {
  70. lsx_fail_errno(ft,SOX_EHDR,"AIFF header does not begin with magic word `FORM'");
  71. return(SOX_EOF);
  72. }
  73. lsx_readdw(ft, &totalsize);
  74. if (lsx_reads(ft, buf, (size_t)4) == SOX_EOF || (strncmp(buf, "AIFF", (size_t)4) != 0 &&
  75. strncmp(buf, "AIFC", (size_t)4) != 0)) {
  76. lsx_fail_errno(ft,SOX_EHDR,"AIFF `FORM' chunk does not specify `AIFF' or `AIFC' as type");
  77. return(SOX_EOF);
  78. }
  79. /* Skip everything but the COMM chunk and the SSND chunk */
  80. /* The SSND chunk must be the last in the file */
  81. while (1) {
  82. if (lsx_reads(ft, buf, (size_t)4) == SOX_EOF) {
  83. if (ssndsize > 0)
  84. break;
  85. else {
  86. lsx_fail_errno(ft,SOX_EHDR,"Missing SSND chunk in AIFF file");
  87. return(SOX_EOF);
  88. }
  89. }
  90. if (strncmp(buf, "COMM", (size_t)4) == 0) {
  91. /* COMM chunk */
  92. lsx_readdw(ft, &chunksize);
  93. lsx_readw(ft, &channels);
  94. lsx_readdw(ft, &frames);
  95. lsx_readw(ft, &bits);
  96. rate = read_ieee_extended(ft);
  97. chunksize -= 18;
  98. if (chunksize > 0) {
  99. lsx_reads(ft, buf, (size_t)4);
  100. chunksize -= 4;
  101. if (strncmp(buf, "sowt", (size_t)4) == 0) {
  102. /* CD audio as read on Mac OS machines */
  103. /* Need to endian swap all the data */
  104. is_sowt = 1;
  105. }
  106. else if (strncmp(buf, "fl32", (size_t)4) == 0 ||
  107. strncmp(buf, "FL32", (size_t)4) == 0) {
  108. enc = SOX_ENCODING_FLOAT;
  109. if (bits != 32) {
  110. lsx_fail_errno(ft, SOX_EHDR,
  111. "Sample size of %u is not consistent with `fl32' compression type", bits);
  112. return SOX_EOF;
  113. }
  114. }
  115. else if (strncmp(buf, "fl64", (size_t)4) == 0 ||
  116. strncmp(buf, "FL64", (size_t)4) == 0) {
  117. enc = SOX_ENCODING_FLOAT;
  118. if (bits != 64) {
  119. lsx_fail_errno(ft, SOX_EHDR,
  120. "Sample size of %u is not consistent with `fl64' compression type", bits);
  121. return SOX_EOF;
  122. }
  123. }
  124. else if (strncmp(buf, "NONE", (size_t)4) != 0 &&
  125. strncmp(buf, "twos", (size_t)4) != 0) {
  126. buf[4] = 0;
  127. lsx_fail_errno(ft, SOX_EHDR, "Unsupported AIFC compression type `%s'", buf);
  128. return(SOX_EOF);
  129. }
  130. }
  131. while(chunksize-- > 0)
  132. lsx_readb(ft, &trash8);
  133. foundcomm = 1;
  134. }
  135. else if (strncmp(buf, "SSND", (size_t)4) == 0) {
  136. /* SSND chunk */
  137. lsx_readdw(ft, &chunksize);
  138. lsx_readdw(ft, &offset);
  139. lsx_readdw(ft, &blocksize);
  140. chunksize -= 8;
  141. ssndsize = chunksize;
  142. /* word-align chunksize in case it wasn't
  143. * done by writing application already.
  144. */
  145. chunksize += (chunksize % 2);
  146. /* if can't seek, just do sound now */
  147. if (!ft->seekable)
  148. break;
  149. /* else, seek to end of sound and hunt for more */
  150. seekto = lsx_tell(ft);
  151. lsx_seeki(ft, (off_t)chunksize, SEEK_CUR);
  152. }
  153. else if (strncmp(buf, "MARK", (size_t)4) == 0) {
  154. /* MARK chunk */
  155. lsx_readdw(ft, &chunksize);
  156. if (chunksize >= sizeof(nmarks)) {
  157. lsx_readw(ft, &nmarks);
  158. chunksize -= sizeof(nmarks);
  159. }
  160. else nmarks = 0;
  161. /* Some programs like to always have a MARK chunk
  162. * but will set number of marks to 0 and force
  163. * software to detect and ignore it.
  164. */
  165. if (nmarks == 0)
  166. foundmark = 0;
  167. else
  168. foundmark = 1;
  169. /* Make sure its not larger then we support */
  170. if (nmarks > 32)
  171. nmarks = 32;
  172. for(i = 0; i < nmarks && chunksize; i++) {
  173. unsigned char len, read_len, tmp_c;
  174. if (chunksize < 6)
  175. break;
  176. lsx_readw(ft, &(marks[i].id));
  177. lsx_readdw(ft, &(marks[i].position));
  178. chunksize -= 6;
  179. /* If error reading length then
  180. * don't try to read more bytes
  181. * based on that value.
  182. */
  183. if (lsx_readb(ft, &len) != SOX_SUCCESS)
  184. break;
  185. --chunksize;
  186. if (len > chunksize)
  187. len = chunksize;
  188. read_len = len;
  189. if (read_len > 39)
  190. read_len = 39;
  191. for(j = 0; j < len && chunksize; j++) {
  192. lsx_readb(ft, &tmp_c);
  193. if (j < read_len)
  194. marks[i].name[j] = tmp_c;
  195. chunksize--;
  196. }
  197. marks[i].name[read_len] = 0;
  198. if ((len & 1) == 0 && chunksize) {
  199. chunksize--;
  200. lsx_readb(ft, &trash8);
  201. }
  202. }
  203. /* HA HA! Sound Designer (and others) makes */
  204. /* bogus files. It spits out bogus chunksize */
  205. /* for MARK field */
  206. while(chunksize-- > 0)
  207. lsx_readb(ft, &trash8);
  208. }
  209. else if (strncmp(buf, "INST", (size_t)4) == 0) {
  210. /* INST chunk */
  211. lsx_readdw(ft, &chunksize);
  212. lsx_readsb(ft, &(ft->oob.instr.MIDInote));
  213. lsx_readb(ft, &trash8);
  214. lsx_readsb(ft, &(ft->oob.instr.MIDIlow));
  215. lsx_readsb(ft, &(ft->oob.instr.MIDIhi));
  216. /* Low velocity */
  217. lsx_readb(ft, &trash8);
  218. /* Hi velocity */
  219. lsx_readb(ft, &trash8);
  220. lsx_readw(ft, &trash16);/* gain */
  221. lsx_readw(ft, &looptype); /* sustain loop */
  222. ft->oob.loops[0].type = looptype;
  223. lsx_readw(ft, &sustainLoopBegin); /* begin marker */
  224. lsx_readw(ft, &sustainLoopEnd); /* end marker */
  225. lsx_readw(ft, &looptype); /* release loop */
  226. ft->oob.loops[1].type = looptype;
  227. lsx_readw(ft, &releaseLoopBegin); /* begin marker */
  228. lsx_readw(ft, &releaseLoopEnd); /* end marker */
  229. foundinstr = 1;
  230. }
  231. else if (strncmp(buf, "APPL", (size_t)4) == 0) {
  232. lsx_readdw(ft, &chunksize);
  233. /* word-align chunksize in case it wasn't
  234. * done by writing application already.
  235. */
  236. chunksize += (chunksize % 2);
  237. while(chunksize-- > 0)
  238. lsx_readb(ft, &trash8);
  239. }
  240. else if (strncmp(buf, "ALCH", (size_t)4) == 0) {
  241. /* I think this is bogus and gets grabbed by APPL */
  242. /* INST chunk */
  243. lsx_readdw(ft, &trash32); /* ENVS - jeez! */
  244. lsx_readdw(ft, &chunksize);
  245. while(chunksize-- > 0)
  246. lsx_readb(ft, &trash8);
  247. }
  248. else if (strncmp(buf, "ANNO", (size_t)4) == 0) {
  249. rc = textChunk(&annotation, "Annotation:", ft);
  250. if (rc) {
  251. /* Fail already called in function */
  252. return(SOX_EOF);
  253. }
  254. if (annotation)
  255. sox_append_comments(&ft->oob.comments, annotation);
  256. free(annotation);
  257. }
  258. else if (strncmp(buf, "COMT", (size_t)4) == 0) {
  259. rc = commentChunk(&comment, "Comment:", ft);
  260. if (rc) {
  261. /* Fail already called in function */
  262. return(SOX_EOF);
  263. }
  264. if (comment)
  265. sox_append_comments(&ft->oob.comments, comment);
  266. free(comment);
  267. }
  268. else if (strncmp(buf, "AUTH", (size_t)4) == 0) {
  269. /* Author chunk */
  270. rc = textChunk(&author, "Author:", ft);
  271. if (rc) {
  272. /* Fail already called in function */
  273. return(SOX_EOF);
  274. }
  275. free(author);
  276. }
  277. else if (strncmp(buf, "NAME", (size_t)4) == 0) {
  278. /* Name chunk */
  279. rc = textChunk(&nametext, "Name:", ft);
  280. if (rc) {
  281. /* Fail already called in function */
  282. return(SOX_EOF);
  283. }
  284. free(nametext);
  285. }
  286. else if (strncmp(buf, "(c) ", (size_t)4) == 0) {
  287. /* Copyright chunk */
  288. rc = textChunk(&copyright, "Copyright:", ft);
  289. if (rc) {
  290. /* Fail already called in function */
  291. return(SOX_EOF);
  292. }
  293. free(copyright);
  294. }
  295. else {
  296. if (lsx_eof(ft))
  297. break;
  298. buf[4] = 0;
  299. lsx_debug("AIFFstartread: ignoring `%s' chunk", buf);
  300. lsx_readdw(ft, &chunksize);
  301. if (lsx_eof(ft))
  302. break;
  303. /* Skip the chunk using lsx_readb() so we may read
  304. from a pipe */
  305. while (chunksize-- > 0) {
  306. if (lsx_readb(ft, &trash8) == SOX_EOF)
  307. break;
  308. }
  309. }
  310. if (lsx_eof(ft))
  311. break;
  312. }
  313. /*
  314. * if a pipe, we lose all chunks after sound.
  315. * Like, say, instrument loops.
  316. */
  317. if (ft->seekable) {
  318. if (seekto > 0)
  319. lsx_seeki(ft, seekto, SEEK_SET);
  320. else {
  321. lsx_fail_errno(ft,SOX_EOF,"AIFF: no sound data on input file");
  322. return(SOX_EOF);
  323. }
  324. }
  325. /* SSND chunk just read */
  326. if (blocksize != 0)
  327. lsx_warn("AIFF header has invalid blocksize. Ignoring but expect a premature EOF");
  328. ssndsize -= offset;
  329. while (offset-- > 0) {
  330. if (lsx_readb(ft, &trash8) == SOX_EOF) {
  331. lsx_fail_errno(ft,errno,"unexpected EOF while skipping AIFF offset");
  332. return(SOX_EOF);
  333. }
  334. }
  335. if (foundcomm) {
  336. if (bits <= 8) bits = 8;
  337. else if (bits <= 16) bits = 16;
  338. else if (bits <= 24) bits = 24;
  339. else if (bits <= 32) bits = 32;
  340. else if (bits == 64 && enc == SOX_ENCODING_FLOAT) /* no-op */;
  341. else {
  342. lsx_fail_errno(ft,SOX_EFMT,"unsupported sample size in AIFF header: %d", bits);
  343. return(SOX_EOF);
  344. }
  345. } else {
  346. if ((ft->signal.channels == SOX_UNSPEC)
  347. || (ft->signal.rate == SOX_UNSPEC)
  348. || (ft->encoding.encoding == SOX_ENCODING_UNKNOWN)
  349. || (ft->encoding.bits_per_sample == 0)) {
  350. lsx_report("You must specify # channels, sample rate, signed/unsigned,");
  351. lsx_report("and 8/16 on the command line.");
  352. lsx_fail_errno(ft,SOX_EFMT,"Bogus AIFF file: no COMM section.");
  353. return(SOX_EOF);
  354. }
  355. }
  356. ssndsize /= bits >> 3;
  357. /* Cope with 'sowt' CD tracks as read on Macs */
  358. if (is_sowt)
  359. ft->encoding.reverse_bytes = !ft->encoding.reverse_bytes;
  360. if (foundmark && !foundinstr) {
  361. lsx_debug("Ignoring MARK chunk since no INSTR found.");
  362. foundmark = 0;
  363. }
  364. if (!foundmark && foundinstr) {
  365. lsx_debug("Ignoring INSTR chunk since no MARK found.");
  366. foundinstr = 0;
  367. }
  368. if (foundmark && foundinstr) {
  369. int i2;
  370. int slbIndex = 0, sleIndex = 0;
  371. int rlbIndex = 0, rleIndex = 0;
  372. /* find our loop markers and save their marker indexes */
  373. for(i2 = 0; i2 < nmarks; i2++) {
  374. if(marks[i2].id == sustainLoopBegin)
  375. slbIndex = i2;
  376. if(marks[i2].id == sustainLoopEnd)
  377. sleIndex = i2;
  378. if(marks[i2].id == releaseLoopBegin)
  379. rlbIndex = i2;
  380. if(marks[i2].id == releaseLoopEnd)
  381. rleIndex = i2;
  382. }
  383. ft->oob.instr.nloops = 0;
  384. if (ft->oob.loops[0].type != 0) {
  385. ft->oob.loops[0].start = marks[slbIndex].position;
  386. ft->oob.loops[0].length =
  387. marks[sleIndex].position - marks[slbIndex].position;
  388. /* really the loop count should be infinite */
  389. ft->oob.loops[0].count = 1;
  390. ft->oob.instr.loopmode = SOX_LOOP_SUSTAIN_DECAY | ft->oob.loops[0].type;
  391. ft->oob.instr.nloops++;
  392. }
  393. if (ft->oob.loops[1].type != 0) {
  394. ft->oob.loops[1].start = marks[rlbIndex].position;
  395. ft->oob.loops[1].length =
  396. marks[rleIndex].position - marks[rlbIndex].position;
  397. /* really the loop count should be infinite */
  398. ft->oob.loops[1].count = 1;
  399. ft->oob.instr.loopmode = SOX_LOOP_SUSTAIN_DECAY | ft->oob.loops[1].type;
  400. ft->oob.instr.nloops++;
  401. }
  402. }
  403. reportInstrument(ft);
  404. return lsx_check_read_params(
  405. ft, channels, rate, enc, bits, (uint64_t)ssndsize, sox_false);
  406. }
  407. /* print out the MIDI key allocations, loop points, directions etc */
  408. static void reportInstrument(sox_format_t * ft)
  409. {
  410. unsigned loopNum;
  411. if(ft->oob.instr.nloops > 0)
  412. lsx_report("AIFF Loop markers:");
  413. for(loopNum = 0; loopNum < ft->oob.instr.nloops; loopNum++) {
  414. if (ft->oob.loops[loopNum].count) {
  415. lsx_report("Loop %d: start: %6lu", loopNum, (unsigned long)ft->oob.loops[loopNum].start);
  416. lsx_report(" end: %6lu",
  417. (unsigned long)(ft->oob.loops[loopNum].start + ft->oob.loops[loopNum].length));
  418. lsx_report(" count: %6d", ft->oob.loops[loopNum].count);
  419. lsx_report(" type: ");
  420. switch(ft->oob.loops[loopNum].type & ~SOX_LOOP_SUSTAIN_DECAY) {
  421. case 0: lsx_report("off"); break;
  422. case 1: lsx_report("forward"); break;
  423. case 2: lsx_report("forward/backward"); break;
  424. }
  425. }
  426. }
  427. lsx_report("Unity MIDI Note: %d", ft->oob.instr.MIDInote);
  428. lsx_report("Low MIDI Note: %d", ft->oob.instr.MIDIlow);
  429. lsx_report("High MIDI Note: %d", ft->oob.instr.MIDIhi);
  430. }
  431. /* Process a text chunk, allocate memory, display it if verbose and return */
  432. static int textChunk(char **text, char *chunkDescription, sox_format_t * ft)
  433. {
  434. uint32_t chunksize;
  435. lsx_readdw(ft, &chunksize);
  436. /* allocate enough memory to hold the text including a terminating \0 */
  437. *text = lsx_malloc((size_t) chunksize + 1);
  438. if (lsx_readbuf(ft, *text, (size_t) chunksize) != chunksize)
  439. {
  440. lsx_fail_errno(ft,SOX_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
  441. return(SOX_EOF);
  442. }
  443. *(*text + chunksize) = '\0';
  444. if (chunksize % 2)
  445. {
  446. /* Read past pad byte */
  447. char c;
  448. if (lsx_readbuf(ft, &c, (size_t)1) != 1)
  449. {
  450. lsx_fail_errno(ft,SOX_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
  451. return(SOX_EOF);
  452. }
  453. }
  454. lsx_debug("%-10s \"%s\"", chunkDescription, *text);
  455. return(SOX_SUCCESS);
  456. }
  457. /* Comment lengths are words, not double words, and we can have several, so
  458. we use a special function, not textChunk().;
  459. */
  460. static int commentChunk(char **text, char *chunkDescription, sox_format_t * ft)
  461. {
  462. uint32_t chunksize;
  463. unsigned short numComments;
  464. uint32_t timeStamp;
  465. unsigned short markerId;
  466. unsigned short totalCommentLength = 0;
  467. unsigned int totalReadLength = 0;
  468. unsigned int commentIndex;
  469. lsx_readdw(ft, &chunksize);
  470. lsx_readw(ft, &numComments);
  471. totalReadLength += 2; /* chunksize doesn't count */
  472. for(commentIndex = 0; commentIndex < numComments; commentIndex++) {
  473. unsigned short commentLength;
  474. lsx_readdw(ft, &timeStamp);
  475. lsx_readw(ft, &markerId);
  476. lsx_readw(ft, &commentLength);
  477. if (((size_t)totalCommentLength) + commentLength > USHRT_MAX) {
  478. lsx_fail_errno(ft,SOX_EOF,"AIFF: Comment too long in %s header", chunkDescription);
  479. return(SOX_EOF);
  480. }
  481. totalCommentLength += commentLength;
  482. /* allocate enough memory to hold the text including a terminating \0 */
  483. if(commentIndex == 0) {
  484. *text = lsx_malloc((size_t) totalCommentLength + 1);
  485. }
  486. else {
  487. *text = lsx_realloc(*text, (size_t) totalCommentLength + 1);
  488. }
  489. if (lsx_readbuf(ft, *text + totalCommentLength - commentLength, (size_t) commentLength) != commentLength) {
  490. lsx_fail_errno(ft,SOX_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
  491. return(SOX_EOF);
  492. }
  493. *(*text + totalCommentLength) = '\0';
  494. totalReadLength += totalCommentLength + 4 + 2 + 2; /* include header */
  495. if (commentLength % 2) {
  496. /* Read past pad byte */
  497. char c;
  498. if (lsx_readbuf(ft, &c, (size_t)1) != 1) {
  499. lsx_fail_errno(ft,SOX_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
  500. return(SOX_EOF);
  501. }
  502. totalReadLength += 1;
  503. }
  504. }
  505. lsx_debug("%-10s \"%s\"", chunkDescription, *text);
  506. /* make sure we read the whole chunk */
  507. if (totalReadLength < chunksize) {
  508. size_t i;
  509. char c;
  510. for (i=0; i < chunksize - totalReadLength; i++ )
  511. lsx_readbuf(ft, &c, (size_t)1);
  512. }
  513. return(SOX_SUCCESS);
  514. }
  515. int lsx_aiffstopread(sox_format_t * ft)
  516. {
  517. char buf[5];
  518. uint32_t chunksize;
  519. uint8_t trash;
  520. if (!ft->seekable)
  521. {
  522. while (! lsx_eof(ft))
  523. {
  524. if (lsx_readbuf(ft, buf, (size_t)4) != 4)
  525. break;
  526. lsx_readdw(ft, &chunksize);
  527. if (lsx_eof(ft))
  528. break;
  529. buf[4] = '\0';
  530. lsx_warn("Ignoring AIFF tail chunk: `%s', %u bytes long",
  531. buf, chunksize);
  532. if (! strcmp(buf, "MARK") || ! strcmp(buf, "INST"))
  533. lsx_warn(" You're stripping MIDI/loop info!");
  534. while (chunksize-- > 0)
  535. {
  536. if (lsx_readb(ft, &trash) == SOX_EOF)
  537. break;
  538. }
  539. }
  540. }
  541. return SOX_SUCCESS;
  542. }
  543. /* When writing, the header is supposed to contain the number of
  544. samples and data bytes written.
  545. Since we don't know how many samples there are until we're done,
  546. we first write the header with an very large number,
  547. and at the end we rewind the file and write the header again
  548. with the right number. This only works if the file is seekable;
  549. if it is not, the very large size remains in the header.
  550. Strictly spoken this is not legal, but the playaiff utility
  551. will still be able to play the resulting file. */
  552. int lsx_aiffstartwrite(sox_format_t * ft)
  553. {
  554. int rc;
  555. /* Needed because lsx_rawwrite() */
  556. rc = lsx_rawstartwrite(ft);
  557. if (rc)
  558. return rc;
  559. /* Compute the "very large number" so that a maximum number
  560. of samples can be transmitted through a pipe without the
  561. risk of causing overflow when calculating the number of bytes.
  562. At 48 kHz, 16 bits stereo, this gives ~3 hours of audio.
  563. Sorry, the AIFF format does not provide for an indefinite
  564. number of samples. */
  565. return(aiffwriteheader(ft, (uint64_t) 0x7f000000 / ((ft->encoding.bits_per_sample>>3)*ft->signal.channels)));
  566. }
  567. int lsx_aiffstopwrite(sox_format_t * ft)
  568. {
  569. /* If we've written an odd number of bytes, write a padding
  570. NUL */
  571. if (ft->olength % 2 == 1 && ft->encoding.bits_per_sample == 8 && ft->signal.channels == 1)
  572. {
  573. sox_sample_t buf = 0;
  574. lsx_rawwrite(ft, &buf, (size_t) 1);
  575. }
  576. if (!ft->seekable)
  577. {
  578. lsx_fail_errno(ft,SOX_EOF,"Non-seekable file.");
  579. return(SOX_EOF);
  580. }
  581. if (lsx_seeki(ft, (off_t)0, SEEK_SET) != 0)
  582. {
  583. lsx_fail_errno(ft,errno,"can't rewind output file to rewrite AIFF header");
  584. return(SOX_EOF);
  585. }
  586. return(aiffwriteheader(ft, ft->olength / ft->signal.channels));
  587. }
  588. static int aiffwriteheader(sox_format_t * ft, uint64_t nframes)
  589. {
  590. int hsize =
  591. 8 /*COMM hdr*/ + 18 /*COMM chunk*/ +
  592. 8 /*SSND hdr*/ + 12 /*SSND chunk*/;
  593. unsigned bits = 0;
  594. unsigned i;
  595. uint64_t size;
  596. size_t padded_comment_size = 0, comment_size = 0;
  597. size_t comment_chunk_size = 0;
  598. char * comment = lsx_cat_comments(ft->oob.comments);
  599. /* MARK and INST chunks */
  600. if (ft->oob.instr.nloops) {
  601. hsize += 8 /* MARK hdr */ + 2 + 16*ft->oob.instr.nloops;
  602. hsize += 8 /* INST hdr */ + 20; /* INST chunk */
  603. }
  604. if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
  605. ft->encoding.bits_per_sample == 8)
  606. bits = 8;
  607. else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
  608. ft->encoding.bits_per_sample == 16)
  609. bits = 16;
  610. else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
  611. ft->encoding.bits_per_sample == 24)
  612. bits = 24;
  613. else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
  614. ft->encoding.bits_per_sample == 32)
  615. bits = 32;
  616. else
  617. {
  618. lsx_fail_errno(ft,SOX_EFMT,"unsupported output encoding/size for AIFF header");
  619. return(SOX_EOF);
  620. }
  621. /* COMT comment chunk -- holds comments text with a timestamp and marker id */
  622. /* We calculate the comment_chunk_size if we will be writing a comment */
  623. if (ft->oob.comments)
  624. {
  625. comment_size = strlen(comment);
  626. /* Must put an even number of characters out.
  627. * True 68k processors OS's seem to require this.
  628. */
  629. padded_comment_size = ((comment_size % 2) == 0) ?
  630. comment_size : comment_size + 1;
  631. /* one comment, timestamp, marker ID and text count */
  632. comment_chunk_size = (2 + 4 + 2 + 2 + padded_comment_size);
  633. hsize += 8 /* COMT hdr */ + comment_chunk_size;
  634. }
  635. lsx_writes(ft, "FORM"); /* IFF header */
  636. /* file size */
  637. size = hsize + nframes * (ft->encoding.bits_per_sample >> 3) * ft->signal.channels;
  638. if (size > UINT_MAX)
  639. {
  640. lsx_warn("file size too big for accurate AIFF header");
  641. size = UINT_MAX;
  642. }
  643. lsx_writedw(ft, (unsigned)size);
  644. lsx_writes(ft, "AIFF"); /* File type */
  645. /* Now we write the COMT comment chunk using the precomputed sizes */
  646. if (ft->oob.comments)
  647. {
  648. lsx_writes(ft, "COMT");
  649. lsx_writedw(ft, (unsigned) comment_chunk_size);
  650. /* one comment */
  651. lsx_writew(ft, 1);
  652. /* time stamp of comment, Unix knows of time from 1/1/1970,
  653. Apple knows time from 1/1/1904 */
  654. lsx_writedw(ft, (unsigned)((sox_globals.repeatable? 0 : time(NULL)) + 2082844800));
  655. /* A marker ID of 0 indicates the comment is not associated
  656. with a marker */
  657. lsx_writew(ft, 0);
  658. /* now write the count and the bytes of text */
  659. lsx_writew(ft, (unsigned) padded_comment_size);
  660. lsx_writes(ft, comment);
  661. if (comment_size != padded_comment_size)
  662. lsx_writes(ft, " ");
  663. }
  664. free(comment);
  665. /* COMM chunk -- describes encoding (and #frames) */
  666. lsx_writes(ft, "COMM");
  667. lsx_writedw(ft, 18); /* COMM chunk size */
  668. lsx_writew(ft, ft->signal.channels); /* nchannels */
  669. lsx_writedw(ft, (unsigned) nframes); /* number of frames */
  670. lsx_writew(ft, bits); /* sample width, in bits */
  671. write_ieee_extended(ft, (double)ft->signal.rate);
  672. /* MARK chunk -- set markers */
  673. if (ft->oob.instr.nloops) {
  674. lsx_writes(ft, "MARK");
  675. if (ft->oob.instr.nloops > 2)
  676. ft->oob.instr.nloops = 2;
  677. lsx_writedw(ft, 2 + 16u*ft->oob.instr.nloops);
  678. lsx_writew(ft, ft->oob.instr.nloops);
  679. for(i = 0; i < ft->oob.instr.nloops; i++) {
  680. unsigned start = ft->oob.loops[i].start > UINT_MAX
  681. ? UINT_MAX
  682. : ft->oob.loops[i].start;
  683. unsigned end = ft->oob.loops[i].start + ft->oob.loops[i].length > UINT_MAX
  684. ? UINT_MAX
  685. : ft->oob.loops[i].start + ft->oob.loops[i].length;
  686. lsx_writew(ft, i + 1);
  687. lsx_writedw(ft, start);
  688. lsx_writeb(ft, 0);
  689. lsx_writeb(ft, 0);
  690. lsx_writew(ft, i*2 + 1);
  691. lsx_writedw(ft, end);
  692. lsx_writeb(ft, 0);
  693. lsx_writeb(ft, 0);
  694. }
  695. lsx_writes(ft, "INST");
  696. lsx_writedw(ft, 20);
  697. /* random MIDI shit that we default on */
  698. lsx_writeb(ft, (uint8_t)ft->oob.instr.MIDInote);
  699. lsx_writeb(ft, 0); /* detune */
  700. lsx_writeb(ft, (uint8_t)ft->oob.instr.MIDIlow);
  701. lsx_writeb(ft, (uint8_t)ft->oob.instr.MIDIhi);
  702. lsx_writeb(ft, 1); /* low velocity */
  703. lsx_writeb(ft, 127); /* hi velocity */
  704. lsx_writew(ft, 0); /* gain */
  705. /* sustain loop */
  706. lsx_writew(ft, ft->oob.loops[0].type);
  707. lsx_writew(ft, 1); /* marker 1 */
  708. lsx_writew(ft, 3); /* marker 3 */
  709. /* release loop, if there */
  710. if (ft->oob.instr.nloops == 2) {
  711. lsx_writew(ft, ft->oob.loops[1].type);
  712. lsx_writew(ft, 2); /* marker 2 */
  713. lsx_writew(ft, 4); /* marker 4 */
  714. } else {
  715. lsx_writew(ft, 0); /* no release loop */
  716. lsx_writew(ft, 0);
  717. lsx_writew(ft, 0);
  718. }
  719. }
  720. /* SSND chunk -- describes data */
  721. lsx_writes(ft, "SSND");
  722. /* chunk size */
  723. lsx_writedw(ft, (unsigned) (8 + nframes * ft->signal.channels * (ft->encoding.bits_per_sample >> 3)));
  724. lsx_writedw(ft, 0); /* offset */
  725. lsx_writedw(ft, 0); /* block size */
  726. return(SOX_SUCCESS);
  727. }
  728. int lsx_aifcstartwrite(sox_format_t * ft)
  729. {
  730. int rc;
  731. /* Needed because lsx_rawwrite() */
  732. rc = lsx_rawstartwrite(ft);
  733. if (rc)
  734. return rc;
  735. /* Compute the "very large number" so that a maximum number
  736. of samples can be transmitted through a pipe without the
  737. risk of causing overflow when calculating the number of bytes.
  738. At 48 kHz, 16 bits stereo, this gives ~3 hours of music.
  739. Sorry, the AIFC format does not provide for an "infinite"
  740. number of samples. */
  741. return(aifcwriteheader(ft, (uint64_t) 0x7f000000 / ((ft->encoding.bits_per_sample >> 3)*ft->signal.channels)));
  742. }
  743. int lsx_aifcstopwrite(sox_format_t * ft)
  744. {
  745. /* If we've written an odd number of bytes, write a padding
  746. NUL */
  747. if (ft->olength % 2 == 1 && ft->encoding.bits_per_sample == 8 && ft->signal.channels == 1)
  748. {
  749. sox_sample_t buf = 0;
  750. lsx_rawwrite(ft, &buf, (size_t) 1);
  751. }
  752. if (!ft->seekable)
  753. {
  754. lsx_fail_errno(ft,SOX_EOF,"Non-seekable file.");
  755. return(SOX_EOF);
  756. }
  757. if (lsx_seeki(ft, (off_t)0, SEEK_SET) != 0)
  758. {
  759. lsx_fail_errno(ft,errno,"can't rewind output file to rewrite AIFC header");
  760. return(SOX_EOF);
  761. }
  762. return(aifcwriteheader(ft, ft->olength / ft->signal.channels));
  763. }
  764. static int aifcwriteheader(sox_format_t * ft, uint64_t nframes)
  765. {
  766. unsigned hsize;
  767. unsigned bits = 0;
  768. uint64_t size;
  769. char *ctype = NULL, *cname = NULL;
  770. unsigned cname_len = 0, comm_len = 0, comm_padding = 0;
  771. if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
  772. ft->encoding.bits_per_sample == 8)
  773. bits = 8;
  774. else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
  775. ft->encoding.bits_per_sample == 16)
  776. bits = 16;
  777. else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
  778. ft->encoding.bits_per_sample == 24)
  779. bits = 24;
  780. else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
  781. ft->encoding.bits_per_sample == 32)
  782. bits = 32;
  783. else if (ft->encoding.encoding == SOX_ENCODING_FLOAT &&
  784. ft->encoding.bits_per_sample == 32)
  785. bits = 32;
  786. else if (ft->encoding.encoding == SOX_ENCODING_FLOAT &&
  787. ft->encoding.bits_per_sample == 64)
  788. bits = 64;
  789. else
  790. {
  791. lsx_fail_errno(ft,SOX_EFMT,"unsupported output encoding/size for AIFC header");
  792. return(SOX_EOF);
  793. }
  794. /* calculate length of COMM chunk (without header) */
  795. switch (ft->encoding.encoding) {
  796. case SOX_ENCODING_SIGN2:
  797. ctype = "NONE";
  798. cname = "not compressed";
  799. break;
  800. case SOX_ENCODING_FLOAT:
  801. if (bits == 32) {
  802. ctype = "fl32";
  803. cname = "32-bit floating point";
  804. } else {
  805. ctype = "fl64";
  806. cname = "64-bit floating point";
  807. }
  808. break;
  809. default: /* can't happen */
  810. break;
  811. }
  812. cname_len = strlen(cname);
  813. comm_len = 18+4+1+cname_len;
  814. comm_padding = comm_len%2;
  815. hsize = 12 /*FVER*/ + 8 /*COMM hdr*/ + comm_len+comm_padding /*COMM chunk*/ +
  816. 8 /*SSND hdr*/ + 12 /*SSND chunk*/;
  817. lsx_writes(ft, "FORM"); /* IFF header */
  818. /* file size */
  819. size = hsize + nframes * (ft->encoding.bits_per_sample >> 3) * ft->signal.channels;
  820. if (size > UINT_MAX)
  821. {
  822. lsx_warn("file size too big for accurate AIFC header");
  823. size = UINT_MAX;
  824. }
  825. lsx_writedw(ft, (unsigned)size);
  826. lsx_writes(ft, "AIFC"); /* File type */
  827. /* FVER chunk */
  828. lsx_writes(ft, "FVER");
  829. lsx_writedw(ft, 4); /* FVER chunk size */
  830. lsx_writedw(ft, 0xa2805140); /* version_date(May23,1990,2:40pm) */
  831. /* COMM chunk -- describes encoding (and #frames) */
  832. lsx_writes(ft, "COMM");
  833. lsx_writedw(ft, comm_len+comm_padding); /* COMM chunk size */
  834. lsx_writew(ft, ft->signal.channels); /* nchannels */
  835. lsx_writedw(ft, (unsigned) nframes); /* number of frames */
  836. lsx_writew(ft, bits); /* sample width, in bits */
  837. write_ieee_extended(ft, (double)ft->signal.rate);
  838. lsx_writes(ft, ctype); /*compression_type*/
  839. lsx_writeb(ft, cname_len);
  840. lsx_writes(ft, cname);
  841. if (comm_padding)
  842. lsx_writeb(ft, 0);
  843. /* SSND chunk -- describes data */
  844. lsx_writes(ft, "SSND");
  845. /* chunk size */
  846. lsx_writedw(ft, (unsigned) (8 + nframes * ft->signal.channels * (ft->encoding.bits_per_sample >> 3)));
  847. lsx_writedw(ft, 0); /* offset */
  848. lsx_writedw(ft, 0); /* block size */
  849. /* Any Private chunks shall appear after the required chunks (FORM,FVER,COMM,SSND) */
  850. return(SOX_SUCCESS);
  851. }
  852. static double read_ieee_extended(sox_format_t * ft)
  853. {
  854. unsigned char buf[10];
  855. if (lsx_readbuf(ft, buf, (size_t)10) != 10)
  856. {
  857. lsx_fail_errno(ft,SOX_EOF,"EOF while reading IEEE extended number");
  858. return(SOX_EOF);
  859. }
  860. return ConvertFromIeeeExtended(buf);
  861. }
  862. static void write_ieee_extended(sox_format_t * ft, double x)
  863. {
  864. char buf[10];
  865. ConvertToIeeeExtended(x, buf);
  866. lsx_debug_more("converted %g to %o %o %o %o %o %o %o %o %o %o",
  867. x,
  868. buf[0], buf[1], buf[2], buf[3], buf[4],
  869. buf[5], buf[6], buf[7], buf[8], buf[9]);
  870. (void)lsx_writebuf(ft, buf, (size_t) 10);
  871. }
  872. /*
  873. * C O N V E R T T O I E E E E X T E N D E D
  874. */
  875. /* Copyright (C) 1988-1991 Apple Computer, Inc.
  876. *
  877. * All rights reserved.
  878. *
  879. * Warranty Information
  880. * Even though Apple has reviewed this software, Apple makes no warranty
  881. * or representation, either express or implied, with respect to this
  882. * software, its quality, accuracy, merchantability, or fitness for a
  883. * particular purpose. As a result, this software is provided "as is,"
  884. * and you, its user, are assuming the entire risk as to its quality
  885. * and accuracy.
  886. *
  887. * Machine-independent I/O routines for IEEE floating-point numbers.
  888. *
  889. * NaN's and infinities are converted to HUGE_VAL, which
  890. * happens to be infinity on IEEE machines. Unfortunately, it is
  891. * impossible to preserve NaN's in a machine-independent way.
  892. * Infinities are, however, preserved on IEEE machines.
  893. *
  894. * These routines have been tested on the following machines:
  895. * Apple Macintosh, MPW 3.1 C compiler
  896. * Apple Macintosh, THINK C compiler
  897. * Silicon Graphics IRIS, MIPS compiler
  898. * Cray X/MP and Y/MP
  899. * Digital Equipment VAX
  900. *
  901. *
  902. * Implemented by Malcolm Slaney and Ken Turkowski.
  903. *
  904. * Malcolm Slaney contributions during 1988-1990 include big- and little-
  905. * endian file I/O, conversion to and from Motorola's extended 80-bit
  906. * floating-point format, and conversions to and from IEEE single-
  907. * precision floating-point format.
  908. *
  909. * In 1991, Ken Turkowski implemented the conversions to and from
  910. * IEEE double-precision format, added more precision to the extended
  911. * conversions, and accommodated conversions involving +/- infinity,
  912. * NaN's, and denormalized numbers.
  913. */
  914. #define FloatToUnsigned(f) ((uint32_t)(((int32_t)(f - 2147483648.0)) + 2147483647) + 1)
  915. static void ConvertToIeeeExtended(double num, char *bytes)
  916. {
  917. int sign;
  918. int expon;
  919. double fMant, fsMant;
  920. uint32_t hiMant, loMant;
  921. if (num < 0) {
  922. sign = 0x8000;
  923. num *= -1;
  924. } else {
  925. sign = 0;
  926. }
  927. if (num == 0) {
  928. expon = 0; hiMant = 0; loMant = 0;
  929. }
  930. else {
  931. fMant = frexp(num, &expon);
  932. if ((expon > 16384) || !(fMant < 1)) { /* Infinity or NaN */
  933. expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */
  934. }
  935. else { /* Finite */
  936. expon += 16382;
  937. if (expon < 0) { /* denormalized */
  938. fMant = ldexp(fMant, expon);
  939. expon = 0;
  940. }
  941. expon |= sign;
  942. fMant = ldexp(fMant, 32);
  943. fsMant = floor(fMant);
  944. hiMant = FloatToUnsigned(fsMant);
  945. fMant = ldexp(fMant - fsMant, 32);
  946. fsMant = floor(fMant);
  947. loMant = FloatToUnsigned(fsMant);
  948. }
  949. }
  950. bytes[0] = expon >> 8;
  951. bytes[1] = expon;
  952. bytes[2] = hiMant >> 24;
  953. bytes[3] = hiMant >> 16;
  954. bytes[4] = hiMant >> 8;
  955. bytes[5] = hiMant;
  956. bytes[6] = loMant >> 24;
  957. bytes[7] = loMant >> 16;
  958. bytes[8] = loMant >> 8;
  959. bytes[9] = loMant;
  960. }
  961. /*
  962. * C O N V E R T F R O M I E E E E X T E N D E D
  963. */
  964. /*
  965. * Copyright (C) 1988-1991 Apple Computer, Inc.
  966. *
  967. * All rights reserved.
  968. *
  969. * Warranty Information
  970. * Even though Apple has reviewed this software, Apple makes no warranty
  971. * or representation, either express or implied, with respect to this
  972. * software, its quality, accuracy, merchantability, or fitness for a
  973. * particular purpose. As a result, this software is provided "as is,"
  974. * and you, its user, are assuming the entire risk as to its quality
  975. * and accuracy.
  976. *
  977. * This code may be used and freely distributed as long as it includes
  978. * this copyright notice and the above warranty information.
  979. *
  980. * Machine-independent I/O routines for IEEE floating-point numbers.
  981. *
  982. * NaN's and infinities are converted to HUGE_VAL, which
  983. * happens to be infinity on IEEE machines. Unfortunately, it is
  984. * impossible to preserve NaN's in a machine-independent way.
  985. * Infinities are, however, preserved on IEEE machines.
  986. *
  987. * These routines have been tested on the following machines:
  988. * Apple Macintosh, MPW 3.1 C compiler
  989. * Apple Macintosh, THINK C compiler
  990. * Silicon Graphics IRIS, MIPS compiler
  991. * Cray X/MP and Y/MP
  992. * Digital Equipment VAX
  993. *
  994. *
  995. * Implemented by Malcolm Slaney and Ken Turkowski.
  996. *
  997. * Malcolm Slaney contributions during 1988-1990 include big- and little-
  998. * endian file I/O, conversion to and from Motorola's extended 80-bit
  999. * floating-point format, and conversions to and from IEEE single-
  1000. * precision floating-point format.
  1001. *
  1002. * In 1991, Ken Turkowski implemented the conversions to and from
  1003. * IEEE double-precision format, added more precision to the extended
  1004. * conversions, and accommodated conversions involving +/- infinity,
  1005. * NaN's, and denormalized numbers.
  1006. */
  1007. #define UnsignedToFloat(u) (((double)((int32_t)(u - 2147483647 - 1))) + 2147483648.0)
  1008. /****************************************************************
  1009. * Extended precision IEEE floating-point conversion routine.
  1010. ****************************************************************/
  1011. static double ConvertFromIeeeExtended(unsigned char *bytes)
  1012. {
  1013. double f;
  1014. int expon;
  1015. uint32_t hiMant, loMant;
  1016. expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
  1017. hiMant = ((uint32_t)(bytes[2] & 0xFF) << 24)
  1018. | ((uint32_t)(bytes[3] & 0xFF) << 16)
  1019. | ((uint32_t)(bytes[4] & 0xFF) << 8)
  1020. | ((uint32_t)(bytes[5] & 0xFF));
  1021. loMant = ((uint32_t)(bytes[6] & 0xFF) << 24)
  1022. | ((uint32_t)(bytes[7] & 0xFF) << 16)
  1023. | ((uint32_t)(bytes[8] & 0xFF) << 8)
  1024. | ((uint32_t)(bytes[9] & 0xFF));
  1025. if (expon == 0 && hiMant == 0 && loMant == 0) {
  1026. f = 0;
  1027. }
  1028. else {
  1029. if (expon == 0x7FFF) { /* Infinity or NaN */
  1030. f = HUGE_VAL;
  1031. }
  1032. else {
  1033. expon -= 16383;
  1034. f = ldexp(UnsignedToFloat(hiMant), expon-=31);
  1035. f += ldexp(UnsignedToFloat(loMant), expon-=32);
  1036. }
  1037. }
  1038. if (bytes[0] & 0x80)
  1039. return -f;
  1040. else
  1041. return f;
  1042. }