%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{Musepack}
Musepack is a lossy audio format based on MP2 and designed for
transparency.
It comes in two varieties: SV7 and SV8 where `SV' stands for
Stream Version.
These container versions differ so heavily that they must be
considered separately from one another.
\section{the SV7 File Stream}
This is the earliest version of Musepack with wide support.
All of its fields are little-endian.
\begin{figure}[h]
\includegraphics{figures/musepack/sv7_stream.pdf}
\end{figure}
Each frame contains 1152 samples per channel.
Therefore:
\begin{equation}
\text{Total Samples} = ((\text{Frame Count} - 1) \times 1152) + \text{Last Frame Samples}
\end{equation}
Musepack files always have exactly 2 channels and its lossy samples
are stored as floating point.
Its sampling rate is one of four values:

\begin{inparaenum}
\item[\texttt{00} = ] 44100Hz,
\item[\texttt{01} = ] 48000Hz,
\item[\texttt{10} = ] 37800Hz,
\item[\texttt{11} = ] 32000Hz
\end{inparaenum}
.

\pagebreak

\section{the SV8 File Stream}
This is the latest version of the Musepack stream.
All of its fields are big-endian.
\begin{figure}[h]
\includegraphics{figures/musepack/sv8_stream.pdf}
\end{figure}
\par
\noindent
\VAR{Key} is a two character uppercase ASCII string
(i.e. each digit must be between the characters 0x41 and 0x5A, inclusive).
\VAR{Length} is a variable length field indicating the size of the entire packet,
including the header.
This is a Nut-encoded field whose total size depends on whether
the eighth bit of each byte is 0 or 1.
The remaining seven bits of each byte combine to form the field's value,
which is big-endian.
\begin{figure}[h]
\includegraphics{figures/musepack/sv8_nut.pdf}
\end{figure}

\subsection{the SH Packet}
This is the Stream Header, which must be found before the first
audio packet in the file.
\begin{figure}[h]
\includegraphics{figures/musepack/sv8_sh.pdf}
\end{figure}
\par
\noindent
\VAR{CRC32} is a checksum of everything in the header, not including the
checksum itself.
\VAR{Sample Count} is the total number of samples, as a Nut-encoded value.
\VAR{Beginning Silence} is the number of silence samples at the start
of the stream, also as a Nut-encoded value.
\VAR{Channels} is the total number of channels in the stream, minus 1.
\VAR{Mid Side Used} indicates the channels are stored using mid-side stereo.
\VAR{Frame Count} is used to calculate the total number of frames per
audio packet:
\begin{equation}
\text{Number of Frames} = 4 ^ \text{Frame Count}
\end{equation}
\VAR{Sample Rate} is one of four values:

\begin{inparaenum}
\item[\texttt{000} = ] 44100Hz,
\item[\texttt{001} = ] 48000Hz,
\item[\texttt{010} = ] 37800Hz,
\item[\texttt{011} = ] 32000Hz
\end{inparaenum}
.

\subsection{the SE Packet}
This is an empty packet that denotes the end of the Musepack stream.
A decoder should ignore everything after this packet, which allows
for metadata tags such as APEv2 to be placed at the end of the file.

\subsection{the RG Packet}
This is ReplayGain information about the file.
\begin{figure}[h]
\includegraphics{figures/musepack/sv8_rg.pdf}
\end{figure}

\subsection{the EI Packet}
This is information about the Musepack encoder.
\begin{figure}[h]
\includegraphics{figures/musepack/sv8_ei.pdf}
\end{figure}
