We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
I am obviously new in Processing and I wanted to code a typewriter-like program that would load sounds every time a key is pressed, as well as printing it. The only thing is that the symbol printed needs to be different from the key (for example, when "2" is pressed, "ah" has to be printed). I managed to make it work for all the keys except for BACKSPACE and RETURN. It works for DELETE, even if I didn't specify anything for this key, but I can't make it work for the other ones. Does anyone have any idea of how I could do it? Here is the code so that you can have a look at it. Thanks for your help!
import ddf.minim.*;
Minim minim;
AudioPlayer song;
String text1 = " ";
PFont font;
int bfill = 150;
boolean randomizer = false;
void setup() {
size(400, 400);
smooth();
font = loadFont("Arial-BoldMT-20.vlw");
textFont(font, 20);
minim = new Minim(this);
}
void draw() {
background(bfill);
fill(255);
text(text1, 0, 40, width, height);
if (randomizer) {
for (int j = 0; j < 400; j++) {
text1 += char(int(random(18, 126)));
}
text(text1, 0, 40, width, height);
text1 = " ";
}
}
void keyPressed() {
println(key);
if (key == BACKSPACE) {
if (text1.length() > 0) {
text1 = text1.substring(0, text1.length() - 1);
}
}
if (key == RETURN) {
text1 += key;
}
if (key == '1') {
text1 += "æ";
song = minim.loadFile("æ.mp4");
song.play();
}
if (key == '2') {
text1 += "ah";
song = minim.loadFile("ah.mp4");
song.play();
}
if (key == '3') {
text1 += "aw";
song = minim.loadFile("aw.mp4");
song.play();
}
if (key == '4') {
text1 += "ɑ";
song = minim.loadFile("ɑ.mp4");
song.play();
}
if (key == '5') {
text1 += "ɑr";
song = minim.loadFile("ɑr.mp4");
song.play();
}
if (key == '6') {
text1 += "ɒ";
song = minim.loadFile("ɒ.mp4");
song.play();
}
if (key == '7') {
text1 += "ə";
song = minim.loadFile("ə.mp4");
song.play();
}
if (key == '8') {
text1 += "ər";
song = minim.loadFile("ər.mp4");
song.play();
}
if (key == '9') {
text1 += "ɛ";
song = minim.loadFile("ɛ.mp4");
song.play();
}
if (key == '0') {
text1 += "ɛr";
song = minim.loadFile("ɛr.mp4");
song.play();
}
if (key == '-') {
text1 += "ɪ";
song = minim.loadFile("ɪ.mp4");
song.play();
}
if (key == '=') {
text1 += "ɪr";
song = minim.loadFile("ɪr.mp4");
song.play();
}
if (key == 'q') {
text1 += "q";
song = minim.loadFile("ð.mp4");
song.play();
}
if (key == 'w') {
text1 += "w";
song = minim.loadFile("w.mp4");
song.play();
}
if (key == 'e') {
text1 += "i";
song = minim.loadFile("i.mp4");
song.play();
}
if (key == 'r') {
text1 += "r";
song = minim.loadFile("r.mp4");
song.play();
}
if (key == 't') {
text1 += "t";
song = minim.loadFile("t.mp4");
song.play();
}
if (key == 'y') {
text1 += "y";
song = minim.loadFile("y.mp4");
song.play();
}
if (key == 'u') {
text1 += "u";
song = minim.loadFile("u.mp4");
song.play();
}
if (key == 'i') {
text1 += "i";
song = minim.loadFile("aj.mp4");
song.play();
}
if (key == 'o') {
text1 += "o";
song = minim.loadFile("o.mp4");
song.play();
}
if (key == 'p') {
text1 += "p";
song = minim.loadFile("p.mp4");
song.play();
}
if (key == '[') {
text1 += "ɔj";
song = minim.loadFile("ɔj.mp4");
song.play();
}
if (key == ']') {
text1 += "ɔr";
song = minim.loadFile("ɔr.mp4");
song.play();
}
if (key == 'a') {
text1 += "a";
song = minim.loadFile("e.mp4");
song.play();
}
if (key == 's') {
text1 += "s";
song = minim.loadFile("s.mp4");
song.play();
}
if (key == 'd') {
text1 += "d";
song = minim.loadFile("d.mp4");
song.play();
}
if (key == 'f') {
text1 += "f";
song = minim.loadFile("f.mp4");
song.play();
}
if (key == 'g') {
text1 += "g";
song = minim.loadFile("g.mp4");
song.play();
}
if (key == 'h') {
text1 += "h";
song = minim.loadFile("h.mp4");
song.play();
}
if (key == 'j') {
text1 += "j";
song = minim.loadFile("j.mp4");
song.play();
}
if (key == 'k') {
text1 += "k";
song = minim.loadFile("c.mp4");
song.play();
}
if (key == 'l') {
text1 += "l";
song = minim.loadFile("l.mp4");
song.play();
}
if (key == ';') {
text1 += "ŋ";
song = minim.loadFile("ŋ.mp4");
song.play();
}
if (key == 'z') {
text1 += "z";
song = minim.loadFile("z.mp4");
song.play();
}
if (key == 'x') {
text1 += "ʃ";
song = minim.loadFile("ʃ.mp4");
song.play();
}
if (key == 'c') {
text1 += "tʃ";
song = minim.loadFile("tʃ.mp4");
song.play();
}
if (key == 'v') {
text1 += "v";
song = minim.loadFile("v.mp4");
song.play();
}
if (key == 'b') {
text1 += "b";
song = minim.loadFile("b.mp4");
song.play();
}
if (key == 'n') {
text1 += "n";
song = minim.loadFile("n.mp4");
song.play();
}
if (key == 'm') {
text1 += "m";
song = minim.loadFile("m.mp4");
song.play();
}
if (key == ',') {
text1 += "ʊ";
song = minim.loadFile("ʊ.mp4");
song.play();
}
if (key == '.') {
text1 += "ʒ";
song = minim.loadFile("ʒ.mp4");
song.play();
}
}
void stop()
{
song.close();
minim.stop();
super.stop();
}
Answers
Please, format your code post: highlight it & hit CTRL+K.
Anyways, a similar online example: http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest
Thanks for the answer GoToLoop, and sorry for the format of my code, will do better next time, I didn't realize it was not keeping the intends as in Processing.
The similar example you showed me is a little bit confusing to be, it is using arrays right? How can I call every independent key and attribute it a sound and specific "printed element" with this system? So far I've been doing it like this:
Is there a way to integrate this to the example you showed me? Not so familiar with arrays sorry.
No need for next time. We can edit our own posts at any time! ;;)
Ha ha perfect, thanks^^ looks way better now
W/o learning arrays (and other data structures), programing becomes an endless repetition of lines!
Some array references:
I'll check this up! Thanks again
I've decided to adapt my online version to match your sketch. Ended up successful but very complicated! 8-|
Besides an array, it uses a class named Phoneme, which represents a key to type in, 1 or 2 phoneme letters plus an AudioPlayer: ch, hash, au respectively.
It relies on Arrays.binarySearch() in order to find out the array's index which matches a keyTyped():
http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#binarySearch-java.lang.Object:A-java.lang.Object-
Well, better check it out for yourself. Any further doubts related to it, just ask here: ;)
After some thought, some stuffs in class Phoneme won't ever be necessary.
More specifically, the whole hashCode() & equals() features.
Cut them out in order to make the class easier to comprehend. Version 3.04: o->
Thank you so much for the help GoToLoop! I tried to run the program but I keep getting this error message and I don't know what it means..!
It says that its createInput()'s failed! Probably file name String, that is
s + "." + ext
, wasn't found!Pay attention that in
phones[0] = new Phoneme('8', "ər", EXT, minim);
, constant EXT = "mp3".You gotta modify those constants to match your own situation! ~:>
Constants you should pay special attention to: QTY, FONT & EXT.