Skip to content

Commit

Permalink
en: music-and-technology-synthesis-two-dimensional-arrays: New file
Browse files Browse the repository at this point in the history
* src/en/sections/music-and-technology-synthesis-two-dimensional-arrays.tex: New
file.
* src/en/sparc.tex: Use it.
* Makefile.am (SECTIONS_EN): Register it.
  • Loading branch information
artyom-poptsov committed Jan 3, 2025
1 parent 78c399f commit dd30dbd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ SECTIONS_EN = \
src/en/sections/music-and-technology-synthesis-harmony.tex \
src/en/sections/music-and-technology-synthesis-octave-system.tex \
src/en/sections/music-and-technology-synthesis-simple-melodies.tex \
src/en/sections/music-and-technology-synthesis-arrays.tex
src/en/sections/music-and-technology-synthesis-arrays.tex \
src/en/sections/music-and-technology-synthesis-two-dimensional-arrays.tex

SECTIONS_OUT_EN = \
$(foreach section, $(SECTIONS_EN), out/$(section))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
\documentclass[../sparc.tex]{subfiles}
\graphicspath{{\subfix{../images/}}}
\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\subsection{Two-dimensional arrays}
\index{Programming!Array!Two-dimensional array}

Wouldn't it be cool to store the notes of our composition along with their
lengths in one array? Luckily we have an option to do that: using a
\emph{two-dimensional array}.

Schematic representation of a two-dimensional array is shown as the table
\ref{table:array-example-2}.

\begin{table}[ht]
\centering
\begin{tabular}{r|l|l|l}
\multicolumn{1}{l}{Row} & \multicolumn{2}{l}{Column} & \\
\multicolumn{1}{l}{} & \multicolumn{1}{l}{0} & \multicolumn{1}{l}{1} & \\
\cline{2-3}
0 & с4 & 4 & \\
\cline{2-3}
1 & с4 & 4 & \\
\cline{2-3}
2 & g4 & 4 & \\
\cline{2-3}
3 & g4 & 4 & \\
\cline{2-3}
4 & a4 & 4 & \\
\cline{2-3}
5 & a4 & 4 & \\
\cline{2-3}
6 & g4 & 2 & \\
\cline{2-3}
\end{tabular}
\label{table:array-example-2}
\end{table}

Each row of the array stores a description of one note. The zeroth column holds
the note frequency and the first column holds its length as the fraction
denominator, where the numerator is the length of the bar. For example, the
zeroth note (``C4'') has the length of $\frac{1}{4}$, so the in the array the
length is specified as 4.

In the program code we can write down this table as the following array:

\begin{minted}{cpp}
float melody[28][2] = {
{c4, 4}, {c4, 4}, {g4, 4}, {g4, 4},
{a4, 4}, {a4, 4}, {g4, 2},
{f4, 4}, {f4, 4}, {e4, 4}, {e4, 4},
{d4, 4}, {d4, 4}, {c4, 2},
{g4, 4}, {g4, 4}, {f4, 4}, {f4, 4},
{e4, 4}, {e4, 4}, {d4, 2},
{g4, 4}, {g4, 4}, {f4, 4}, {f4, 4},
{e4, 4}, {e4, 4}, {d4, 2},
};
\end{minted}

As can be seen from this example, each element of our array in fact just an
one-dimensional array, surrounded with curly brackets. For example, the element
with the index 0 of our array stores an one-dimensional array \texttt{\{c4,
4\}}, that in turn holds the note (``c4'') and its length (4.)

Now we can adapt the code that plays the melody for using the two-dimensional
array:

\begin{minted}{cpp}
// ...

void loop() {
const long BPM = 120;
const long MINUTE = 60000000;
const long T = (MINUTE / BPM) * 4;

for (int note_idx = 0; note_idx < 28; note_idx++) {
play_tone(SPEAKER_PIN,
melody[note_idx][0],
T / melody[note_idx][1]);
delay(100);
}
}
\end{minted}

Using a two-dimensional array, we can briefly and succinctly describe even more
complicated melodies than ``Twinkle, Twinkle, Little Star''.

At this stage we have to discuss how the \emph{musical staff}, that holds the
music notes, works. We need this to be able to read the notes on our own.

\end{document}
2 changes: 1 addition & 1 deletion src/en/sparc.tex
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ \chapter{Music and Technology Synthesis}
\subfile{sections/music-and-technology-synthesis-octave-system}
\subfile{sections/music-and-technology-synthesis-simple-melodies}
\subfile{sections/music-and-technology-synthesis-arrays}
\subfile{sections/music-and-technology-synthesis-two-dimensional-arrays}

%% TODO:
%% \subfile{sections/music-and-technology-synthesis-two-dimensional-arrays}
%% \subfile{sections/music-and-technology-synthesis-staff}
%% \subfile{sections/music-and-technology-synthesis-rest}
%% \subfile{sections/music-and-technology-synthesis-dotted-notes}
Expand Down

0 comments on commit dd30dbd

Please sign in to comment.