Guest User

Untitled

a guest
Oct 27th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import ddf.minim.*;        
  2. import ddf.minim.ugens.*;
  3.  
  4. Minim minim=new Minim(this);
  5. AudioOutput out=minim.getLineOut();
  6.  
  7. Player[] test=new Player[50]; //Create 50 players
  8. Sample[] samples; //Create an empty array of samples
  9.  
  10. boolean Dragging=false;
  11. int Dragged=0;
  12.  
  13. int sample=0;
  14.  
  15. void loadSamples(){ //Load all samples in the data folder into the samples array
  16.   File[] files=new File(dataPath("")).listFiles();
  17.   samples=new Sample[files.length];
  18.   for(int i=0;i<files.length;i++){
  19.     samples[i]=new Sample(new MultiChannelBuffer(1,1),1);
  20.     samples[i].sampleRate = minim.loadFileIntoBuffer(files[i].getAbsolutePath(),samples[i].s);
  21.   }
  22. }
  23.  
  24. void setup(){
  25.   size(500,500,P2D);
  26.   for(int i=0;i<test.length;i++){
  27.     test[i]=new Player(); //Init players.
  28.   }
  29.   loadSamples();
  30. }
  31. void draw(){
  32.   background(0);
  33.   fill(255);
  34.   fill(0,128);
  35.   rect(0,0,50,25);
  36.   fill(255);
  37.   text((sample+1)+" out of "+samples.length,1,15);
  38.  
  39.   if(mousePressed&&!Dragging){ //Equivalent to mousePressed()...
  40.     Dragged=(Dragged+1)%test.length;
  41.     Dragging=true;
  42.     test[Dragged].setSample(samples[sample]);
  43.     //try{
  44.     test[Dragged].Sound.trigger();//}catch(Exception e){println("ow.");}
  45.   }
  46.   if(mousePressed&&Dragging){
  47.     test[Dragged].x=mouseX-width/2;
  48.     test[Dragged].y=mouseY;
  49.   }
  50.   if(!mousePressed&&Dragging){
  51.     Dragging=false;
  52.   }
  53.  
  54.   for(Player i:test){ //Refresh the player
  55.     i.Draw();
  56.     i.Pan();
  57.   }
  58. }
  59.  
  60.  
  61.  
  62.  
  63. void keyPressed(){
  64.   if((int(str(key))-1<samples.length)&&int(str(key))>0){sample=int(str(key))-1;}
  65. }
  66.  
  67.  
  68. class Sample{
  69.   MultiChannelBuffer s;
  70.   float sampleRate;
  71.   Sample(MultiChannelBuffer s,int sampleRate){this.s=s; this.sampleRate=sampleRate;}
  72. }
  73. class Player{
  74.   int x=0;
  75.   int y=0;
  76.   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!
  77.   Pan pan=new Pan(0);
  78.   TickRate rate=new TickRate(1);
  79.  
  80.   void Draw(){rect(width/2+x-25,y-25,50,50);}
  81.   void Pan(){pan.setPan(clamp((float)x/(width/2),-1,1)); rate.value.setLastValue((1-(float)y/height)*2);}
  82.   void setSample(Sample in){
  83.     this.Sound.setSample(in.s,in.sampleRate);
  84.   }
  85.  
  86.   String toString(){return "Player "+x+" "+y;}
  87.  
  88.   Player(){Sound.patch(pan).patch(rate).patch(out); rate.setInterpolation( true );}
  89. }
  90.  
  91. float clamp(float in,float min,float max){
  92.  return max(min(in,max),min);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment