%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.

\section{Shorten Decoding}
{\relsize{-1}
  \input{shorten/algorithms/decode_shorten}
}

\begin{table}[h]
{\relsize{-1}
\begin{tabular}{|r|l||r|l|}
\hline
file type & format & file type & format \\
\hline
0 & lossless \textmu-Law &
7 & lossy \textmu-Law \\
1 & signed 8 bit &
8 & new \textmu-Law with zero mapping \\
2 & unsigned 8 bit &
9 & lossless a-Law \\
3 & signed 16 bit, big-endian &
10 & lossy a-Law \\
4 & unsigned 16 bit, big-endian &
11 & Microsoft .wav \\
5 & signed 16 bit, little-endian &
12 & Apple .aiff \\
6 & unsigned 16 bit, little-endian &
& \\
\hline
\end{tabular}
}
\end{table}
\par
\noindent
\VAR{channels} is the total number of channels in the stream
and \VAR{block length} is the number of PCM frames output
from the next command, which is typically constant
until the final set of commands in the stream.

\clearpage

\subsubsection{Header Decoding Example}

\begin{figure}[h]
  \includegraphics{shorten/figures/header_parse.pdf}
\end{figure}
In this example, \VAR{magic number} is \texttt{"ajkg"} and
\VAR{version} is 2.
Since \VAR{file type} is a \texttt{long}\footnote{which is defined as
  \texttt{unsigned}(\texttt{unsigned}(2))}
it breaks down as follows:
\par
\noindent
\begin{wrapfigure}[3]{r}{140pt}
  \includegraphics{shorten/figures/filetype.pdf}
\end{wrapfigure}
{\relsize{-1}
  \begin{align*}
    \textsf{MSB}_0 &\leftarrow 0
    \text{ (number of \texttt{0} bits until next \texttt{1})} \\
    \textsf{LSB}_0 &\leftarrow 3
    \text{ (read 2 unsigned bits)} \\
    \texttt{unsigned}(2) &\leftarrow 0 \times 2 ^ 2 + 3 = 3 \\
    \textsf{MSB}_1 &\leftarrow 0
    \text{ (number of \texttt{0} bits until next \texttt{1})} \\
    \textsf{LSB}_1 &\leftarrow 5
    \text{ (read 3 unsigned bits)} \\
    \textsf{file type} &\leftarrow 0 \times 2 ^ 3 + 5 = 5 \\
  \end{align*}
}
\par
\noindent
meaning our file consists of signed, 16-bit, little-endian data.
The remaining fields are determined as follows:
\begin{table}[h]
  \begin{tabular}{r>{$}c<{$}rr>{$}r<{$}rr>{$}r<{$}}
    & & $\textsf{MSB}_0$ & $\textsf{LSB}_0$ & $\texttt{unsigned}(2)$ &
    $\textsf{MSB}_1$ & $\textsf{LSB}_1$ & \texttt{long} \text{ value} \\
    \hline
    \textsf{channels} & \leftarrow & {\color{red} 0} & {\color{orange} 2} &
    {\color{red} 0} \times 2 ^ 2 + {\color{orange} 2} = {\color{fuchsia} 2} &
    {\color{blue} 0} & {\color{green} 2} &
    {\color{blue} 0} \times 2 ^ {\color{fuchsia} 2} + {\color{green} 2} =
    \textbf{2} \\
    \textsf{block length} & \leftarrow & {\color{red} 2} & {\color{orange} 1} &
    {\color{red} 2} \times 2 ^ 2 + {\color{orange} 1} = {\color{fuchsia} 9} &
    {\color{blue} 0} & {\color{green} 256} &
    {\color{blue} 0} \times 2 ^ {\color{fuchsia} 9} + {\color{green} 256} =
    \textbf{256} \\
    \textsf{max LPC} & \leftarrow & {\color{red} 0} & {\color{orange} 0} &
    {\color{red} 0} \times 2 ^ 2 + {\color{orange} 0} = {\color{fuchsia} 0} &
    {\color{blue} 0} & {\color{green} 0} &
    {\color{blue} 0} \times 2 ^ {\color{fuchsia} 0} + {\color{green} 0} =
    \textbf{0} \\
    \textsf{number of means} & \leftarrow & {\color{red} 0} &
    {\color{orange} 3} &
    {\color{red} 0} \times 2 ^ 2 + {\color{orange} 3} =
    {\color{fuchsia} 3} &
    {\color{blue} 0} & {\color{green} 4} &
    {\color{blue} 0} \times 2 ^ {\color{fuchsia} 3} + {\color{green} 4} =
    \textbf{4} \\
    \textsf{bytes to skip} & \leftarrow & {\color{red} 0} & {\color{orange} 0} &
    {\color{red} 0} \times 2 ^ 2 + {\color{orange} 0} = {\color{fuchsia} 0} &
    {\color{blue} 0} & {\color{green} 0} &
    {\color{blue} 0} \times 2 ^ {\color{fuchsia} 0} + {\color{green} 0} =
    \textbf{0} \\
  \end{tabular}
