如果来自组 "x" 的字母播放声音 "a",如果来自组 "y" 播放声音 "b" + 文本格式

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

我有一段代码(见下文)可以在字母 A 到 X 之间随机选择。我在字符串中的每个字母后创建了几个空字符 space 因为我不确定如何否则,也许你们中的一些人也知道如何解决这个问题?这肯定会使代码看起来更简洁和整洁。

目前,每次选择这些字母时,都会播放一种声音 (click_001.wav)。

我想多样化,所以当它选择字母A到P时,它播放“click_001.wav”,而当从Q到X时,它播放“mouse_click.wav”。

我知道代码不会完全适合你,因为你没有 wave 文件,所以在下面的 link 中,我准备了文件,可以在需要时下载。我在这个例子中也使用了“processing.sound”库,它可以通过处理中的菜单添加到处理中:Sketch > Import library > Add Library > 在搜索栏中输入:sound。这是来自 The Processing Foundation 的声音库。

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

你们能帮我吗?

此致,
还是人

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;
}

您可以通过为每个文本调用设置 x 坐标而不是使用变量来删除 'words' 数组中的空格。我添加了你显然遗漏的字母 'K' 。还添加了一个新功能,可以为节拍字符串中的每个字符播放声音。这两个声音文件需要放在 sketch 文件夹中名为 'data' 的文件夹中。


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;
}

此函数 playSoundForChar() 的修订演示了更改每个声音的速率和音高。它应该允许更清楚地区分两种声音。播放声音的控制台'println'记录应该是准确的。

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;
}