%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{AccurateRip}
AccurateRip is a service to verify the accuracy of one's CD rip
by comparing that rip against those performed by others.
If a lot of other people have extracted the same disc with the same data,
one can be confident the rip has been performed correctly.

To accomplish this, one needs to calculate the disc's
AccurateRip ID using its table-of-contents,
query the server with that ID and receive a file of checksums,
and finally verify the extracted tracks' data against those checksums.

\clearpage

\section{Calculating the AccurateRip ID}
An AccurateRip disc ID consists of 4 sections:
\vskip .25in
\par
\noindent
$\texttt{\huge{dBAR-}}\underbrace{\texttt{\huge{003}}}_{\text{tracks}}\texttt{\huge{-}}\underbrace{\texttt{\huge{00014846}}}_{\text{offset sum}_1}\texttt{\huge{-}}\underbrace{\texttt{\huge{00044301}}}_{\text{offset sum}_2}\texttt{\huge{-}}\underbrace{\texttt{\huge{10023003}}}_{\text{FreeDB ID}}\texttt{\huge{.bin}}$
\vskip .25in
\par
\noindent
{\relsize{-1}
\begin{description}
  \item[tracks]
    the number of tracks on the CD, in base-10, and padded with 0s to 3 digits
  \item[$\text{offset sum}_1$]
    the sum of all the track offsets, in base-16, and padded with 0s to 8 digits
  \item[$\text{offset sum}_2$]
    the sum of each track offset multiplied by its track number,
    in base-16, and padded with 0s to 8 digits
  \item[FreeDB ID]
    the CD's FreeDB ID, as calculated on page \pageref{freedb:discid}.
\end{description}
}
\par
\noindent
For example, given the following CD:
\begin{table}[h]
  \begin{tabular}{r|rrr}
    track number & length (in M:SS) & length (in CD frames) & offset (in CD frames) \\
    \hline
    1 & 3:18 & 14868 & 0 \\
    2 & 2:43 & 12252 & 14868 \\
    3 & 3:19 & 14930 & 27120 \\
    lead-out & & & 42050 \\
  \end{tabular}
  \vskip .25in
  \par
  \noindent
  \begin{tabular}{r>{$}r<{$}l}
    tracks = & 3 & = \texttt{003} \\
    $\text{offset sum}_1$ = & 0 + 14868 + 27120 + 42050 & = \texttt{00014846} \\
    $\text{offset sum}_2$ = & (1 \times 1) + (2 \times 14868) + (3 \times 27120) + (4 \times 42050) & = \texttt{00044301} \\
    FreeDB ID = & & = \texttt{10023003} \\
  \end{tabular}
\end{table}
\par
\noindent
These 4 numeric fields combine into the AccurateRip ID seen above.
The lead-out track is treated as track number 4
during the \VAR{offset sum} calculations, but the total number of tracks
is actually 3.
In addition, note the offset of track 1 is treated as 0
during calculation of $\text{offset sum}_1$, but it is treated
as 1 during calculation of $\text{offset sum}_2$.

\section{Retrieving AccurateRip Data}
The data itself for this disc ID can be retrieved by making an
\texttt{HTTP GET} request to:
{\relsize{-1}
\begin{Verbatim}[frame=single]
  http://www.accuraterip.com/accuraterip/6/4/8/dBAR-003-00014846-00044301-10023003.bin
\end{Verbatim}
}
\par
\noindent
The 1st digit after \texttt{accuraterip} is the last digit of
$\text{offset sum}_1$,
the 2nd digit is the 2nd-to-last digit of $\text{offset sum}_1$,
and the 3rd digit is the 3rd-to-last digit of $\text{offset sum}_1$.
This layout is presumably to spread the files on the server
in a relatively even fashion.

\clearpage

\section{Parsing AccurateRip Data}
If there's a matching CD, the AccurateRip server
returns a set of 1 or more releases as binary, little-endian data:
\begin{figure}[h]
  \includegraphics{figures/accuraterip-data.pdf}
\end{figure}
\par
\noindent
A given disc ID may return one or more releases,
each containing the same \VAR{track count}, \VAR{$\text{offset sum}_0$},
\VAR{$\text{offset sum}_1$} and \VAR{FreeDB ID} values
as calculated in the disc ID itself.
The number of 72-bit track values equals the \VAR{track count},
and each track value contains a \VAR{checksum} and \VAR{confidence}
value needed to verify the track itself.

\subsection{AccurateRip Data Parsing Example}
\begin{figure}[h]
  \includegraphics{figures/accuraterip-data-example.pdf}
\end{figure}
{\relsize{-1}
\begin{tabular}{rl}
  track count & 3 \\
  $\text{offset sum}_0$ & \texttt{0x14846} \\
  $\text{offset sum}_1$ & \texttt{0x44301} \\
  FreeDB ID & \texttt{0x10023003} \\
\end{tabular}
\vskip .25in
\begin{tabular}{rrrr}
  release & track & confidence & $\text{checksum}$  \\
  \hline
  0 & 1 & 4 & \texttt{0x594F4F3B} \\
  0 & 2 & 4 & \texttt{0x54C71EE8} \\
  0 & 3 & 4 & \texttt{0xBA1B82A8} \\
  \hline
  1 & 1 & 3 & \texttt{0x8C85F5D9} \\
  1 & 2 & 3 & \texttt{0x56A7AEE0} \\
  1 & 3 & 3 & \texttt{0x510809C6} \\
\end{tabular}
}

