-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
en: music-and-technology-synthesis-two-dimensional-arrays: New file
* 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
1 parent
78c399f
commit dd30dbd
Showing
3 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/en/sections/music-and-technology-synthesis-two-dimensional-arrays.tex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters