sox-devel@lists.sourceforge.net unofficial mirror
 help / color / mirror / code / Atom feed
blob 240d2e1fdf63ce5dfbf3f0e373fb6940788788fd 39262 bytes (raw)
name: src/aiff.c 	 # note: path name is non-authoritative(*)

   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
 
/* libSoX SGI/Amiga AIFF format.
 * Copyright 1991-2007 Guido van Rossum And Sundry Contributors
 *
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained.
 * Guido van Rossum And Sundry Contributors are not responsible for
 * the consequences of using this software.
 *
 * Used by SGI on 4D/35 and Indigo.
 * This is a subformat of the EA-IFF-85 format.
 * This is related to the IFF format used by the Amiga.
 * But, apparently, not the same.
 * Also AIFF-C format output that is defined in DAVIC 1.4 Part 9 Annex B
 * (usable for japanese-data-broadcasting, specified by ARIB STD-B24.)
 */

#include "sox_i.h"
#include "aiff.h"

#include <time.h>      /* for time stamping comments */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>

/* forward declarations */
static double read_ieee_extended(sox_format_t *);
static int aiffwriteheader(sox_format_t *, uint64_t);
static int aifcwriteheader(sox_format_t *, uint64_t);
static void write_ieee_extended(sox_format_t *, double);
static double ConvertFromIeeeExtended(unsigned char*);
static void ConvertToIeeeExtended(double, char *);
static int textChunk(char **text, char *chunkDescription, sox_format_t * ft);
static int commentChunk(char **text, char *chunkDescription, sox_format_t * ft);
static void reportInstrument(sox_format_t * ft);