\end{table}


\clearpage

\subsection{Processing Shorten Commands}
\label{shorten:process_commands}
{\relsize{-1}
  \input{shorten/algorithms/process_commands}
}

\clearpage

\subsection{Processing Audio Command}
\label{shorten:process_audio_command}
{\relsize{-1}
  \input{shorten/algorithms/process_audio_command}
}

\clearpage

\subsection{Processing Verbatim Data}
\label{shorten:process_verbatim}
These are non-audio blocks designed to hold Wave/AIFF headers or footers.
They are expected to be in the Shorten file in the same order
they would be output to disk.
\par
\noindent
\input{shorten/algorithms/process_verbatim}
\begin{figure}[h]
\includegraphics{shorten/figures/verbatim.pdf}
\end{figure}

\clearpage

\subsection{\texttt{DIFF3} Parsing Example}
Given a \texttt{DIFF3} command issued with a current \VAR{block length} of 15
and the bytes:
\begin{figure}[h]
\includegraphics{shorten/figures/block1.pdf}
\end{figure}
\begin{equation*}
\text{energy} \leftarrow 0 \times 2 ^ 3 + 1 = 1
\end{equation*}
\begin{table}[h]
  {\relsize{-1}
    \renewcommand{\arraystretch}{1.25}
    \begin{tabular}{rrr>{$}r<{$}>{$}r<{$}>{$}r<{$}}
    $i$ & $\textsf{MSB}_i$ & $\textsf{LSB}_i$ &
    \textsf{unsigned}_i & \textsf{residual}_i & \textsf{sample}_i \\
    \hline
    -3 & & & & & 0 \\
    -2 & & & & & 0 \\
    -1 & & & & & 0 \\
    \hline
    0 &
    0 & 0 & 0 \times 2 ^ {2} + 0 = 0 &
    \lfloor 0 \div 2 \rfloor = 0 &
    (3 \times (0 - 0)) + 0 + 0 = 0 \\
    1 &
    8 & 0 & 8 \times 2 ^ {2} + 0 = 32 &
    \lfloor 32 \div 2 \rfloor = 16 &
    (3 \times (0 - 0)) + 0 + 16 = 16 \\
    2 &
    8 & 1 & 8 \times 2 ^ {2} + 1 = 33 &
    -\lfloor 33 \div 2 \rfloor - 1 = -17 &
    (3 \times (16 - 0)) + 0 - 17 = 31 \\
    3 &
    0 & 1 & 0 \times 2 ^ {2} + 1 = 1 &
    -\lfloor 1 \div 2 \rfloor - 1 = -1 &
    (3 \times (31 - 16)) + 0 - 1 = 44 \\
    4 &
    0 & 1 & 0 \times 2 ^ {2} + 1 = 1 &
    -\lfloor 1 \div 2 \rfloor - 1 = -1 &
    (3 \times (44 - 31)) + 16 - 1 = 54 \\
    5 &
    0 & 0 & 0 \times 2 ^ {2} + 0 = 0 &
    \lfloor 0 \div 2 \rfloor = 0 &
    (3 \times (54 - 44)) + 31 + 0 = 61 \\
    6 &
    0 & 1 & 0 \times 2 ^ {2} + 1 = 1 &
    -\lfloor 1 \div 2 \rfloor - 1 = -1 &
    (3 \times (61 - 54)) + 44 - 1 = 64 \\
    7 &
    0 & 0 & 0 \times 2 ^ {2} + 0 = 0 &
    \lfloor 0 \div 2 \rfloor = 0 &
    (3 \times (64 - 61)) + 54 + 0 = 63 \\
    8 &
    0 & 0 & 0 \times 2 ^ {2} + 0 = 0 &
    \lfloor 0 \div 2 \rfloor = 0 &
    (3 \times (63 - 64)) + 61 + 0 = 58 \\
    9 &
    0 & 0 & 0 \times 2 ^ {2} + 0 = 0 &
    \lfloor 0 \div 2 \rfloor = 0 &
    (3 \times (58 - 63)) + 64 + 0 = 49 \\
    10 &
    1 & 0 & 1 \times 2 ^ {2} + 0 = 4 &
    \lfloor 4 \div 2 \rfloor = 2 &
    (3 \times (49 - 58)) + 63 + 2 = 38 \\
    11 &
    0 & 1 & 0 \times 2 ^ {2} + 1 = 1 &
    -\lfloor 1 \div 2 \rfloor - 1 = -1 &
    (3 \times (38 - 49)) + 58 - 1 = 24 \\
    12 &
    0 & 2 & 0 \times 2 ^ {2} + 2 = 2 &
    \lfloor 2 \div 2 \rfloor = 1 &
    (3 \times (24 - 38)) + 49 + 1 = 8 \\
    13 &
    1 & 0 & 1 \times 2 ^ {2} + 0 = 4 &
    \lfloor 4 \div 2 \rfloor = 2 &
    (3 \times (8 - 24)) + 38 + 2 = -8 \\
    14 &
    0 & 0 & 0 \times 2 ^ {2} + 0 = 0 &
    \lfloor 0 \div 2 \rfloor = 0 &
    (3 \times (-8 - 8)) + 24 + 0 = -24 \\
    \hline
  \end{tabular}
    \renewcommand{\arraystretch}{1.0}
  }
