Search code examples
audiorandomtimeprocessingmetronome

Processing - Building Visual Metronome - Drummer craving some code


I'm new to Processing and trying to build a metronome of a specific kind (I'm a drummer) that would enable me to create a random letter generator (string [or is it an array?] from A to X) that would randomly choose a character from a string and then would continue randomly generating those characters but also showing what the previously chosen character was as well.

I've got a certain drum pattern assigned to each letter so instead of playing them from A to X, and then X to A, I would like to push my practise further and play that particular alphabet at random. I could obviously try writing it all down on paper or find some other easier way of creating random variations of all that, but why should I make my life easier? :D

The "metronome" would start with just one character, let's say "D", and then after 2 seconds for example, it would randomly choose another one, let's say "X" and so on. So the first run would show D, and then the next DX, and the next XL etc. I've figured out how to generate one character from that string, which I know is very basic:

// Get a random element from an array<br>
String[] words = {"A","B","C","D","E","F","G","H","I","J","L","M","N","O","P","Q","R","S","T","U","V","W","X"};<br>
int index = int(random(words.length));  
println(words[index]);  // Prints one of the 24 letters words

So, then I'm aiming to be able to change the amount of time the particular "beat" lasts.

And in the end, I want to assign a sound to each new variation change, so it would sound like an actual metronome.

I'll be grateful for any help P{rocessing}eople,

Best, SHB


Solution

  • The following demo shows how to concatenate two random String array elements into two letter text, always adding the new element to the right hand side. The editor 'println()' output should show you what is being done. The demo could be run faster by increasing the frameRate();

    String[] words = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X"};
    int newIndex = 0;
    int oldIndex = -1;
    
    void setup() {
      size(200, 200);
      background(219);
    }
    
    void draw() {
      frameRate(1);
      background(219);
      // Get a random element from array
      newIndex = int(random(words.length));
      fill(0);
      if (oldIndex > -1) {
        text(words[oldIndex] + words[newIndex], 60, 100);
        println("old =", words[oldIndex] + " : " +  "new =", words[newIndex] );
      } else {
        text(words[newIndex], 60, 100);
        println("new =", words[newIndex]);
      }
      oldIndex = newIndex;
    }