int lsx_aiffstartread(sox_format_t * ft)
{
  char buf[5];
  uint32_t totalsize;
  uint32_t chunksize;
  unsigned short channels = 0;
  sox_encoding_t enc = SOX_ENCODING_SIGN2;
  uint32_t frames;
  unsigned short bits = 0;
  double rate = 0.0;
  uint32_t offset = 0;
  uint32_t blocksize = 0;
  int foundcomm = 0, foundmark = 0, foundinstr = 0, is_sowt = 0;
  struct mark {
    unsigned short id;
    uint32_t position;
    char name[40];
  } marks[32];
  unsigned short looptype;
  int i, j;
  unsigned short nmarks = 0;
  unsigned short sustainLoopBegin = 0, sustainLoopEnd = 0,
                 releaseLoopBegin = 0, releaseLoopEnd = 0;
  off_t seekto = 0;
  size_t ssndsize = 0;
  char *annotation;
  char *author;
  char *comment = NULL;
  char *copyright;
  char *nametext;

  uint8_t trash8;
  uint16_t trash16;
  uint32_t trash32;

  int rc;

  /* FORM chunk */
  if (lsx_reads(ft, buf, (size_t)4) == SOX_EOF || strncmp(buf, "FORM", (size_t)4) != 0) {
    lsx_fail_errno(ft,SOX_EHDR,"AIFF header does not begin with magic word `FORM'");
    return(SOX_EOF);
  }
  lsx_readdw(ft, &totalsize);
  if (lsx_reads(ft, buf, (size_t)4) == SOX_EOF || (strncmp(buf, "AIFF", (size_t)4) != 0 &&
        strncmp(buf, "AIFC", (size_t)4) != 0)) {
    lsx_fail_errno(ft,SOX_EHDR,"AIFF `FORM' chunk does not specify `AIFF' or `AIFC' as type");
    return(SOX_EOF);
  }


  /* Skip everything but the COMM chunk and the SSND chunk */
  /* The SSND chunk must be the last in the file */
  while (1) {
    if (lsx_reads(ft, buf, (size_t)4) == SOX_EOF) {
      if (ssndsize > 0)
        break;
      else {
        lsx_fail_errno(ft,SOX_EHDR,"Missing SSND chunk in AIFF file");
        return(SOX_EOF);
      }
    }
    if (strncmp(buf, "COMM", (size_t)4) == 0) {
      /* COMM chunk */
      lsx_readdw(ft, &chunksize);
      lsx_readw(ft, &channels);
      lsx_readdw(ft, &frames);
      lsx_readw(ft, &bits);
      rate = read_ieee_extended(ft);
      chunksize -= 18;
      if (chunksize > 0) {
        lsx_reads(ft, buf, (size_t)4);
        chunksize -= 4;
        if (strncmp(buf, "sowt", (size_t)4) == 0) {
          /* CD audio as read on Mac OS machines */
          /* Need to endian swap all the data */
          is_sowt = 1;
        }
        else if (strncmp(buf, "fl32", (size_t)4) == 0 ||
            strncmp(buf, "FL32", (size_t)4) == 0) {
          enc = SOX_ENCODING_FLOAT;
          if (bits != 32) {
            lsx_fail_errno(ft, SOX_EHDR,
              "Sample size of %u is not consistent with `fl32' compression type", bits);
            return SOX_EOF;
          }
        }
        else if (strncmp(buf, "fl64", (size_t)4) == 0 ||
            strncmp(buf, "FL64", (size_t)4) == 0) {
          enc = SOX_ENCODING_FLOAT;
          if (bits != 64) {
            lsx_fail_errno(ft, SOX_EHDR,
              "Sample size of %u is not consistent with `fl64' compression type", bits);
            return SOX_EOF;
          }
        }
        else if (strncmp(buf, "NONE", (size_t)4) != 0 &&
            strncmp(buf, "twos", (size_t)4) != 0) {
          buf[4] = 0;
          lsx_fail_errno(ft, SOX_EHDR, "Unsupported AIFC compression type `%s'", buf);
          return(SOX_EOF);
        }
      }
      while(chunksize-- > 0)
        lsx_readb(ft, &trash8);
      foundcomm = 1;
    }
    else if (strncmp(buf, "SSND", (size_t)4) == 0) {
      /* SSND chunk */
      lsx_readdw(ft, &chunksize);
      lsx_readdw(ft, &offset);
      lsx_readdw(ft, &blocksize);
      chunksize -= 8;
      ssndsize = chunksize;
      /* word-align chunksize in case it wasn't
       * done by writing application already.
       */
      chunksize += (chunksize % 2);
      /* if can't seek, just do sound now */
      if (!ft->seekable)
        break;
      /* else, seek to end of sound and hunt for more */
      seekto = lsx_tell(ft);
      lsx_seeki(ft, (off_t)chunksize, SEEK_CUR);
    }
    else if (strncmp(buf, "MARK", (size_t)4) == 0) {
      /* MARK chunk */
      lsx_readdw(ft, &chunksize);
      if (chunksize >= sizeof(nmarks)) {
        lsx_readw(ft, &nmarks);
        chunksize -= sizeof(nmarks);
      }
      else nmarks = 0;

      /* Some programs like to always have a MARK chunk
       * but will set number of marks to 0 and force
       * software to detect and ignore it.
       */
      if (nmarks == 0)
        foundmark = 0;
      else
        foundmark = 1;

      /* Make sure its not larger then we support */
      if (nmarks > 32)
        nmarks = 32;

      for(i = 0; i < nmarks && chunksize; i++) {
        unsigned char len, read_len, tmp_c;

        if (chunksize < 6)
          break;
        lsx_readw(ft, &(marks[i].id));
        lsx_readdw(ft, &(marks[i].position));
        chunksize -= 6;
        /* If error reading length then
         * don't try to read more bytes
         * based on that value.
         */
        if (lsx_readb(ft, &len) != SOX_SUCCESS)
          break;
        --chunksize;
        if (len > chunksize)
          len = chunksize;
        read_len = len;
        if (read_len > 39)
          read_len = 39;
        for(j = 0; j < len && chunksize; j++) {
          lsx_readb(ft, &tmp_c);
          if (j < read_len)
            marks[i].name[j] = tmp_c;
          chunksize--;
        }
        marks[i].name[read_len] = 0;
        if ((len & 1) == 0 && chunksize) {
          chunksize--;
          lsx_readb(ft, &trash8);
        }
      }
      /* HA HA!  Sound Designer (and others) makes */
      /* bogus files. It spits out bogus chunksize */
      /* for MARK field */
      while(chunksize-- > 0)
        lsx_readb(ft, &trash8);
    }
    else if (strncmp(buf, "INST", (size_t)4) == 0) {
      /* INST chunk */
      lsx_readdw(ft, &chunksize);
      lsx_readsb(ft, &(ft->oob.instr.MIDInote));
      lsx_readb(ft, &trash8);
      lsx_readsb(ft, &(ft->oob.instr.MIDIlow));
      lsx_readsb(ft, &(ft->oob.instr.MIDIhi));
      /* Low  velocity */
      lsx_readb(ft, &trash8);
      /* Hi  velocity */
      lsx_readb(ft, &trash8);
      lsx_readw(ft, &trash16);/* gain */
      lsx_readw(ft, &looptype); /* sustain loop */
      ft->oob.loops[0].type = looptype;
      lsx_readw(ft, &sustainLoopBegin); /* begin marker */
      lsx_readw(ft, &sustainLoopEnd);    /* end marker */
      lsx_readw(ft, &looptype); /* release loop */
      ft->oob.loops[1].type = looptype;
      lsx_readw(ft, &releaseLoopBegin);  /* begin marker */
      lsx_readw(ft, &releaseLoopEnd);    /* end marker */

      foundinstr = 1;
    }
    else if (strncmp(buf, "APPL", (size_t)4) == 0) {
      lsx_readdw(ft, &chunksize);
      /* word-align chunksize in case it wasn't
       * done by writing application already.
       */
      chunksize += (chunksize % 2);
      while(chunksize-- > 0)
        lsx_readb(ft, &trash8);
    }
    else if (strncmp(buf, "ALCH", (size_t)4) == 0) {
      /* I think this is bogus and gets grabbed by APPL */
      /* INST chunk */
      lsx_readdw(ft, &trash32);                /* ENVS - jeez! */
      lsx_readdw(ft, &chunksize);
      while(chunksize-- > 0)
        lsx_readb(ft, &trash8);
    }
    else if (strncmp(buf, "ANNO", (size_t)4) == 0) {
      rc = textChunk(&annotation, "Annotation:", ft);
      if (rc) {
        /* Fail already called in function */
        return(SOX_EOF);
      }
      if (annotation)
        sox_append_comments(&ft->oob.comments, annotation);
      free(annotation);
    }
    else if (strncmp(buf, "COMT", (size_t)4) == 0) {
      rc = commentChunk(&comment, "Comment:", ft);
      if (rc) {
        /* Fail already called in function */
        return(SOX_EOF);
      }
      if (comment)
        sox_append_comments(&ft->oob.comments, comment);
      free(comment);
    }
    else if (strncmp(buf, "AUTH", (size_t)4) == 0) {
      /* Author chunk */
      rc = textChunk(&author, "Author:", ft);
      if (rc) {
        /* Fail already called in function */
        return(SOX_EOF);
      }
      free(author);
    }
    else if (strncmp(buf, "NAME", (size_t)4) == 0) {
      /* Name chunk */
      rc = textChunk(&nametext, "Name:", ft);
      if (rc) {
        /* Fail already called in function */
        return(SOX_EOF);
      }
      free(nametext);
    }
    else if (strncmp(buf, "(c) ", (size_t)4) == 0) {
      /* Copyright chunk */
      rc = textChunk(&copyright, "Copyright:", ft);
      if (rc) {
        /* Fail already called in function */
        return(SOX_EOF);
      }
      free(copyright);
    }
    else {
      if (lsx_eof(ft))
        break;
      buf[4] = 0;
      lsx_debug("AIFFstartread: ignoring `%s' chunk", buf);
      lsx_readdw(ft, &chunksize);
      if (lsx_eof(ft))
        break;
      /* Skip the chunk using lsx_readb() so we may read
         from a pipe */
      while (chunksize-- > 0) {
        if (lsx_readb(ft, &trash8) == SOX_EOF)
          break;
      }
    }
    if (lsx_eof(ft))
      break;
  }

  /*
   * if a pipe, we lose all chunks after sound.
   * Like, say, instrument loops.
   */
  if (ft->seekable) {
    if (seekto > 0)
      lsx_seeki(ft, seekto, SEEK_SET);
    else {
      lsx_fail_errno(ft,SOX_EOF,"AIFF: no sound data on input file");
      return(SOX_EOF);
    }
  }
  /* SSND chunk just read */
  if (blocksize != 0)
    lsx_warn("AIFF header has invalid blocksize.  Ignoring but expect a premature EOF");

  ssndsize -= offset;
  while (offset-- > 0) {
    if (lsx_readb(ft, &trash8) == SOX_EOF) {
      lsx_fail_errno(ft,errno,"unexpected EOF while skipping AIFF offset");
      return(SOX_EOF);
    }
  }

  if (foundcomm) {
    if      (bits <=  8) bits = 8;
    else if (bits <= 16) bits = 16;
    else if (bits <= 24) bits = 24;
    else if (bits <= 32) bits = 32;
    else if (bits == 64 && enc == SOX_ENCODING_FLOAT) /* no-op */;
    else {
      lsx_fail_errno(ft,SOX_EFMT,"unsupported sample size in AIFF header: %d", bits);
      return(SOX_EOF);
    }
  } else  {
    if ((ft->signal.channels == SOX_UNSPEC)
        || (ft->signal.rate == SOX_UNSPEC)
        || (ft->encoding.encoding == SOX_ENCODING_UNKNOWN)
        || (ft->encoding.bits_per_sample == 0)) {
      lsx_report("You must specify # channels, sample rate, signed/unsigned,");
      lsx_report("and 8/16 on the command line.");
      lsx_fail_errno(ft,SOX_EFMT,"Bogus AIFF file: no COMM section.");
      return(SOX_EOF);
    }

  }
  ssndsize /= bits >> 3;

  /* Cope with 'sowt' CD tracks as read on Macs */
  if (is_sowt)
    ft->encoding.reverse_bytes = !ft->encoding.reverse_bytes;

  if (foundmark && !foundinstr) {
    lsx_debug("Ignoring MARK chunk since no INSTR found.");
    foundmark = 0;
  }
  if (!foundmark && foundinstr) {
    lsx_debug("Ignoring INSTR chunk since no MARK found.");
    foundinstr = 0;
  }
  if (foundmark && foundinstr) {
    int i2;
    int slbIndex = 0, sleIndex = 0;
    int rlbIndex = 0, rleIndex = 0;

    /* find our loop markers and save their marker indexes */
    for(i2 = 0; i2 < nmarks; i2++) {
      if(marks[i2].id == sustainLoopBegin)
        slbIndex = i2;
      if(marks[i2].id == sustainLoopEnd)
        sleIndex = i2;
      if(marks[i2].id == releaseLoopBegin)
        rlbIndex = i2;
      if(marks[i2].id == releaseLoopEnd)
        rleIndex = i2;
    }

    ft->oob.instr.nloops = 0;
    if (ft->oob.loops[0].type != 0) {
      ft->oob.loops[0].start = marks[slbIndex].position;
      ft->oob.loops[0].length =
        marks[sleIndex].position - marks[slbIndex].position;
      /* really the loop count should be infinite */
      ft->oob.loops[0].count = 1;
      ft->oob.instr.loopmode = SOX_LOOP_SUSTAIN_DECAY | ft->oob.loops[0].type;
      ft->oob.instr.nloops++;
    }
    if (ft->oob.loops[1].type != 0) {
      ft->oob.loops[1].start = marks[rlbIndex].position;
      ft->oob.loops[1].length =
        marks[rleIndex].position - marks[rlbIndex].position;
      /* really the loop count should be infinite */
      ft->oob.loops[1].count = 1;
      ft->oob.instr.loopmode = SOX_LOOP_SUSTAIN_DECAY | ft->oob.loops[1].type;
      ft->oob.instr.nloops++;
    }
  }
  reportInstrument(ft);

  return lsx_check_read_params(
      ft, channels, rate, enc, bits, (uint64_t)ssndsize, sox_false);
}