\end{table}
\par
\noindent
Note that the negative $i$ samples are only used
for calculation and not re-output by the \texttt{DIFF} command.

\clearpage

\subsection{\texttt{DIFF3} Parsing Example 2}
Given a \texttt{DIFF3} command issued from the same channel as
the previous example with a current \VAR{block length} of 10
and the bytes:

\begin{figure}[h]
\includegraphics{shorten/figures/block2.pdf}
\end{figure}
\begin{equation*}
  \text{energy} \leftarrow 0 \times 2 ^ 3 + 0 = 0
\end{equation*}
\begin{table}[h]
  {\relsize{-1}
    \renewcommand{\arraystretch}{1.25}
    \begin{tabular}{rrr>{$}r<{$}>{$}r<{$}>{$}r<{$}}
      $i$ & $\textsf{MSB}_i$ & $\textsf{LSB}_i$ &
      \textsf{unsigned}_i & \textsf{residual}_i & \textsf{sample}_i \\
      \hline
      -3 & & & & & 8 \\
      -2 & & & & & -8 \\
      -1 & & & & & -24 \\
      \hline
      0 &
      2 & 0 & 2 \times 2 ^ {1} + 0 = 4 &
      \lfloor 4 \div 2 \rfloor = 2 &
      (3 \times (-24 + 8)) + 8 + 2 = -38 \\
      1 &
      1 & 0 & 1 \times 2 ^ {1} + 0 = 2 &
      \lfloor 2 \div 2 \rfloor = 1 &
      (3 \times (-38 + 24)) - 8 + 1 = -49 \\
      2 &
      0 & 1 & 0 \times 2 ^ {1} + 1 = 1 &
      -\lfloor 1 \div 2 \rfloor - 1 = -1 &
      (3 \times (-49 + 38)) - 24 - 1 = -58 \\
      3 &
      2 & 0 & 2 \times 2 ^ {1} + 0 = 4 &
      \lfloor 4 \div 2 \rfloor = 2 &
      (3 \times (-58 + 49)) - 38 + 2 = -63 \\
      4 &
      0 & 0 & 0 \times 2 ^ {1} + 0 = 0 &
      \lfloor 0 \div 2 \rfloor = 0 &
      (3 \times (-63 + 58)) - 49 + 0 = -64 \\
      5 &
      0 & 0 & 0 \times 2 ^ {1} + 0 = 0 &
      \lfloor 0 \div 2 \rfloor = 0 &
      (3 \times (-64 + 63)) - 58 + 0 = -61 \\
      6 &
      0 & 0 & 0 \times 2 ^ {1} + 0 = 0 &
      \lfloor 0 \div 2 \rfloor = 0 &
      (3 \times (-61 + 64)) - 63 + 0 = -54 \\
      7 &
      0 & 1 & 0 \times 2 ^ {1} + 1 = 1 &
      -\lfloor 1 \div 2 \rfloor - 1 = -1 &
      (3 \times (-54 + 61)) - 64 - 1 = -44 \\
      8 &
      0 & 0 & 0 \times 2 ^ {1} + 0 = 0 &
      \lfloor 0 \div 2 \rfloor = 0 &
      (3 \times (-44 + 54)) - 61 + 0 = -31 \\
      9 &
      0 & 1 & 0 \times 2 ^ {1} + 1 = 1 &
      -\lfloor 1 \div 2 \rfloor - 1 = -1 &
      (3 \times (-31 + 44)) - 54 - 1 = -16 \\
    \end{tabular}
    \renewcommand{\arraystretch}{1.0}
  }
