Loading...
Logo
Processing Forum
hi --

i'm quite new to processing, but have a university class where we have to use it for a project. i have collected lyrics from songs, divided these into six categories (emotions: energetic, happy, relaxed, melancholic, stressed + sleepy). the lyrics are in individual .txt files. using minim to import songs for background music.

i want to make an applet that lets you select an emotion, and get a new (randomly generated) song with random lines of lyrics from the array of songs with the chosen emotion. 


i am in serious need of some help here; with several things:

1 // can i make a "front page" where you can click on happy, which then opens up a new "page" with the song?

2 // is there a quick way to load several strings at a time? have about 40 of them, as you can see below.

3 // how do i time the lyrics? they can (at least for now) be the same amount of time. using framerate now, which kind of works, but there's a long pause before it starts. don' know how to use mills(), but could that be something?

4 // is there a way that i can have a set of "presets" for each emotion -- like background color and image?


help with anything would be GREATLY appreciated -- thanks!




sorry about the mess, but this is what the script looks like now:


PFont lanefont, satellite, dashfont;
String[][] happy, melancholic, relaxed, sleepy, stressed, energized;
PImage beach, surf, boom, neon, floral, frame;

import ddf.minim.*;
AudioPlayer player;
Minim minim;

void setup () {
  size (800, 800);
  background (0);
  smooth();
  frameRate(0.15);

  minim = new Minim(this);
  player = minim.loadFile ("melancholic.mp3", 500); // melancholic
  player.play();

  lanefont = loadFont ("Lane-Narrow-48.vlw");
  satellite = loadFont ("Satellite-Oblique-48.vlw");
  dashfont = loadFont ("PrintDashed-48.vlw"); 

  beach = loadImage ("beach.jpg"); // relaxed
  surf = loadImage ("surf.jpg"); // happy
  boom = loadImage ("boom.jpg"); // stressed
  neon = loadImage ("neon.jpg"); // energized
  floral = loadImage ("floral.jpg"); // melancholic
  frame = loadImage ("frame.jpg"); // sleepy

 
}


void draw() {
  String header [] = {"I M P R O V I S E D", "H A P P Y ", "M E L A N C H O L I C", 
                      "R E L A X E D", "E N E R G I Z E D", "S T R E S S E D", "S L E E P Y"};
  
  // background (241, 163, 189); // happy pink
     background (145, 165, 145); // melancholic green
  // background (255, 247, 0); // energized yellow
  // background (0, 108, 255); // relaxed blue
  // background (255, 5, 5); // stressed red
  // background (116, 56, 222); // sleepy purple

  tint (255, 10);
  image (floral, 0, -100, 800, 1000);
  stroke (241, 163, 189); 
  strokeCap (PROJECT); 
  strokeWeight (2.5);
  line (28, 60, 230, 60);
  textFont (lanefont, 35);
  textAlign (CENTER);
  fill(0);
  text ("K A R A O K E", 129, 100);
  textFont (dashfont, 25);
  text (header[2], 129, 135);
  stroke (0);
  triangle (129, 182, 120, 165, 138, 165);
  

  String t1[]  = loadStrings ("1.txt");
  String t2[]  = loadStrings ("2.txt");
  String t3[]  = loadStrings ("3.txt");
  String t4[]  = loadStrings ("4.txt");
  String t5[]  = loadStrings ("5.txt");
  String t6[]  = loadStrings ("6.txt");
  String t7[]  = loadStrings ("7.txt");
  // String t8[]  = loadStrings ("8.txt");
  // String t9[]  = loadStrings ("9.txt");
  String t10[] = loadStrings ("10.txt");
  String t11[] = loadStrings ("11.txt");
  String t12[] = loadStrings ("12.txt");
  String t13[] = loadStrings ("13.txt");
  String t14[] = loadStrings ("14.txt");
  String t15[] = loadStrings ("15.txt");
  String t16[] = loadStrings ("16.txt");
  String t17[] = loadStrings ("17.txt");
  String t18[] = loadStrings ("18.txt");
  String t19[] = loadStrings ("19.txt");
  String t20[] = loadStrings ("20.txt");
  String t21[] = loadStrings ("21.txt");
  String t22[] = loadStrings ("22.txt");
  String t23[] = loadStrings ("23.txt");
  String t24[] = loadStrings ("24.txt");
  String t25[] = loadStrings ("25.txt");
  String t26[] = loadStrings ("26.txt");
  // String t27[] = loadStrings ("27.txt");
  String t28[] = loadStrings ("28.txt");
  String t29[] = loadStrings ("29.txt");
  String t30[] = loadStrings ("30.txt");
  String t31[] = loadStrings ("31.txt");
  String t32[] = loadStrings ("32.txt");
  String t33[] = loadStrings ("33.txt");
  String t34[] = loadStrings ("34.txt");
  String t35[] = loadStrings ("35.txt");
  String t36[] = loadStrings ("36.txt");
  String t37[] = loadStrings ("37.txt");

  String[][] happy = {t2, t4, t5, t6, t19, t20, t21, t22, t23};
  String[][] melancholic = {t26, t14, t13, t7, t1, t15, t2, t4, t5, t6, t19, t20, t21, t22, t23};
  String[][] relaxed = {t10, t11, t24, t30, t31, t36, t37};
  String[][] stressed = {t12};
  String[][] energized = {t3, t4, t16, t17, t18, t28, t29, t33, t34, t35};
  String[][] sleepy = {t25};

  int xpos = width/2;
  int ypos = height/2;


        for (int i = 0; i < 2; i++) {
          fill (255);
          textFont (satellite, 30);
          textAlign (CENTER, CENTER);
          text (melancholic[int(random(melancholic.length))][int(random(10))], xpos, ypos);
          ypos = ypos+50;
        }
}