/* print out the MIDI key allocations, loop points, directions etc */
static void reportInstrument(sox_format_t * ft)
{
  unsigned loopNum;

  if(ft->oob.instr.nloops > 0)
    lsx_report("AIFF Loop markers:");
  for(loopNum  = 0; loopNum < ft->oob.instr.nloops; loopNum++) {
    if (ft->oob.loops[loopNum].count) {
      lsx_report("Loop %d: start: %6lu", loopNum, (unsigned long)ft->oob.loops[loopNum].start);
      lsx_report(" end:   %6lu",
              (unsigned long)(ft->oob.loops[loopNum].start + ft->oob.loops[loopNum].length));
      lsx_report(" count: %6d", ft->oob.loops[loopNum].count);
      lsx_report(" type:  ");
      switch(ft->oob.loops[loopNum].type & ~SOX_LOOP_SUSTAIN_DECAY) {
      case 0: lsx_report("off"); break;
      case 1: lsx_report("forward"); break;
      case 2: lsx_report("forward/backward"); break;
      }
    }
  }
  lsx_report("Unity MIDI Note: %d", ft->oob.instr.MIDInote);
  lsx_report("Low   MIDI Note: %d", ft->oob.instr.MIDIlow);
  lsx_report("High  MIDI Note: %d", ft->oob.instr.MIDIhi);
}

