0

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

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

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

我知道代码不能完全为你工作,因为你没有波形文件,所以在下面的链接中,我准备了文件准备好在需要时下载。我在这个例子中也使用了“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;
}
4

1 回答 1

0

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


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;
}
于 2022-01-07T19:34:58.423 回答