\end{table}
\par
\noindent
Note that because this \texttt{DIFF} is issued on the same channel
as the previous \texttt{DIFF}:
\begin{table}[h]
\begin{tabular}{rcl}
  $\text{current sample}_{(-3)}$ & = & $\text{previous sample}_{12}$ \\
  $\text{current sample}_{(-2)}$ & = & $\text{previous sample}_{13}$ \\
  $\text{current sample}_{(-1)}$ & = & $\text{previous sample}_{14}$ \\
\end{tabular}
\end{table}
\par
\noindent
the samples have been ``wrapped around'' from one channel to the next.
But again, those negative samples are not re-output by
this \texttt{DIFF} command.

\clearpage

\subsection{Processing \texttt{QLPC}}
\label{shorten:process_qlpc_command}
{\relsize{-1}
  \input{shorten/algorithms/process_qlpc_command}
}
\begin{figure}[h]
\includegraphics{shorten/figures/qlpc.pdf}
\end{figure}
\par
\noindent
As with the \texttt{DIFF} commands, negative samples are from the
previously decoded samples on channel $c$, or 0 if there are none.

In practice, encoded Shorten files typically contain no
\texttt{QLPC} commands at all.
Because the reference implementation uses a 32-bit
accumulator for the LPC sum,
calculation will overflow when using a nontrivial number of
coefficients.
Instead, files usually contain only \texttt{DIFF1}, \texttt{DIFF2},
\texttt{DIFF3}, and \texttt{ZERO} audio commands.

\clearpage

\subsection{Reading \texttt{QLPC} Example}
\begin{figure}[h]
\includegraphics{shorten/figures/qlpc1.pdf}
\end{figure}

In this example:
\begin{table}[h]
  \begin{tabular}{r>{$}c<{$}>{$}l<{$}}
    \textsf{offset} & \leftarrow & 0 \\
    $\textsf{LPC count}$ & \leftarrow & 1 \\
    $\textsf{LPC coefficient}_0$ & \leftarrow & 29 \\
  \end{tabular}