/* Process a text chunk, allocate memory, display it if verbose and return */
static int textChunk(char **text, char *chunkDescription, sox_format_t * ft)
{
  uint32_t chunksize0;
  size_t chunksize;
  lsx_readdw(ft, &chunksize0);
  chunksize = chunksize0;

  /* allocate enough memory to hold the text including a terminating \0 */
  if (chunksize != SOX_SIZE_MAX)
    *text = lsx_malloc((size_t)chunksize+1);
  else
    *text = lsx_malloc((size_t)chunksize);

  if (lsx_readbuf(ft, *text, (size_t) chunksize) != chunksize)
  {
    lsx_fail_errno(ft,SOX_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
    return(SOX_EOF);
  }
  if (chunksize != SOX_SIZE_MAX)
    *(*text + chunksize) = '\0';
  else
    *(*text + chunksize-1) = '\0';
  if (chunksize % 2)
  {
    /* Read past pad byte */
    char c;
    if (lsx_readbuf(ft, &c, (size_t)1) != 1)
    {
      lsx_fail_errno(ft,SOX_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
      return(SOX_EOF);
    }
  }
  lsx_debug("%-10s   \"%s\"", chunkDescription, *text);
  return(SOX_SUCCESS);
}

/* Comment lengths are words, not double words, and we can have several, so
   we use a special function, not textChunk().;
 */
static int commentChunk(char **text, char *chunkDescription, sox_format_t * ft)
{
  uint32_t chunksize;
  unsigned short numComments;
  uint32_t timeStamp;
  unsigned short markerId;
  unsigned short totalCommentLength = 0;
  unsigned int totalReadLength = 0;
  unsigned int commentIndex;

  lsx_readdw(ft, &chunksize);
  lsx_readw(ft, &numComments);
  totalReadLength += 2; /* chunksize doesn't count */
  for(commentIndex = 0; commentIndex < numComments; commentIndex++) {
    unsigned short commentLength;

    lsx_readdw(ft, &timeStamp);
    lsx_readw(ft, &markerId);
    lsx_readw(ft, &commentLength);
    if (((size_t)totalCommentLength) + commentLength > USHRT_MAX) {
        lsx_fail_errno(ft,SOX_EOF,"AIFF: Comment too long in %s header", chunkDescription);
        return(SOX_EOF);
    }
    totalCommentLength += commentLength;
    /* allocate enough memory to hold the text including a terminating \0 */
    if(commentIndex == 0) {
      *text = lsx_malloc((size_t) totalCommentLength + 1);
    }
    else {
      *text = lsx_realloc(*text, (size_t) totalCommentLength + 1);
    }

    if (lsx_readbuf(ft, *text + totalCommentLength - commentLength, (size_t) commentLength) != commentLength) {
        lsx_fail_errno(ft,SOX_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
        return(SOX_EOF);
    }
    *(*text + totalCommentLength) = '\0';
    totalReadLength += totalCommentLength + 4 + 2 + 2; /* include header */
    if (commentLength % 2) {
        /* Read past pad byte */
        char c;
        if (lsx_readbuf(ft, &c, (size_t)1) != 1) {
            lsx_fail_errno(ft,SOX_EOF,"AIFF: Unexpected EOF in %s header", chunkDescription);
            return(SOX_EOF);
        }
        totalReadLength += 1;
    }
  }
  lsx_debug("%-10s   \"%s\"", chunkDescription, *text);
  /* make sure we read the whole chunk */
  if (totalReadLength < chunksize) {
       size_t i;
       char c;
       for (i=0; i < chunksize - totalReadLength; i++ )
           lsx_readbuf(ft, &c, (size_t)1);
  }
  return(SOX_SUCCESS);
}

int lsx_aiffstopread(sox_format_t * ft)
{
        char buf[5];
        uint32_t chunksize;
        uint8_t trash;

        if (!ft->seekable)
        {
            while (! lsx_eof(ft))
            {
                if (lsx_readbuf(ft, buf, (size_t)4) != 4)
                        break;

                lsx_readdw(ft, &chunksize);
                if (lsx_eof(ft))
                        break;
                buf[4] = '\0';
                lsx_warn("Ignoring AIFF tail chunk: `%s', %u bytes long",
                        buf, chunksize);
                if (! strcmp(buf, "MARK") || ! strcmp(buf, "INST"))
                        lsx_warn("       You're stripping MIDI/loop info!");
                while (chunksize-- > 0)
                {
                        if (lsx_readb(ft, &trash) == SOX_EOF)
                                break;
                }
            }
        }
        return SOX_SUCCESS;
}

/* When writing, the header is supposed to contain the number of
   samples and data bytes written.
   Since we don't know how many samples there are until we're done,
   we first write the header with an very large number,
   and at the end we rewind the file and write the header again
   with the right number.  This only works if the file is seekable;
   if it is not, the very large size remains in the header.
   Strictly spoken this is not legal, but the playaiff utility
   will still be able to play the resulting file. */

int lsx_aiffstartwrite(sox_format_t * ft)
{
        int rc;

        /* Needed because lsx_rawwrite() */
        rc = lsx_rawstartwrite(ft);
        if (rc)
            return rc;

        /* Compute the "very large number" so that a maximum number
           of samples can be transmitted through a pipe without the
           risk of causing overflow when calculating the number of bytes.
           At 48 kHz, 16 bits stereo, this gives ~3 hours of audio.
           Sorry, the AIFF format does not provide for an indefinite
           number of samples. */
        return(aiffwriteheader(ft, (uint64_t) 0x7f000000 / ((ft->encoding.bits_per_sample>>3)*ft->signal.channels)));
}

int lsx_aiffstopwrite(sox_format_t * ft)
{
        /* If we've written an odd number of bytes, write a padding
           NUL */
        if (ft->olength % 2 == 1 && ft->encoding.bits_per_sample == 8 && ft->signal.channels == 1)
        {
            sox_sample_t buf = 0;
            lsx_rawwrite(ft, &buf, (size_t) 1);
        }

        if (!ft->seekable)
        {
            lsx_fail_errno(ft,SOX_EOF,"Non-seekable file.");
            return(SOX_EOF);
        }
        if (lsx_seeki(ft, (off_t)0, SEEK_SET) != 0)
        {
                lsx_fail_errno(ft,errno,"can't rewind output file to rewrite AIFF header");
                return(SOX_EOF);
        }
        return(aiffwriteheader(ft, ft->olength / ft->signal.channels));
}

static int aiffwriteheader(sox_format_t * ft, uint64_t nframes)
{
        int hsize =
                8 /*COMM hdr*/ + 18 /*COMM chunk*/ +
                8 /*SSND hdr*/ + 12 /*SSND chunk*/;
        unsigned bits = 0;
        unsigned i;
        uint64_t size;
        size_t padded_comment_size = 0, comment_size = 0;
        size_t comment_chunk_size = 0;
        char * comment = lsx_cat_comments(ft->oob.comments);

        /* MARK and INST chunks */
        if (ft->oob.instr.nloops) {
          hsize += 8 /* MARK hdr */ + 2 + 16*ft->oob.instr.nloops;
          hsize += 8 /* INST hdr */ + 20; /* INST chunk */
        }

        if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
            ft->encoding.bits_per_sample == 8)
                bits = 8;
        else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
                 ft->encoding.bits_per_sample == 16)
                bits = 16;
        else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
                 ft->encoding.bits_per_sample == 24)
                bits = 24;
        else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
                 ft->encoding.bits_per_sample == 32)
                bits = 32;
        else
        {
                lsx_fail_errno(ft,SOX_EFMT,"unsupported output encoding/size for AIFF header");
                return(SOX_EOF);
        }

        /* COMT comment chunk -- holds comments text with a timestamp and marker id */
        /* We calculate the comment_chunk_size if we will be writing a comment */
        if (ft->oob.comments)
        {
          comment_size = strlen(comment);
          /* Must put an even number of characters out.
           * True 68k processors OS's seem to require this.
           */
          padded_comment_size = ((comment_size % 2) == 0) ?
                                comment_size : comment_size + 1;
          /* one comment, timestamp, marker ID and text count */
          comment_chunk_size = (2 + 4 + 2 + 2 + padded_comment_size);
          hsize += 8 /* COMT hdr */ + comment_chunk_size;
        }

        lsx_writes(ft, "FORM"); /* IFF header */
        /* file size */
        size = hsize + nframes * (ft->encoding.bits_per_sample >> 3) * ft->signal.channels;
        if (size > UINT_MAX)
        {
            lsx_warn("file size too big for accurate AIFF header");
            size = UINT_MAX;
        }
        lsx_writedw(ft, (unsigned)size);
        lsx_writes(ft, "AIFF"); /* File type */

        /* Now we write the COMT comment chunk using the precomputed sizes */
        if (ft->oob.comments)
        {
          lsx_writes(ft, "COMT");
          lsx_writedw(ft, (unsigned) comment_chunk_size);

          /* one comment */
          lsx_writew(ft, 1);

          /* time stamp of comment, Unix knows of time from 1/1/1970,
             Apple knows time from 1/1/1904 */
          lsx_writedw(ft, (unsigned)((sox_globals.repeatable? 0 : time(NULL)) + 2082844800));

          /* A marker ID of 0 indicates the comment is not associated
             with a marker */
          lsx_writew(ft, 0);

          /* now write the count and the bytes of text */
          lsx_writew(ft, (unsigned) padded_comment_size);
          lsx_writes(ft, comment);
          if (comment_size != padded_comment_size)
                lsx_writes(ft, " ");
        }
        free(comment);

        /* COMM chunk -- describes encoding (and #frames) */
        lsx_writes(ft, "COMM");
        lsx_writedw(ft, 18); /* COMM chunk size */
        lsx_writew(ft, ft->signal.channels); /* nchannels */
        lsx_writedw(ft, (unsigned) nframes); /* number of frames */
        lsx_writew(ft, bits); /* sample width, in bits */
        write_ieee_extended(ft, (double)ft->signal.rate);

        /* MARK chunk -- set markers */
        if (ft->oob.instr.nloops) {
                lsx_writes(ft, "MARK");
                if (ft->oob.instr.nloops > 2)
                        ft->oob.instr.nloops = 2;
                lsx_writedw(ft, 2 + 16u*ft->oob.instr.nloops);
                lsx_writew(ft, ft->oob.instr.nloops);

                for(i = 0; i < ft->oob.instr.nloops; i++) {
                        unsigned start = ft->oob.loops[i].start > UINT_MAX
                            ? UINT_MAX
                            : ft->oob.loops[i].start;
                        unsigned end = ft->oob.loops[i].start + ft->oob.loops[i].length > UINT_MAX
                            ? UINT_MAX
                            : ft->oob.loops[i].start + ft->oob.loops[i].length;
                        lsx_writew(ft, i + 1);
                        lsx_writedw(ft, start);
                        lsx_writeb(ft, 0);
                        lsx_writeb(ft, 0);
                        lsx_writew(ft, i*2 + 1);
                        lsx_writedw(ft, end);
                        lsx_writeb(ft, 0);
                        lsx_writeb(ft, 0);
                }

                lsx_writes(ft, "INST");
                lsx_writedw(ft, 20);
                /* random MIDI shit that we default on */
                lsx_writeb(ft, (uint8_t)ft->oob.instr.MIDInote);
                lsx_writeb(ft, 0);                       /* detune */
                lsx_writeb(ft, (uint8_t)ft->oob.instr.MIDIlow);
                lsx_writeb(ft, (uint8_t)ft->oob.instr.MIDIhi);
                lsx_writeb(ft, 1);                       /* low velocity */
                lsx_writeb(ft, 127);                     /* hi  velocity */
                lsx_writew(ft, 0);                               /* gain */

                /* sustain loop */
                lsx_writew(ft, ft->oob.loops[0].type);
                lsx_writew(ft, 1);                               /* marker 1 */
                lsx_writew(ft, 3);                               /* marker 3 */
                /* release loop, if there */
                if (ft->oob.instr.nloops == 2) {
                        lsx_writew(ft, ft->oob.loops[1].type);
                        lsx_writew(ft, 2);                       /* marker 2 */
                        lsx_writew(ft, 4);                       /* marker 4 */
                } else {
                        lsx_writew(ft, 0);                       /* no release loop */
                        lsx_writew(ft, 0);
                        lsx_writew(ft, 0);
                }
        }

        /* SSND chunk -- describes data */
        lsx_writes(ft, "SSND");
        /* chunk size */
        lsx_writedw(ft, (unsigned) (8 + nframes * ft->signal.channels * (ft->encoding.bits_per_sample >> 3)));
        lsx_writedw(ft, 0); /* offset */
        lsx_writedw(ft, 0); /* block size */
        return(SOX_SUCCESS);
}

