src/wav.c: Fix WAV Sampler Chunk tune parsing
[platform/upstream/libsndfile.git] / src / wav.c
index 1d396ad..0f8d8e8 100644 (file)
--- a/src/wav.c
+++ b/src/wav.c
@@ -1,5 +1,5 @@
 /*
-** Copyright (C) 1999-2015 Erik de Castro Lopo <erikd@mega-nerd.com>
+** Copyright (C) 1999-2017 Erik de Castro Lopo <erikd@mega-nerd.com>
 ** Copyright (C) 2004-2005 David Viens <davidv@plogue.com>
 **
 ** This program is free software; you can redistribute it and/or modify
@@ -29,7 +29,7 @@
 #include       "sndfile.h"
 #include       "sfendian.h"
 #include       "common.h"
-#include       "wav_w64.h"
+#include       "wavlike.h"
 
 /*------------------------------------------------------------------------------
  * Macros to handle big/little endian issues.
 #define RIFX_MARKER            (MAKE_MARKER ('R', 'I', 'F', 'X'))
 #define WAVE_MARKER            (MAKE_MARKER ('W', 'A', 'V', 'E'))
 #define fmt_MARKER             (MAKE_MARKER ('f', 'm', 't', ' '))
-#define data_MARKER            (MAKE_MARKER ('d', 'a', 't', 'a'))
 #define fact_MARKER            (MAKE_MARKER ('f', 'a', 'c', 't'))
-#define PEAK_MARKER            (MAKE_MARKER ('P', 'E', 'A', 'K'))
 
 #define cue_MARKER             (MAKE_MARKER ('c', 'u', 'e', ' '))
-#define LIST_MARKER            (MAKE_MARKER ('L', 'I', 'S', 'T'))
 #define slnt_MARKER            (MAKE_MARKER ('s', 'l', 'n', 't'))
 #define wavl_MARKER            (MAKE_MARKER ('w', 'a', 'v', 'l'))
-#define INFO_MARKER            (MAKE_MARKER ('I', 'N', 'F', 'O'))
 #define plst_MARKER            (MAKE_MARKER ('p', 'l', 's', 't'))
-#define adtl_MARKER            (MAKE_MARKER ('a', 'd', 't', 'l'))
-#define labl_MARKER            (MAKE_MARKER ('l', 'a', 'b', 'l'))
-#define ltxt_MARKER            (MAKE_MARKER ('l', 't', 'x', 't'))
-#define note_MARKER            (MAKE_MARKER ('n', 'o', 't', 'e'))
 #define smpl_MARKER            (MAKE_MARKER ('s', 'm', 'p', 'l'))
-#define bext_MARKER            (MAKE_MARKER ('b', 'e', 'x', 't'))
 #define iXML_MARKER            (MAKE_MARKER ('i', 'X', 'M', 'L'))
 #define levl_MARKER            (MAKE_MARKER ('l', 'e', 'v', 'l'))
 #define MEXT_MARKER            (MAKE_MARKER ('M', 'E', 'X', 'T'))
-#define DISP_MARKER            (MAKE_MARKER ('D', 'I', 'S', 'P'))
 #define acid_MARKER            (MAKE_MARKER ('a', 'c', 'i', 'd'))
 #define strc_MARKER            (MAKE_MARKER ('s', 't', 'r', 'c'))
-#define PAD_MARKER             (MAKE_MARKER ('P', 'A', 'D', ' '))
 #define afsp_MARKER            (MAKE_MARKER ('a', 'f', 's', 'p'))
 #define clm_MARKER             (MAKE_MARKER ('c', 'l', 'm', ' '))
 #define elmo_MARKER            (MAKE_MARKER ('e', 'l', 'm', 'o'))
-#define cart_MARKER            (MAKE_MARKER ('c', 'a', 'r', 't'))
 #define FLLR_MARKER            (MAKE_MARKER ('F', 'L', 'L', 'R'))
 
-#define exif_MARKER            (MAKE_MARKER ('e', 'x', 'i', 'f'))
-#define ever_MARKER            (MAKE_MARKER ('e', 'v', 'e', 'r'))
-#define etim_MARKER            (MAKE_MARKER ('e', 't', 'i', 'm'))
-#define ecor_MARKER            (MAKE_MARKER ('e', 'c', 'o', 'r'))
-#define emdl_MARKER            (MAKE_MARKER ('e', 'm', 'd', 'l'))
-#define emnt_MARKER            (MAKE_MARKER ('e', 'm', 'n', 't'))
-#define erel_MARKER            (MAKE_MARKER ('e', 'r', 'e', 'l'))
-#define eucm_MARKER            (MAKE_MARKER ('e', 'u', 'c', 'm'))
-#define olym_MARKER            (MAKE_MARKER ('o', 'l', 'y', 'm'))
 #define minf_MARKER            (MAKE_MARKER ('m', 'i', 'n', 'f'))
 #define elm1_MARKER            (MAKE_MARKER ('e', 'l', 'm', '1'))
 #define regn_MARKER            (MAKE_MARKER ('r', 'e', 'g', 'n'))
 #define AFAn_MARKER            (MAKE_MARKER ('A', 'F', 'A', 'n'))
 
 
-#define ISFT_MARKER            (MAKE_MARKER ('I', 'S', 'F', 'T'))
-#define ICRD_MARKER            (MAKE_MARKER ('I', 'C', 'R', 'D'))
-#define ICOP_MARKER            (MAKE_MARKER ('I', 'C', 'O', 'P'))
-#define IARL_MARKER            (MAKE_MARKER ('I', 'A', 'R', 'L'))
-#define IART_MARKER            (MAKE_MARKER ('I', 'A', 'R', 'T'))
-#define INAM_MARKER            (MAKE_MARKER ('I', 'N', 'A', 'M'))
-#define IENG_MARKER            (MAKE_MARKER ('I', 'E', 'N', 'G'))
-#define IGNR_MARKER            (MAKE_MARKER ('I', 'G', 'N', 'R'))
-#define ICOP_MARKER            (MAKE_MARKER ('I', 'C', 'O', 'P'))
-#define IPRD_MARKER            (MAKE_MARKER ('I', 'P', 'R', 'D'))
-#define ISRC_MARKER            (MAKE_MARKER ('I', 'S', 'R', 'C'))
-#define ISBJ_MARKER            (MAKE_MARKER ('I', 'S', 'B', 'J'))
-#define ICMT_MARKER            (MAKE_MARKER ('I', 'C', 'M', 'T'))
-#define IAUT_MARKER            (MAKE_MARKER ('I', 'A', 'U', 'T'))
-#define ITRK_MARKER            (MAKE_MARKER ('I', 'T', 'R', 'K'))
-
 /* Weird WAVPACK marker which can show up at the start of the DATA section. */
 #define wvpk_MARKER (MAKE_MARKER ('w', 'v', 'p', 'k'))
 #define OggS_MARKER (MAKE_MARKER ('O', 'g', 'g', 'S'))
 
-#define WAV_PEAK_CHUNK_SIZE(ch)        (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
-#define WAV_BEXT_MIN_CHUNK_SIZE                602
-#define WAV_BEXT_MAX_CHUNK_SIZE                (10 * 1024)
-
-#define WAV_CART_MIN_CHUNK_SIZE                2048
-#define WAV_CART_MAX_CHUNK_SIZE                0xffffffff
+#define WAVLIKE_PEAK_CHUNK_SIZE(ch)    (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
 
 
 enum
