To use the library to create WavPack files from raw PCM audio, the user must provide a WavpackBlockOutput function that is used by the library to write finished WavPack blocks to the output. Unlike the read case, there is no facility to write directly to named files. Here is the function required:
typedef int (*WavpackBlockOutput)(void *id, void *data, int32_t bcount);
where the "id" is used to differentiate the regular WavPack data "wv" from the correction data "wvc" (or for the case of multiple streams running at the same time). The return value is simply TRUE for success and FALSE for error. An example of this function can be found in wavpack.c called write_block().
The basic procedure for creating WavPack files is this:
1. get a context and set block output function with WavpackOpenFileOutput()
2. set the data format and specify modes with WavpackSetConfiguration()
3. optionally write a RIFF header with WavpackAddWrapper()
4. allocate buffers and prepare for packing with WavpackPackInit()
5. actually compress audio and write blocks with WavpackPackSamples()
6. flush final samples into blocks with WavpackFlushSamples()
7. optionally write MD5 sum with WavpackStoreMD5Sum()
8. optionally write RIFF trailer with WavpackAddWrapper()
9. if MD5 sum or RIFF trailer written, call WavpackFlushSamples() again
10. optionally append metadata tag with functions in next section
11. optionally update number of samples with WavpackUpdateNumSamples()
12. close the context with WavpackCloseFile()
Note that this does not show opening and closing the output files which is done by the application itself.
WavpackContext *WavpackOpenFileOutput (WavpackBlockOutput blockout,void *wv_id, void *wvc_id);
-----------------------------------------------------------------
Open context for writing WavPack files. The returned context pointer is used in all following calls to the library. The "blockout" function will be used to store the actual completed WavPack blocks and will be called with the id pointers containing user defined data (one for the wv file and one for the wvc file). A return value of NULL indicates that memory could not be allocated for the context.
int WavpackSetConfiguration (WavpackContext *wpc,WavpackConfig *config,uint32_t total_samples);
----------------------------------------------------
Set configuration for writing WavPack files. This must be done before sending any actual samples, however it is okay to send wrapper or other metadata before calling this. The "config" structure contains the following *required* information:
config->bytes_per_sample see WavpackGetBytesPerSample() for info
config->bits_per_sample see WavpackGetBitsPerSample() for info
config->channel_mask Microsoft standard (mono = 4, stereo = 3)
config->num_channels self evident
config->sample_rate self evident
Be particularly careful with the "channel_mask" field. If this is not set to the correct value (3 or 4) then everything will still appear to work
correctly, but the resulting WavPack file will have undefined channel assignments. When this file is unpacked back to WAV it will then get a
WAVEFORMATEXTENSIBLE header which some programs refuse to recognize.
Specifying these 5 parameters alone would create a default lossless WavPack file, identical to the one produced by using the command-line program without options. For optional configuration, the following fields and flags may be set:
config->flags:
--------------
o CONFIG_HYBRID_FLAG select hybrid mode (must set bitrate)
o CONFIG_JOINT_STEREO select joint stereo (must set override also)
o CONFIG_JOINT_OVERRIDE override default joint stereo selection
o CONFIG_HYBRID_SHAPE select hybrid noise shaping (set override & shaping_weight != 0.0)
o CONFIG_SHAPE_OVERRIDE override default hybrid noise shaping (set CONFIG_HYBRID_SHAPE and shaping_weight)
o CONFIG_DYNAMIC_SHAPING force dynamic noise shaping even when WavPack
would not use it (no need to set any of the
other shaping flags when using this one)
o CONFIG_FAST_FLAG "fast" compression mode (same as -f)
o CONFIG_HIGH_FLAG "high" compression mode (same as -h)
o CONFIG_VERY_HIGH_FLAG "very high" compression mode (same as -hh)
o CONFIG_BITRATE_KBPS hybrid bitrate is kbps, not bits / sample
o CONFIG_CREATE_WVC create correction file
o CONFIG_OPTIMIZE_WVC maximize bybrid compression (same as -cc)
o CONFIG_CALC_NOISE calc noise in hybrid mode
o CONFIG_EXTRA_MODE extra processing mode (same as -x)
o CONFIG_SKIP_WVX no wvx stream for floats & large ints (same as -p)
o CONFIG_MD5_CHECKSUM specify if you plan to store MD5 signature (the sum is calculated by the application, NOT by the library)
o CONFIG_CREATE_EXE specify if you plan to prepend sfx module
o CONFIG_OPTIMIZE_MONO detect and optimize for mono files posing as
stereo (uses a more recent stream format that
is not compatible with decoders < 4.3)
config->bitrate hybrid bitrate in either bits/sample or kbps
config->shaping_weight hybrid noise shaping coefficient override
config->block_samples force samples per WavPack block (0 = use default, else 1-131072)
config->float_norm_exp select floating-point data (127 for +/-1.0)
config->xmode extra mode processing value override (1-6)
If the number of samples to be written is known then it should be passed here.
If the duration is not known then pass -1. In the case that the size is not
known (or the writing is terminated early) then it is suggested that the
application retrieve the first block written and let the library update the
total samples indication. A function is provided to do this update and it
should be done to the "correction" file also. If this cannot be done (because
a pipe is being used, for instance) then a valid WavPack will still be created
(assuming the initial duration was set to -1), but when applications want to
access that file they will have to seek all the way to the end to determine the
actual duration (the library takes care of this). Also, if a RIFF header has
been included then it should be updated as well or the WavPack file will not be
directly unpackable to a valid wav file (although it will still be usable by
itself). A return of FALSE indicates an error (use WavpackGetErrorMessage() to
find out what happened).
int WavpackPackInit (WavpackContext *wpc);
-----------------------------------------
Prepare to actually pack samples by determining the size of the WavPack blocks
and allocating sample buffers and initializing each stream. Call after
WavpackSetConfiguration() and before WavpackPackSamples(). A return of FALSE
indicates an error.
int WavpackPackSamples (WavpackContext *wpc,
int32_t *sample_buffer,
uint32_t sample_count);
----------------------------------------------
Pack the specified samples. Samples must be stored in 32-bit longs in the
native endian format of the executing processor. The number of samples specified
indicates composite samples (sometimes called "frames"). So, the actual number
of data points would be this "sample_count" times the number of channels. Note
that samples are accumulated here until enough exist to create a complete
WavPack block (or several blocks for multichannel audio). If an application
wants to break a block at a specific sample, then it just calls
WavpackFlushSamples() to force an early termination. Completed WavPack blocks
are sent to the function provided in the initial call to
WavpackOpenFileOutput(). A return of FALSE indicates an error (which most likely
indicates the that user-supplied blockout function returned an error).
int WavpackFlushSamples (WavpackContext *wpc);
---------------------------------------------
Flush all accumulated samples into WavPack blocks. This is normally called after
all samples have been sent to WavpackPackSamples(), but can also be called to
terminate a WavPack block at a specific sample (in other words it is possible
to continue after this operation). This also must be called to dump non-audio
blocks like those holding metadata for MD5 sums or RIFF trailers. A return of
FALSE indicates an error.
void WavpackUpdateNumSamples (WavpackContext *wpc, void *first_block);
---------------------------------------------------------------------
Given the pointer to the first block written (to either a .wv or .wvc file),
update the block with the actual number of samples written. If the wav header
was generated by the library, then it is updated also. This should be done if
WavpackSetConfiguration() was called with an incorrect number of samples (or
-1). This should also be done in the case where the application did not provide
the RIFF header but did add some chunks for a RIFF trailer (see the section
on adding RIFF metadata). It is the responsibility of the application to read
and rewrite the block. An example of this can be found in the Audition filter
or in the command-line packer when the -i option is used. ON MACHINES WITH
ALIGNMENT REQUIREMENTS, BE SURE THAT THE PASSED POINTER IS PROPERLY ALIGNED!
int WavpackStoreMD5Sum (WavpackContext *wpc, uchar data [16]);
-------------------------------------------------------------
Store computed MD5 sum in WavPack metadata. Note that the user must compute the
16 byte sum; it is not done here. It is also required that WavpackFlushSamples()
be called after this to make sure the block containing the MD5 sum is actually
written. A return of FALSE indicates an error.
WavpackContext *WavpackCloseFile (WavpackContext *wpc);
------------------------------------------------------
Close the specified WavPack file and release all resources used by it.
Returns NULL.
With respect to the RIFF WAV wrapper that is inormally appended to the WavPack
file, there are three options available for the application to handle this.
The first is the simplest, and that is for the application to simply do
nothing. The library will automatically create a RIFF header appropriate for
the audio data (including WAVEFORMATEXTENSIBLE if needed) and store this
in the WavPack file. If the application does not know the actual number of
samples beforehand and needs to reread the first block and have the library
update it, then the library will handle updating its own RIFF header also.
In this scenario, there will be no trailing RIFF data. For the vast majority
of applications, this will be all that is required.
However, some applications may want to store a custom RIFF header (and trailer)
so that they can store extra RIFF chunks in there or perhaps they want to
create an exact copy of a .wav file (like the WavPack command-line program
does). To accomplish this, functions are provided (see below) to add RIFF
metadata to a WavPack file being written. In this case, the application must
provide a verbatim copy of all the RIFF data from the initial "RIFF" at the
beginning of the file up to (and including) the size field of the "data"
chunk. If any trailing RIFF data is desired, then this is appended after all
samples have been packed and flushed. This wrapper data will be *exactly*
what wvunpack will use to restore an original WAV file. In no circumstances
does the WavPack library parse, verify, or otherwise use this data.
If the RIFF header information needs to be updated after packing (because the
total file size or the data chunk size need to be update) then the first
block must be reread and the function WavpackGetWrapperLocation() must be
called to find the wrapper in the block (it would be possible to find it by
searching for the "RIFF", but using this function is significantly less ugly).
The WavPack library will not touch a wrapper that it did not create when
calling WavpackUpdateNumSamples().
The third option for including RIFF data chunks is a hybrid between these
two methods and is useful if an application wants to add a few specific RIFF
chunks and does not mind if the chunks appear at the end of the WAV file. In
this method, the application does not send any RIFF header before packing the
audio data which forces the library to create and store a standard RIFF header.
However, when all samples have been packed, the application sends the RIFF
chunks that it wants to add at the end of the file (RIFF trailer). Then, after
flushing, it must reread the first block and call WavpackUpdateNumSamples() so
that the library can update the RIFF header to reflect the added chunks (even
if the number of samples was correct). Because the library created the header,
it will update it, and will take into account the RIFF chunks added to the end.
All of this wrapper stuff can be a little confusing. It is a good idea to
test that the final application is working correctly and creating WavPack
files that will unpack to valid WAV files (even if the RIFF info is totally
wrong, the files will still work perfectly well as WavPack files because,
again, the RIFF wrapper is just informational). The first test is to use the
-ss option on WvUnpack to make sure the RIFF wrapper is reported correctly.
Then, unpack the WavPack file into a WAV using WvUnpack and repack it again
with the standard WavPack command-line program (without options). If it
doesn't complain about anything then there is a good chance that all the
wrapper information is valid.
Here are the appropriate functions:
int WavpackAddWrapper (WavpackContext *wpc, void *data, uint32_t bcount);
------------------------------------------------------------------------
Add wrapper (currently RIFF only) to WavPack blocks. This should be called
before sending any audio samples in the case of the RIFF header or after all
samples have been sent (and flushed) for any RIFF trailer. It is also
required that WavpackFlushSamples() be called again after specifying a RIFF
trailer to make sure it is actually written to the file.
If the exact contents of the RIFF header are not known because, for example,
the file duration is uncertain or trailing chunks are possible, simply write
a "dummy" header of the correct length. When all data has been written it
will be possible to read the first block written and update the header
directly. An example of this can be found in the Audition filter. A return of
FALSE indicates an error.
void *WavpackGetWrapperLocation (void *first_block, uint32_t *size);
-------------------------------------------------------------------
Given the pointer to the first block written to a WavPack file, this function
returns the location of the stored RIFF header that was originally written with
WavpackAddWrapper(). This would normally be used to update the wav header to
indicate that a different number of samples was actually written or if
additional RIFF chunks are written at the end of the file. The "size" parameter
can be set to non-NULL to obtain the exact size of the RIFF header, and the
function will return FALSE if the header is not found in the block's metadata
(or it is not a valid WavPack block). Note that the size of the RIFF header
cannot be changed and it is the responsibility of the application to read and
rewrite the block. An example of this can be found in the Audition filter.
******************************************************************************
************************* 5.0 TAGGING FUNCTIONS **************************
******************************************************************************
The WavPack library contains rudimentary support to read and write metadata tags
on WavPack files. This includes creating new APEv2 tags during WavPack file
creation, reading text fields from both ID3v1 and APEv2 tags on existing
WavPack files, and editing data in APEv2 tags.
Users should be aware of the following limitations of this functionality:
1. ID3v1 tags are read-only, and cannot be accessed if there is an APEv2 tag
prior to them in the file. ID3v1 tags are lost if the prior APEv2 tag is
edited.
2. The text items in APEv2 tags are UTF-8 encoded. The functionality of
converting to/from any local or multi-byte encoding must be handled by the
calling application.
3. The binary items of APEv2 tags can now (version 4.60+) be read and written.
To implement this in a backward-compatible way, the existing functions for
accessing text tag items were simply duplicated as versions handling only
binary tags items. The versions for text tag items work exactly as before
(ignoring binary items) and the binary versions ignore text items.
The convention for binary tag items in APEv2 tags is that the data starts
with a NULL-terminated string representing a filename. After the terminating
NULL, the actual binary data starts. In the WavPack code this filename has
only the extension of the actual file; the name portion is made up of the
tag item name. NOTE THAT THIS FUNCTIONALITY IS NOT HANDLED IN THE LIBRARY.
THE LIBRARY ONLY STORES AND RETRIEVES A BINARY IMAGE AND IT IS UP TO THE
CALLING APPLICATION TO APPEND AND HANDLE THIS FILENAME.
4. When APEv2 tags are edited and the resulting tags are shorter than the
original tags, the tag is padded with zeros at the front rather than having
the file shortened, and this padding cannot be reclaimed by future editing.
The net result of this is that repeated editing of tags will cause the file
to grow indefinitely (although this will only happen when the tag is
actually made smaller).
In these descriptions the meaning of the word "tag" refers to the whole bundle
that is appended to the end of the WavPack file. This bundle may contain many
individual items, each consisting of a key/value pair. The key is referred to
here as the "item", meaning the item's name (like "artist"). Some people refer
to the individual items as "tags", but that usage is not used here. Also note
that APEv2 tags store the case of tag item names and values, but are not case
sensitive when locating tag item names (and this is carried here into the
lookup of ID3v1 tag item names).
int WavpackGetNumTagItems (WavpackContext *wpc);
int WavpackGetNumBinaryTagItems (WavpackContext *wpc);
------------------------------------------------------
Count and return the total number of tag items (either text or binary) in the
specified file. This works with either ID3v1 tags or APEv2 tags (although ID3V1
tags do not have binary items).
int WavpackGetTagItem (WavpackContext *wpc,
const char *item,
char *value,
int size);
int WavpackGetBinaryTagItem (WavpackContext *wpc,
const char *item,
char *value,
int size)
------------------------------------------------------------
Attempt to get the specified item from the specified file's ID3v1 or APEv2 tag.
The "size" parameter specifies the amount of space available at "value", if the
desired text item will not fit in this space then ellipses (...) will be
appended and the string terminated (binary tag data is simply truncated). The
actual length of the string (or binary data) is returned (or 0 if no matching
value found). Note that with APEv2 text tags the length might not be the same
as the number of characters because UTF-8 encoding is used. Also, APEv2 text
tags can have multiple (NULL separated) strings for a single value (this is why
the length is returned). If this function is called with a NULL "value" pointer
(or a zero "length") then only the actual length of the value data is returned
(not counting the terminating NULL of text tag items). This can be used to
determine the actual memory to be allocated beforehand.
For ID3v1 tags the only "item" names supported are "title", "artist", "album",
"year", "comment" and "track" (which is converted to numeric text by the
library).
int WavpackGetTagItemIndexed (WavpackContext *wpc,
int index,
char *item,
int size);
int WavpackGetBinaryTagItemIndexed (WavpackContext *wpc,
int index,
char *item,
int size);
-------------------------------------------------
This function looks up the tag item name by index and is used when the
application wants to access all the items in the file's ID3v1 or APEv2 tag.
Note that this function accesses only the item's name; WavpackGetTagItem()
still must be called to get the actual value. The "size" parameter specifies
the amount of space available at "item", if the desired item will not fit in
this space then ellipses (...) will be appended and the string terminated.
The actual length of the string is returned (or 0 if no item exists for index).
If this function is called with a NULL "value" pointer (or a zero "length")
then only the actual length of the item name is returned (not counting the
terminating NULL). This can be used to determine the actual memory to be
allocated beforehand.
int WavpackAppendTagItem (WavpackContext *wpc,
const char *item,
const char *value,
int vsize);
int WavpackAppendBinaryTagItem (WavpackContext *wpc,
const char *item,
const char *value,
int vsize);
---------------------------------------------
This function is used to append (or replace) the specified field to the tag
being created or edited. If no tag has been started, then an empty one will be
allocated first. When finished adding all the items to the tag, use
WavpackWriteTag() to write the completed tag to the file. Note that ID3 tags
are not supported. A size parameter is included so that text values containing
multiple (NULL separated) strings (and binary data) can be written. A FALSE
return indicates an error.
int WavpackDeleteTagItem (WavpackContext *wpc, const char *item);
----------------------------------------------------------------
Delete the specified tag item from the APEv2 tag being created or edited. This
function works with either text or binary fields. Returns TRUE to indicate that
an item was actually deleted from the tag.
int WavpackWriteTag (WavpackContext *wpc);
-----------------------------------------
Once a APEv2 tag has been created (or edited) using WavpackAppendTagItem()
(and WavpackDeleteTagItem()), this function is used to write the completed tag
to the end of the WavPack file. Note that this is NOT done for EACH item in the
tag, but only after ALL items have been added to the tag.
If this function is called when creating a WavPack file, then it uses the same
"blockout" function that is used to write regular WavPack blocks (and should be
called after flushing all the audio data and writing any WavPack metadata like
RIFF trailers and MD5 sums). It may call the blockout function multiple times
to write the tag.
If this function is called when editing an existing APEv2 tag, then it will
seek to the correct position and write the tag using the WavpackStreamReader
function that has been added for this purpose (write_bytes) or using its
own standard I/O functions (if the WavpackStreamReader is not being used).
Because there is not currently a method implemented to truncate an existing
file to a shorter length, this function will pad the file with 0's in front
of a tag that had been edited to a shorter length.
******************************************************************************
********************** 6.0 HANDLING WAVPACK STREAMS **********************
******************************************************************************
In some applications (for example streaming applications and some filters, or
cases where WavPack data might be embedded into another multimedia container)
it is required for the audio file parsing functions to be separated from the
decoding functions. This is accomplished in two steps with the WavPack library.
First, the parsing functions are implemented outside the WavPack library. It is
very straightforward to parse WavPack files because the WavPack block header is
easy to recognize, and contains easy to parse and interpret information about
the block's contents and its relation to the whole WavPack file (or stream). The
exact file and block formats are described in detail in the file_format.txt
document. The wputils.c module also contains helper functions and a useful
seeking function that may also be useful in creating a WavPack parser.
Next, the individual parsed blocks are decoded to PCM audio by the library. To
accomplish this a "file" is opened with WavpackOpenFileInputEx() where the
specified WavpackStreamReader is a function that will feed the raw WavPack
block's bytes into the library when requested. The flags parameter should have
the OPEN_STREAMING bit set so that the decoder will ignore the position and any
other "whole file" information in the block headers. The decoder will suck up
the first block (through the stream reader) that actually contains audio and
stop, ready to decode. The next step is to call WavpackUnpackSamples() to
unpack the actual number of samples in the block (which should be known by the
parser).
Normally, the entire block would be unpacked and then the decoder would be
ready for the next block. If a single additional sample is requested past the
size of the current block the decoder will attempt to read the next block, so
it is important to request the exact number of samples (unless this behavior is
okay). If it is not desired to finish decoding the block then there are two
options. The easiest would be to simply decode the rest of the block anyway and
discard the results. Another option would be to close the context with the
function WavpackCloseFile() and then open another context when needed.
This procedure will also work fine for multichannel WavPack files. The decoder
will have to suck up all the blocks for the various channels before decoding may
begin.
******************************************************************************
************************** 7.0 ZIP FORMAT USAGE **************************
******************************************************************************
With version 11.0, WinZip Computing has added WavPack Audio to the official ZIP
file format standard as compression method 97, as described here:
http://www.winzip.com/ppmd_info.htm
The WavPack library can easily be used to create or decode the WavPack images
stored in the ZIP files. Some issues to keep in mind:
1. Only lossless mode is used. WinZip's implementation uses the "very high"
mode with no extra processing, although there is no reason that a different
profile could not be employed as they would still be fully compatible (for
example, the new "high" mode and/or the new "extra" mode could be used).
2. All bitdepths (including 32-bit floating-point) are supported. However,
bitdepths that are not multiples of 8 should be rounded up to the next
multiple of 8 to ensure that all samples (even illegal ones) are encoded
losslessly (this is described in more detail in the WinZip document).
3. Multichannel data (with or without WAVEFORMATEXTENSIBLE) is fully supported.
4. CONFIG_OPTIMIZE_MONO is not available during decoding and therefore should
not be used for encoding.
5. The WavPack data must have RIFF headers (to generate .wav files) and may
optionally have RIFF trailers. It would not be appropriate to have WavPack
generate the RIFF headers (either during encode or decode) because of the
obvious danger of generating files that don't match exactly.
6. The WavPack data should probably NOT have metadata tags or MD5 sums added
to it. This information would be discarded during decoding anyway and could
possibly trigger an error condition in a decoder.
The easiest way of using the WavPack library to decode the embedded WavPack
data would be to open a WavPack context using WavpackOpenFileInputEx() and
provide a WavpackStreamReader that would read the appropriate part of the
ZIP file by using an offset. If only the standard unpacking operations are
used, then the WavPack library will not attempt to seek during a decode. The
only flag to use for the "open" call would be OPEN_WRAPPER.
The most critical aspect of creating WavPack images to embed in ZIP files is
making sure that the decoded data will exactly match the source. This is in
contrast to the case of WavPack command-line programs where some invalid WAV
files may not encode (or exactly decode) for one reason or another. For this
reason it is important to check the parameters in the WAV header carefully and
allow only a well-defined set of known good combinations. The size of the audio
data should be checked to make sure it contains the correct number of whole
composite samples (or if it doesn't then this is properly handled). Since
WavPack cannot properly decode WavPack files that contain no audio data (i.e.
zero samples), this case should also be avoided. In all situations where some
question exists, the prudent choice would be to default to some other (more
genereralized) data compression method.