pythonaudiotextprocessingmetronome

If a letter from group "x" play sound "a", if from group "y" play sound "b" + text formatting


I've got that bit of code (see below) which randomly selects between letters A through to X. I created few characters empty space after each letter in string because I'm not sure how to do that otherwise, maybe some of you knows how to fix that as well? That would surely make the code look a bit more condense and neat.

At the moment every time each of these letters gets selected one sound is being played (click_001.wav).

I want to diversify it, so when it selects letters from A to P, it plays "click_001.wav", and when from Q to X, it plays "mouse_click.wav".

I know the code won't work for you fully since you haven't got the wave file so in the link below I prepared the files ready to download if needed. I'm using "processing.sound" library in this example as well which can be added to processing via menu in processing: Sketch > Import library > Add Library > in the search bar type: sound. It's the sound library from The Processing Foundation.

https://www.dropbox.com/sh/madgao8vhjum6yz/AADJsNWQAvcyIaP8aVjWN6-Sa?dl=0

Could any of you help me with that, please?

Best regards,
Still human being

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;
PFont SansSerif;
String beat = "";
int x = 0;
import processing.sound.*;
SoundFile file;

void setup() {
  size(950, 600);
  SansSerif = createFont("SansSerif", 150);
  textFont(SansSerif);
}

void draw() {
  frameRate(.6); 
  background(35); 
  // Get a random element from array
  newIndex = int(random(words.length));  
  if (oldIndex > -1) {
     file = new SoundFile(this, "click_001.wav");
     file.play();
    beat = words[oldIndex] + words[newIndex];
    int x = 250;
    for (int i = 0; i < beat.length(); i++){
      if(i == 0){
        
        fill(250);
      } else {
      
        fill(50);
      }
      text(beat.charAt(i), x, 350);
      x += 65;
    }
    println("old =", words[oldIndex] + " : " +  "new =", words[newIndex] ); 
  } else {
    fill(250);
    text(words[newIndex],  250, 350);
    println("new =", words[newIndex]);
  }
  oldIndex = newIndex;
}

Solution

  • You can drop the spaces in the 'words' array by just setting the x coordinate for each text call instead of using a variable. I added the letter 'K' which you apparently left out. Also added was a new function to play a sound for each character in the beat string. The two sound files need to be placed in a folder titled 'data' in the sketch folder.

    
    String[] words = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X"};
    
    int newIndex = 0;
    int oldIndex = -1;
    PFont SansSerif;
    String beat = "";
    
    import processing.sound.*;
    SoundFile file;
    
    void setup() {
      size(800, 600);
      SansSerif = createFont("SansSerif", 150);
      textFont(SansSerif);
    }
    
    void playSoundForChar(char inChar) {
      if ((inChar=='Q')||(inChar=='R')||(inChar=='S')||(inChar=='T')||(inChar=='U')||(inChar=='V')||(inChar=='W')||(inChar=='X')||(inChar=='Y')||(inChar=='Z')) {
        file = new SoundFile(this, "mouse_click.wav");
        println("click2", inChar);
      } else {
        file = new SoundFile(this, "click_001.wav");
        println("click1", inChar);
      }
      file.play();
    }
    
    void draw() {
      // frameRate(.6);
      frameRate(1);
      background(35);
      // Get a random element from array
      newIndex = int(random(words.length));
      if (oldIndex > -1) {
        beat = words[oldIndex] + words[newIndex];
        println("old =", words[oldIndex] + " : " +  "new =", words[newIndex] );
        for (int i = 0; i < beat.length(); i++) {
          if (i == 0) {
            fill(250);
            text(beat.charAt(i), 250, 350);
            playSoundForChar(beat.charAt(i));
          } else {
            fill(50);
            text(beat.charAt(i), 450, 350);
            playSoundForChar(beat.charAt(i));
          }
        }
      } else {
        beat = words[newIndex];
        println("new =", words[newIndex]);
        for (int i = 0; i < beat.length(); i++) {
          if (i == 0) {
            fill(250);
            text(beat.charAt(i), 250, 350);
            playSoundForChar(beat.charAt(i));
          }
        }
      }
      oldIndex = newIndex;
    }
    
    

    This revision of function playSoundForChar() demonstrates changing the rate and pitch of each sound. It should allow the two sounds to be more clearly distinguished. The console 'println' record of which sound was played should be accurate.

    import processing.sound.*;
    SoundFile file;
    
    String[] words = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X"};
    int newIndex = 0;
    int oldIndex = -1;
    PFont SansSerif;
    String beat = "";
    
    void setup() {
      size(800, 600);
      SansSerif = createFont("SansSerif", 150);
      textFont(SansSerif);
    }
    
    void playSoundForChar(char inChar) {
      if ((inChar=='Q')||(inChar=='R')||(inChar=='S')||(inChar=='T')||(inChar=='U')||(inChar=='V')||(inChar=='W')||(inChar=='X')||(inChar=='Y')||(inChar=='Z')) {
        file = new SoundFile(this, "4clicks.mp3");
        println("click2", inChar);
        file.play(2); // plays it twice as fast, one octave up
      } else {
        file = new SoundFile(this, "3clicks.mp3");
        println("click1", inChar);
        file.play(0.5); // plays it half as fast, one octave down
      }
    }
    
    void draw() {
      frameRate(.6);
      background(35);
      // Get a random element from array
      newIndex = int(random(words.length));
      if (oldIndex > -1) {
        beat = words[oldIndex] + words[newIndex];
        println("old =", words[oldIndex] + " : " +  "new =", words[newIndex] );
        for (int i = 0; i < beat.length(); i++) {
          if (i == 0) {
            fill(250);
            text(beat.charAt(i), 250, 350);
            playSoundForChar(beat.charAt(i));
          } else {
            fill(50);
            text(beat.charAt(i), 450, 350);
            playSoundForChar(beat.charAt(i));
          }
        }
      } else {
        beat = words[newIndex];
        println("new =", words[newIndex]);
        for (int i = 0; i < beat.length(); i++) {
          if (i == 0) {
            fill(250);
            text(beat.charAt(i), 250, 350);
            playSoundForChar(beat.charAt(i));
          }
        }
      }
      oldIndex = newIndex;
    }