@@ -135,9 +93,11 @@ static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
 {      0x00000001, 0x0000, 0x0010, {   0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
 } ;
 
+#if 0
 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
 {      0x00000002, 0x0000, 0x0010, {   0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
 } ;
+#endif
 
 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
 {      0x00000003, 0x0000, 0x0010, {   0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
@@ -180,12 +140,9 @@ static int wav_read_header         (SF_PRIVATE *psf, int *blockalign, int *framesperblo
 static int     wav_write_header        (SF_PRIVATE *psf, int calc_length) ;
 
 static int     wav_write_tailer (SF_PRIVATE *psf) ;
-static void wav_write_strings (SF_PRIVATE *psf, int location) ;
 static int     wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
 static int     wav_close (SF_PRIVATE *psf) ;
 
-static int     wav_subchunk_parse      (SF_PRIVATE *psf, int chunk, uint32_t length) ;
-static int     exif_subchunk_parse     (SF_PRIVATE *psf, uint32_t length) ;
 static int     wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
 static int     wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
 
@@ -200,10 +157,10 @@ static int wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterat
 
 int
 wav_open       (SF_PRIVATE *psf)
-{      WAV_PRIVATE * wpriv ;
+{      WAVLIKE_PRIVATE * wpriv ;
        int     format, subformat, error, blockalign = 0, framesperblock = 0 ;
 
-       if ((wpriv = calloc (1, sizeof (WAV_PRIVATE))) == NULL)
+       if ((wpriv = calloc (1, sizeof (WAVLIKE_PRIVATE))) == NULL)
                return SFE_MALLOC_FAILED ;
        psf->container_data = wpriv ;
 
@@ -248,7 +205,7 @@ wav_open    (SF_PRIVATE *psf)
                        } ;
 
                if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
-               {       blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
+               {       blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
                        framesperblock = -1 ; /* Corrected later. */
                        } ;
 
@@ -294,22 +251,15 @@ wav_open  (SF_PRIVATE *psf)
                                        break ;
 
                case SF_FORMAT_IMA_ADPCM :
-                                       error = wav_w64_ima_init (psf, blockalign, framesperblock) ;
+                                       error = wavlike_ima_init (psf, blockalign, framesperblock) ;
                                        break ;
 
                case SF_FORMAT_MS_ADPCM :
-                                       error = wav_w64_msadpcm_init (psf, blockalign, framesperblock) ;
+                                       error = wavlike_msadpcm_init (psf, blockalign, framesperblock) ;
                                        break ;
 
-               case SF_FORMAT_G721_32 :
-                                       error = g72x_init (psf) ;
-                                       break ;
                /* Lite remove end */
 
-               case SF_FORMAT_GSM610 :
-                                       error = gsm610_init (psf) ;
-                                       break ;
-
                default :       return SFE_UNIMPLEMENTED ;
                } ;
 
@@ -325,12 +275,11 @@ wav_open  (SF_PRIVATE *psf)
 
 static int
 wav_read_header        (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
-{      WAV_PRIVATE     *wpriv ;
+{      WAVLIKE_PRIVATE *wpriv ;
        WAV_FMT         *wav_fmt ;
        FACT_CHUNK      fact_chunk ;
-       uint32_t        marker, chunk_size = 0, RIFFsize = 0, done = 0, uk ;
+       uint32_t        marker, chunk_size = 0, RIFFsize = 0, done = 0 ;
        int                     parsestage = 0, error, format = 0 ;
-       char            buffer [256] ;
 
        if (psf->is_pipe == 0 && psf->filelength > SF_PLATFORM_S64 (0xffffffff))
                psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
@@ -341,7 +290,7 @@ wav_read_header     (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
 
        /* Set position to start of file to begin reading header. */
        psf_binheader_readf (psf, "pmj", 0, &marker, -4) ;
-       psf->headindex = 0 ;
+       psf->header.indx = 0 ;
 
        /* RIFX signifies big-endian format for all header and data  to prevent
        ** lots of code copying here, we'll set the psf->rwf_endian flag once here,
@@ -415,7 +364,7 @@ wav_read_header     (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
 
                                        psf_log_printf (psf, "fmt  : %d\n", chunk_size) ;
 
-                                       if ((error = wav_w64_read_fmt_chunk (psf, chunk_size)))
+                                       if ((error = wavlike_read_fmt_chunk (psf, chunk_size)))
                                                return error ;
 
                                        format = wav_fmt->format ;
@@ -496,53 +445,21 @@ wav_read_header   (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
                                        parsestage |= HAVE_PEAK ;
 
                                        psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
-                                       if (chunk_size != WAV_PEAK_CHUNK_SIZE (psf->sf.channels))
-                                       {       psf_binheader_readf (psf, "j", chunk_size) ;
-                                               psf_log_printf (psf, "*** File PEAK chunk size doesn't fit with number of channels (%d).\n", psf->sf.channels) ;
-                                               return SFE_WAV_BAD_PEAK ;
-                                               } ;
-
-                                       if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
-                                               return SFE_MALLOC_FAILED ;
-
-                                       /* read in rest of PEAK chunk. */
-                                       psf_binheader_readf (psf, "44", & (psf->peak_info->version), & (psf->peak_info->timestamp)) ;
-
-                                       if (psf->peak_info->version != 1)
-                                               psf_log_printf (psf, "  version    : %d *** (should be version 1)\n", psf->peak_info->version) ;
-                                       else
-                                               psf_log_printf (psf, "  version    : %d\n", psf->peak_info->version) ;
-
-                                       psf_log_printf (psf, "  time stamp : %d\n", psf->peak_info->timestamp) ;
-                                       psf_log_printf (psf, "    Ch   Position       Value\n") ;
-
-                                       for (uk = 0 ; uk < (uint32_t) psf->sf.channels ; uk++)
-                                       {       float value ;
-                                               uint32_t position ;
-
-                                               psf_binheader_readf (psf, "f4", &value, &position) ;
-                                               psf->peak_info->peaks [uk].value = value ;
-                                               psf->peak_info->peaks [uk].position = position ;
-
-                                               snprintf (buffer, sizeof (buffer), "    %2d   %-12" PRId64 "   %g\n",
-                                                               uk, psf->peak_info->peaks [uk].position, psf->peak_info->peaks [uk].value) ;
-                                               buffer [sizeof (buffer) - 1] = 0 ;
-                                               psf_log_printf (psf, "%s", buffer) ;
-                                               } ;
-
+                                       if ((error = wavlike_read_peak_chunk (psf, chunk_size)) != 0)
+                                               return error ;
                                        psf->peak_info->peak_loc = ((parsestage & HAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
                                        break ;
 
                        case cue_MARKER :
                                        parsestage |= HAVE_other ;
 
-                                       {       uint32_t thisread, bytesread, cue_count ;
-                                               int id, position, chunk_id, chunk_start, block_start, offset ;
+                                       {       uint32_t thisread, bytesread, cue_count, position, offset ;
+                                               int id, chunk_id, chunk_start, block_start, cue_index ;
 
                                                bytesread = psf_binheader_readf (psf, "4", &cue_count) ;
                                                psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
 
-                                               if (cue_count > 10)
+                                               if (cue_count > 1000)
                                                {       psf_log_printf (psf, "  Count : %u (skipping)\n", cue_count) ;
                                                        psf_binheader_readf (psf, "j", (cue_count > 20 ? 20 : cue_count) * 24) ;
                                                        break ;
@@ -550,9 +467,14 @@ wav_read_header    (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
 
                                                psf_log_printf (psf, "  Count : %d\n", cue_count) ;
 
+                                               if ((psf->cues = psf_cues_alloc (cue_count)) == NULL)
+                                                       return SFE_MALLOC_FAILED ;
+
+                                               cue_index = 0 ;
+
                                                while (cue_count)
                                                {
-                                                       if ((thisread = psf_binheader_readf (psf, "444444", &id, &position, &chunk_id, &chunk_start, &block_start, &offset)) == 0)
+                                                       if ((thisread = psf_binheader_readf (psf, "e44m444", &id, &position, &chunk_id, &chunk_start, &block_start, &offset)) == 0)
                                                                break ;
                                                        bytesread += thisread ;
 
@@ -561,7 +483,15 @@ wav_read_header    (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
                                                                                                        "  Chk Start : %d  Blk Start : %d"
                                                                                                        "  Offset : %5d\n",
                                                                        id, position, chunk_id, chunk_start, block_start, offset) ;
+                                                       psf->cues->cue_points [cue_index].indx = id ;
+                                                       psf->cues->cue_points [cue_index].position = position ;
+                                                       psf->cues->cue_points [cue_index].fcc_chunk = chunk_id ;
+                                                       psf->cues->cue_points [cue_index].chunk_start = chunk_start ;
+                                                       psf->cues->cue_points [cue_index].block_start = block_start ;
+                                                       psf->cues->cue_points [cue_index].sample_offset = offset ;
+                                                       psf->cues->cue_points [cue_index].name [0] = '\0' ;
                                                        cue_count -- ;
+                                                       cue_index ++ ;
                                                        } ;
 
                                                if (bytesread != chunk_size)
@@ -593,7 +523,7 @@ wav_read_header     (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
                        case LIST_MARKER :
                                        parsestage |= HAVE_other ;
 
-                                       if ((error = wav_subchunk_parse (psf, marker, chunk_size)) != 0)
+                                       if ((error = wavlike_subchunk_parse (psf, marker, chunk_size)) != 0)
                                                return error ;
                                        break ;
 
@@ -602,7 +532,7 @@ wav_read_header     (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
                                        The 'bext' chunk can actually be updated, so don't need to set this.
                                        parsestage |= HAVE_other ;
                                        */
-                                       if ((error = wav_read_bext_chunk (psf, chunk_size)))
+                                       if ((error = wavlike_read_bext_chunk (psf, chunk_size)))
                                                return error ;
                                        break ;
 
@@ -616,7 +546,7 @@ wav_read_header     (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
                                        break ;
 
                        case cart_MARKER:
-                                       if ((error = wav_read_cart_chunk (psf, chunk_size)))
+                                       if ((error = wavlike_read_cart_chunk (psf, chunk_size)))
                                                return error ;
                                        break ;
 
@@ -772,7 +702,7 @@ wav_read_header     (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
                } ;
 
        if (wpriv->fmt_is_broken)
-               wav_w64_analyze (psf) ;
+               wavlike_analyze (psf) ;
 
        /* Only set the format endian-ness if its non-standard big-endian. */
        if (psf->endian == SF_ENDIAN_BIG)
@@ -795,11 +725,11 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                                        fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
 
                                        /* fmt : format, channels, samplerate */
-                                       psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
+                                       psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_PCM), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
                                        /*  fmt : bytespersec */
-                                       psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
+                                       psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
                                        /*  fmt : blockalign, bitwidth */
-                                       psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
+                                       psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
                                        break ;
 
                case SF_FORMAT_FLOAT :
@@ -807,11 +737,11 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                                        fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
 
                                        /* fmt : format, channels, samplerate */
-                                       psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
+                                       psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_IEEE_FLOAT), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
                                        /*  fmt : bytespersec */
-                                       psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
+                                       psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
                                        /*  fmt : blockalign, bitwidth */
-                                       psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
+                                       psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
 
                                        add_fact_chunk = SF_TRUE ;
                                        break ;
@@ -820,11 +750,11 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                                        fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
 
                                        /* fmt : format, channels, samplerate */
-                                       psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
+                                       psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MULAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
                                        /*  fmt : bytespersec */
-                                       psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
+                                       psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
                                        /*  fmt : blockalign, bitwidth, extrabytes */
-                                       psf_binheader_writef (psf, "222", psf->bytewidth * psf->sf.channels, 8, 0) ;
+                                       psf_binheader_writef (psf, "222", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8), BHW2 (0)) ;
 
                                        add_fact_chunk = SF_TRUE ;
                                        break ;
@@ -833,11 +763,11 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                                        fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
 
                                        /* fmt : format, channels, samplerate */
-                                       psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
+                                       psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_ALAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
                                        /*  fmt : bytespersec */
-                                       psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
+                                       psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
                                        /*  fmt : blockalign, bitwidth, extrabytes */
-                                       psf_binheader_writef (psf, "222", psf->bytewidth * psf->sf.channels, 8, 0) ;
+                                       psf_binheader_writef (psf, "222", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8), BHW2 (0)) ;
 
                                        add_fact_chunk = SF_TRUE ;
                                        break ;
@@ -846,7 +776,7 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                case SF_FORMAT_IMA_ADPCM :
                                        {       int blockalign, framesperblock, bytespersec ;
 
-                                               blockalign              = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
+                                               blockalign              = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
                                                framesperblock  = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
                                                bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
 
@@ -854,11 +784,11 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                                                fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
 
                                                /* fmt : size, WAV format type, channels, samplerate, bytespersec */
-                                               psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
-                                                                       psf->sf.channels, psf->sf.samplerate, bytespersec) ;
+                                               psf_binheader_writef (psf, "42244", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_IMA_ADPCM),
+                                                                       BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
 
                                                /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
-                                               psf_binheader_writef (psf, "2222", blockalign, 4, 2, framesperblock) ;
+                                               psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (4), BHW2 (2), BHW2 (framesperblock)) ;
                                                } ;
 
                                        add_fact_chunk = SF_TRUE ;
@@ -867,24 +797,24 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                case SF_FORMAT_MS_ADPCM :
                                        {       int     blockalign, framesperblock, bytespersec, extrabytes ;
 
-                                               blockalign              = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
+                                               blockalign              = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
                                                framesperblock  = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
                                                bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
 
                                                /* fmt chunk. */
-                                               extrabytes      = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
+                                               extrabytes      = 2 + 2 + WAVLIKE_MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
                                                fmt_size        = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
 
                                                /* fmt : size, WAV format type, channels. */
-                                               psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
+                                               psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MS_ADPCM), BHW2 (psf->sf.channels)) ;
 
                                                /* fmt : samplerate, bytespersec. */
-                                               psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
+                                               psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
 
                                                /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
-                                               psf_binheader_writef (psf, "22222", blockalign, 4, extrabytes, framesperblock, 7) ;
+                                               psf_binheader_writef (psf, "22222", BHW2 (blockalign), BHW2 (4), BHW2 (extrabytes), BHW2 (framesperblock), BHW2 (7)) ;
 
-                                               msadpcm_write_adapt_coeffs (psf) ;
+                                               wavlike_msadpcm_write_adapt_coeffs (psf) ;
                                                } ;
 
                                        add_fact_chunk = SF_TRUE ;
@@ -896,11 +826,11 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                                        fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
 
                                        /* fmt : size, WAV format type, channels, samplerate, bytespersec */
-                                       psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_G721_ADPCM,
-                                                               psf->sf.channels, psf->sf.samplerate, psf->sf.samplerate * psf->sf.channels / 2) ;
+                                       psf_binheader_writef (psf, "42244", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_G721_ADPCM),
+                                                               BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate), BHW4 (psf->sf.samplerate * psf->sf.channels / 2)) ;
 
                                        /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
-                                       psf_binheader_writef (psf, "2222", 64, 4, 2, 0) ;
+                                       psf_binheader_writef (psf, "2222", BHW2 (64), BHW2 (4), BHW2 (2), BHW2 (0)) ;
 
                                        add_fact_chunk = SF_TRUE ;
                                        break ;
@@ -910,21 +840,21 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                case SF_FORMAT_GSM610 :
                                        {       int     blockalign, framesperblock, bytespersec ;
 
-                                               blockalign              = WAV_W64_GSM610_BLOCKSIZE ;
-                                               framesperblock  = WAV_W64_GSM610_SAMPLES ;
+                                               blockalign              = WAVLIKE_GSM610_BLOCKSIZE ;
+                                               framesperblock  = WAVLIKE_GSM610_SAMPLES ;
                                                bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
 
                                                /* fmt chunk. */
                                                fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
 
                                                /* fmt : size, WAV format type, channels. */
-                                               psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
+                                               psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_GSM610), BHW2 (psf->sf.channels)) ;
 
                                                /* fmt : samplerate, bytespersec. */
-                                               psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
+                                               psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
 
                                                /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
-                                               psf_binheader_writef (psf, "2222", blockalign, 0, 2, framesperblock) ;
+                                               psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (0), BHW2 (2), BHW2 (framesperblock)) ;
                                                } ;
 
                                        add_fact_chunk = SF_TRUE ;
@@ -934,14 +864,14 @@ wav_write_fmt_chunk (SF_PRIVATE *psf)
                } ;
 
        if (add_fact_chunk)
-               psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
+               psf_binheader_writef (psf, "tm48", BHWm (fact_MARKER), BHW4 (4), BHW8 (psf->sf.frames)) ;
 
        return 0 ;
 } /* wav_write_fmt_chunk */
 
 static int
 wavex_write_fmt_chunk (SF_PRIVATE *psf)
-{      WAV_PRIVATE     *wpriv ;
+{      WAVLIKE_PRIVATE *wpriv ;
        int subformat, fmt_size ;
 
        if ((wpriv = psf->container_data) == NULL)
@@ -962,25 +892,25 @@ wavex_write_fmt_chunk (SF_PRIVATE *psf)
                        fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
 
                        /* fmt : format, channels, samplerate */
-                       psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
+                       psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_EXTENSIBLE), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
                        /*  fmt : bytespersec */
-                       psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
+                       psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
                        /*  fmt : blockalign, bitwidth */
-                       psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
+                       psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
 
                        /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
-                       psf_binheader_writef (psf, "2", 22) ;
+                       psf_binheader_writef (psf, "2", BHW2 (22)) ;
 
                        /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
-                       psf_binheader_writef (psf, "2", psf->bytewidth * 8) ;
+                       psf_binheader_writef (psf, "2", BHW2 (psf->bytewidth * 8)) ;
 
                        /* For an Ambisonic file set the channel mask to zero.
                        ** Otherwise use a default based on the channel count.
                        */
                        if (wpriv->wavex_ambisonic != SF_AMBISONIC_NONE)
-                               psf_binheader_writef (psf, "4", 0) ;
+                               psf_binheader_writef (psf, "4", BHW4 (0)) ;
                        else if (wpriv->wavex_channelmask != 0)
-                               psf_binheader_writef (psf, "4", wpriv->wavex_channelmask) ;
+                               psf_binheader_writef (psf, "4", BHW4 (wpriv->wavex_channelmask)) ;
                        else
                        {       /*
                                ** Ok some liberty is taken here to use the most commonly used channel masks
@@ -989,27 +919,27 @@ wavex_write_fmt_chunk (SF_PRIVATE *psf)
                                */
                                switch (psf->sf.channels)
                                {       case 1 :        /* center channel mono */
-                                               psf_binheader_writef (psf, "4", 0x4) ;
+                                               psf_binheader_writef (psf, "4", BHW4 (0x4)) ;
                                                break ;
 
                                        case 2 :        /* front left and right */
-                                               psf_binheader_writef (psf, "4", 0x1 | 0x2) ;
+                                               psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2)) ;
                                                break ;
 
                                        case 4 :        /* Quad */
-                                               psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
+                                               psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x10 | 0x20)) ;
                                                break ;
 
                                        case 6 :        /* 5.1 */
-                                               psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
+                                               psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)) ;
                                                break ;
 
                                        case 8 :        /* 7.1 */