\clearpage

\section{Calculating Track's Checksum}
{\relsize{-1}
\ALGORITHM{the track's PCM data as signed integers, the track's total number of CD frames}{a 32-bit unsigned checksum}
\SetKwData{CHECKSUM}{checksum}
\SetKwData{CDFRAMES}{CD frames}
\SetKwData{CHANNEL}{channel}
\SetKwData{LEFT}{left}
\SetKwData{RIGHT}{right}
$\CHECKSUM \leftarrow 0$\;
\For{$i \leftarrow 0$ \KwTo$(\CDFRAMES \times 588)$}{
  \eIf(\tcc*[f]{convert left signed value to unsigned}){$\text{\CHANNEL}_{i~0} \geq 0$}{
    $\text{\LEFT}_{i} \leftarrow \text{\CHANNEL}_{i~0}$\;
  }{
    $\text{\LEFT}_{i} \leftarrow 2 ^ {16} - (-\text{\CHANNEL}_{i~0})$\;
  }
  \BlankLine
  \eIf(\tcc*[f]{convert right signed value to unsigned}){$\text{\CHANNEL}_{i~1} \geq 0$}{
    $\text{\RIGHT}_{i} \leftarrow \text{\CHANNEL}_{i~1}$\;
  }{
    $\text{\RIGHT}_{i} \leftarrow 2 ^ {16} - (-\text{\CHANNEL}_{i~1})$\;
  }
  \BlankLine
  $\text{\CHECKSUM}' \leftarrow \text{\CHECKSUM} + ((\text{\RIGHT}_{i} \times 2 ^ {16} + \text{\LEFT}_{i}) \times (i + 1))$\;
}
\BlankLine
\Return $(\CHECKSUM \bmod \texttt{0x100000000})$\tcc*{truncate checksum to 32 bits}
\EALGORITHM
}

\subsection{Checksum Calculation Example}
Given a track's first 10 PCM frames, its checksum is calculated as follows:
\begin{table}[h]
  {\relsize{-1}
  \begin{tabular}{r|rr|rr|lr}
    $i$ & $\textsf{channel}_{i~0}$ & $\textsf{channel}_{i~1}$ & $\textsf{left}_{i}$ & $\textsf{right}_{i}$ & & \textsf{checksum} \\
    \hline
    0 & 0 & 64 &
$0$ &
$64$ &
$\texttt{0x00000000}~+~$ &
$((64 \times 2 ^ {16}) + 0) \times 1 = \texttt{0x00400000}$
\\
1 & 16 & 62 &
$16$ &
$62$ &
$\texttt{0x00400000}~+~$ &
$((62 \times 2 ^ {16}) + 16) \times 2 = \texttt{0x00BC0020}$
\\
2 & 31 & 56 &
$31$ &
$56$ &
$\texttt{0x00BC0020}~+~$ &
$((56 \times 2 ^ {16}) + 31) \times 3 = \texttt{0x0164007D}$
\\
3 & 44 & 47 &
$44$ &
$47$ &
$\texttt{0x0164007D}~+~$ &
$((47 \times 2 ^ {16}) + 44) \times 4 = \texttt{0x0220012D}$
\\
4 & 54 & 34 &
$54$ &
$34$ &
$\texttt{0x0220012D}~+~$ &
$((34 \times 2 ^ {16}) + 54) \times 5 = \texttt{0x02CA023B}$
\\
5 & 61 & 20 &
$61$ &
$20$ &
$\texttt{0x02CA023B}~+~$ &
$((20 \times 2 ^ {16}) + 61) \times 6 = \texttt{0x034203A9}$
\\
6 & 64 & 4 &
$64$ &
$4$ &
$\texttt{0x034203A9}~+~$ &
$((4 \times 2 ^ {16}) + 64) \times 7 = \texttt{0x035E0569}$
\\
7 & 63 & -12 &
$63$ &
$65524$ &
$\texttt{0x035E0569}~+~$ &
$((65524 \times 2 ^ {16}) + 63) \times 8 = \texttt{0x02FE0761}$
\\
8 & 58 & -27 &
$58$ &
$65509$ &
$\texttt{0x02FE0761}~+~$ &
$((65509 \times 2 ^ {16}) + 58) \times 9 = \texttt{0x020B096B}$
\\
9 & 49 & -41 &
$49$ &
$65495$ &
$\texttt{0x020B096B}~+~$ &
$((65495 \times 2 ^ {16}) + 49) \times 10 = \texttt{0x00710B55}$
\\
  \end{tabular}
  }
\end{table}
\par
\noindent
If the track's final calculated checksum matches its corresponding
checksum in \textit{any} of the releases returned by AccurateRip
for that disc ID, then the given confidence level is how many other
people have ripped it with that same checksum.
