Preparing the data • tim rodenbröker creative coding

Preparing the data

Courses / Form follows Music / Preparing the data

In this lesson, I’ll explain how I prepared the data. It’s going to be quite theoretical here. If you want to jump right in, feel free to skip this lesson.

About the data

The Prelude in C is the first piece from Johann Sebastian Bach’s “Well-tempered Clavier”, a collection of 24 very different pieces for piano. What is unique about this first piece is its strict structure: It consists of a sequence of single notes played in evenly-spaced succession. In music, this is called an arpeggio.

Theoretically, the prelude in C can be said to be a simple array of notes. When I got the idea to create visualizations of the piece with Processing, I looked out to find a solution to really put the music into the form of an numerical array in Processing.

There are websites that offer the music of Johann Sebastian Bach for download in the MIDI format. MIDI files contain the notes of a piece of music, but with many additional data points such as velocity, timing, etc.

The MIDI format is not human readable: If you open such a file in the code editor you get a wall of cryptic symbols.

�h�Q�N� �Q�C� �Q� �QA+� �QjW� �Q�Jx�Q~�x�Q�6�X�Q�ux�Q�6�`�Q@$x�Q3{�p�Q"���8�Q!��Fine��Q�h�/MTrk
��!�Piano right��d
@[� bdca426d104a26ac9dcb070447587523�q�C8xCH<xHL?lLC2xCH/xHL/xL�pC8xCH<xHL>lLC2xCH0xHL0w�[�L�pE8xEJ<xJMAlME3xEJ2xJM2xM�pE:xEJ<xJMAlME4xEJ2xJM2w�[�M�pC8xCJ<xJMAlMC3xCJ2xJM2xM�pC8xCJ<xJMAlMC4xCJ2xJM2w�[�M�pC5xCH7xHL<lLC2xCH/xHL/xL�pC7xCH9xHL=lLC2xCH0xHL0w�[�L�pE=xELBxLQFlQE7xEL3xLQ3xQ�pE=xELAxLQDlQE7xEL4xLQ4w�[�Q�pB8xBE<xEJ?lJB2xBE/xEJ/xJ�pB8xBE<xEJ>lJB2xBE0xEJ0w�[�J�pC:xCJ?xJOBlOC3xCJ2xJO2xO�pC:xCJ>xJOAlOC4xCJ2xJO2w�[�O�p@8x@C<xCH?lH@2x@C/xCH/xH�p@8x@C<xCH>lH@2x@C0xCH0w�[�H�p@8x@C<xCH?lH@2x@C/xCH/xH�p@8x@C<xCH>lH@2x@C0xCH0w�[�H�p>8x>B<xBH?lH>2x>B/xBH/xH�p>8x>B<xBH>lH>2x>B0xBH0w�[�H�p>5x>C8xCG<lG>.x>C-xCG-xG�p>5x>C8xCG:lG>/x>C-xCG-w�[�G�p@5x@C8xCI<lI@.x@C-xCI-xI�p@5x@C8xCI:lI@/x@C-xCI-w�[�I�p>8x>E<xEJ?lJ>2x>E/xEJ/xJ�p>8x>E<xEJ>lJ>2x>E0xEJ0w�[�J�p>5x>A8xAG<lG>.x>A-xAG-xG�p>5x>A8xAG:lG>/x>A-xAG-w�[�G�p<5x<C8xCH<lH<.x<C-xCH-xH�p<5x<C8xCH:lH</x<C-xCH-w�[�H�p92x9<5x<A8lA9-x9<*x<A*xA�p92x9<5x<A7lA9-x9<+x<A+w�[�A�p92x9<5x<A8lA9-x9<*x<A*xA�p92x9<5x<A7lA9-x9<+x<A+w�[�A�p72x7;5x;A8lA7-x7;*x;A*xA�p72x7;5x;A7lA7-x7;+x;A+w�[�A�p72x7<5x<@8l@7-x7<*x<@*x@�p72x7<5x<@7l@7-x7<+x<@+w�[�@�p:/x:<2x<@5l@:)x:<(x<@(x@�p:/x:<2x<@4l@:*x:<(x<@(w�[�@�p9/x9<2x<@5l@9)x9<(x<@(x@�p9/x9<2x<@4l@9*x9<(x<@(w�[�@�p92x9<5x<?8l?9-x9<*x<?*x?�p92x9<5x<?7l?9-x9<+x<?+w�[�?�p;/x;<2x<>5l>;)x;<(x<>(x>�p;/x;<2x<>4l>;*x;<(x<>(w�[�>�p72x7;5x;>8l>7-x7;*x;>*x>�p72x7;5x;>7l>7-x7;+x;>+w�[�>�p72x7<5x<@8l@7-x7<*x<@*x@�p72x7<5x<@7l@7-x7<+x<@+w�[�@�p75x7<8x<A<lA7.x7<-x<A-xA�p75x7<8x<A:lA7/x7<-x<A-w�[�A�p75x7;8x;A<lA7.x7;-x;A-xA�p75x7;8x;A:lA7/x7;-x;A-w�[�A�p98x9<<x<B?lB92x9</x<B/xB�p98x9<<x<B>lB92x9<0x<B0w�[�B�p7:x7<?x<CBlC73x7<2x<C2xC�p7:x7<>x<CAlC74x7<2x<C2w�[�C�p72x7<5x<A8lA7-x7<*x<A*xA�p72x7<5x<A7lA7-x7<+x<A+w�[�A�p72x7;5x;A8lA7-x7;*x;A*xA�p72x7;5x;A7lA7-x7;+x;A+w�[�A�p75x7:8x:@<l@7.x7:-x:@-x@�p75x7:8x:@:l@7/x7:-x:@-x@�o52x595x9<7x<A5xA<5x<95x9<7x<92x95/x59.x95.x52*x25+x52&x2�pC2xCG5xGJ7xJM<xMJ7xJG5xGJ8xJG5xGC8xCG5xG>7x>A2xA@2x@>2x>H2C2@2� @CH�/MTrkX�!�

To make MIDI data readable, you can use this tool to convert it to a so-called JSON format, which makes each note with all its properties accessible as an object.

{ 
  "name": "G4",
    "midi": 67,
    "time": 129.9324627499999,
    "velocity": 0.4409448818897638,
    "duration": 0.20270274999998605
}

The entry "midi" is especially important here, because it represents the pitch. In the next step I wrote a script, from the JSON file I created a simple array of integers for the midi pitch.

int[] notes = {24, 28, 31, 36, 40, 31, 36, 40, 24, 28, 31, 36, 40, 31, 36, 40, 24, 26, 33, 38, 41, 33, 38, 41, 24, 26, 33, 38, 41, 33, 38, 41, 23, 26, 31, 38, 41, 31, 38, 41, 23, 26, 31, 38, 41, 31, 38, 41, 24, 28, 31, 36, 40, 31, 36, 40, 24, 28, 31, 36, 40, 31, 36, 40, 24, 28, 33, 40, 45, 33, 40, 45, 24, 28, 33, 40, 45, 33, 40, 45, 24, 26, 30, 33, 38, 30, 33, 38, 24, 26, 30, 33, 38, 30, 33, 38, 23, 26, 31, 38, 43, 31, 38, 43, 23, 26, 31, 38, 43, 31, 38, 43, 23, 24, 28, 31, 36, 28, 31, 36, 23, 24, 28, 31, 36, 28, 31, 36, 21, 24, 28, 31, 36, 28, 31, 36, 21, 24, 28, 31, 36, 28, 31, 36, 14, 21, 26, 30, 36, 26, 30, 36, 14, 21, 26, 30, 36, 26, 30, 36, 19, 23, 26, 31, 35, 26, 31, 35, 19, 23, 26, 31, 35, 26, 31, 35, 19, 22, 28, 31, 37, 28, 31, 37, 19, 22, 28, 31, 37, 28, 31, 37, 17, 21, 26, 33, 38, 26, 33, 38, 17, 21, 26, 33, 38, 26, 33, 38, 17, 20, 26, 29, 35, 26, 29, 35, 17, 20, 26, 29, 35, 26, 29, 35, 16, 19, 24, 31, 36, 24, 31, 36, 16, 19, 24, 31, 36, 24, 31, 36, 16, 17, 21, 24, 29, 21, 24, 29, 16, 17, 21, 24, 29, 21, 24, 29, 14, 17, 21, 24, 29, 21, 24, 29, 14, 17, 21, 24, 29, 21, 24, 29, 7, 14, 19, 23, 29, 19, 23, 29, 7, 14, 19, 23, 29, 19, 23, 29, 12, 16, 19, 24, 28, 19, 24, 28, 12, 16, 19, 24, 28, 19, 24, 28, 12, 19, 22, 24, 28, 22, 24, 28, 12, 19, 22, 24, 28, 22, 24, 28, 5, 17, 21, 24, 28, 21, 24, 28, 5, 17, 21, 24, 28, 21, 24, 28, 6, 12, 21, 24, 27, 21, 24, 27, 6, 12, 21, 24, 27, 21, 24, 27, 8, 17, 23, 24, 26, 23, 24, 26, 8, 17, 23, 24, 26, 23, 24, 26, 7, 17, 19, 23, 26, 19, 23, 26, 7, 17, 19, 23, 26, 19, 23, 26, 7, 16, 19, 24, 28, 19, 24, 28, 7, 16, 19, 24, 28, 19, 24, 28, 7, 14, 19, 24, 29, 19, 24, 29, 7, 14, 19, 24, 29, 19, 24, 29, 7, 14, 19, 23, 29, 19, 23, 29, 7, 14, 19, 23, 29, 19, 23, 29, 7, 15, 21, 24, 30, 21, 24, 30, 7, 15, 21, 24, 30, 21, 24, 30, 7, 16, 19, 24, 31, 19, 24, 31, 7, 16, 19, 24, 31, 19, 24, 31, 7, 14, 19, 24, 29, 19, 24, 29, 7, 14, 19, 24, 29, 19, 24, 29, 7, 14, 19, 23, 29, 19, 23, 29, 7, 14, 19, 23, 29, 19, 23, 29, 0, 12, 19, 22, 28, 19, 22, 28, 0, 12, 19, 22, 28, 19, 22, 28, 0, 12, 17, 21, 24, 29, 24, 21, 24, 21, 17, 21, 17, 14, 17, 14, 0, 14, 31, 35, 38, 41, 38, 35, 38, 35, 31, 35, 26, 29, 28, 26, 0};

The numeric values in this array now each represent a key on the keyboard. To make the data more expressive for the planned visualizations, I compressed the numeric range of tones between 0 and 47 to a much narrower range between 0 and 11 by grouping the same tones from different octaves.

int[] notes = {0, 4, 7, 0, 4, 7, 0, 4, 0, 4, 7, 0, 4, 7, 0, 4, 0, 2, 9, 2, 5, 9, 2, 5, 0, 2, 9, 2, 5, 9, 2, 5, 11, 2, 7, 2, 5, 7, 2, 5, 11, 2, 7, 2, 5, 7, 2, 5, 0, 4, 7, 0, 4, 7, 0, 4, 0, 4, 7, 0, 4, 7, 0, 4, 0, 4, 9, 4, 9, 9, 4, 9, 0, 4, 9, 4, 9, 9, 4, 9, 0, 2, 6, 9, 2, 6, 9, 2, 0, 2, 6, 9, 2, 6, 9, 2, 11, 2, 7, 2, 7, 7, 2, 7, 11, 2, 7, 2, 7, 7, 2, 7, 11, 0, 4, 7, 0, 4, 7, 0, 11, 0, 4, 7, 0, 4, 7, 0, 9, 0, 4, 7, 0, 4, 7, 0, 9, 0, 4, 7, 0, 4, 7, 0, 2, 9, 2, 6, 0, 2, 6, 0, 2, 9, 2, 6, 0, 2, 6, 0, 7, 11, 2, 7, 11, 2, 7, 11, 7, 11, 2, 7, 11, 2, 7, 11, 7, 10, 4, 7, 1, 4, 7, 1, 7, 10, 4, 7, 1, 4, 7, 1, 5, 9, 2, 9, 2, 2, 9, 2, 5, 9, 2, 9, 2, 2, 9, 2, 5, 8, 2, 5, 11, 2, 5, 11, 5, 8, 2, 5, 11, 2, 5, 11, 4, 7, 0, 7, 0, 0, 7, 0, 4, 7, 0, 7, 0, 0, 7, 0, 4, 5, 9, 0, 5, 9, 0, 5, 4, 5, 9, 0, 5, 9, 0, 5, 2, 5, 9, 0, 5, 9, 0, 5, 2, 5, 9, 0, 5, 9, 0, 5, 7, 2, 7, 11, 5, 7, 11, 5, 7, 2, 7, 11, 5, 7, 11, 5, 0, 4, 7, 0, 4, 7, 0, 4, 0, 4, 7, 0, 4, 7, 0, 4, 0, 7, 10, 0, 4, 10, 0, 4, 0, 7, 10, 0, 4, 10, 0, 4, 5, 5, 9, 0, 4, 9, 0, 4, 5, 5, 9, 0, 4, 9, 0, 4, 6, 0, 9, 0, 3, 9, 0, 3, 6, 0, 9, 0, 3, 9, 0, 3, 8, 5, 11, 0, 2, 11, 0, 2, 8, 5, 11, 0, 2, 11, 0, 2, 7, 5, 7, 11, 2, 7, 11, 2, 7, 5, 7, 11, 2, 7, 11, 2, 7, 4, 7, 0, 4, 7, 0, 4, 7, 4, 7, 0, 4, 7, 0, 4, 7, 2, 7, 0, 5, 7, 0, 5, 7, 2, 7, 0, 5, 7, 0, 5, 7, 2, 7, 11, 5, 7, 11, 5, 7, 2, 7, 11, 5, 7, 11, 5, 7, 3, 9, 0, 6, 9, 0, 6, 7, 3, 9, 0, 6, 9, 0, 6, 7, 4, 7, 0, 7, 7, 0, 7, 7, 4, 7, 0, 7, 7, 0, 7, 7, 2, 7, 0, 5, 7, 0, 5, 7, 2, 7, 0, 5, 7, 0, 5, 7, 2, 7, 11, 5, 7, 11, 5, 7, 2, 7, 11, 5, 7, 11, 5, 0, 0, 7, 10, 4, 7, 10, 4, 0, 0, 7, 10, 4, 7, 10, 4, 0, 0, 5, 9, 0, 5, 0, 9, 0, 9, 5, 9, 5, 2, 5, 2, 0, 2, 7, 11, 2, 5, 2, 11, 2, 11, 7, 11, 2, 5, 4, 2, 0};

This simple dataset is now perfectly prepared for a wide range of generative experiments.


Related links

Published by Tim on Monday August 1, 2022

Last modified on April 4th, 2023 at 15:48

  1. Preparing the data
  2. Linear Distribution
  3. Two-dimensional distribution of one-dimensional data
  4. Addon: Circular distribution
  5. Example: Bach Mechanics
  6. Example: 3D Whirl
  7. Example: Digital Mobilé
  8. Wrapping up