-                                               psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
+                                               psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80)) ;
                                                break ;
 
                                        default :       /* 0 when in doubt , use direct out, ie NO mapping*/
-                                               psf_binheader_writef (psf, "4", 0x0) ;
+                                               psf_binheader_writef (psf, "4", BHW4 (0x0)) ;
                                                break ;
                                        } ;
                                } ;
@@ -1027,28 +957,28 @@ wavex_write_fmt_chunk (SF_PRIVATE *psf)
                case SF_FORMAT_PCM_16 :
                case SF_FORMAT_PCM_24 :
                case SF_FORMAT_PCM_32 :
-                       wavex_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
+                       wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
                                                &MSGUID_SUBTYPE_PCM : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM) ;
                        break ;
 
                case SF_FORMAT_FLOAT :
                case SF_FORMAT_DOUBLE :
-                       wavex_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
+                       wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
                                                &MSGUID_SUBTYPE_IEEE_FLOAT : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT) ;
                        break ;
 
                case SF_FORMAT_ULAW :
-                       wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
+                       wavlike_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
                        break ;
 
                case SF_FORMAT_ALAW :
-                       wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
+                       wavlike_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
                        break ;
 
 #if 0
                /* This is dead code due to return in previous switch statement. */
                case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
-                       wavex_write_guid (psf, &MSGUID_SUBTYPE_MS_ADPCM) ;
+                       wavlike_write_guid (psf, &MSGUID_SUBTYPE_MS_ADPCM) ;
                        break ;
                        return SFE_UNIMPLEMENTED ;
 #endif