\end{table}
\begin{table}[h]
{\relsize{-1}
  \renewcommand{\arraystretch}{1.25}
  \begin{tabular}{rr>{$}r<{$}>{$}r<{$}||>{$}r<{$}}
    $i$ & $\textsf{residual}_i$ & \textsf{sum}_i & \textsf{unoffset}_i &
    \textsf{sample}_{c~i} \\
    \hline
    0 & -1 &
    2 ^ 5 + 29 \times (0 - 0) = 32 &
    \left\lfloor\frac{32}{2 ^ 5}\right\rfloor - 1 = 0 &
    0 + 0 = 0 \\
    1 & 15 &
    2 ^ 5 + 29 \times 0 = 32 &
    \left\lfloor\frac{32}{2 ^ 5}\right\rfloor + 15 = 16 &
    16 + 0 = 16 \\
    2 & 16 &
    2 ^ 5 + 29 \times 16 = 496 &
    \left\lfloor\frac{496}{2 ^ 5}\right\rfloor + 16 = 31 &
    31 + 0 = 31 \\
    3 & 15 &
    2 ^ 5 + 29 \times 31 = 931 &
    \left\lfloor\frac{931}{2 ^ 5}\right\rfloor + 15 = 44 &
    44 + 0 = 44 \\
    4 & 14 &
    2 ^ 5 + 29 \times 44 = 1308 &
    \left\lfloor\frac{1308}{2 ^ 5}\right\rfloor + 14 = 54 &
    54 + 0 = 54 \\
    5 & 12 &
    2 ^ 5 + 29 \times 54 = 1598 &
    \left\lfloor\frac{1598}{2 ^ 5}\right\rfloor + 12 = 61 &
    61 + 0 = 61 \\
    6 & 8 &
    2 ^ 5 + 29 \times 61 = 1801 &
    \left\lfloor\frac{1801}{2 ^ 5}\right\rfloor + 8 = 64 &
    64 + 0 = 64 \\
    7 & 4 &
    2 ^ 5 + 29 \times 64 = 1888 &
    \left\lfloor\frac{1888}{2 ^ 5}\right\rfloor + 4 = 63 &
    63 + 0 = 63 \\
    8 & 0 &
    2 ^ 5 + 29 \times 63 = 1859 &
    \left\lfloor\frac{1859}{2 ^ 5}\right\rfloor + 0 = 58 &
    58 + 0 = 58 \\
    9 & -4 &
    2 ^ 5 + 29 \times 58 = 1714 &
    \left\lfloor\frac{1714}{2 ^ 5}\right\rfloor - 4 = 49 &
    49 + 0 = 49 \\
    %% 10 & -7 &
    %% 2 ^ 5 + 29 \times 49 = 1453 &
    %% \left\lfloor\frac{1453}{2 ^ 5}\right\rfloor - 7 = 38 &
    %% 38 + 0 = 38 \\
    %% 11 & -11 &
    %% 2 ^ 5 + 29 \times 38 = 1134 &
    %% \left\lfloor\frac{1134}{2 ^ 5}\right\rfloor - 11 = 24 &
    %% 24 + 0 = 24 \\
    %% 12 & -14 &
    %% 2 ^ 5 + 29 \times 24 = 728 &
    %% \left\lfloor\frac{728}{2 ^ 5}\right\rfloor - 14 = 8 &
    %% 8 + 0 = 8 \\
    %% 13 & -16 &
    %% 2 ^ 5 + 29 \times 8 = 264 &
    %% \left\lfloor\frac{264}{2 ^ 5}\right\rfloor - 16 = -8 &
    %% -8 + 0 = -8 \\
    %% 14 & -17 &
    %% 2 ^ 5 + 29 \times -8 = -200 &
    %% \left\lfloor\frac{-200}{2 ^ 5}\right\rfloor - 17 = -24 &
    %% -24 + 0 = -24 \\
    %% 15 & -17 &
    %% 2 ^ 5 + 29 \times -24 = -664 &
    %% \left\lfloor\frac{-664}{2 ^ 5}\right\rfloor - 17 = -38 &
    %% -38 + 0 = -38 \\
    %% 16 & -15 &
    %% 2 ^ 5 + 29 \times -38 = -1070 &
    %% \left\lfloor\frac{-1070}{2 ^ 5}\right\rfloor - 15 = -49 &
    %% -49 + 0 = -49 \\
    %% 17 & -14 &
    %% 2 ^ 5 + 29 \times -49 = -1389 &
    %% \left\lfloor\frac{-1389}{2 ^ 5}\right\rfloor - 14 = -58 &
    %% -58 + 0 = -58 \\
    %% 18 & -11 &
    %% 2 ^ 5 + 29 \times -58 = -1650 &
    %% \left\lfloor\frac{-1650}{2 ^ 5}\right\rfloor - 11 = -63 &
    %% -63 + 0 = -63 \\
    %% 19 & -7 &
    %% 2 ^ 5 + 29 \times -63 = -1795 &
    %% \left\lfloor\frac{-1795}{2 ^ 5}\right\rfloor - 7 = -64 &
    %% -64 + 0 = -64 \\
  \end{tabular}
  \renewcommand{\arraystretch}{1.0}
}
\end{table}
