Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ddf.minim.*;
- import ddf.minim.ugens.*;
- Minim minim=new Minim(this);
- AudioOutput out=minim.getLineOut();
- Player[] test=new Player[50]; //Create 50 players
- Sample[] samples; //Create an empty array of samples
- boolean Dragging=false;
- int Dragged=0;
- int sample=0;
- void loadSamples(){ //Load all samples in the data folder into the samples array
- File[] files=new File(dataPath("")).listFiles();
- samples=new Sample[files.length];
- for(int i=0;i<files.length;i++){
- samples[i]=new Sample(new MultiChannelBuffer(1,1),1);
- samples[i].sampleRate = minim.loadFileIntoBuffer(files[i].getAbsolutePath(),samples[i].s);
- }
- }
- void setup(){
- size(500,500,P2D);
- for(int i=0;i<test.length;i++){
- test[i]=new Player(); //Init players.
- }
- loadSamples();
- }
- void draw(){
- background(0);
- fill(255);
- fill(0,128);
- rect(0,0,50,25);
- fill(255);
- text((sample+1)+" out of "+samples.length,1,15);
- if(mousePressed&&!Dragging){ //Equivalent to mousePressed()...
- Dragged=(Dragged+1)%test.length;
- Dragging=true;
- test[Dragged].setSample(samples[sample]);
- //try{
- test[Dragged].Sound.trigger();//}catch(Exception e){println("ow.");}
- }
- if(mousePressed&&Dragging){
- test[Dragged].x=mouseX-width/2;
- test[Dragged].y=mouseY;
- }
- if(!mousePressed&&Dragging){
- Dragging=false;
- }
- for(Player i:test){ //Refresh the player
- i.Draw();
- i.Pan();
- }
- }
- void keyPressed(){
- if((int(str(key))-1<samples.length)&&int(str(key))>0){sample=int(str(key))-1;}
- }
- class Sample{
- MultiChannelBuffer s;
- float sampleRate;
- Sample(MultiChannelBuffer s,int sampleRate){this.s=s; this.sampleRate=sampleRate;}
- }
- class Player{
- int x=0;
- int y=0;
- Sampler Sound=new Sampler("test.wav",1,minim); //Couldn't figure out how to make it load up without any sound in it - not even an empty new MultiChannelBuffer instance. Hopefully you have a spare test.wav!
- Pan pan=new Pan(0);
- TickRate rate=new TickRate(1);
- void Draw(){rect(width/2+x-25,y-25,50,50);}
- void Pan(){pan.setPan(clamp((float)x/(width/2),-1,1)); rate.value.setLastValue((1-(float)y/height)*2);}
- void setSample(Sample in){
- this.Sound.setSample(in.s,in.sampleRate);
- }
- String toString(){return "Player "+x+" "+y;}
- Player(){Sound.patch(pan).patch(rate).patch(out); rate.setInterpolation( true );}
- }
- float clamp(float in,float min,float max){
- return max(min(in,max),min);
- }
Advertisement
Add Comment
Please, Sign In to add comment