1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
|
/*
Audio File Library
Copyright (C) 1998-2000, 2003-2004, 2010-2013, Michael Pruett <michael@68k.org>
Copyright (C) 2000-2002, Silicon Graphics, Inc.
Copyright (C) 2002-2003, Davy Durham
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA
*/
/*
WAVE.cpp
This file contains code for reading and writing RIFF WAVE format
sound files.
*/
#include "config.h"
#include "WAVE.h"
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "File.h"
#include "Instrument.h"
#include "Marker.h"
#include "Setup.h"
#include "Tag.h"
#include "Track.h"
#include "UUID.h"
#include "byteorder.h"
#include "util.h"
/* These constants are from RFC 2361. */
enum
{
WAVE_FORMAT_UNKNOWN = 0x0000, /* Microsoft Unknown Wave Format */
WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */
WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */
WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */
WAVE_FORMAT_VSELP = 0x0004, /* Compaq Computer's VSELP */
WAVE_FORMAT_IBM_CVSD = 0x0005, /* IBM CVSD */
WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */
WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */
WAVE_FORMAT_OKI_ADPCM = 0x0010, /* OKI ADPCM */
WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */
WAVE_FORMAT_MEDIASPACE_ADPCM = 0x0012, /* Videologic's MediaSpace ADPCM */
WAVE_FORMAT_SIERRA_ADPCM = 0x0013, /* Sierra ADPCM */
WAVE_FORMAT_G723_ADPCM = 0x0014, /* G.723 ADPCM */
WAVE_FORMAT_DIGISTD = 0x0015, /* DSP Solutions' DIGISTD */
WAVE_FORMAT_DIGIFIX = 0x0016, /* DSP Solutions' DIGIFIX */
WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */
WAVE_FORMAT_MEDIAVISION_ADPCM = 0x0018, /* MediaVision ADPCM */
WAVE_FORMAT_CU_CODEC = 0x0019, /* HP CU */
WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */
WAVE_FORMAT_SONARC = 0x0021, /* Speech Compression's Sonarc */
WAVE_FORMAT_DSP_TRUESPEECH = 0x0022, /* DSP Group's True Speech */
WAVE_FORMAT_ECHOSC1 = 0x0023, /* Echo Speech's EchoSC1 */
WAVE_FORMAT_AUDIOFILE_AF36 = 0x0024, /* Audiofile AF36 */
WAVE_FORMAT_APTX = 0x0025, /* APTX */
WAVE_FORMAT_DOLBY_AC2 = 0x0030, /* Dolby AC2 */
WAVE_FORMAT_GSM610 = 0x0031, /* GSM610 */
WAVE_FORMAT_MSNAUDIO = 0x0032, /* MSNAudio */
WAVE_FORMAT_ANTEX_ADPCME = 0x0033, /* Antex ADPCME */
WAVE_FORMAT_MPEG = 0x0050, /* MPEG */
WAVE_FORMAT_MPEGLAYER3 = 0x0055, /* MPEG layer 3 */
WAVE_FORMAT_LUCENT_G723 = 0x0059, /* Lucent G.723 */
WAVE_FORMAT_G726_ADPCM = 0x0064, /* G.726 ADPCM */
WAVE_FORMAT_G722_ADPCM = 0x0065, /* G.722 ADPCM */
IBM_FORMAT_MULAW = 0x0101,
IBM_FORMAT_ALAW = 0x0102,
IBM_FORMAT_ADPCM = 0x0103,
WAVE_FORMAT_CREATIVE_ADPCM = 0x0200,
WAVE_FORMAT_EXTENSIBLE = 0xfffe
};
const int _af_wave_compression_types[_AF_WAVE_NUM_COMPTYPES] =
{
AF_COMPRESSION_G711_ULAW,
AF_COMPRESSION_G711_ALAW,
AF_COMPRESSION_IMA,
AF_COMPRESSION_MS_ADPCM
};
const InstParamInfo _af_wave_inst_params[_AF_WAVE_NUM_INSTPARAMS] =
{
{ AF_INST_MIDI_BASENOTE, AU_PVTYPE_LONG, "MIDI base note", {60} },
{ AF_INST_NUMCENTS_DETUNE, AU_PVTYPE_LONG, "Detune in cents", {0} },
{ AF_INST_MIDI_LOVELOCITY, AU_PVTYPE_LONG, "Low velocity", {1} },
{ AF_INST_MIDI_HIVELOCITY, AU_PVTYPE_LONG, "High velocity", {127} },
{ AF_INST_MIDI_LONOTE, AU_PVTYPE_LONG, "Low note", {0} },
{ AF_INST_MIDI_HINOTE, AU_PVTYPE_LONG, "High note", {127} },
{ AF_INST_NUMDBS_GAIN, AU_PVTYPE_LONG, "Gain in dB", {0} }
};
static const _AFfilesetup waveDefaultFileSetup =
{
_AF_VALID_FILESETUP, /* valid */
AF_FILE_WAVE, /* fileFormat */
true, /* trackSet */
true, /* instrumentSet */
true, /* miscellaneousSet */
1, /* trackCount */
NULL, /* tracks */
0, /* instrumentCount */
NULL, /* instruments */
0, /* miscellaneousCount */
NULL /* miscellaneous */
};
static const UUID _af_wave_guid_pcm =
{{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
}};
static const UUID _af_wave_guid_ieee_float =
{{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
}};
static const UUID _af_wave_guid_ulaw =
{{
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
}};
static const UUID _af_wave_guid_alaw =
{{
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
}};
WAVEFile::WAVEFile()
{
setFormatByteOrder(AF_BYTEORDER_LITTLEENDIAN);
m_factOffset = 0;
m_miscellaneousOffset = 0;
m_markOffset = 0;
m_dataSizeOffset = 0;
m_msadpcmNumCoefficients = 0;
}
status WAVEFile::parseFrameCount(const Tag &id, uint32_t size)
{
Track *track = getTrack();
uint32_t totalFrames;
readU32(&totalFrames);
track->totalfframes = totalFrames;
return AF_SUCCEED;
}
status WAVEFile::parseFormat(const Tag &id, uint32_t size)
{
Track *track = getTrack();
uint16_t formatTag;
readU16(&formatTag);
uint16_t channelCount;
readU16(&channelCount);
uint32_t sampleRate;
readU32(&sampleRate);
uint32_t averageBytesPerSecond;
readU32(&averageBytesPerSecond);
uint16_t blockAlign;
readU16(&blockAlign);
if (!channelCount)
{
_af_error(AF_BAD_CHANNELS, "invalid file with 0 channels");
return AF_FAIL;
}
track->f.channelCount = channelCount;
track->f.sampleRate = sampleRate;
track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN;
/* Default to uncompressed audio data. */
track->f.compressionType = AF_COMPRESSION_NONE;
track->f.framesPerPacket = 1;
switch (formatTag)
{
case WAVE_FORMAT_PCM:
{
uint16_t bitsPerSample;
readU16(&bitsPerSample);
track->f.sampleWidth = bitsPerSample;
if (bitsPerSample == 0 || bitsPerSample > 32)
{
_af_error(AF_BAD_WIDTH,
"bad sample width of %d bits",
bitsPerSample);
return AF_FAIL;
}
if (bitsPerSample <= 8)
track->f.sampleFormat = AF_SAMPFMT_UNSIGNED;
else
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
}
break;
case WAVE_FORMAT_MULAW:
case IBM_FORMAT_MULAW:
track->f.sampleWidth = 16;
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
track->f.byteOrder = _AF_BYTEORDER_NATIVE;
track->f.compressionType = AF_COMPRESSION_G711_ULAW;
track->f.bytesPerPacket = track->f.channelCount;
break;
case WAVE_FORMAT_ALAW:
case IBM_FORMAT_ALAW:
track->f.sampleWidth = 16;
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
track->f.byteOrder = _AF_BYTEORDER_NATIVE;
track->f.compressionType = AF_COMPRESSION_G711_ALAW;
track->f.bytesPerPacket = track->f.channelCount;
break;
case WAVE_FORMAT_IEEE_FLOAT:
{
uint16_t bitsPerSample;
readU16(&bitsPerSample);
if (bitsPerSample == 64)
{
track->f.sampleWidth = 64;
track->f.sampleFormat = AF_SAMPFMT_DOUBLE;
}
else
{
track->f.sampleWidth = 32;
track->f.sampleFormat = AF_SAMPFMT_FLOAT;
}
}
break;
case WAVE_FORMAT_ADPCM:
{
uint16_t bitsPerSample, extraByteCount,
samplesPerBlock, numCoefficients;
if (track->f.channelCount != 1 &&
track->f.channelCount != 2)
{
_af_error(AF_BAD_CHANNELS,
"WAVE file with MS ADPCM compression "
"must have 1 or 2 channels");
}
readU16(&bitsPerSample);
readU16(&extraByteCount);
readU16(&samplesPerBlock);
readU16(&numCoefficients);
/* numCoefficients should be at least 7. */
assert(numCoefficients >= 7 && numCoefficients <= 255);
if (numCoefficients < 7 || numCoefficients > 255)
{
_af_error(AF_BAD_HEADER,
"Bad number of coefficients");
return AF_FAIL;
}
m_msadpcmNumCoefficients = numCoefficients;
for (int i=0; i<m_msadpcmNumCoefficients; i++)
{
readS16(&m_msadpcmCoefficients[i][0]);
readS16(&m_msadpcmCoefficients[i][1]);
}
track->f.sampleWidth = 16;
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
track->f.compressionType = AF_COMPRESSION_MS_ADPCM;
track->f.byteOrder = _AF_BYTEORDER_NATIVE;
track->f.framesPerPacket = samplesPerBlock;
track->f.bytesPerPacket = blockAlign;
// Create the parameter list.
AUpvlist pv = AUpvnew(2);
AUpvsetparam(pv, 0, _AF_MS_ADPCM_NUM_COEFFICIENTS);
AUpvsetvaltype(pv, 0, AU_PVTYPE_LONG);
long l = m_msadpcmNumCoefficients;
AUpvsetval(pv, 0, &l);
AUpvsetparam(pv, 1, _AF_MS_ADPCM_COEFFICIENTS);
AUpvsetvaltype(pv, 1, AU_PVTYPE_PTR);
void *v = m_msadpcmCoefficients;
AUpvsetval(pv, 1, &v);
track->f.compressionParams = pv;
}
break;
case WAVE_FORMAT_DVI_ADPCM:
{
uint16_t bitsPerSample, extraByteCount, samplesPerBlock;
readU16(&bitsPerSample);
readU16(&extraByteCount);
readU16(&samplesPerBlock);
if (bitsPerSample != 4)
{
_af_error(AF_BAD_NOT_IMPLEMENTED,
"IMA ADPCM compression supports only 4 bits per sample");
return AF_FAIL;
}
int bytesPerBlock = (samplesPerBlock + 14) / 8 * 4 * channelCount;
if (bytesPerBlock > blockAlign || (samplesPerBlock % 8) != 1)
{
_af_error(AF_BAD_CODEC_CONFIG,
"Invalid samples per block for IMA ADPCM compression");
return AF_FAIL;
}
track->f.sampleWidth = 16;
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
track->f.compressionType = AF_COMPRESSION_IMA;
track->f.byteOrder = _AF_BYTEORDER_NATIVE;
initIMACompressionParams();
track->f.framesPerPacket = samplesPerBlock;
track->f.bytesPerPacket = blockAlign;
}
break;
case WAVE_FORMAT_EXTENSIBLE:
{
uint16_t bitsPerSample;
readU16(&bitsPerSample);
uint16_t extraByteCount;
readU16(&extraByteCount);
uint16_t reserved;
readU16(&reserved);
uint32_t channelMask;
readU32(&channelMask);
UUID subformat;
readUUID(&subformat);
if (subformat == _af_wave_guid_pcm)
{
track->f.sampleWidth = bitsPerSample;
if (bitsPerSample == 0 || bitsPerSample > 32)
{
_af_error(AF_BAD_WIDTH,
"bad sample width of %d bits",
bitsPerSample);
return AF_FAIL;
}
// Use valid bits per sample if bytes per sample is the same.
if (reserved <= bitsPerSample &&
(reserved + 7) / 8 == (bitsPerSample + 7) / 8)
track->f.sampleWidth = reserved;
if (bitsPerSample <= 8)
track->f.sampleFormat = AF_SAMPFMT_UNSIGNED;
else
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
}
else if (subformat == _af_wave_guid_ieee_float)
{
if (bitsPerSample == 64)
{
track->f.sampleWidth = 64;
track->f.sampleFormat = AF_SAMPFMT_DOUBLE;
}
else
{
track->f.sampleWidth = 32;
track->f.sampleFormat = AF_SAMPFMT_FLOAT;
}
}
else if (subformat == _af_wave_guid_alaw ||
subformat == _af_wave_guid_ulaw)
{
track->f.compressionType = subformat == _af_wave_guid_alaw ?
AF_COMPRESSION_G711_ALAW : AF_COMPRESSION_G711_ULAW;
track->f.sampleWidth = 16;
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
track->f.byteOrder = _AF_BYTEORDER_NATIVE;
track->f.bytesPerPacket = channelCount;
}
else
{
_af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE extensible data format %s is not currently supported", subformat.name().c_str());
return AF_FAIL;
}
}
break;
case WAVE_FORMAT_YAMAHA_ADPCM:
case WAVE_FORMAT_OKI_ADPCM:
case WAVE_FORMAT_CREATIVE_ADPCM:
case IBM_FORMAT_ADPCM:
_af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE ADPCM data format 0x%x is not currently supported", formatTag);
return AF_FAIL;
break;
case WAVE_FORMAT_MPEG:
_af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE MPEG data format is not supported");
return AF_FAIL;
break;
case WAVE_FORMAT_MPEGLAYER3:
_af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE MPEG layer 3 data format is not supported");
return AF_FAIL;
break;
default:
_af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE file data format 0x%x not currently supported != 0xfffe ? %d, != EXTENSIBLE? %d", formatTag, formatTag != 0xfffe, formatTag != WAVE_FORMAT_EXTENSIBLE);
return AF_FAIL;
break;
}
if (track->f.isUncompressed())
track->f.computeBytesPerPacketPCM();
_af_set_sample_format(&track->f, track->f.sampleFormat, track->f.sampleWidth);
return AF_SUCCEED;
}
status WAVEFile::parseData(const Tag &id, uint32_t size)
{
Track *track = getTrack();
track->fpos_first_frame = m_fh->tell();
track->data_size = size;
return AF_SUCCEED;
}
status WAVEFile::parsePlayList(const Tag &id, uint32_t size)
{
uint32_t segmentCount;
readU32(&segmentCount);
if (segmentCount == 0)
{
m_instrumentCount = 0;
m_instruments = NULL;
return AF_SUCCEED;
}
for (unsigned segment=0; segment<segmentCount; segment++)
{
uint32_t startMarkID, loopLength, loopCount;
readU32(&startMarkID);
readU32(&loopLength);
readU32(&loopCount);
}
return AF_SUCCEED;
}
status WAVEFile::parseCues(const Tag &id, uint32_t size)
{
Track *track = getTrack();
uint32_t markerCount;
readU32(&markerCount);
track->markerCount = markerCount;
if (markerCount == 0)
{
track->markers = NULL;
return AF_SUCCEED;
}
if ((track->markers = _af_marker_new(markerCount)) == NULL)
return AF_FAIL;
for (unsigned i=0; i<markerCount; i++)
{
uint32_t id, position, chunkid;
uint32_t chunkByteOffset, blockByteOffset;
uint32_t sampleFrameOffset;
Marker *marker = &track->markers[i];
readU32(&id);
readU32(&position);
readU32(&chunkid);
readU32(&chunkByteOffset);
readU32(&blockByteOffset);
/*
sampleFrameOffset represents the position of
the mark in units of frames.
*/
readU32(&sampleFrameOffset);
marker->id = id;
marker->position = sampleFrameOffset;
marker->name = _af_strdup("");
marker->comment = _af_strdup("");
}
return AF_SUCCEED;
}
/* Parse an adtl sub-chunk within a LIST chunk. */
status WAVEFile::parseADTLSubChunk(const Tag &id, uint32_t size)
{
Track *track = getTrack();
AFfileoffset endPos = m_fh->tell() + size;
while (m_fh->tell() < endPos)
{
Tag chunkID;
uint32_t chunkSize;
readTag(&chunkID);
readU32(&chunkSize);
if (chunkID == "labl" || chunkID == "note")
{
uint32_t id;
long length=chunkSize-4;
char *p = (char *) _af_malloc(length);
readU32(&id);
m_fh->read(p, length);
Marker *marker = track->getMarker(id);
if (marker)
{
if (chunkID == "labl")
{
free(marker->name);
marker->name = p;
}
else if (chunkID == "note")
{
free(marker->comment);
marker->comment = p;
}
else
free(p);
}
else
free(p);
/*
If chunkSize is odd, skip an extra byte
at the end of the chunk.
*/
if ((chunkSize % 2) != 0)
m_fh->seek(1, File::SeekFromCurrent);
}
else
{
/* If chunkSize is odd, skip an extra byte. */
m_fh->seek(chunkSize + (chunkSize % 2), File::SeekFromCurrent);
}
}
return AF_SUCCEED;
}
/* Parse an INFO sub-chunk within a LIST chunk. */
status WAVEFile::parseINFOSubChunk(const Tag &id, uint32_t size)
{
AFfileoffset endPos = m_fh->tell() + size;
while (m_fh->tell() < endPos)
{
int misctype = AF_MISC_UNRECOGNIZED;
Tag miscid;
uint32_t miscsize;
readTag(&miscid);
readU32(&miscsize);
if (miscid == "IART")
misctype = AF_MISC_AUTH;
else if (miscid == "INAM")
misctype = AF_MISC_NAME;
else if (miscid == "ICOP")
misctype = AF_MISC_COPY;
else if (miscid == "ICMT")
misctype = AF_MISC_ICMT;
else if (miscid == "ICRD")
misctype = AF_MISC_ICRD;
else if (miscid == "ISFT")
misctype = AF_MISC_ISFT;
if (misctype != AF_MISC_UNRECOGNIZED)
{
char *string = (char *) _af_malloc(miscsize);
m_fh->read(string, miscsize);
m_miscellaneousCount++;
m_miscellaneous = (Miscellaneous *) _af_realloc(m_miscellaneous, sizeof (Miscellaneous) * m_miscellaneousCount);
m_miscellaneous[m_miscellaneousCount-1].id = m_miscellaneousCount;
m_miscellaneous[m_miscellaneousCount-1].type = misctype;
m_miscellaneous[m_miscellaneousCount-1].size = miscsize;
m_miscellaneous[m_miscellaneousCount-1].position = 0;
m_miscellaneous[m_miscellaneousCount-1].buffer = string;
}
else
{
m_fh->seek(miscsize, File::SeekFromCurrent);
}
/* Make the current position an even number of bytes. */
if (miscsize % 2 != 0)
m_fh->seek(1, File::SeekFromCurrent);
}
return AF_SUCCEED;
}
status WAVEFile::parseList(const Tag &id, uint32_t size)
{
Tag typeID;
readTag(&typeID);
size-=4;
if (typeID == "adtl")
{
/* Handle adtl sub-chunks. */
return parseADTLSubChunk(typeID, size);
}
else if (typeID == "INFO")
{
/* Handle INFO sub-chunks. */
return parseINFOSubChunk(typeID, size);
}
else
{
/* Skip unhandled sub-chunks. */
m_fh->seek(size, File::SeekFromCurrent);
return AF_SUCCEED;
}
return AF_SUCCEED;
}
status WAVEFile::parseInstrument(const Tag &id, uint32_t size)
{
uint8_t baseNote;
int8_t detune, gain;
uint8_t lowNote, highNote, lowVelocity, highVelocity;
uint8_t padByte;
readU8(&baseNote);
readS8(&detune);
readS8(&gain);
readU8(&lowNote);
readU8(&highNote);
readU8(&lowVelocity);
readU8(&highVelocity);
readU8(&padByte);
return AF_SUCCEED;
}
bool WAVEFile::recognize(File *fh)
{
uint8_t buffer[8];
fh->seek(0, File::SeekFromBeginning);
if (fh->read(buffer, 8) != 8 || memcmp(buffer, "RIFF", 4) != 0)
return false;
if (fh->read(buffer, 4) != 4 || memcmp(buffer, "WAVE", 4) != 0)
return false;
return true;
}
status WAVEFile::readInit(AFfilesetup setup)
{
Tag type, formtype;
uint32_t size;
uint32_t index = 0;
bool hasFormat = false;
bool hasData = false;
bool hasFrameCount = false;
Track *track = allocateTrack();
m_fh->seek(0, File::SeekFromBeginning);
readTag(&type);
readU32(&size);
readTag(&formtype);
assert(type == "RIFF");
assert(formtype == "WAVE");
/* Include the offset of the form type. */
index += 4;
while (index < size)
{
Tag chunkid;
uint32_t chunksize = 0;
status result;
readTag(&chunkid);
readU32(&chunksize);
if (chunkid == "fmt ")
{
result = parseFormat(chunkid, chunksize);
if (result == AF_FAIL)
return AF_FAIL;
hasFormat = true;
}
else if (chunkid == "data")
{
/* The format chunk must precede the data chunk. */
if (!hasFormat)
{
_af_error(AF_BAD_HEADER, "missing format chunk in WAVE file");
return AF_FAIL;
}
result = parseData(chunkid, chunksize);
if (result == AF_FAIL)
return AF_FAIL;
hasData = true;
}
else if (chunkid == "inst")
{
result = parseInstrument(chunkid, chunksize);
if (result == AF_FAIL)
return AF_FAIL;
}
else if (chunkid == "fact")
{
hasFrameCount = true;
result = parseFrameCount(chunkid, chunksize);
if (result == AF_FAIL)
return AF_FAIL;
}
else if (chunkid == "cue ")
{
result = parseCues(chunkid, chunksize);
if (result == AF_FAIL)
return AF_FAIL;
}
else if (chunkid == "LIST" || chunkid == "list")
{
result = parseList(chunkid, chunksize);
if (result == AF_FAIL)
return AF_FAIL;
}
else if (chunkid == "INST")
{
result = parseInstrument(chunkid, chunksize);
if (result == AF_FAIL)
return AF_FAIL;
}
else if (chunkid == "plst")
{
result = parsePlayList(chunkid, chunksize);
if (result == AF_FAIL)
return AF_FAIL;
}
index += chunksize + 8;
/* All chunks must be aligned on an even number of bytes */
if ((index % 2) != 0)
index++;
m_fh->seek(index + 8, File::SeekFromBeginning);
}
/* The format chunk and the data chunk are required. */
if (!hasFormat || !hasData)
{
return AF_FAIL;
}
/*
At this point we know that the file has a format chunk and a
data chunk, so we can assume that track->f and track->data_size
have been initialized.
*/
if (!hasFrameCount)
{
if (track->f.bytesPerPacket && track->f.framesPerPacket)
{
track->computeTotalFileFrames();
}
else
{
_af_error(AF_BAD_HEADER, "Frame count required but not found");
return AF_FAIL;
}
}
return AF_SUCCEED;
}
AFfilesetup WAVEFile::completeSetup(AFfilesetup setup)
{
if (setup->trackSet && setup->trackCount != 1)
{
_af_error(AF_BAD_NUMTRACKS, "WAVE file must have 1 track");
return AF_NULL_FILESETUP;
}
TrackSetup *track = setup->getTrack();
if (track->f.isCompressed())
{
if (!track->sampleFormatSet)
_af_set_sample_format(&track->f, AF_SAMPFMT_TWOSCOMP, 16);
else
_af_set_sample_format(&track->f, track->f.sampleFormat, track->f.sampleWidth);
}
else if (track->sampleFormatSet)
{
switch (track->f.sampleFormat)
{
case AF_SAMPFMT_FLOAT:
if (track->sampleWidthSet &&
track->f.sampleWidth != 32)
{
_af_error(AF_BAD_WIDTH,
"Warning: invalid sample width for floating-point WAVE file: %d (must be 32 bits)\n",
track->f.sampleWidth);
_af_set_sample_format(&track->f, AF_SAMPFMT_FLOAT, 32);
}
break;
case AF_SAMPFMT_DOUBLE:
if (track->sampleWidthSet &&
track->f.sampleWidth != 64)
{
_af_error(AF_BAD_WIDTH,
"Warning: invalid sample width for double-precision floating-point WAVE file: %d (must be 64 bits)\n",
track->f.sampleWidth);
_af_set_sample_format(&track->f, AF_SAMPFMT_DOUBLE, 64);
}
break;
case AF_SAMPFMT_UNSIGNED:
if (track->sampleWidthSet)
{
if (track->f.sampleWidth < 1 || track->f.sampleWidth > 32)
{
_af_error(AF_BAD_WIDTH, "invalid sample width for WAVE file: %d (must be 1-32 bits)\n", track->f.sampleWidth);
return AF_NULL_FILESETUP;
}
if (track->f.sampleWidth > 8)
{
_af_error(AF_BAD_SAMPFMT, "WAVE integer data of more than 8 bits must be two's complement signed");
_af_set_sample_format(&track->f, AF_SAMPFMT_TWOSCOMP, track->f.sampleWidth);
}
}
else
/*
If the sample width is not set but the user requests
unsigned data, set the width to 8 bits.
*/
_af_set_sample_format(&track->f, track->f.sampleFormat, 8);
break;
case AF_SAMPFMT_TWOSCOMP:
if (track->sampleWidthSet)
{
if (track->f.sampleWidth < 1 || track->f.sampleWidth > 32)
{
_af_error(AF_BAD_WIDTH, "invalid sample width %d for WAVE file (must be 1-32)", track->f.sampleWidth);
return AF_NULL_FILESETUP;
}
else if (track->f.sampleWidth <= 8)
{
_af_error(AF_BAD_SAMPFMT, "Warning: WAVE format integer data of 1-8 bits must be unsigned; setting sample format to unsigned");
_af_set_sample_format(&track->f, AF_SAMPFMT_UNSIGNED, track->f.sampleWidth);
}
}
else
/*
If no sample width was specified, we default to 16 bits
for signed integer data.
*/
_af_set_sample_format(&track->f, track->f.sampleFormat, 16);
break;
}
}
/*
Otherwise set the sample format depending on the sample
width or set completely to default.
*/
else
{
if (!track->sampleWidthSet)
{
track->f.sampleWidth = 16;
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
}
else
{
if (track->f.sampleWidth < 1 || track->f.sampleWidth > 32)
{
_af_error(AF_BAD_WIDTH, "invalid sample width %d for WAVE file (must be 1-32)", track->f.sampleWidth);
return AF_NULL_FILESETUP;
}
else if (track->f.sampleWidth > 8)
/* Here track->f.sampleWidth is in {1..32}. */
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
else
/* Here track->f.sampleWidth is in {1..8}. */
track->f.sampleFormat = AF_SAMPFMT_UNSIGNED;
}
}
if (track->f.compressionType != AF_COMPRESSION_NONE &&
track->f.compressionType != AF_COMPRESSION_G711_ULAW &&
track->f.compressionType != AF_COMPRESSION_G711_ALAW &&
track->f.compressionType != AF_COMPRESSION_IMA &&
track->f.compressionType != AF_COMPRESSION_MS_ADPCM)
{
_af_error(AF_BAD_NOT_IMPLEMENTED, "compression format not supported in WAVE format");
return AF_NULL_FILESETUP;
}
if (track->f.isUncompressed() &&
track->byteOrderSet &&
track->f.byteOrder != AF_BYTEORDER_LITTLEENDIAN &&
track->f.isByteOrderSignificant())
{
_af_error(AF_BAD_BYTEORDER, "WAVE format only supports little-endian data");
return AF_NULL_FILESETUP;
}
if (track->f.isUncompressed())
track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN;
if (track->aesDataSet)
{
_af_error(AF_BAD_FILESETUP, "WAVE files cannot have AES data");
return AF_NULL_FILESETUP;
}
if (setup->instrumentSet)
{
if (setup->instrumentCount > 1)
{
_af_error(AF_BAD_NUMINSTS, "WAVE files can have 0 or 1 instrument");
return AF_NULL_FILESETUP;
}
else if (setup->instrumentCount == 1)
{
if (setup->instruments[0].loopSet &&
setup->instruments[0].loopCount > 0 &&
(!track->markersSet || track->markerCount == 0))
{
_af_error(AF_BAD_NUMMARKS, "WAVE files with loops must contain at least 1 marker");
return AF_NULL_FILESETUP;
}
}
}
/* Make sure the miscellaneous data is of an acceptable type. */
if (setup->miscellaneousSet)
{
for (int i=0; i<setup->miscellaneousCount; i++)
{
switch (setup->miscellaneous[i].type)
{
case AF_MISC_COPY:
case AF_MISC_AUTH:
case AF_MISC_NAME:
case AF_MISC_ICRD:
case AF_MISC_ISFT:
case AF_MISC_ICMT:
break;
default:
_af_error(AF_BAD_MISCTYPE, "illegal miscellaneous type [%d] for WAVE file", setup->miscellaneous[i].type);
return AF_NULL_FILESETUP;
}
}
}
/*
Allocate an AFfilesetup and make all the unset fields correct.
*/
AFfilesetup newsetup = _af_filesetup_copy(setup, &waveDefaultFileSetup, false);
/* Make sure we do not copy loops if they are not specified in setup. */
if (setup->instrumentSet && setup->instrumentCount > 0 &&
setup->instruments[0].loopSet)
{
free(newsetup->instruments[0].loops);
newsetup->instruments[0].loopCount = 0;
}
return newsetup;
}
bool WAVEFile::isInstrumentParameterValid(AUpvlist list, int i)
{
int param, type;
AUpvgetparam(list, i, ¶m);
AUpvgetvaltype(list, i, &type);
if (type != AU_PVTYPE_LONG)
return false;
long lval;
AUpvgetval(list, i, &lval);
switch (param)
{
case AF_INST_MIDI_BASENOTE:
return ((lval >= 0) && (lval <= 127));
case AF_INST_NUMCENTS_DETUNE:
return ((lval >= -50) && (lval <= 50));
case AF_INST_MIDI_LOVELOCITY:
return ((lval >= 1) && (lval <= 127));
case AF_INST_MIDI_HIVELOCITY:
return ((lval >= 1) && (lval <= 127));
case AF_INST_MIDI_LONOTE:
return ((lval >= 0) && (lval <= 127));
case AF_INST_MIDI_HINOTE:
return ((lval >= 0) && (lval <= 127));
case AF_INST_NUMDBS_GAIN:
return true;
default:
return false;
}
return true;
}
status WAVEFile::writeFormat()
{
uint16_t formatTag, channelCount;
uint32_t sampleRate, averageBytesPerSecond;
uint16_t blockAlign;
uint32_t chunkSize;
uint16_t bitsPerSample;
Track *track = getTrack();
m_fh->write("fmt ", 4);
switch (track->f.compressionType)
{
case AF_COMPRESSION_NONE:
chunkSize = 16;
if (track->f.sampleFormat == AF_SAMPFMT_FLOAT ||
track->f.sampleFormat == AF_SAMPFMT_DOUBLE)
{
formatTag = WAVE_FORMAT_IEEE_FLOAT;
}
else if (track->f.sampleFormat == AF_SAMPFMT_TWOSCOMP ||
track->f.sampleFormat == AF_SAMPFMT_UNSIGNED)
{
formatTag = WAVE_FORMAT_PCM;
}
else
{
_af_error(AF_BAD_COMPTYPE, "bad sample format");
return AF_FAIL;
}
blockAlign = _af_format_frame_size(&track->f, false);
bitsPerSample = 8 * _af_format_sample_size(&track->f, false);
break;
/*
G.711 compression uses eight bits per sample.
*/
case AF_COMPRESSION_G711_ULAW:
chunkSize = 18;
formatTag = IBM_FORMAT_MULAW;
blockAlign = track->f.channelCount;
bitsPerSample = 8;
break;
case AF_COMPRESSION_G711_ALAW:
chunkSize = 18;
formatTag = IBM_FORMAT_ALAW;
blockAlign = track->f.channelCount;
bitsPerSample = 8;
break;
case AF_COMPRESSION_IMA:
chunkSize = 20;
formatTag = WAVE_FORMAT_DVI_ADPCM;
blockAlign = track->f.bytesPerPacket;
bitsPerSample = 4;
break;
case AF_COMPRESSION_MS_ADPCM:
chunkSize = 50;
formatTag = WAVE_FORMAT_ADPCM;
blockAlign = track->f.bytesPerPacket;
bitsPerSample = 4;
break;
default:
_af_error(AF_BAD_COMPTYPE, "bad compression type");
return AF_FAIL;
}
writeU32(&chunkSize);
writeU16(&formatTag);
channelCount = track->f.channelCount;
writeU16(&channelCount);
sampleRate = track->f.sampleRate;
writeU32(&sampleRate);
averageBytesPerSecond =
track->f.sampleRate * _af_format_frame_size(&track->f, false);
if (track->f.compressionType == AF_COMPRESSION_IMA ||
track->f.compressionType == AF_COMPRESSION_MS_ADPCM)
averageBytesPerSecond = track->f.sampleRate * track->f.bytesPerPacket /
track->f.framesPerPacket;
writeU32(&averageBytesPerSecond);
writeU16(&blockAlign);
writeU16(&bitsPerSample);
if (track->f.compressionType == AF_COMPRESSION_G711_ULAW ||
track->f.compressionType == AF_COMPRESSION_G711_ALAW)
{
uint16_t zero = 0;
writeU16(&zero);
}
else if (track->f.compressionType == AF_COMPRESSION_IMA)
{
uint16_t extraByteCount = 2;
writeU16(&extraByteCount);
uint16_t samplesPerBlock = track->f.framesPerPacket;
writeU16(&samplesPerBlock);
}
else if (track->f.compressionType == AF_COMPRESSION_MS_ADPCM)
{
uint16_t extraByteCount = 2 + 2 + m_msadpcmNumCoefficients * 4;
writeU16(&extraByteCount);
uint16_t samplesPerBlock = track->f.framesPerPacket;
writeU16(&samplesPerBlock);
uint16_t numCoefficients = m_msadpcmNumCoefficients;
writeU16(&numCoefficients);
for (int i=0; i<m_msadpcmNumCoefficients; i++)
{
writeS16(&m_msadpcmCoefficients[i][0]);
writeS16(&m_msadpcmCoefficients[i][1]);
}
}
return AF_SUCCEED;
}
status WAVEFile::writeFrameCount()
{
uint32_t factSize = 4;
uint32_t totalFrameCount;
Track *track = getTrack();
/* Omit the fact chunk only for uncompressed integer audio formats. */
if (track->f.compressionType == AF_COMPRESSION_NONE &&
(track->f.sampleFormat == AF_SAMPFMT_TWOSCOMP ||
track->f.sampleFormat == AF_SAMPFMT_UNSIGNED))
return AF_SUCCEED;
/*
If the offset for the fact chunk hasn't been set yet,
set it to the file's current position.
*/
if (m_factOffset == 0)
m_factOffset = m_fh->tell();
else
m_fh->seek(m_factOffset, File::SeekFromBeginning);
m_fh->write("fact", 4);
writeU32(&factSize);
totalFrameCount = track->totalfframes;
writeU32(&totalFrameCount);
return AF_SUCCEED;
}
status WAVEFile::writeData()
{
Track *track = getTrack();
m_fh->write("data", 4);
m_dataSizeOffset = m_fh->tell();
uint32_t chunkSize = track->data_size;
writeU32(&chunkSize);
track->fpos_first_frame = m_fh->tell();
return AF_SUCCEED;
}
status WAVEFile::update()
{
Track *track = getTrack();
if (track->fpos_first_frame != 0)
{
uint32_t dataLength, fileLength;
// Update the frame count chunk if present.
writeFrameCount();
// Update the length of the data chunk.
m_fh->seek(m_dataSizeOffset, File::SeekFromBeginning);
dataLength = (uint32_t) track->data_size;
writeU32(&dataLength);
// Update the length of the RIFF chunk.
fileLength = (uint32_t) m_fh->length();
fileLength -= 8;
m_fh->seek(4, File::SeekFromBeginning);
writeU32(&fileLength);
}
/*
Write the actual data that was set after initializing
the miscellaneous IDs. The size of the data will be
unchanged.
*/
writeMiscellaneous();
// Write the new positions; the size of the data will be unchanged.
writeCues();
return AF_SUCCEED;
}
/* Convert an Audio File Library miscellaneous type to a WAVE type. */
static bool misc_type_to_wave (int misctype, Tag *miscid)
{
if (misctype == AF_MISC_AUTH)
*miscid = "IART";
else if (misctype == AF_MISC_NAME)
*miscid = "INAM";
else if (misctype == AF_MISC_COPY)
*miscid = "ICOP";
else if (misctype == AF_MISC_ICMT)
*miscid = "ICMT";
else if (misctype == AF_MISC_ICRD)
*miscid = "ICRD";
else if (misctype == AF_MISC_ISFT)
*miscid = "ISFT";
else
return false;
return true;
}
status WAVEFile::writeMiscellaneous()
{
if (m_miscellaneousCount != 0)
{
uint32_t miscellaneousBytes;
uint32_t chunkSize;
/* Start at 12 to account for 'LIST', size, and 'INFO'. */
miscellaneousBytes = 12;
/* Then calculate the size of the whole INFO chunk. */
for (int i=0; i<m_miscellaneousCount; i++)
{
Tag miscid;
// Skip miscellaneous data of an unsupported type.
if (!misc_type_to_wave(m_miscellaneous[i].type, &miscid))
continue;
// Account for miscellaneous type and size.
miscellaneousBytes += 8;
miscellaneousBytes += m_miscellaneous[i].size;
// Add a pad byte if necessary.
if (m_miscellaneous[i].size % 2 != 0)
miscellaneousBytes++;
assert(miscellaneousBytes % 2 == 0);
}
if (m_miscellaneousOffset == 0)
m_miscellaneousOffset = m_fh->tell();
else
m_fh->seek(m_miscellaneousOffset, File::SeekFromBeginning);
/*
Write the data. On the first call to this
function (from _af_wave_write_init), the
data won't be available, fh->seek is used to
reserve space until the data has been provided.
On subseuent calls to this function (from
_af_wave_update), the data will really be written.
*/
/* Write 'LIST'. */
m_fh->write("LIST", 4);
/* Write the size of the following chunk. */
chunkSize = miscellaneousBytes-8;
writeU32(&chunkSize);
/* Write 'INFO'. */
m_fh->write("INFO", 4);
/* Write each miscellaneous chunk. */
for (int i=0; i<m_miscellaneousCount; i++)
{
uint32_t miscsize = m_miscellaneous[i].size;
Tag miscid;
// Skip miscellaneous data of an unsupported type.
if (!misc_type_to_wave(m_miscellaneous[i].type, &miscid))
continue;
writeTag(&miscid);
writeU32(&miscsize);
if (m_miscellaneous[i].buffer != NULL)
{
uint8_t zero = 0;
m_fh->write(m_miscellaneous[i].buffer, m_miscellaneous[i].size);
// Pad if necessary.
if ((m_miscellaneous[i].size%2) != 0)
writeU8(&zero);
}
else
{
int size;
size = m_miscellaneous[i].size;
// Pad if necessary.
if ((size % 2) != 0)
size++;
m_fh->seek(size, File::SeekFromCurrent);
}
}
}
return AF_SUCCEED;
}
status WAVEFile::writeCues()
{
Track *track = getTrack();
if (!track->markerCount)
return AF_SUCCEED;
if (m_markOffset == 0)
m_markOffset = m_fh->tell();
else
m_fh->seek(m_markOffset, File::SeekFromBeginning);
Tag cue("cue ");
writeTag(&cue);
/*
The cue chunk consists of 4 bytes for the number of cue points
followed by 24 bytes for each cue point record.
*/
uint32_t cueChunkSize = 4 + track->markerCount * 24;
writeU32(&cueChunkSize);
uint32_t numCues = track->markerCount;
writeU32(&numCues);
// Write each marker to the file.
for (int i=0; i<track->markerCount; i++)
{
uint32_t identifier = track->markers[i].id;
writeU32(&identifier);
uint32_t position = i;
writeU32(&position);
Tag data("data");
writeTag(&data);
/*
For an uncompressed WAVE file which contains only one data chunk,
chunkStart and blockStart are zero.
*/
uint32_t chunkStart = 0;
writeU32(&chunkStart);
uint32_t blockStart = 0;
writeU32(&blockStart);
AFframecount markPosition = track->markers[i].position;
uint32_t sampleOffset = markPosition;
writeU32(&sampleOffset);
}
// Now write the cue names and comments within a master list chunk.
uint32_t listChunkSize = 4;
for (int i=0; i<track->markerCount; i++)
{
const char *name = track->markers[i].name;
const char *comment = track->markers[i].comment;
/*
Each 'labl' or 'note' chunk consists of 4 bytes for the chunk ID,
4 bytes for the chunk data size, 4 bytes for the cue point ID,
and then the length of the label as a null-terminated string.
In all, this is 12 bytes plus the length of the string, its null
termination byte, and a trailing pad byte if the length of the
chunk is otherwise odd.
*/
listChunkSize += 12 + zStringLength(name);
listChunkSize += 12 + zStringLength(comment);
}
Tag list("LIST");
writeTag(&list);
writeU32(&listChunkSize);
Tag adtl("adtl");
writeTag(&adtl);
for (int i=0; i<track->markerCount; i++)
{
uint32_t cuePointID = track->markers[i].id;
const char *name = track->markers[i].name;
uint32_t labelSize = 4 + zStringLength(name);
Tag lablTag("labl");
writeTag(&lablTag);
writeU32(&labelSize);
writeU32(&cuePointID);
writeZString(name);
const char *comment = track->markers[i].comment;
uint32_t noteSize = 4 + zStringLength(comment);
Tag noteTag("note");
writeTag(¬eTag);
writeU32(¬eSize);
writeU32(&cuePointID);
writeZString(comment);
}
return AF_SUCCEED;
}
bool WAVEFile::writeZString(const char *s)
{
ssize_t lengthPlusNull = strlen(s) + 1;
if (m_fh->write(s, lengthPlusNull) != lengthPlusNull)
return false;
if (lengthPlusNull & 1)
{
uint8_t zero = 0;
if (!writeU8(&zero))
return false;
}
return true;
}
size_t WAVEFile::zStringLength(const char *s)
{
size_t lengthPlusNull = strlen(s) + 1;
return lengthPlusNull + (lengthPlusNull & 1);
}
status WAVEFile::writeInit(AFfilesetup setup)
{
if (initFromSetup(setup) == AF_FAIL)
return AF_FAIL;
initCompressionParams();
uint32_t zero = 0;
m_fh->seek(0, File::SeekFromBeginning);
m_fh->write("RIFF", 4);
m_fh->write(&zero, 4);
m_fh->write("WAVE", 4);
writeMiscellaneous();
writeCues();
writeFormat();
writeFrameCount();
writeData();
return AF_SUCCEED;
}
bool WAVEFile::readUUID(UUID *u)
{
return m_fh->read(u->data, 16) == 16;
}
bool WAVEFile::writeUUID(const UUID *u)
{
return m_fh->write(u->data, 16) == 16;
}
void WAVEFile::initCompressionParams()
{
Track *track = getTrack();
if (track->f.compressionType == AF_COMPRESSION_IMA)
initIMACompressionParams();
else if (track->f.compressionType == AF_COMPRESSION_MS_ADPCM)
initMSADPCMCompressionParams();
}
void WAVEFile::initIMACompressionParams()
{
Track *track = getTrack();
track->f.framesPerPacket = 505;
track->f.bytesPerPacket = 256 * track->f.channelCount;
AUpvlist pv = AUpvnew(1);
AUpvsetparam(pv, 0, _AF_IMA_ADPCM_TYPE);
AUpvsetvaltype(pv, 0, AU_PVTYPE_LONG);
long l = _AF_IMA_ADPCM_TYPE_WAVE;
AUpvsetval(pv, 0, &l);
track->f.compressionParams = pv;
}
void WAVEFile::initMSADPCMCompressionParams()
{
const int16_t coefficients[7][2] =
{
{ 256, 0 },
{ 512, -256 },
{ 0, 0 },
{ 192, 64 },
{ 240, 0 },
{ 460, -208 },
{ 392, -232 }
};
memcpy(m_msadpcmCoefficients, coefficients, sizeof (int16_t) * 7 * 2);
m_msadpcmNumCoefficients = 7;
Track *track = getTrack();
track->f.framesPerPacket = 500;
track->f.bytesPerPacket = 256 * track->f.channelCount;
AUpvlist pv = AUpvnew(2);
AUpvsetparam(pv, 0, _AF_MS_ADPCM_NUM_COEFFICIENTS);
AUpvsetvaltype(pv, 0, AU_PVTYPE_LONG);
long l = m_msadpcmNumCoefficients;
AUpvsetval(pv, 0, &l);
AUpvsetparam(pv, 1, _AF_MS_ADPCM_COEFFICIENTS);
AUpvsetvaltype(pv, 1, AU_PVTYPE_PTR);
void *v = m_msadpcmCoefficients;
AUpvsetval(pv, 1, &v);
track->f.compressionParams = pv;
}
|