void stop() {
  player.close();
  minim.stop();
  super.stop();
}


Replies(4)

Sorry, I have no time this morning to address all your questions, but I have some remarks (which I do often...) about your code.
- Very minor remark: in Java, we usually write String[] array; not String array[]; (which is more a C[++] notation). Both work, though.
- header can be defined only once, at global level. Not important, but cleaner.
- Idem for the other arrays.
- Ouch, massive copy / paste is rarely a good idea in programming! Learn to make double dimension arrays:
String[][] lyrics = new String[37][];
and in setup() (not draw()!):
for (int i = 0; i < lyrics.length; i++)
{
  lyrics[i] = loadStrings((i + 1) + ".txt");
}
Isn't that cleaner? Beware, lyrics[0] corresponds to t1.

" don' know how to use mills()"
You should learn! They are very important to do timed things, like games and such. There are plenty of examples around.
Beside the Learning section of Processing, perhaps the articles at the Technical FAQ can help you, they are made to address common caveats where beginners fall often.
Hi & thank you so much for answering so quickly!

YES, it's a mess with copy / pasting -- that's why I was in need of a better way to load. I tried with the double dimension array, but get: cannot convert from string[] to string[][]. Any suggestions? It currently looks like this:



Copy code
  1. PFont lanefont, satellite, dashfont;
  2. PImage beach, surf, boom, neon, floral;
  3. String[][] lyrics = new String[37];
  4. String[][] melancholic;


  5. void setup () {
  6.   size (800, 800);
  7.   background (255);
  8.   smooth();
  9.   frameRate(0.3);

  10.   lanefont = loadFont ("Lane-Narrow-48.vlw");
  11.   satellite = loadFont ("Satellite-Oblique-48.vlw");
  12.   dashfont = loadFont ("PrintDashed-48.vlw"); 
  13.   floral = loadImage ("floral.jpg"); // melancholic
  14.   
  15.   for (int i = 0; i < lyrics.length; i++) {
  16.  lyrics[i] = loadStrings((i + 1) + ".txt");
  17.   }
  18.   
  19. }


  20. void draw() {
  21.   background (145, 165, 145); // melancholic green
  22.   
  23.   tint (255, 10);
  24.   image (floral, 0, -100, 800, 1000);
  25.   stroke (241, 163, 189); // pink
  26.   strokeCap (PROJECT); // straight capped edges
  27.   strokeWeight (2.5);
  28.   line (28, 60, 230, 60);
  29.   textFont (lanefont, 35);
  30.   textAlign (CENTER);
  31.   fill(0);
  32.   text ("K A R A O K E", 129, 100);
  33.   textFont (dashfont, 25);
  34.   text ("I M P R O V I S E D", 129, 135); // make this text change? adjectives from songs?
  35.   stroke (0);
  36.   triangle (129, 182, 120, 165, 138, 165);
  37.   

  38.  //String t1[]  = loadStrings ("1.txt"); // the "old" way i loaded them
  39.  //String t7[]  = loadStrings ("7.txt");
  40.  //String t13[] = loadStrings ("13.txt");
  41.  //String t14[] = loadStrings ("14.txt");
  42.  //String t15[] = loadStrings ("15.txt");
  43.  //String t26[] = loadStrings ("26.txt");


  44.   String[][]melancholic = {lyrics[27], lyrics[13], lyrics[12], lyrics[6], lyrics[0], lyrics[14]}; // not sure what to use here -- this is what i tried?
  45. //  String[][] melancholic = {t26, t14, t13, t7, t1, t15};



  46.   int xpos = width/2;
  47.   int ypos = height/2;


  48.         for (int i = 0; i < 2; i++) {
  49.           fill (255);
  50.           textFont (satellite, 30);
  51.           textAlign (CENTER, CENTER);
  52.           text (melancholic[int(random(melancholic.length))][int(random(10))], xpos, ypos);
  53.           ypos = ypos+50;
  54.         }
  55. }



