Minim looping sounds, how to get end of sound
in
Core Library Questions
•
1 year ago
Dear all interested readers,
I have put together a script that loads a textfile, trims the words, scans the words for specific information, and then let the Minim library speak out the words via Google Speech (last part is taken from an example that I found on this forum).
The problem is that I want to load the new word when the playing of the MP3 file is done, to play the next MP3 (of the new word) right after ending the MP3 that was playing before. Not before that, which is now the case.
Who can help me with this? I've been trying to fix it for the last days, but I cannot seem to find the right sollution.
I would be forever grateful.
Code I have now:
The part to get the new word to processed is:
if (counter < origtext.length) {
teller = teller+1;
counter = origtext.length-origtext.length+teller;
}
I want this part only to occur when the previous file is done playing, but I cannot seem to get it right. Been allready testing by using -- if (player.isPlaying()), -- and put the code inside this if statement, but that doesn't work. Does anybody knows a good solution for this? Would be great If someone could give me a tip.
Thanks in advance,
Joshua
I have put together a script that loads a textfile, trims the words, scans the words for specific information, and then let the Minim library speak out the words via Google Speech (last part is taken from an example that I found on this forum).
The problem is that I want to load the new word when the playing of the MP3 file is done, to play the next MP3 (of the new word) right after ending the MP3 that was playing before. Not before that, which is now the case.
Who can help me with this? I've been trying to fix it for the last days, but I cannot seem to find the right sollution.
I would be forever grateful.
Code I have now:
- boolean kollom1 = true;
boolean kollom2 = false;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
String s = "";
int verplaats = 0;
int teller;
int kollom = 30;
int stopteller;
int timer;
PFont reg;
PFont reg2;
float textbreedte;
PFont number;
String[] origtext;
int counter;
String delimiters =" ,.?!;:[]><-";
float yplaats = 28;
PGraphics blok;
PGraphics tekstlaag;
PGraphics tekstlaag2;
int telx;
int tely;
HashSet<String> lidwoorden = new HashSet<String>(Arrays.asList(new String[] {
"de", "het", "een", "De", "Het", "Een"
}
));
HashSet<String> voorzetsels = new HashSet<String>(Arrays.asList(new String[] {
"aan", "Bij", "Om", "In", "Aan", "aan", "aangaande", "achter", "ad", "af", "anno", "betreffende", "bij", "buiten", "conform", "daar", "dankzij", "door", "gezien", "hangende", "in", "ingevolge", "inzake", "jegens", "krachtens", "langs", "met", "min", "na", "naar", "naast", "nabij", "namens", "niettegenstaande", "om", "omstreeks", "omtrent", "ondanks", "onder", "ongeacht", "op", "over", "per", "plus", "qua", "rond", "rondom", "sedert", "te", "tegen", "tegenover", "tijdens", "tot", "tussen", "uit", "van", "vanaf", "vanuit", "vanwege", "via", "volgens", "voor", "voorbij", "wegens", "zonder"
}
));
HashSet<String> bijvoegelijkenaamwoorden = new HashSet<String>(Arrays.asList(new String[] { "wiskundig", "wisselvallig", "wit", "woedend", "woelig", "woest", "wonderbaarlijk", "wonderlijk", "wrang", "wreed", "wrevelig", "wulps", "zacht", "zachtaardig", "zachtzinnig", "zakelijk", "zalig", "zat", "zedelijk", "zeer", "zeker", "zeldzaam", "zelfstandig", "zelfverzekerd", "zenuwachtig", "zichtbaar", "ziek", "ziekelijk", "zielig", "zijden", "zijwaarts", "zindelijk", "zinloos", "zinnelijk", "zinnig", "zinvol", "zoek", "zoet", "zogenaamd", "zomers", "zonderling", "zondig", "zonnig", "zorgelijk", "zorgeloos", "zorgvuldig", "zorgzaam", "zot", "zout", "zuinig", "zuiver", "zuur", "zwaar", "zwaarlijvig", "zwaarmoedig", "zwak", "zwakzinnig", "zwanger", "zwart", "zwierig", "zwijgzaam", "zwoel"
}
));
HashSet<String> landen = new HashSet<String>(Arrays.asList(new String[] {
"Amerika", "Afghanistan", "Akrotiri", "Albanië", "Algerije", "Amerikaanse Maagdeneilanden", "Amerikaans-Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua en Barbuda", "Arctic Ocean", "Argentinië", "Armenië", "Aruba", "Ashmore and Cartier Islands", "Atlantic Ocean", "Australië", "Azerbeidzjan", "Bahama's", "Bahrein", "Bangladesh", "Barbados", "Belarus", "België", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivië", "Bosnië-Herzegovina", "Botswana", "Bouvet Island", "Brazilië", "British Indian Ocean Territory", "Britse Maagdeneilanden", "Brunei", "Bulgarije", "Burkina Faso", "Burundi", "Cambodja", "Canada", "Caymaneilanden", "Centraal-Afrikaanse Republiek", "Chili", "China", "Christmas Island", "Clipperton Island", "Cocos (Keeling) Islands", "Colombia", "Comoren (Unie)", "Congo (Democratische Republiek)", "Congo (Volksrepubliek)", "Cook", "Coral Sea Islands", "Costa Rica", "Cuba", "Cyprus"}
));
void setup() {
size(700, 500);
reg = createFont("Arial", 55);
reg2 = createFont("Arial", 25);
number = createFont("Arial", 50);
String[]rawtext = loadStrings("test.txt");
String all = join(rawtext, "");
origtext=splitTokens(all, delimiters);
frameRate(1);
noStroke();
tekstlaag = createGraphics(width, height, JAVA2D);
tekstlaag.beginDraw();
tekstlaag.endDraw();
tekstlaag2 = createGraphics(width, height, JAVA2D);
tekstlaag2.beginDraw();
tekstlaag2.endDraw();
blok = createGraphics(width, height, JAVA2D);
blok.beginDraw();
blok.endDraw();
blok.noStroke();
tekstlaag.noStroke();
tekstlaag2.noStroke();
background(255);
minim = new Minim(this);
}
void draw() {
background(255);
image(blok, 0, 0);
image(tekstlaag, 0, 0);
image(tekstlaag2, 0, 0);
tekstlaag.beginDraw();
tekstlaag2.beginDraw();
blok.beginDraw();
telx = telx+1;
tely = tely+1;
if (telx > width) {
telx = 0;
tely = height;
}
String theword = origtext[counter];
if (counter+stopteller < origtext.length) {
int total = 0;
for (int i =0; i < origtext.length; i++) {
if (theword.equals(origtext[i])) {
total++;
}
}
noStroke();
fill(255);
// rect(0,0,width/4,height-total);
textFont(reg2);
float balk2 = textWidth(theword);
if (total > 1 && total <5) {
tekstlaag2.fill(0, 0, 255);
tekstlaag.fill(0, 0, 255);
blok.fill(0, 0, 255);
}
else {
tekstlaag2.fill(0);
tekstlaag.fill(0);
blok.fill(0);
}
if (total > 5 && total <8) {
tekstlaag2.fill(255, 100, 30);
tekstlaag.fill(255, 100, 30);
blok.fill(255, 100, 30);
}
if (lidwoorden.contains(theword)) {
tekstlaag2.fill(0, 100, 0);
tekstlaag.fill(0, 100, 0);
blok.fill(0, 100, 0);
}
if (voorzetsels.contains(theword)) {
tekstlaag2.fill(255, 0, 0);
tekstlaag.fill(255, 0, 0);
blok.fill(255, 0, 0);
}
if (bijvoegelijkenaamwoorden.contains(theword)) {
tekstlaag2.fill(0, 100, 100);
tekstlaag.fill(0, 100, 100);
blok.fill(0, 100, 100);
}
if (landen.contains(theword)) {
tekstlaag2.fill(200, 0, 200);
tekstlaag.fill(200, 0, 200);
blok.fill(200, 0, 200);
}
if (textbreedte+balk2+7 > 277) {
textbreedte = 0;
yplaats = yplaats+28;
}
if (yplaats > height-60 && kollom == width/2+15) {
tekstlaag.background(255, 0);
yplaats = 28;
kollom = 30;
kollom1 = true;
kollom2 = false;
}
if (yplaats > height-60) {
tekstlaag2.background(255, 0);
yplaats = 28;
kollom = width/2+15;
kollom1 = false;
kollom2 = true;
}
if (kollom1 == true) {
// tekstlaag.fill(0);
tekstlaag.fill(255);
tekstlaag.rect(kollom+textbreedte, yplaats+5, balk2+7, 28);
tekstlaag.fill(255);
}
if (kollom1 == false) {
tekstlaag2.fill(255);
// tekstlaag.fill(0);
tekstlaag2.rect(kollom+textbreedte, yplaats+5, balk2+7, 28);
}
if (total > 1 && total <5) {
tekstlaag2.fill(0, 0, 255);
tekstlaag.fill(0, 0, 255);
blok.fill(0, 0, 255);
}
else {
tekstlaag2.fill(0);
tekstlaag.fill(0);
blok.fill(0);
}
if (total > 5 && total <8) {
tekstlaag2.fill(255, 100, 30);
tekstlaag.fill(255, 100, 30);
blok.fill(255, 100, 30);
}
if (lidwoorden.contains(theword)) {
tekstlaag2.fill(0, 100, 0);
tekstlaag.fill(0, 100, 0);
blok.fill(0, 100, 0);
}
if (voorzetsels.contains(theword)) {
tekstlaag2.fill(255, 0, 0);
tekstlaag.fill(255, 0, 0);
blok.fill(255, 0, 0);
}
if (bijvoegelijkenaamwoorden.contains(theword)) {
tekstlaag2.fill(0, 100, 100);
tekstlaag.fill(0, 100, 100);
blok.fill(0, 100, 100);
}
if (landen.contains(theword)) {
tekstlaag2.fill(200, 0, 200);
tekstlaag.fill(200, 0, 200);
blok.fill(200, 0, 200);
}
if (kollom1 == true) {
// tekstlaag.fill(0);
// tekstlaag.rect(kollom+textbreedte, yplaats+5, balk2+7, 28);
// tekstlaag.fill(255);
tekstlaag.textFont(reg2);
tekstlaag.text(theword, kollom+textbreedte+1, yplaats+28);
textbreedte = textbreedte+balk2+7;
}
if (kollom1 == false) {
// tekstlaag.fill(0);
// tekstlaag2.rect(kollom+textbreedte, yplaats+5, balk2+7, 28);
// tekstlaag2.fill(255);
tekstlaag2.textFont(reg2);
tekstlaag2.text(theword, kollom+textbreedte+1, yplaats+28);
textbreedte = textbreedte+balk2+7;
}
blok.rect(0, 0, width, height);
// blok.rect(verplaats, 0, 20, height);
// blok.filter(BLUR, 4);
//verplaats = verplaats+20;
s = trim(theword.toLowerCase());
// geluidsverwerking Google
googleTTS(s, "nl");
if (player != null) {
player.close();
}
player = minim.loadFile(s + ".mp3", 2048);
player.loop();
if (player.isPlaying()) {
}
//
if (counter < origtext.length) {
teller = teller+1;
counter = origtext.length-origtext.length+teller;
}
if (verplaats > width) {
verplaats = 0;
}
}
else {
exit();
}
// counter = (counter + 1)%origtext.length;
tekstlaag.endDraw();
tekstlaag2.endDraw();
blok.endDraw();
// image(beeldlaag, 0, 0);
}
void googleTTS(String txt, String language) {
String u = "http://translate.google.com/translate_tts?tl=";
u = u + language + "&q=" + txt;
u = u.replace(" ", " ");
try {
URL url = new URL(u);
try {
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");
connection.connect();
InputStream is = connection.getInputStream();
File f = new File(sketchPath + "/" + txt + ".mp3");
OutputStream out = new FileOutputStream(f);
byte buf[] = new byte[1024];
int len;
while ( (len = is.read (buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
is.close();
println("Bestand weggeschreven: " + txt + ".mp3");
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
The part to get the new word to processed is:
if (counter < origtext.length) {
teller = teller+1;
counter = origtext.length-origtext.length+teller;
}
I want this part only to occur when the previous file is done playing, but I cannot seem to get it right. Been allready testing by using -- if (player.isPlaying()), -- and put the code inside this if statement, but that doesn't work. Does anybody knows a good solution for this? Would be great If someone could give me a tip.
Thanks in advance,
Joshua
1