Now that we know how to generate random (and other) functions, it is time to put them in a more practical framework. As we've seen (and heard), just throwing out random notes may be interesting for a short time, but can get pretty stale in a hurry. So much for 'fully non-restricted' music composition. While one could spend some time – a lot of time – looking for some 'magic functions' that would always generate pleasing western-style music, I'd like to move on with just our tiny handful. In the end, it's how you use those random numbers, more so than the functions. And there are plenty of ways to organize the output. We're going to start with some 1890's technology: the piano roll. If you're not familiar with the 'Pianola' or player piano, here's a rendition of Scott Joplin, himself, playing his Maple Leaf Rag, courtesy of the player piano. In a nutshell, it works as follows: a piece of paper is stretched across a metal bar, called the 'tracking bar', that has openings spaced across its face. Each hole in the bar has a pneumatic suction behind it, so that it gently grabs the paper, as it rolls by, pulling it to the metal bar. Every once in a while, there are one or more holes punched in specific locations on the paper. As the hole moves over a suction hole, the air will flow and trigger a piano key to move, playing a note. Short notes may have one or two holes in a row: longer notes have a line of holes. On the left edge, holes will activate the piano's pedals, usually the sustain pedal. Varying the speed of the paper moving across the metal bar increases or decreases the speed, or tempo, of the performance. Dynamics, how loudly or softly the notes were played, were captured and encoded separately. Sometimes this was a manual process, apparently. But it varied, depending on the roll manufacturer. We can electronify that system and go one (or two) better, since we can manipulate 127 note pitches and a velocity as well as the tempo. Not only that, but we can play any sound our MIDI equipment generates: not just a piano sound. The basic concept is the same. Instead of an air-sucking tracker bar, we'll use an array of bytes. Since MIDI supports almost twice the number of notes of the original 65-note player piano, our array will contain 128 note 'slots', representing each MIDI note available. To simulate the holes punched in the paper, we'll need to use a special feature of the Arduino. Since, in the paper version, there either is a hole, or isn't a hole, we can go binary and say a 1 represents a hole and a 0 represents no hole. The special feature I mentioned is the bitRead() function. It allows us to take a byte value, having 8 bits, and to extract each bit at will. Using these two concepts, here's what we'll do: 1): turn off all notes, just to make sure there are no hangers-on2): put a random value in each of the 127 'slots' – one for each possible MIDI note3): loop through each of the eight bits of all 127 notes. If the current note we are looking at has a 1 in the current bit position we are looking at, we will play the note. Otherwise that note will be turned off.4): wash, rinse, repeat. As you can see, this allows us to get eight note events from each single byte value.

In this short example of a single note, the system will play MIDI note #60 when the loop pulls bit 2 and then again when it reaches bit 5. Remember, though, this happens for all 127 notes. Since most keyboards only allow a finite number of notes to be played at a single time, you won't necessarily hear all the notes. Which is okay. That would sound like nothing much musical. Which it won't, anyway. But we will fix that, soon enough.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.*/#include <stdarg.h>#include <stdio.h>#include <SoftwareSerial.h>

If you load up the code and let it run, I'm willing to bet that maybe 15 seconds goes by before you pull the plugs. This is not music. This is even worse than me at age 5, banging on as many piano keys as I could. On a positive note, we have certainly got ample room for improvement. If you uncomment the last line of the noteSlot loop in the loop() function, there is a somewhat more pleasing effect. We are inserting a delay to play the notes in sequence – arpeggio is the musical term. By adding a random amount of delay, it starts being more interesting. For a while, anyway.If you play with the fillRoll(RND_TAN); statement by changing the random type, you'll notice it is difficult to hear any huge difference using the random types. In the previous code, we used the random number to select a pitch. Now, the random number is being used to provide a set of switches, if you will, that turns each note either on or off. The randomness is now masked by the element of time. Since the algorithm determines when a note will sound and not which note will be heard, the ability to 'lock on' to the random pattern is harder.If you uncomment the last two lines of the setup() function, you can more easily hear a difference, as the system will generate only notes between the rollLow and rollHi MIDI note numbers. Have fun with it, until next time, when we will have several more techniques and start beginning to believe this thing can actually make some music...