Sorry -- it's ok, got it to work now. THANK YOU, it's a lot cleaner!


Copy code
  1. int lines = 20;
  2. PFont lanefont, satellite, dashfont;
  3. PImage beach, surf, boom, neon, floral;
  4. String[][] lyrics = new String[37][lines];
  5. String[][] melancholic;


  6. void setup () {
  7.   size (800, 800);
  8.   background (255);
  9.   smooth();
  10.   frameRate(0.3);

  11.   lanefont = loadFont ("Lane-Narrow-48.vlw");
  12.   satellite = loadFont ("Satellite-Oblique-48.vlw");
  13.   dashfont = loadFont ("PrintDashed-48.vlw"); 
  14.   floral = loadImage ("floral.jpg"); // melancholic
  15.   
  16.   for (int i = 0; i < lyrics.length; i++) {
  17.   lyrics[i] = loadStrings((i + 1) + ".txt");
  18.   }
  19.   
  20. }


  21. void draw() {
  22.   background (145, 165, 145); // melancholic green
  23.   
  24.   String[][] melancholic = {lyrics[27], lyrics[13], lyrics[12], lyrics[6], lyrics[0], lyrics[14]};
  25.   
  26.   tint (255, 10);
  27.   image (floral, 0, -100, 800, 1000);
  28.   stroke (241, 163, 189); // pink
  29.   strokeCap (PROJECT); // straight capped edges
  30.   strokeWeight (2.5);
  31.   line (28, 60, 230, 60);
  32.   textFont (lanefont, 35);
  33.   textAlign (CENTER);
  34.   fill(0);
  35.   text ("K A R A O K E", 129, 100);
  36.   textFont (dashfont, 25);
  37.   text ("I M P R O V I S E D", 129, 135); // make this text change? adjectives from songs?
  38.   stroke (0);
  39.   triangle (129, 182, 120, 165, 138, 165);
  40.   

  41.   int xpos = width/2;
  42.   int ypos = height/2;


  43.         for (int i = 0; i < 2; i++) {
  44.           fill (255);
  45.           textFont (satellite, 30);
  46.           textAlign (CENTER, CENTER);
  47.           text (melancholic[int(random(melancholic.length))][int(random(10))], xpos, ypos);
  48.           ypos = ypos+50;
  49.         }
  50. }

Oops, there was an error in the example (fixed in my message...): it should be
String[][] lyrics = new String[37][];
not
String[][] lyrics = new String[37];

Note the empty brackets, indicating there will be an array of unknown length there.