@@ -1056,7 +986,7 @@ wavex_write_fmt_chunk (SF_PRIVATE *psf)
                default : return SFE_UNIMPLEMENTED ;
                } ;
 
-       psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
+       psf_binheader_writef (psf, "tm48", BHWm (fact_MARKER), BHW4 (4), BHW8 (psf->sf.frames)) ;
 
        return 0 ;
 } /* wavex_write_fmt_chunk */
@@ -1065,8 +995,7 @@ wavex_write_fmt_chunk (SF_PRIVATE *psf)
 static int
 wav_write_header (SF_PRIVATE *psf, int calc_length)
 {      sf_count_t      current ;
-       uint32_t        uk ;
-       int             k, error, has_data = SF_FALSE ;
+       int             error, has_data = SF_FALSE ;
 
        current = psf_ftell (psf) ;
 
@@ -1085,8 +1014,8 @@ wav_write_header (SF_PRIVATE *psf, int calc_length)
                } ;
 
        /* Reset the current header length to zero. */
-       psf->header [0] = 0 ;
-       psf->headindex = 0 ;
+       psf->header.ptr [0] = 0 ;
+       psf->header.indx = 0 ;
        psf_fseek (psf, 0, SEEK_SET) ;
 
        /*
@@ -1098,12 +1027,12 @@ wav_write_header (SF_PRIVATE *psf, int calc_length)
        /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
 
        if (psf->endian == SF_ENDIAN_LITTLE)
-               psf_binheader_writef (psf, "etm8", RIFF_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
+               psf_binheader_writef (psf, "etm8", BHWm (RIFF_MARKER), BHW8 ((psf->filelength < 8) ? 8 : psf->filelength - 8)) ;
        else
-               psf_binheader_writef (psf, "Etm8", RIFX_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
+               psf_binheader_writef (psf, "Etm8", BHWm (RIFX_MARKER), BHW8 ((psf->filelength < 8) ? 8 : psf->filelength - 8)) ;
 
        /* WAVE and 'fmt ' markers. */
-       psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
+       psf_binheader_writef (psf, "mm", BHWm (WAVE_MARKER), BHWm (fmt_MARKER)) ;
 
        /* Write the 'fmt ' chunk. */
        switch (SF_CONTAINER (psf->sf.format))
@@ -1123,33 +1052,40 @@ wav_write_header (SF_PRIVATE *psf, int calc_length)
 
        /* The LIST/INFO chunk. */
        if (psf->strings.flags & SF_STR_LOCATE_START)
-               wav_write_strings (psf, SF_STR_LOCATE_START) ;
+               wavlike_write_strings (psf, SF_STR_LOCATE_START) ;
 
        if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
-       {       psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
-               psf_binheader_writef (psf, "44", 1, time (NULL)) ;
-               for (k = 0 ; k < psf->sf.channels ; k++)
-                       psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
-               } ;
+               wavlike_write_peak_chunk (psf) ;
 
        if (psf->broadcast_16k != NULL)
-               wav_write_bext_chunk (psf) ;
+               wavlike_write_bext_chunk (psf) ;
 
        if (psf->cart_16k != NULL)
-               wav_write_cart_chunk (psf) ;
+               wavlike_write_cart_chunk (psf) ;
+
+       if (psf->cues != NULL)
+       {       uint32_t k ;
+
+               psf_binheader_writef (psf, "em44", BHWm (cue_MARKER), BHW4 (4 + psf->cues->cue_count * 6 * 4), BHW4 (psf->cues->cue_count)) ;
+
+               for (k = 0 ; k < psf->cues->cue_count ; k++)
+                       psf_binheader_writef (psf, "e44m444", BHW4 (psf->cues->cue_points [k].indx), BHW4 (psf->cues->cue_points [k].position),
+                                               BHWm (psf->cues->cue_points [k].fcc_chunk), BHW4 (psf->cues->cue_points [k].chunk_start),
+                                               BHW4 (psf->cues->cue_points [k].block_start), BHW4 (psf->cues->cue_points [k].sample_offset)) ;
+               } ;
 
        if (psf->instrument != NULL)
        {       int             tmp ;
                double  dtune = (double) (0x40000000) / 25.0 ;
 
-               psf_binheader_writef (psf, "m4", smpl_MARKER, 9 * 4 + psf->instrument->loop_count * 6 * 4) ;
-               psf_binheader_writef (psf, "44", 0, 0) ; /* Manufacturer zero is everyone */
+               psf_binheader_writef (psf, "m4", BHWm (smpl_MARKER), BHW4 (9 * 4 + psf->instrument->loop_count * 6 * 4)) ;
+               psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (0)) ; /* Manufacturer zero is everyone */
                tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
