%Copyright (C) 2007-2015  Brian Langenberger
%This work is licensed under the
%Creative Commons Attribution-Share Alike 3.0 United States License.
%To view a copy of this license, visit
%http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
%Creative Commons,
%171 Second Street, Suite 300,
%San Francisco, California, 94105, USA.

\chapter{Waveform Audio File Format}
The Waveform Audio File Format is the most common form of PCM container.
What that means is that the file is mostly PCM data with a
small amount of header data to tell applications what format the
PCM data is in.
Since RIFF WAVE originated on Intel processors, everything in it
is little-endian.
\section{the RIFF WAVE Stream}
\includegraphics{figures/wav/stream.pdf}
\par
\noindent
\VAR{Chunk Size} is the total size of the chunk, minus
8 bytes for the chunk header.
\section{the Classic `fmt' Chunk}
Wave files with 2 channels or less, and 16 bits-per-sample or less,
use a classic `fmt' chunk to indicate its PCM data format.
This chunk is required to appear before the `data' chunk.
\includegraphics{figures/wav/fmt.pdf}
\begin{align}
\text{Average Bytes per Second} &= \frac{\text{Sample Rate}
  \times \text{Channel Count} \times \text{Bits per Sample}}{8} \\
\text{Block Align} &= \frac{\text{Channel Count} \times \text{Bits per Sample}}{8}
\end{align}
\section{the WAVEFORMATEXTENSIBLE `fmt' Chunk}
Wave files with more than 2 channels or more than 16 bits-per-sample
should use a WAVEFORMATEXTENSIBLE `fmt' chunk which contains
additional fields for channel assignment.
\begin{figure}[h]
\includegraphics{figures/wav/fmtext.pdf}
\end{figure}
\par
\noindent
Note that the \VAR{Average Bytes per Second} and \VAR{Block Align} fields
are calculated the same as a classic fmt chunk.

\section{the `data' Chunk}
\begin{figure}[h]
\includegraphics{figures/wav/data.pdf}
\end{figure}
\par
\noindent
\VAR{PCM Data} is a stream of PCM samples stored in little-endian format.

\pagebreak

\section{Channel Assignment}
\label{wave_channel_assignment}
Channels whose bits are set in the WAVEFORMATEXTENSIBLE `fmt' chunk
appear in the following order:
\begin{figure}[h]
\begin{tabular}{| r | r | l |}
\hline
Index & Channel & Mask Bit \\
\hline
1 & front left & \texttt{0x1} \\
2 & front right & \texttt{0x2} \\
3 & front center & \texttt{0x4} \\
4 & LFE & \texttt{0x8} \\
5 & back left & \texttt{0x10} \\
6 & back right & \texttt{0x20} \\
7 & front left of center & \texttt{0x40} \\
8 & front right of center & \texttt{0x80} \\
9 & back center & \texttt{0x100} \\
10 & side left & \texttt{0x200} \\
11 & side right & \texttt{0x400} \\
12 & top center & \texttt{0x800} \\
13 & top front left & \texttt{0x1000} \\
14 & top front center & \texttt{0x2000} \\
15 & top front right & \texttt{0x4000} \\
16 & top back left & \texttt{0x8000} \\
17 & top back center & \texttt{0x10000} \\
18 & top back right & \texttt{0x20000} \\
\hline
\end{tabular}
\end{figure}
\par
\noindent
For example, if the file's channel mask is set to \texttt{0x33},
it contains the channels `front left', `front right',
`back left' and `back right', in that order.
