1

I'm trying to make it possible to make a functional import button, by this I mean I click the button, the file browser pops up and I can click a song then the player can play it. Just like any other music player. Here is a basic view of my code so far excluding the classes:

import ddf.minim.spi.*;
import ddf.minim.*; 
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.ugens.*;
boolean play;
boolean repeat;
int k;
String filename;//
Minim minim;
AudioPlayer player;

Pics p;
Mechs m;
Importbt b;
ArrayList<Songs> s;
int i=0;
void setup() {
size(600, 400);
b=new Importbt();
m=new Mechs();
p=new Pics();
p.Thepics();

minim=new Minim(this);
s = new ArrayList();
s.add(new Songs(player, "SONG 1", "SONG 1"));
s.add(new Songs(player, "SONG 2", "SONG 2"));
s.add(new Songs(player, "SONG 3", "SONG 3"));
s.add(new Songs(player, "SONG 4", "SONG 4"));
k = s.size()-1;
}
void draw() {
background(0);
p.getFunction();
}

void fileSelected(File selection) {
if (selection == null) {
} 
else {
filename = selection.getAbsolutePath();
player = minim.loadFile(filename);//loads the file
// s.add(k, new Songs(player, filename, "a song"));
//s.get(k).playmusic();
s.add(new Songs(player, filename, "ftyu"));
s.get(k).waveform();//function from my songs class
player.play();//plays the file

As for my class if its needed

class Songs {
AudioPlayer song; 
String directory;
String songName;
Songs(AudioPlayer song, String directory, String songName) {

song=minim.loadFile(directory);

this.song=song;
this.songName=songName;
}
void waveform() {
for (int j = 1; j < song.bufferSize() - 1; j++)
{
  if (j>0) {

    line(j, 214  + song.left.get(j)*50, j+1, 214 + song.left.get(j+1)*50);
    //waves from the left.
    stroke( 255, 0, 0 );  //this is the colour of the first line (red)
    line(j, 214 + song.right.get(j)*50, j+1, 214 + song.right.get(j+1)*50);
    //waves from the right.
    stroke(255, 255, 0);

  }
}
double-beep
  • 5,031
  • 17
  • 33
  • 41

1 Answers1

1

Here's something to get you going:

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

float buttonX;
float buttonY;
float buttonW;
float buttonH;

Minim minim;
AudioPlayer player;

String filename;

void setup() {

  textSize(24);

  frame.setResizable(false);

  background(255);

  size(600, 200);

  fill(0);
  stroke(0);
  noFill();

  buttonW = 200;
  buttonH = 50;
  buttonX = width - width/2 - buttonW/2;
  buttonY = height/2 - buttonH/2;

  // Minim stuff
  minim = new Minim(this);
}

void draw() {

  background(255);
  fill(0);

  rectMode(CORNER);

  rect(buttonX, buttonY, buttonW, buttonH);

  fill(255);

  textAlign(LEFT);
  text("Import File", buttonX+35, buttonY+30);
}

void mouseClicked() {
  if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
    selectInput("Import music file", "fileSelected");
  }
}

/* Taken from Processing.org */
void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or user hit cancel");
  } else {
    filename = selection.getAbsolutePath();
    player = minim.loadFile(filename);
    player.play();
    println("User selected " + filename);
  }
}

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

This is a very simple example. All this does is creates objects for Minim and AudioPlayer and then uses the file selection mechanism to feed the path and name of the file to loadFile(filename). Then if the file is a legitimate audio file (I only tested with a .wav file) then it plays it. After it is done, the player is stopped.

Keep in mind that this does not do any error checking or anything so if you chose a .jpeg file, for example, it will throw exceptions. You should play with these things to try and see how you can streamline your player.

One thing you should try and understand is that this is all very straightforward. The filename is simply a String and nothing extremely complicated.

You can find a tutorial for Minim here: http://artandtech.aalto.fi/wp-content/uploads/2012/06/minim.pdf

UPDATE: SINE WAVES WORKING WITH THE SELECTED SONG

I have updated my code that I provided here to work with the wave thing that you have going. I added a boolean which becomes true when a file is selected. Now you will have to tweak this code to work with multiple files. This is just an example.

UPDATE 2: IMPLEMENTED THE CODE TO WORK WITH YOUR SONGS CLASS

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

float buttonX;
float buttonY;
float buttonW;
float buttonH;

Minim minim;
AudioPlayer player;
ArrayList<Songs> s;
int k;

String filename;

boolean isSelected = false;

void setup() {

  s = new ArrayList();

  textSize(24);

  frame.setResizable(false);

  background(255);

  size(600, 600);

  fill(0);
  stroke(0);
  noFill();

  buttonW = 200;
  buttonH = 50;
  buttonX = width - width/2 - buttonW/2;
  buttonY = height/2 - buttonH/2;

  // Minim stuff
  minim = new Minim(this);
}

void draw() {

  background(255);
  fill(0);

  rectMode(CORNER);

  rect(buttonX, buttonY, buttonW, buttonH);

  fill(255);

  textAlign(LEFT);
  text("Import File", buttonX+35, buttonY+30);

  if (isSelected) {
    s.get(k).waveform();
  }
}

void mouseClicked() {
  if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
    selectInput("Import music file", "fileSelected");
  }
}