-               psf_binheader_writef (psf, "44", tmp, psf->instrument->basenote) ;
+               psf_binheader_writef (psf, "44", BHW4 (tmp), BHW4 (psf->instrument->basenote)) ;
                tmp = (uint32_t) (psf->instrument->detune * dtune + 0.5) ;
-               psf_binheader_writef (psf, "4", tmp) ;
-               psf_binheader_writef (psf, "44", 0, 0) ; /* SMTPE format */
-               psf_binheader_writef (psf, "44", psf->instrument->loop_count, 0) ;
+               psf_binheader_writef (psf, "4", BHW4 (tmp)) ;
+               psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (0)) ; /* SMTPE format */
+               psf_binheader_writef (psf, "44", BHW4 (psf->instrument->loop_count), BHW4 (0)) ;
 
                for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
                {       int type ;
@@ -1157,33 +1093,33 @@ wav_write_header (SF_PRIVATE *psf, int calc_length)
                        type = psf->instrument->loops [tmp].mode ;
                        type = (type == SF_LOOP_FORWARD ? 0 : type == SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
 
-                       psf_binheader_writef (psf, "44", tmp, type) ;
-                       psf_binheader_writef (psf, "44", psf->instrument->loops [tmp].start, psf->instrument->loops [tmp].end - 1) ;
-                       psf_binheader_writef (psf, "44", 0, psf->instrument->loops [tmp].count) ;
+                       psf_binheader_writef (psf, "44", BHW4 (tmp), BHW4 (type)) ;
+                       psf_binheader_writef (psf, "44", BHW4 (psf->instrument->loops [tmp].start), BHW4 (psf->instrument->loops [tmp].end - 1)) ;
+                       psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (psf->instrument->loops [tmp].count)) ;
                        } ;
                } ;
 
        /* Write custom headers. */
-       for (uk = 0 ; uk < psf->wchunks.used ; uk++)
-               psf_binheader_writef (psf, "m4b", (int) psf->wchunks.chunks [uk].mark32, psf->wchunks.chunks [uk].len, psf->wchunks.chunks [uk].data, make_size_t (psf->wchunks.chunks [uk].len)) ;
+       if (psf->wchunks.used > 0)
+               wavlike_write_custom_chunks (psf) ;
 
-       if (psf->headindex + 16 < psf->dataoffset)
+       if (psf->header.indx + 16 < psf->dataoffset)
        {       /* Add PAD data if necessary. */
-               k = psf->dataoffset - (psf->headindex + 16) ;
-               psf_binheader_writef (psf, "m4z", PAD_MARKER, k, make_size_t (k)) ;
+               size_t k = psf->dataoffset - (psf->header.indx + 16) ;
+               psf_binheader_writef (psf, "m4z", BHWm (PAD_MARKER), BHW4 (k), BHWz (k)) ;
                } ;
 
-       psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
-       psf_fwrite (psf->header, psf->headindex, 1, psf) ;
+       psf_binheader_writef (psf, "tm8", BHWm (data_MARKER), BHW8 (psf->datalength)) ;
+       psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
        if (psf->error)
                return psf->error ;
 