int lsx_aifcstartwrite(sox_format_t * ft)
{
        int rc;

        /* Needed because lsx_rawwrite() */
        rc = lsx_rawstartwrite(ft);
        if (rc)
            return rc;

        /* Compute the "very large number" so that a maximum number
           of samples can be transmitted through a pipe without the
           risk of causing overflow when calculating the number of bytes.
           At 48 kHz, 16 bits stereo, this gives ~3 hours of music.
           Sorry, the AIFC format does not provide for an "infinite"
           number of samples. */
        return(aifcwriteheader(ft, (uint64_t) 0x7f000000 / ((ft->encoding.bits_per_sample >> 3)*ft->signal.channels)));
}

int lsx_aifcstopwrite(sox_format_t * ft)
{
        /* If we've written an odd number of bytes, write a padding
           NUL */
        if (ft->olength % 2 == 1 && ft->encoding.bits_per_sample == 8 && ft->signal.channels == 1)
        {
            sox_sample_t buf = 0;
            lsx_rawwrite(ft, &buf, (size_t) 1);
        }

        if (!ft->seekable)
        {
            lsx_fail_errno(ft,SOX_EOF,"Non-seekable file.");
            return(SOX_EOF);
        }
        if (lsx_seeki(ft, (off_t)0, SEEK_SET) != 0)
        {
                lsx_fail_errno(ft,errno,"can't rewind output file to rewrite AIFC header");
                return(SOX_EOF);
        }
        return(aifcwriteheader(ft, ft->olength / ft->signal.channels));
}