/* Taken from Processing.org */
void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or user hit cancel");
  } 
  else {
    filename = selection.getAbsolutePath();
    s.add(new Songs(player, filename, "Filename"));
    isSelected = true;
  }
}

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

class Songs {
  AudioPlayer song; 
  String directory;
  String songName;
  Songs(AudioPlayer song, String directory, String songName) {

    song=minim.loadFile(directory);    

    this.song=song;
    this.songName=songName;
    song.play();
  }
  void waveform() {
    for (int j = 1; j < song.bufferSize() - 1; j++)
    {
      if (j>0) {

        line(j, 214  + song.left.get(j)*50, j+1, 214 + song.left.get(j+1)*50);
        //waves from the left.
        stroke( 255, 0, 0 );  //this is the colour of the first line (red)
        line(j, 214 + song.right.get(j)*50, j+1, 214 + song.right.get(j+1)*50);
        //waves from the right.
        stroke(255, 255, 0);
      }
    }
  }
}

The reason you see a flash of red and then nothing the way you had your code set up was because it reads the waveform() method and goes through the for loop really fast and then moves on to the rest of the operations like playing the song. Putting it in draw() like I've done helps you avoid that.

Nico
  • 3,471
  • 2
  • 29
  • 40
  • @user3423784 Check out this answer and let me know if you need any help with this. – Nico Mar 26 '14 at 00:49
  • Ty for that, ive got the import working, but the only issue I have now is that when I play an imported song, my sound waves I made doesn't play with the imported song, I tired to define it as much as I can. – user3423784 Mar 28 '14 at 12:53
  • @user3423784 I don't understand what you mean. What sound waves? Are you trying to play two songs simultaneously? – Nico Mar 28 '14 at 14:56
  • You know how music players such as windows media player has those animations when you play a song? Like waves...That is what I have added in my player. Here is what they look like:http://www.kungfupunk.com/wp-content/uploads/2009/06/music-visualization-1.jpg And the waves will work with the songs in my arraylist but not the songs I get from the import button. – user3423784 Mar 28 '14 at 15:14
  • @user3423784 okay. Why not add the imported songs to your ArrayList? – Nico Mar 28 '14 at 16:02
  • @user3423784 Also how exactly are you adding the songs to the ArrayList? And what do you mean when you say *it doesn't work*? – Nico Mar 28 '14 at 16:04
  • I tried adding the songs to the arraylist, when I say it doesn't work I mean that when the imported song is loaded,itdoesnt follow the wave format,it will play,but therewill be no waves when it is playing. ArrayList s; s.add(new Songs(player, filename,"imported song")); That will just make the imported music play,however when I add a variable as such called k, which gets the my wave function from my class s.get(k).waveform(); player.play(); The end result being the imported song plays,there is this flash which shows my waves,but it is only there when the song starts for some reason :s. – user3423784 Mar 28 '14 at 19:20
  • @user3423784 Can you update your question with all of your code so I can then see where its messing up? – Nico Mar 28 '14 at 19:30
  • @user3423784 Alright, I'll check it out and update the answer. – Nico Mar 28 '14 at 19:55
  • @user3423784 updated the code again to work with your Songs class. This should now be sufficient for you to implement it for more songs, etc. – Nico Mar 28 '14 at 20:54
  • Thanks for that, you're really helpful, although, the song.play(); is causing all the music I already have on the arraylist to all play when I play my player. The waves are starting to work for my imported songs – user3423784 Mar 29 '14 at 14:13
  • @user3423784 No problem at all. So this is fixed then or do you still have questions on this problem? – Nico Mar 29 '14 at 17:12
  • This is fixed yes, all I going to do now is make sure when I upload the import, I'm able to pause it etc. – user3423784 Mar 29 '14 at 17:26
  • @user3423784 Awesome. Could you accept the answer then and if you have any other problem with the code, leave a comment here or open a new question? – Nico Mar 29 '14 at 17:28