-       if (has_data && psf->dataoffset != psf->headindex)
-       {       psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->headindex\n") ;
+       if (has_data && psf->dataoffset != psf->header.indx)
+       {       psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->header.indx\n") ;
                return psf->error = SFE_INTERNAL ;
                } ;
 
-       psf->dataoffset = psf->headindex ;
+       psf->dataoffset = psf->header.indx ;
 
        if (! has_data)
                psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
@@ -1196,11 +1132,10 @@ wav_write_header (SF_PRIVATE *psf, int calc_length)
 
 static int
 wav_write_tailer (SF_PRIVATE *psf)
-{      int             k ;
-
+{
        /* Reset the current header buffer length to zero. */
-       psf->header [0] = 0 ;
-       psf->headindex = 0 ;
+       psf->header.ptr [0] = 0 ;
+       psf->header.indx = 0 ;
 
        if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
        {       psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
@@ -1213,92 +1148,22 @@ wav_write_tailer (SF_PRIVATE *psf)
                psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
 
        if (psf->dataend & 1)
-               psf_binheader_writef (psf, "z", 1) ;
+               psf_binheader_writef (psf, "z", BHWz (1)) ;
 
        /* Add a PEAK chunk if requested. */
        if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
-       {       psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
-               psf_binheader_writef (psf, "44", 1, time (NULL)) ;
-               for (k = 0 ; k < psf->sf.channels ; k++)
-                       psf_binheader_writef (psf, "f4", psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
-               } ;
+               wavlike_write_peak_chunk (psf) ;
 
        if (psf->strings.flags & SF_STR_LOCATE_END)
-               wav_write_strings (psf, SF_STR_LOCATE_END) ;
+               wavlike_write_strings (psf, SF_STR_LOCATE_END) ;
 
        /* Write the tailer. */
-       if (psf->headindex > 0)
-               psf_fwrite (psf->header, psf->headindex, 1, psf) ;
+       if (psf->header.indx > 0)
+               psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
 
        return 0 ;
 } /* wav_write_tailer */
 
-static void
-wav_write_strings (SF_PRIVATE *psf, int location)
-{      int     k, prev_head_index, saved_head_index ;
-
-       if (psf_location_string_count (psf, location) == 0)
-               return ;
-
-       prev_head_index = psf->headindex + 4 ;
-
-       psf_binheader_writef (psf, "m4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
-
-       for (k = 0 ; k < SF_MAX_STRINGS ; k++)
-       {       if (psf->strings.data [k].type == 0)
-                       break ;
-               if (psf->strings.data [k].type < 0 || psf->strings.data [k].flags != location)
-                       continue ;
-
-               switch (psf->strings.data [k].type)
-               {       case SF_STR_SOFTWARE :
-                               psf_binheader_writef (psf, "ms", ISFT_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       case SF_STR_TITLE :
-                               psf_binheader_writef (psf, "ms", INAM_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       case SF_STR_COPYRIGHT :
-                               psf_binheader_writef (psf, "ms", ICOP_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       case SF_STR_ARTIST :
-                               psf_binheader_writef (psf, "ms", IART_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       case SF_STR_COMMENT :
-                               psf_binheader_writef (psf, "ms", ICMT_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       case SF_STR_DATE :
-                               psf_binheader_writef (psf, "ms", ICRD_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       case SF_STR_GENRE :
-                               psf_binheader_writef (psf, "ms", IGNR_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       case SF_STR_ALBUM :
-                               psf_binheader_writef (psf, "ms", IPRD_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       case SF_STR_TRACKNUMBER :
-                               psf_binheader_writef (psf, "ms", ITRK_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
-                               break ;
-
-                       default :
-                               break ;
-                       } ;
-               } ;
-
-       saved_head_index = psf->headindex ;
-       psf->headindex = prev_head_index ;
-       psf_binheader_writef (psf, "4", saved_head_index - prev_head_index - 4) ;
-       psf->headindex = saved_head_index ;
-
-} /* wav_write_strings */
-
 static int
 wav_close (SF_PRIVATE *psf)
 {
@@ -1327,7 +1192,7 @@ wav_close (SF_PRIVATE *psf)
 
 static int
 wav_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
-{      WAV_PRIVATE     *wpriv ;
+{      WAVLIKE_PRIVATE *wpriv ;
 
        if ((wpriv = psf->container_data) == NULL)
                return SFE_INTERNAL ;
@@ -1348,7 +1213,7 @@ wav_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
                        return wpriv->wavex_ambisonic ;
 
                case SFC_SET_CHANNEL_MAP_INFO :
-                       wpriv->wavex_channelmask = wavex_gen_channel_mask (psf->channel_map, psf->sf.channels) ;
+                       wpriv->wavex_channelmask = wavlike_gen_channel_mask (psf->channel_map, psf->sf.channels) ;
                        return (wpriv->wavex_channelmask != 0) ;
 
                default :
@@ -1359,198 +1224,10 @@ wav_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
 } /* wav_command */
 
 static int
-wav_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length)
-{      sf_count_t      current_pos ;
-       char            buffer [512] ;
-       uint32_t        chunk_size, bytesread ;
-
-       current_pos = psf_fseek (psf, 0, SEEK_CUR) - 4 ;
-
-       bytesread = sizeof (chunk_length) ;
-
-       if (chunk_length <= 8)
-       {       /* This case is for broken files generated by PEAK. */
-               psf_log_printf (psf, "%M : %u (weird length)\n", chunk, chunk_length) ;
-               psf_binheader_readf (psf, "mj", &chunk, chunk_length - 4) ;
-               psf_log_printf (psf, "  %M\n", chunk) ;
-               return 0 ;
-               } ;
-
-       if (psf->headindex + chunk_length > SIGNED_SIZEOF (psf->header))
-       {       psf_log_printf (psf, "%M : %u (too long)\n", chunk, chunk_length) ;
-               psf_binheader_readf (psf, "j", chunk_length) ;
-               return 0 ;
-               } ;
-
-       if (current_pos + chunk_length > psf->filelength)
-       {       psf_log_printf (psf, "%M : %u (should be %d)\n", chunk, chunk_length, (int) (psf->filelength - current_pos)) ;
-               chunk_length = psf->filelength - current_pos ;
-               }
-       else
-               psf_log_printf (psf, "%M : %u\n", chunk, chunk_length) ;
-
-       while (bytesread < chunk_length)
-       {       uint32_t thisread ;
-
-               if ((thisread = psf_binheader_readf (psf, "m", &chunk)) == 0)
-                       break ;
-               bytesread += thisread ;
-
-               switch (chunk)
-               {       case adtl_MARKER :
-                       case INFO_MARKER :
-                                       /* These markers don't contain anything, not even a chunk lebgth. */
-                                       psf_log_printf (psf, "  %M\n", chunk) ;
-                                       continue ;
-
-                       case exif_MARKER :
-                                       psf_log_printf (psf, "  %M\n", chunk) ;
-                                       bytesread += exif_subchunk_parse (psf, chunk_length - bytesread) ;
-                                       continue ;
-
-                       case data_MARKER :
-                                       psf_log_printf (psf, "  %M inside a LIST block??? Backing out.\n", chunk) ;
-                                       /* Jump back four bytes and return to caller. */
-                                       psf_binheader_readf (psf, "j", -4) ;
-                                       return 0 ;
-
-                       case 0 :
-                                       /*
-                                       **      Four zero bytes where a marker was expected. Assume this means
-                                       **      the rest of the chunk is garbage.
-                                       */
-                                       psf_log_printf (psf, "    *** Found weird-ass zero marker. Jumping to end of chunk.\n") ;
-                                       if (bytesread < chunk_length)
-                                               bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread + 4) ;
-                                       psf_log_printf (psf, "    *** Offset is now : 0x%X\n", psf_fseek (psf, 0, SEEK_CUR)) ;
-                                       return 0 ;
-
-                       default :
-                                       break ;
-                       } ;
-
-               switch (chunk)
-               {       case ISFT_MARKER :
-                       case ICOP_MARKER :
-                       case IARL_MARKER :
-                       case IART_MARKER :
-                       case ICMT_MARKER :
-                       case ICRD_MARKER :
-                       case IENG_MARKER :
-                       case IGNR_MARKER :
-                       case INAM_MARKER :
-                       case IPRD_MARKER :
-                       case ISBJ_MARKER :
-                       case ISRC_MARKER :
-                       case IAUT_MARKER :
-                       case ITRK_MARKER :
-                                       bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
-                                       chunk_size += (chunk_size & 1) ;
-                                       if (chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
-                                       {       psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
-                                               psf_binheader_readf (psf, "j", chunk_length) ;
-                                               return 0 ;
-                                               } ;
-
-                                       bytesread += psf_binheader_readf (psf, "b", buffer, chunk_size) ;
-                                       buffer [chunk_size] = 0 ;
-                                       psf_log_printf (psf, "    %M : %s\n", chunk, buffer) ;
-                                       break ;
-
-                       case labl_MARKER :
-                                       {       int mark_id ;
-
-                                               bytesread += psf_binheader_readf (psf, "44", &chunk_size, &mark_id) ;
-                                               chunk_size -= 4 ;
-                                               chunk_size += (chunk_size & 1) ;
-                                               if (chunk_size < 1 || chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
-                                               {       psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
-                                                       psf_binheader_readf (psf, "j", chunk_length) ;
-                                                       break ;
-                                                       } ;
-
-                                               bytesread += psf_binheader_readf (psf, "b", buffer, chunk_size) ;
-                                               buffer [chunk_size] = 0 ;
-                                               psf_log_printf (psf, "    %M : %u : %s\n", chunk, mark_id, buffer) ;
-                                               } ;
-                                       break ;
-
-
-                       case DISP_MARKER :
-                       case ltxt_MARKER :
-                       case note_MARKER :
-                                       bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
-                                       chunk_size += (chunk_size & 1) ;
-                                       if (chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
-                                       {       psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
-                                               psf_binheader_readf (psf, "j", chunk_length) ;
-                                               return 0 ;
-                                               } ;
-
-                                       bytesread += psf_binheader_readf (psf, "j", chunk_size) ;
-                                       psf_log_printf (psf, "    %M : %u\n", chunk, chunk_size) ;
-                                       break ;
-
-                       default :
-                                       bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
-                                       chunk_size += (chunk_size & 1) ;
-                                       psf_log_printf (psf, "    *** %M : %u\n", chunk, chunk_size) ;
-                                       if (bytesread + chunk_size > chunk_length)
-                                       {       bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread + 4) ;
-                                               continue ;
-                                               }
-                                       else
-                                               bytesread += psf_binheader_readf (psf, "j", chunk_size) ;
-
-                                       if (chunk_size >= chunk_length)
-                                               return 0 ;
-                                       break ;
-                       } ;
-
-               switch (chunk)
-               {       case ISFT_MARKER :
-                                       psf_store_string (psf, SF_STR_SOFTWARE, buffer) ;
-                                       break ;
-                       case ICOP_MARKER :
-                                       psf_store_string (psf, SF_STR_COPYRIGHT, buffer) ;
-                                       break ;
-                       case INAM_MARKER :
-                                       psf_store_string (psf, SF_STR_TITLE, buffer) ;
-                                       break ;
-                       case IART_MARKER :
-                                       psf_store_string (psf, SF_STR_ARTIST, buffer) ;
-                                       break ;
-                       case ICMT_MARKER :
-                                       psf_store_string (psf, SF_STR_COMMENT, buffer) ;
-                                       break ;
-                       case ICRD_MARKER :
-                                       psf_store_string (psf, SF_STR_DATE, buffer) ;
-                                       break ;
-                       case IGNR_MARKER :
-                                       psf_store_string (psf, SF_STR_GENRE, buffer) ;
-                                       break ;
-                       case IPRD_MARKER :
-                                       psf_store_string (psf, SF_STR_ALBUM, buffer) ;
-                                       break ;
-                       case ITRK_MARKER :
-                                       psf_store_string (psf, SF_STR_TRACKNUMBER, buffer) ;
-                                       break ;
-                       } ;
-               } ;
-
-       current_pos = psf_fseek (psf, 0, SEEK_CUR) - current_pos ;
-
-       if (current_pos - 4 != chunk_length)
-               psf_log_printf (psf, "**** Bad chunk length %d sbould be %D\n", chunk_length, current_pos - 4) ;
-
-       return 0 ;
-} /* wav_subchunk_parse */
-
-static int
 wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen)
 {      char buffer [512] ;
        uint32_t thisread, bytesread = 0, dword, sampler_data, loop_count ;
-       uint32_t note, start, end, type = -1, count ;
+       uint32_t note, pitch, start, end, type = -1, count ;
        int j, k ;
 
        chunklen += (chunklen & 1) ;
@@ -1567,10 +1244,10 @@ wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen)
        bytesread += psf_binheader_readf (psf, "4", &note) ;
        psf_log_printf (psf, "  Midi Note    : %u\n", note) ;
 
-       bytesread += psf_binheader_readf (psf, "4", &dword) ;
-       if (dword != 0)
+       bytesread += psf_binheader_readf (psf, "4", &pitch) ;
+       if (pitch != 0)
        {       snprintf (buffer, sizeof (buffer), "%f",
-                                       (1.0 * 0x80000000) / ((uint32_t) dword)) ;
+                                       (1.0 * 0x80000000) / ((uint32_t) pitch)) ;
                psf_log_printf (psf, "  Pitch Fract. : %s\n", buffer) ;
                }
        else
@@ -1676,6 +1353,7 @@ wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen)
                } ;
 
        psf->instrument->basenote = note ;
+       psf->instrument->detune = (int8_t)(pitch / (0x40000000 / 25.0) + 0.5) ;
        psf->instrument->gain = 1 ;
        psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
        psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
@@ -1758,293 +1436,6 @@ wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen)
        return 0 ;
 } /* wav_read_acid_chunk */
 
-int
-wav_read_bext_chunk (SF_PRIVATE *psf, uint32_t chunksize)
-{
-       SF_BROADCAST_INFO_16K * b ;
-       uint32_t bytes = 0 ;
-
-       if (chunksize < WAV_BEXT_MIN_CHUNK_SIZE)
-       {       psf_log_printf (psf, "bext : %u (should be >= %d)\n", chunksize, WAV_BEXT_MIN_CHUNK_SIZE) ;
-               psf_binheader_readf (psf, "j", chunksize) ;
-               return 0 ;
-               } ;
-
-       if (chunksize > WAV_BEXT_MAX_CHUNK_SIZE)
-       {       psf_log_printf (psf, "bext : %u (should be < %d)\n", chunksize, WAV_BEXT_MAX_CHUNK_SIZE) ;
-               psf_binheader_readf (psf, "j", chunksize) ;
-               return 0 ;
-               } ;
-
-       if (chunksize >= sizeof (SF_BROADCAST_INFO_16K))
-       {       psf_log_printf (psf, "bext : %u too big to be handled\n", chunksize) ;
-               psf_binheader_readf (psf, "j", chunksize) ;
-               return 0 ;
-               } ;
-
-       psf_log_printf (psf, "bext : %u\n", chunksize) ;
-
-       if ((psf->broadcast_16k = broadcast_var_alloc ()) == NULL)
-       {       psf->error = SFE_MALLOC_FAILED ;
-               return psf->error ;
-               } ;
-
-       b = psf->broadcast_16k ;
-
-       bytes += psf_binheader_readf (psf, "b", b->description, sizeof (b->description)) ;
-       bytes += psf_binheader_readf (psf, "b", b->originator, sizeof (b->originator)) ;
-       bytes += psf_binheader_readf (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
-       bytes += psf_binheader_readf (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
-       bytes += psf_binheader_readf (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
-       bytes += psf_binheader_readf (psf, "442", &b->time_reference_low, &b->time_reference_high, &b->version) ;
-       bytes += psf_binheader_readf (psf, "bj", &b->umid, sizeof (b->umid), 190) ;
-
-       if (chunksize > WAV_BEXT_MIN_CHUNK_SIZE)
-       {       /* File has coding history data. */
-
-               b->coding_history_size = chunksize - WAV_BEXT_MIN_CHUNK_SIZE ;
-
-               /* We do not parse the coding history */
-               bytes += psf_binheader_readf (psf, "b", b->coding_history, b->coding_history_size) ;
-               } ;
-
-       if (bytes < chunksize)
-               psf_binheader_readf (psf, "j", chunksize - bytes) ;
-
-       return 0 ;
-} /* wav_read_bext_chunk */
-
-int
-wav_write_bext_chunk (SF_PRIVATE *psf)
-{      SF_BROADCAST_INFO_16K *b ;
-
-       if (psf->broadcast_16k == NULL)
-               return -1 ;
-
-       b = psf->broadcast_16k ;
-
-       psf_binheader_writef (psf, "m4", bext_MARKER, WAV_BEXT_MIN_CHUNK_SIZE + b->coding_history_size) ;
-
-       /*
-       **      Note that it is very important that the field widths of the SF_BROADCAST_INFO
-       **      struct match those of the bext chunk fields.
-       */
-
-       psf_binheader_writef (psf, "b", b->description, sizeof (b->description)) ;
-       psf_binheader_writef (psf, "b", b->originator, sizeof (b->originator)) ;
-       psf_binheader_writef (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
-       psf_binheader_writef (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
-       psf_binheader_writef (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
-       psf_binheader_writef (psf, "442", b->time_reference_low, b->time_reference_high, b->version) ;
-       psf_binheader_writef (psf, "b", b->umid, sizeof (b->umid)) ;
-       psf_binheader_writef (psf, "z", make_size_t (190)) ;
-
-       if (b->coding_history_size > 0)
-               psf_binheader_writef (psf, "b", b->coding_history, make_size_t (b->coding_history_size)) ;
-
-       return 0 ;
-} /* wav_write_bext_chunk */
-
-int
-wav_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize)
-{      SF_CART_INFO_16K *c ;
-       uint32_t bytes = 0 ;
-       int k ;
-
-       if (chunksize < WAV_CART_MIN_CHUNK_SIZE)
-       {       psf_log_printf (psf, "cart : %u (should be >= %d)\n", chunksize, WAV_CART_MIN_CHUNK_SIZE) ;
-               psf_binheader_readf (psf, "j", chunksize) ;
-               return 0 ;
-               } ;
-       if (chunksize > WAV_CART_MAX_CHUNK_SIZE)
-       {       psf_log_printf (psf, "cart : %u (should be < %d)\n", chunksize, WAV_CART_MAX_CHUNK_SIZE) ;
-               psf_binheader_readf (psf, "j", chunksize) ;
-               return 0 ;
-               } ;
-
-       if (chunksize >= sizeof (SF_CART_INFO_16K))
-       {       psf_log_printf (psf, "cart : %u too big to be handled\n", chunksize) ;
-               psf_binheader_readf (psf, "j", chunksize) ;
-               return 0 ;
-               } ;
-
-       psf_log_printf (psf, "cart : %u\n", chunksize) ;
-
-       if ((psf->cart_16k = cart_var_alloc ()) == NULL)
-       {       psf->error = SFE_MALLOC_FAILED ;
-               return psf->error ;
-               } ;
-
-       c = psf->cart_16k ;
-       bytes += psf_binheader_readf (psf, "b", c->version, sizeof (c->version)) ;
-       bytes += psf_binheader_readf (psf, "b", c->title, sizeof (c->title)) ;
-       bytes += psf_binheader_readf (psf, "b", c->artist, sizeof (c->artist)) ;
-       bytes += psf_binheader_readf (psf, "b", c->cut_id, sizeof (c->cut_id)) ;
-       bytes += psf_binheader_readf (psf, "b", c->client_id, sizeof (c->client_id)) ;
-       bytes += psf_binheader_readf (psf, "b", c->category, sizeof (c->category)) ;
-       bytes += psf_binheader_readf (psf, "b", c->classification, sizeof (c->classification)) ;
-       bytes += psf_binheader_readf (psf, "b", c->out_cue, sizeof (c->out_cue)) ;
-       bytes += psf_binheader_readf (psf, "b", c->start_date, sizeof (c->start_date)) ;
-       bytes += psf_binheader_readf (psf, "b", c->start_time, sizeof (c->start_time)) ;
-       bytes += psf_binheader_readf (psf, "b", c->end_date, sizeof (c->end_date)) ;
-       bytes += psf_binheader_readf (psf, "b", c->end_time, sizeof (c->end_time)) ;
-       bytes += psf_binheader_readf (psf, "b", c->producer_app_id, sizeof (c->producer_app_id)) ;
-       bytes += psf_binheader_readf (psf, "b", c->producer_app_version, sizeof (c->producer_app_version)) ;
-       bytes += psf_binheader_readf (psf, "b", c->user_def, sizeof (c->user_def)) ;
-       bytes += psf_binheader_readf (psf, "e4", &c->level_reference, sizeof (c->level_reference)) ;
-
-       for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
-               bytes += psf_binheader_readf (psf, "b4", &c->post_timers [k].usage, make_size_t (4), &c->post_timers [k].value) ;
-
-       bytes += psf_binheader_readf (psf, "b", c->reserved, sizeof (c->reserved)) ;
-       bytes += psf_binheader_readf (psf, "b", c->url, sizeof (c->url)) ;
-
-       if (chunksize > WAV_CART_MIN_CHUNK_SIZE)
-       {       /* File has tag text. */
-               c->tag_text_size = chunksize - WAV_CART_MIN_CHUNK_SIZE ;
-               bytes += psf_binheader_readf (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ;
-               } ;
-
-       return 0 ;
-} /* wav_read_cart_chunk */
-
-int
-wav_write_cart_chunk (SF_PRIVATE *psf)
-{      SF_CART_INFO_16K *c ;
-       int k ;
-
-       if (psf->cart_16k == NULL)
-               return -1 ;
-
-       c = psf->cart_16k ;
-       psf_binheader_writef (psf, "m4", cart_MARKER, WAV_CART_MIN_CHUNK_SIZE + c->tag_text_size) ;
-       /*
-       **      Note that it is very important that the field widths of the SF_CART_INFO
-       **      struct match those of the cart chunk fields.
-       */
-       psf_binheader_writef (psf, "b", c->version, sizeof (c->version)) ;
-       psf_binheader_writef (psf, "b", c->title, sizeof (c->title)) ;
-       psf_binheader_writef (psf, "b", c->artist, sizeof (c->artist)) ;
-       psf_binheader_writef (psf, "b", c->cut_id, sizeof (c->cut_id)) ;
-       psf_binheader_writef (psf, "b", c->client_id, sizeof (c->client_id)) ;
-       psf_binheader_writef (psf, "b", c->category, sizeof (c->category)) ;
-       psf_binheader_writef (psf, "b", c->classification, sizeof (c->classification)) ;
-       psf_binheader_writef (psf, "b", c->out_cue, sizeof (c->out_cue)) ;
-       psf_binheader_writef (psf, "b", c->start_date, sizeof (c->start_date)) ;
-       psf_binheader_writef (psf, "b", c->start_time, sizeof (c->start_time)) ;
-       psf_binheader_writef (psf, "b", c->end_date, sizeof (c->end_date)) ;
-       psf_binheader_writef (psf, "b", c->end_time, sizeof (c->end_time)) ;
-       psf_binheader_writef (psf, "b", c->producer_app_id, sizeof (c->producer_app_id)) ;
-       psf_binheader_writef (psf, "b", c->producer_app_version, sizeof (c->producer_app_version)) ;
-       psf_binheader_writef (psf, "b", c->user_def, sizeof (c->user_def)) ;
-       psf_binheader_writef (psf, "4", c->level_reference, sizeof (c->level_reference)) ;
-
-       for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
-               psf_binheader_writef (psf, "b4", c->post_timers [k].usage, make_size_t (4), c->post_timers [k].value) ;
-
-       psf_binheader_writef (psf, "z", sizeof (c->reserved)) ; // just write zeros, we don't have any other use for it
-       psf_binheader_writef (psf, "b", c->url, sizeof (c->url)) ;
-
-       if (c->tag_text_size > 0)
-               psf_binheader_writef (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ;
-
-       return 0 ;
-} /* wav_write_cart_chunk */
-
-static int
-exif_fill_and_sink (SF_PRIVATE *psf, char* buf, size_t bufsz, size_t toread)
-{
-       size_t bytesread = 0 ;
-
-       buf [0] = 0 ;
-       bufsz -= 1 ;
-       if (toread < bufsz)
-               bufsz = toread ;
-       bytesread = psf_binheader_readf (psf, "b", buf, bufsz) ;
-       buf [bufsz] = 0 ;
-
-       if (bytesread == bufsz && toread > bufsz)
-               bytesread += psf_binheader_readf (psf, "j", toread - bufsz) ;
-
-       return bytesread ;
-} /* exif_fill_and_sink */
-
-/*
-** Exif specification for audio files, at JEITA CP-3451 Exif 2.2 section 5
-** (Exif Audio File Specification) http://www.exif.org/Exif2-2.PDF
-*/
-static int
-exif_subchunk_parse (SF_PRIVATE *psf, uint32_t length)
-{      uint32_t marker, dword, vmajor = -1, vminor = -1, bytesread = 0 ;
-       char buf [4096] ;
-       int thisread ;
-
-       while (bytesread < length)
-       {
-               if ((thisread = psf_binheader_readf (psf, "m", &marker)) == 0)
-                       break ;
-               bytesread += thisread ;
-
-               switch (marker)
-               {
-                       case 0 : /* camera padding? */
-                               break ;
-
-                       case ever_MARKER :
-                               bytesread += psf_binheader_readf (psf, "j4", 4, &dword) ;
-                               vmajor = 10 * (((dword >> 24) & 0xff) - '0') + (((dword >> 16) & 0xff) - '0') ;
-                               vminor = 10 * (((dword >> 8) & 0xff) - '0') + ((dword & 0xff) - '0') ;
-                               psf_log_printf (psf, "    EXIF Version : %u.%02u\n", vmajor, vminor) ;
-                               break ;
-
-                       case olym_MARKER :
-                               bytesread += psf_binheader_readf (psf, "4", &dword) ;
-                               psf_log_printf (psf, "%M : %u\n", marker, dword) ;
-                               if (bytesread + dword > length)
-                                       break ;
-                               dword += (dword & 1) ;
-                               bytesread += psf_binheader_readf (psf, "j", dword) ;
-                               break ;
-
-                       case emnt_MARKER : /* design information: null-terminated string */
-                       case emdl_MARKER : /* model name ; null-terminated string */
-                       case ecor_MARKER : /* manufacturer: null-terminated string */
-                       case etim_MARKER : /* creation time: null-terminated string in the format "hour:minute:second.subsecond" */
-                       case erel_MARKER : /* relation info: null-terminated string (filename) */
-                       case eucm_MARKER : /* user comment: 4-byte size follows, then possibly unicode data */
-                               bytesread += psf_binheader_readf (psf, "4", &dword) ;
-                               bytesread += sizeof (dword) ;
-                               dword += (dword & 1) ;
-
-                               if (dword >= sizeof (buf))
-                               {       psf_log_printf (psf, "*** Marker '%M' is too big %u\n\n", marker, dword) ;
-                                       return bytesread ;
-                                       } ;
-
-                               bytesread += exif_fill_and_sink (psf, buf, sizeof (buf), dword) ;
-
-                               /* BAD - don't know what's going on here -- maybe a bug in the camera */
-                               /* field should be NULL-terminated but there's no room for it with the reported number */
-                               /*  example output:     emdl : 8 (EX-Z1050) */
-                               if (marker == emdl_MARKER && dword == strlen (buf) /* should be >= strlen+1*/)
-                               {       psf_log_printf (psf, "    *** field size too small for string (sinking 2 bytes)\n") ;
-                                       bytesread += psf_binheader_readf (psf, "j", 2) ;
-                                       } ;
-
-                               psf_log_printf (psf, "    %M : %u (%s)\n", marker, dword, buf) ;
-                               if (dword > length)
-                                       return bytesread ;
-                               break ;
-
-                       default :
-                               psf_log_printf (psf, "    *** %M (%u): -- ignored --\n", marker, marker) ;
-                               break ;
-                       } ;
-               } ;
-
-       return bytesread ;
-} /* exif_subchunk_parse */
-
 /*==============================================================================
 */