Assignment #1: Random Melody

Your assignment is to make a patch that plays back a short random melody (5 notes) and then asks the user to play it back using the keys 1-5 on the computer keyboard. The user will be informed whether he did good or not.

Let’s first think about what components are needed to build something like this:

  1. Something which plays 5 random notes
  2. Something where the user can play a melody using the computer keys
  3. Something which compares the two melodies and gives the user feedback.

Begin with the first part and again, have a look which components are needed to do so.

Goal 1: playback five random notes

1 a) you need to playback five random notes, thus we need a counter.

1 b) you need  to playback five random notes, thus we need an object to to produce randomness: random.

1 c) you need  to playback five random notes, thus we need  a mechanism to produce notes: the makenote object will produce midi-messages which then can cause notes.

1 d) you need  to playback five random notes, thus we need a playback mechanism: the noteout object will be used to send the midi-messages to an synthesizer which will play back the notes.

Now that you know what you need, build it:

1 a) we need to playback five random notes, thus we need something which counts the notes and stops the process once 5 has been reached. We can use the object called counter for that. The following counter object counts from 1 5 when it receives bangs.

In order to receive the bangs, we connect it to a metro object which can be turned on and off. In order to display the counted number, we connect it to a number box:

When the metro is turned on, this counter will continue to count from 1 to 5 and 1 to 5 and 1 to 5 and 1 to 5 and 1 to 5 and 1 to 5 and so on and on and on. Therefor, we add a select object, which puts the metro off, once 5 has been reached.

As we have to trigger a random note five times, let’s a add a button, which bangs exactly 5 times:

1 b) we need  to playback random notes, thus we need something to produce randomness. We could for example choose randomly among all keys of the piano. As this is very difficult and we do not have so many keys on our computer keyboard, we make it easier. We choose randomly among 5 black keys in the middle of the piano. The random object will produce random numbers between 0 and 4 when it receives a bang. We then can map those to the 5 notes on a piano later.


We  now want to map the numbers 0-4 to five black keys somewhere in the middle of a piano. Let’s have a look at the kslider object’s help patch to learn more about that.

We press the black keys starting in the middle, and read out the numbers:

61, 63, 66, 68, 70

We also can see that the numbers are automatically transformed into a midi-message by the makenote object and then send to operatings systems software synthesizer by the noteout object. That is exactly what we need. We copy the part of the help file in our own patch and use the select object to map the random notes to the the numbers 61,63,66,68,70 which cause the black keys to play.

 1 c) we need  to playback five random notes, thus we need  a mechanism to produce notes. We luckily already have found the makenote object.

1 d)  we need  to playback five random notes, thus we need a playback mechanism. We can use the built in software synthesizer and send our Midi-messages to it.  We have already found the noteout object to do so.

Goal 2: we need to be able to play  the same notes using the keys 1-5 on our computer keyboard.

We can do this using the key object to detect keypresses and triggering the same numbers (61,63, 66,68,70) with those presses. We add it to out patch:

Goal 3: Compare the two melodies and give user feedback.

We can devide this task:

3 a) store the notes played back by the computer

3 b) compare user’s melody to the stored notes

3 c) give feedback

We begin with 3 a). There are several methods we could store the played notes. One is using an append message with a replaceable argument to combine the notes into a list.

You can ‘record’ the played notes like this:

 

We can then make sure we produce one list from the notes which the computer played and another one from the notes the user played. We use a gate to make sure the computer’s melody is send to one list and the human’s melody to another list. Furthermore, we use the metro as an indication on who is playing. When the metro is on, the computer is playing and the gate is open on the second outlet. When the metro is not playing, the human is playing and the note-numbers are send out of the left outlet :

To receive the togglestate, we have to add a little “send toggelstate” on the top of the patch. When this toggle is on, the computer is playing. If it is off, it’s time for the human to replay the melody.

Note that we send the toggelstate first (from right-to-left order), and only afterwards the program continues and triggers the first random note.

Once the computer has played the melody and the human player is done replaying it, we want to compare the lists. We use the zl compare object to do so. We send the two lists to the [zl compare] object (first list to the right, second list to the left) when the player presses “enter”. We also provide the user with some feedback (“You did good!/You made a mistake”).

The last thing that we have to do now is to clear the messages which contain the notes before or after every game. Otherwise the new melody would just be added at the end of the previous melody. We can also do this when the metro on top is switched on. The object which detects such switches from zero to one is called togedge.

This is the finished patch:

Don’t worry if you don’t understand everything about this patch. Try to understand as much about it as possible and write down questions you have so we can discuss them in the next lecture. Hand in the patch to the student assistant.

Assignment #1b:

Make a version of this patch that let’s a user play a short melody with the computer keyboard and record these notes into a table. You can limit the melody length/table size to 9 successive notes. Hint: Put in 66 at index 0 of the table if the first note is a 66. Similarly, put 70 at index 1 in the table if the second note is number 70 and so on. Make it possible to clear the table and start from the beginning. Let the computer play back the recorded melody when the user presses enter. Double click on the table to see a visual representation of what you played. Note that no duration/rhythmical information is recorded. Try changing the content of the table by drawing in it and let the computer ‘play back your drawing’.

Assignment #1c:

Make a version of the last patch which uses the coll object instead of the table.