static int aifcwriteheader(sox_format_t * ft, uint64_t nframes)
{
        unsigned hsize;
        unsigned bits = 0;
        uint64_t size;
        char *ctype = NULL, *cname = NULL;
        unsigned cname_len = 0, comm_len = 0, comm_padding = 0;

        if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
            ft->encoding.bits_per_sample == 8)
                bits = 8;
        else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
                 ft->encoding.bits_per_sample == 16)
                bits = 16;
        else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
                 ft->encoding.bits_per_sample == 24)
                bits = 24;
        else if (ft->encoding.encoding == SOX_ENCODING_SIGN2 &&
                 ft->encoding.bits_per_sample == 32)
                bits = 32;
        else if (ft->encoding.encoding == SOX_ENCODING_FLOAT &&
                 ft->encoding.bits_per_sample == 32)
                bits = 32;
        else if (ft->encoding.encoding == SOX_ENCODING_FLOAT &&
                 ft->encoding.bits_per_sample == 64)
                bits = 64;
        else
        {
                lsx_fail_errno(ft,SOX_EFMT,"unsupported output encoding/size for AIFC header");
                return(SOX_EOF);
        }

        /* calculate length of COMM chunk (without header) */
        switch (ft->encoding.encoding) {
          case SOX_ENCODING_SIGN2:
            ctype = "NONE";
            cname = "not compressed";
            break;
          case SOX_ENCODING_FLOAT:
            if (bits == 32) {
              ctype = "fl32";
              cname = "32-bit floating point";
            } else {
              ctype = "fl64";
              cname = "64-bit floating point";
            }
            break;
          default: /* can't happen */
            break;
        }
        cname_len = strlen(cname);
        comm_len = 18+4+1+cname_len;
        comm_padding = comm_len%2;

        hsize = 12 /*FVER*/ + 8 /*COMM hdr*/ + comm_len+comm_padding /*COMM chunk*/ +
                8 /*SSND hdr*/ + 12 /*SSND chunk*/;

        lsx_writes(ft, "FORM"); /* IFF header */
        /* file size */
        size = hsize + nframes * (ft->encoding.bits_per_sample >> 3) * ft->signal.channels;
        if (size > UINT_MAX)
        {
            lsx_warn("file size too big for accurate AIFC header");
            size = UINT_MAX;
        }
        lsx_writedw(ft, (unsigned)size);
        lsx_writes(ft, "AIFC"); /* File type */

        /* FVER chunk */
        lsx_writes(ft, "FVER");
        lsx_writedw(ft, 4); /* FVER chunk size */
        lsx_writedw(ft, 0xa2805140); /* version_date(May23,1990,2:40pm) */

        /* COMM chunk -- describes encoding (and #frames) */
        lsx_writes(ft, "COMM");
        lsx_writedw(ft, comm_len+comm_padding); /* COMM chunk size */
        lsx_writew(ft, ft->signal.channels); /* nchannels */
        lsx_writedw(ft, (unsigned) nframes); /* number of frames */
        lsx_writew(ft, bits); /* sample width, in bits */
        write_ieee_extended(ft, (double)ft->signal.rate);

        lsx_writes(ft, ctype); /*compression_type*/
        lsx_writeb(ft, cname_len);
        lsx_writes(ft, cname);
        if (comm_padding)
          lsx_writeb(ft, 0);

        /* SSND chunk -- describes data */
        lsx_writes(ft, "SSND");
        /* chunk size */
        lsx_writedw(ft, (unsigned) (8 + nframes * ft->signal.channels * (ft->encoding.bits_per_sample >> 3)));
        lsx_writedw(ft, 0); /* offset */
        lsx_writedw(ft, 0); /* block size */

        /* Any Private chunks shall appear after the required chunks (FORM,FVER,COMM,SSND) */
        return(SOX_SUCCESS);
}

