3-Way Branch, Part 1

ArtWonk has the ability to test and branch according to the results, which puts it in the same company as other programming languages like C, Basic, and Forth, although AW straddles the line into application territory as well - which makes some of the programming features less obvious because they are built into the modules.

Here is an example of a common algorithmic programming task, a 3 way branch. The task is simple: start with random pitches between 24 and 48, and separate them out so that the low octave is constrained to a major triad, the upper octave is constrained to 5ths and octaves, and the two octaves in the middle play the major scale.

There is much more you could do in an example like this, for example it would be a simple matter to give each of the three groups a different instrument (MIDI Program numbers), and/or to place each one in the Pan field: left, center, and right. But for this tutorial we will keep it simple, and leave the fancy stuff for you to do on your own.

There are many ways to approach this, but they mostly come down to one of two basic paths. We can either separate out three flags that can then be used to enable the three branches, or we can simply switch the processed values through a 3 input switch. The first is more general, the 2nd is more efficient. We will take the first, more general approach here, and then follow up with the 2nd in a separate Part 2 tutorial.

1. Start with a Clock and a RandInt. Connect the Clock strobe Out to the RandInt Strobe input. Set the RandInt Range to 48.

Now you are getting a random stream 4 octaves wide. The task will be to separate that into the 3 streams and to constrain the streams as described above.

2. To test for the upper octave, values 37-48, use the Logic/Greater module. Connect In A to the output of the RandInt; set In B to 36. Now, whenever you get a value greater than 36, the output of the Greater will go True (1), otherwise it will stay at 0.

3. Blinky lights make visulaizing these kinds of tasks much easier, so throw in a Widgets/LED that will turn on when the Greater module goes True. Also, it may help if you document which of the three branches you are testing for, with the Toolbar "Document Workspace" tool. You see this in the example image, below.

4. To test for the lower octave, use GrEqual (Greater than or Equal) module, and reverse the test inputs, so the module tests for Less than or Equal. Connect the output of the RanInt module to the In B of the GrEqual, and set In A to 12. Thus, the module will go True (1) when the RandInt value is 12 or less.

5. Set up a blinky LED and doc label as in step 3.

6. Now we use OR logic to flag the in between values from 13-36. Connect the two test modules, Greater & GrEqual to the 2 inputs of an OR module. What we are really interested in is not the True (1) values at the output, which doesn't tell us much, but the False (0) values, which tell us that the value is not in either the lower or the upper octave, so it must be in the middle range. We invert that with a NOT module to get a flag for the 13-36 range. Finally, set up a blinky and doc label as above.

This gives you the 3 flags for each of the ranges, and if you set up your LED indicators, you will see that always one LED is on, but never more than one.

From here on, you have a great deal of flexibility as to how you use the 3 flags. In this example we will keep it very simple, but you are encouraged to experiment.

7. Set up 3 Process/FitScale modules. Connect the RandInt output to the input of each of them; then set the scale to 5 (Fifths) for the high octave, to 7 (Maj Triad) for the low octave, and 1 (Major) for the middle octaves.

8. Set up a MIDIVoice module and two NoteOut modules. Alternately, you could set up 3 MIDIVoice modules, setting them each to a different MIDI channel. Do this if you plan to set up different instruments, pan positions, etc.

9. Connect the Clock strobe Out to each of the MIDI module Clock inputs. Connect the 3 FitScale modules to the Note input of the 3 MIDI modules. Finally, connect the 3 range flags from the Greater, GrEqual, and Not module to the Play inputs of the MIDI modules. You should hear your sound now.

10. Since we are using the MIDI module Play inputs to do the switching, we need another way to turn the overall sound on and off. Do this by connecting the System module Stop output to its Mute input. then clicking on the toolbar Play button will turn sound on and off.

[Go to Part 2] [Return to Tutorial Page]