static double read_ieee_extended(sox_format_t * ft)
{
        unsigned char buf[10];
        if (lsx_readbuf(ft, buf, (size_t)10) != 10)
        {
                lsx_fail_errno(ft,SOX_EOF,"EOF while reading IEEE extended number");
                return(SOX_EOF);
        }
        return ConvertFromIeeeExtended(buf);
}

static void write_ieee_extended(sox_format_t * ft, double x)
{
        char buf[10];
        ConvertToIeeeExtended(x, buf);
        lsx_debug_more("converted %g to %o %o %o %o %o %o %o %o %o %o",
                x,
                buf[0], buf[1], buf[2], buf[3], buf[4],
                buf[5], buf[6], buf[7], buf[8], buf[9]);
        (void)lsx_writebuf(ft, buf, (size_t) 10);
}


/*
 * C O N V E R T   T O   I E E E   E X T E N D E D
 */

/* Copyright (C) 1988-1991 Apple Computer, Inc.
 *
 * All rights reserved.
 *
 * Warranty Information
 *  Even though Apple has reviewed this software, Apple makes no warranty
 *  or representation, either express or implied, with respect to this
 *  software, its quality, accuracy, merchantability, or fitness for a
 *  particular purpose.  As a result, this software is provided "as is,"
 *  and you, its user, are assuming the entire risk as to its quality
 *  and accuracy.
 *
 * Machine-independent I/O routines for IEEE floating-point numbers.
 *
 * NaN's and infinities are converted to HUGE_VAL, which
 * happens to be infinity on IEEE machines.  Unfortunately, it is
 * impossible to preserve NaN's in a machine-independent way.
 * Infinities are, however, preserved on IEEE machines.
 *
 * These routines have been tested on the following machines:
 *    Apple Macintosh, MPW 3.1 C compiler
 *    Apple Macintosh, THINK C compiler
 *    Silicon Graphics IRIS, MIPS compiler
 *    Cray X/MP and Y/MP
 *    Digital Equipment VAX
 *
 *
 * Implemented by Malcolm Slaney and Ken Turkowski.
 *
 * Malcolm Slaney contributions during 1988-1990 include big- and little-
 * endian file I/O, conversion to and from Motorola's extended 80-bit
 * floating-point format, and conversions to and from IEEE single-
 * precision floating-point format.
 *
 * In 1991, Ken Turkowski implemented the conversions to and from
 * IEEE double-precision format, added more precision to the extended
 * conversions, and accommodated conversions involving +/- infinity,
 * NaN's, and denormalized numbers.
 */

#define FloatToUnsigned(f) ((uint32_t)(((int32_t)(f - 2147483648.0)) + 2147483647) + 1)

static void ConvertToIeeeExtended(double num, char *bytes)
{
    int    sign;
    int expon;
    double fMant, fsMant;
    uint32_t hiMant, loMant;

    if (num < 0) {
        sign = 0x8000;
        num *= -1;
    } else {
        sign = 0;
    }

    if (num == 0) {
        expon = 0; hiMant = 0; loMant = 0;
    }
    else {
        fMant = frexp(num, &expon);
        if ((expon > 16384) || !(fMant < 1)) {    /* Infinity or NaN */
            expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */
        }
        else {    /* Finite */
            expon += 16382;
            if (expon < 0) {    /* denormalized */
                fMant = ldexp(fMant, expon);
                expon = 0;
            }
            expon |= sign;
            fMant = ldexp(fMant, 32);
            fsMant = floor(fMant);
            hiMant = FloatToUnsigned(fsMant);
            fMant = ldexp(fMant - fsMant, 32);
            fsMant = floor(fMant);
            loMant = FloatToUnsigned(fsMant);
        }
    }

    bytes[0] = expon >> 8;
    bytes[1] = expon;
    bytes[2] = hiMant >> 24;
    bytes[3] = hiMant >> 16;
    bytes[4] = hiMant >> 8;
    bytes[5] = hiMant;
    bytes[6] = loMant >> 24;
    bytes[7] = loMant >> 16;
    bytes[8] = loMant >> 8;
    bytes[9] = loMant;
}


/*
 * C O N V E R T   F R O M   I E E E   E X T E N D E D
 */

/*
 * Copyright (C) 1988-1991 Apple Computer, Inc.
 *
 * All rights reserved.
 *
 * Warranty Information
 *  Even though Apple has reviewed this software, Apple makes no warranty
 *  or representation, either express or implied, with respect to this
 *  software, its quality, accuracy, merchantability, or fitness for a
 *  particular purpose.  As a result, this software is provided "as is,"
 *  and you, its user, are assuming the entire risk as to its quality
 *  and accuracy.
 *
 * This code may be used and freely distributed as long as it includes
 * this copyright notice and the above warranty information.
 *
 * Machine-independent I/O routines for IEEE floating-point numbers.
 *
 * NaN's and infinities are converted to HUGE_VAL, which
 * happens to be infinity on IEEE machines.  Unfortunately, it is
 * impossible to preserve NaN's in a machine-independent way.
 * Infinities are, however, preserved on IEEE machines.
 *
 * These routines have been tested on the following machines:
 *    Apple Macintosh, MPW 3.1 C compiler
 *    Apple Macintosh, THINK C compiler
 *    Silicon Graphics IRIS, MIPS compiler
 *    Cray X/MP and Y/MP
 *    Digital Equipment VAX
 *
 *
 * Implemented by Malcolm Slaney and Ken Turkowski.
 *
 * Malcolm Slaney contributions during 1988-1990 include big- and little-
 * endian file I/O, conversion to and from Motorola's extended 80-bit
 * floating-point format, and conversions to and from IEEE single-
 * precision floating-point format.
 *
 * In 1991, Ken Turkowski implemented the conversions to and from
 * IEEE double-precision format, added more precision to the extended
 * conversions, and accommodated conversions involving +/- infinity,
 * NaN's, and denormalized numbers.
 */

#define UnsignedToFloat(u)         (((double)((int32_t)(u - 2147483647 - 1))) + 2147483648.0)

/****************************************************************
 * Extended precision IEEE floating-point conversion routine.
 ****************************************************************/

static double ConvertFromIeeeExtended(unsigned char *bytes)
{
    double    f;
    int    expon;
    uint32_t hiMant, loMant;

    expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
    hiMant    =    ((uint32_t)(bytes[2] & 0xFF) << 24)
            |    ((uint32_t)(bytes[3] & 0xFF) << 16)
            |    ((uint32_t)(bytes[4] & 0xFF) << 8)
            |    ((uint32_t)(bytes[5] & 0xFF));
    loMant    =    ((uint32_t)(bytes[6] & 0xFF) << 24)
            |    ((uint32_t)(bytes[7] & 0xFF) << 16)
            |    ((uint32_t)(bytes[8] & 0xFF) << 8)
            |    ((uint32_t)(bytes[9] & 0xFF));

    if (expon == 0 && hiMant == 0 && loMant == 0) {
        f = 0;
    }
    else {
        if (expon == 0x7FFF) {    /* Infinity or NaN */
            f = HUGE_VAL;
        }
        else {
            expon -= 16383;
            f  = ldexp(UnsignedToFloat(hiMant), expon-=31);
            f += ldexp(UnsignedToFloat(loMant), expon-=32);
        }
    }

    if (bytes[0] & 0x80)
        return -f;
    else
        return f;
}

debug log:

solving 240d2e1fdf63 ...
found 240d2e1fdf63 in https://80x24.org/mirrors/sox.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/sox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).