Here is Version 3 of my Slide Show algorithm!
This Program works simply by using one of the two slide potentiometers to control the picture on screen as well as the music in the background. The volume level of the sample just called as well as the volume level of the background music can be controlled.
It’s the prototype for an Art Installation that requires user participation to “mix” the music with these selectable fragments/
Problems: Overall it was not too challenging getting everything working, the only hurdle encountered was truly getting a grasp for how the serial commands work. The handshake between the serial port and processing can be tricky which is why a number of lines of code are needed to make sure the data streams are lined up properly. I was accidently streaming data in before the system was ready to accept any data, that lead to a number of issues that were cleanly resolved with a if statement that checked if the data stream was truly ready to be read in.
In regards to the program I will hit some of the highlight functions. I built a custom class duoPA that was able to load in the music files as well as the pictures and pair them together. So whenever my sample selector would select a point it would be referencing two files. Also have a pixel scrambler running in the background as well as a function that randomly selects sections of a loaded in picture and throw it into the main screen. Everything is controlled by millis() and modulo division looking for zero for timing.
It was a ton of fun getting this all working and I can’t wait to ship out v4!
-JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
int on_off = 0; int sampleNum_scale = 0; int gain_scale = 0; int ledPin = 8; int inByte = 0; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(7, INPUT); pinMode(ledPin,OUTPUT); establishContact(); } void loop() { if (Serial.available() > 0) { inByte = Serial.read(); on_off = map(digitalRead(7),0,1,0,255); if(on_off == 255) digitalWrite(ledPin,HIGH); else digitalWrite(ledPin,LOW); sampleNum_scale= map(analogRead(0),0,1023,0,255); gain_scale = map(analogRead(1),0,1023,0,255); Serial.print(on_off); Serial.print(","); Serial.print(sampleNum_scale); Serial.print(","); Serial.println(gain_scale); } } void establishContact() { while (Serial.available() <= 0) { Serial.println("0,0,0"); // send an initial string delay(300); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
import ddf.minim.spi.*; import ddf.minim.signals.*; import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.ugens.*; import ddf.minim.effects.*; import processing.serial.*; // import the Processing serial library Serial myPort; // The serial port PImage face; PImage face2; //final String pic = "test_2.jpg"; final String window = "test_1.jpg"; PFont f = createFont("Helvetica",32,true); boolean disp_text = false; boolean same_face = true; int pos_lr = 0; int pos_ud = 0; int dimension; int pix_mix; boolean mix = true; int time; boolean count_down = true; PImage[] histo = new PImage[1]; int histo_counter =0; float val = 1.0; float dec_time; int[] cur_time = new int[2]; int onset_type = 1; int loop_count = 2; int cur_pos = 0; boolean play_ori = false; AudioPlayer ori; duoPA tocheck; int sec=0; int min=0; float timechk=0; boolean change_again=false; float samp_choice; boolean hold_val = false; float gain_set; File [] files=new File("/Users/Axilium/Documents/Processing/SlideShow_Manipulator_PREMUSFIN/rachels_images").listFiles();//"/Users/Axilium/Documents/Processing/SlideShow_Manipulator_V2/data").listFiles(); File [] audioc= new File("/Users/Axilium/Documents/Processing/SlideShow_Manipulator_PREMUSFIN/rachels_sound").listFiles(); String filenames[]=new String[files.length]; String audioFiles[]=new String[audioc.length]; ArrayList <duoPA> c= new ArrayList<duoPA>(); int inc = 1; Minim minim; duoPA g; void setup() { //println(Serial.list()); // myPort.write("A"); minim = new Minim(this); size(1280, 720); //face = loadImage(pic); //face2 = loadImage(pic); //histo[histo_counter] = loadImage(files[inc].getAbsolutePath());//loadImage(pic); histo_counter++; time = millis(); for(int i=0; i<files.length; i++) { filenames[i]=files[i].getAbsolutePath(); println(filenames[i]); } minim = new Minim(this); for(int i=0; i<files.length; i++) { filenames[i]=files[i].getAbsolutePath(); } for(int i=0; i<audioc.length; i++) { audioFiles[i]=audioc[i].getAbsolutePath(); } for(int i=0; i<files.length; i++) { c.add(new duoPA(filenames[i],audioFiles[i])); } //face = loadImage(files[inc].getAbsolutePath()); // g = c.get(6); //g.source.loop(); //face = g.face; cur_pos=ini(c.size(),cur_pos); ori = minim.loadFile("test_1.wav",1024); ori.play(); //ori.mute(); samp_choice = 1; myPort = new Serial(this, Serial.list()[7], 9600); myPort.bufferUntil('\n'); } void draw() { float randX =random(1,width); float randY =random(1,height); if(mix && time%4==0){ //face = mixer(face,pos_lr,pos_ud); face = mixer(face).get();//face.get(); //histo = (PImage[]) expand(histo,histo.length+1); //histo[histo_counter]=tmpImage; histo_counter++; } // if(!mix && time%4==0 && count_down){ // if(histo_counter>1){ // face = (PImage)histo[histo_counter-1]; // } // histo_counter = histo_counter-1; // arrayCopy(histo,histo,histo.length-1); // if(histo_counter <= 1){ // histo_counter=1; // face = loadImage(pic); // } // } tint(histo_counter%12+100,50);//tint(#456326,histo_counter%12); if(histo_counter == 1)tint(#456326,12); switch(onset_type){ case 1: if(g.beat.isKick()){ // PImage tmpImage3 = chunkTranspose(randPic(),randX,randY); // face.mask(tmpImage3); PImage tmpFace = randPic().get(round(random(randY,width/2)),round(random(randX,width/2)),round(random(randX,width/2)),round(random(randX,width/2))); noStroke(); pushMatrix(); //translate(mouseX-200,mouseY-200);//Using Mouse translate(width/2,height/2); tint(255,135); scale(val); image(tmpFace,0,0); popMatrix(); } break; case 2: if(g.beat.isSnare()){ PImage tmpImage3 = chunkTranspose(randPic(),randX,randY); face.mask(tmpImage3); } break; case 3: if(g.beat.isHat()){ PImage tmpImage3 = chunkTranspose(randPic(),randX,randY); face.mask(tmpImage3); } break; case 4: if(g.beat.isOnset()){ // PImage tmpImage3 = chunkTranspose(randPic(),randX,randY); // face.mask(tmpImage3); } break; } if(!mix&&histo_counter>0)histo_counter--; if(histo_counter == 35){ //cur_pos = ini(c.size(),cur_pos); //histo_counter = 0; } image(face, pos_lr, pos_ud); time = millis(); dec_time= map(histo_counter,0,150,0,24); cur_time = timeOut(dec_time); if(millis()-timechk>1000){ sec++; timechk=time; change_again=true; } if(sec%59==0 && change_again){ change_again= false; min++; sec=0; } // String text = (cur_time[1] < 10 ? "0" : "") + cur_time[1]; //int con_b = int(text); //String conv = String.format("%02d",cur_time[1]); //int con_a = int(conv); textFont(f,72); //String c = (cur_time[0]%24+":"+conv); String c = (min+":"+sec); if(histo_counter == 1){ c =("00"+":"+"00"); } String samp_foll = (""+samp_choice); // String c =(""+histo_counter); fill(255); text(c,width/1.2,height/9); //2.2/4.4 specs for test_2 text(samp_choice,width/2.2, height/4.4); //PImage tmpFace = face2.get(mouseX-200,mouseY-200,400,400);//Using Mouse // if( time%8 == 0 && histo_counter!=1){ // PImage tmpFace = randPic().get(mouseX,mouseY,round(random(randX,width/2)),round(random(randX,width/2))); // noStroke(); // pushMatrix(); // //translate(mouseX-200,mouseY-200);//Using Mouse // translate(randX,randY); // tint(255,135); // scale(val); // image(tmpFace,0,0); // popMatrix(); // } switchtrack(play_ori); } void keyPressed(){ if( key =='a'){//Scale Up //val+=.1; onset_type = 1; println("kick"); } if( key =='s'){//Scale Up //val+=.1; onset_type = 2; println("snare"); } if( key =='d'){//Scale Up //val+=.1; onset_type = 3; println("hat"); } if( key =='f'){//Scale Up //val+=.1; onset_type = 4; println("onset"); } if( key == 'h'){//Scale Down cur_pos=ini(c.size(),cur_pos); } if( key == ' ') count_down = !count_down;//Reverse if( key == 'v'){// Exchanges Pixels Randomly; Starts mix =!mix; } if( key == 't') g.stop(); //Loads Another Background Image //Moving the Image if( keyCode == UP){ play_ori=!play_ori; // //pos_ud+=50; // if(inc<files.length-1)inc++; // else inc=files.length-1; // println(inc); // face = loadImage(files[inc].getAbsolutePath()); } if( keyCode == DOWN){ //pos_ud-=50; if(inc>1)inc--; else inc =1; println(inc); face = loadImage(files[inc].getAbsolutePath()); } if( keyCode == RIGHT){ pos_lr+=50; } if( keyCode == LEFT){ pos_lr-=50; } } PImage sameface(PImage option){ if(same_face) return option; return option = loadImage(window); } PImage mixer(PImage item){ pix_mix = round(pow(2,round(random(4,6)))); int dim = item.width * item.height; item= sameface(item); item.loadPixels(); int randp1; color hold; for (int i = 0; i < dim; i += pix_mix) { randp1=round(random(0,dim-1)); hold = item.pixels[i]; item.pixels[i] = item.pixels[randp1]; item.pixels[randp1]=hold; } item.updatePixels(); return item; } PImage chunkTranspose(PImage item,float _randX,float _randY){ int randX = round(_randX); int randY = round(_randY); int randZ = abs(randY+randX/2); PImage tmpImage1 = item.get(randY,randY,randX,randX); PImage tmpImage2 = item.get(randZ,randZ,randX,randX); item.set(randY,randZ,tmpImage1); item.set(randX,randZ,tmpImage2); return item; } //Randomly Selects a Picture to Use PImage randPic(){ int randVal = round(random(1,files.length-1)); return loadImage(files[randVal].getAbsolutePath()); } int[] timeOut(float val){ int[] outpos = new int[2]; outpos[0]=floor(val);//Hour float holder=(val-outpos[0])*3600; outpos[1] = floor(holder/60); return outpos; } //class duoPA implements AudioListener{ // PImage face; // AudioPlayer source; // BeatDetect beat; // // duoPA(PImage _face, AudioPlayer _source, BeatDetect _beat){ // face = _face; // source = _source; // source.addListener(this); // beat = _beat; // } // void samples(float[] samps) // { // beat.detect(source.mix); // } // // void samples(float[] sampsL, float[] sampsR) // { // beat.detect(source.mix); // } // //} class duoPA { PImage face; private BeatDetect beat; AudioPlayer source; BeatListener bl; duoPA(String _face, String _source){ face = loadImage(_face); source =minim.loadFile(_source, 1024); beat = new BeatDetect(source.bufferSize(), source.sampleRate()); bl = new BeatListener(beat,source); } public void stop(){ // always close Minim audio classes when you finish with them this.source.close(); //this.minim.stop(); } } class BeatListener implements AudioListener { private BeatDetect beat; private AudioPlayer source; public BeatListener(BeatDetect beat, AudioPlayer source) { this.source = source; this.source.addListener(this); this.beat = beat; } void samples(float[] samps) { beat.detect(source.mix); } void samples(float[] sampsL, float[] sampsR) { beat.detect(source.mix); } } void call_sample(int val, boolean hold){ if(hold == true) return; //do nothing if there is something held g = c.get(val); g.source.loop(loop_count); face = g.face; println("NEW SAMPLE"); } int ini(int val, int cur_pos){ int randval = floor(random(1,val)); if(cur_pos != 0){ g.source.shiftGain(g.source.getGain(),-40,9000); } else cur_pos = randval; g = c.get(randval); g.source.loop(loop_count); face = g.face; return randval; } void switchtrack(boolean play_ori){ if(play_ori){ ori.unmute(); for(int i = 0; i<30;i++){ tocheck = c.get(i); tocheck.source.mute(); } } else{ ori.mute(); for(int i = 0; i<30;i++){ tocheck = c.get(i); tocheck.source.unmute(); } } } void serialEvent(Serial myPort) { // make sure we have a valid byte if (myPort == null) { return; } //println("HERE"); // read the serial buffer: String myString = myPort.readStringUntil('\n'); // if you got any bytes other than the linefeed: if(myString != null){ // print(myString); myString = trim(myString); // split the string at the commas // and convert the sections into integers: int sensors[] = int(split(myString, ',')); if (sensors.length > 1) { //Printing Values if(sensors[0] == 0) println("Data Held: No"); else println("Data Held: Yes"); // add a linefeed after all the sensor values are printed: //println("here2"); samp_choice = map(sensors[1], 0, 255, 1, 29); samp_choice = floor(samp_choice); // println(sensors[1]); gain_set = map(sensors[2],0,255,-16,16); //println(sensors[2]); println("Sample Choice:",samp_choice); println("Gain Level:",gain_set); switch (sensors[0]){ case 0: hold_val = false; ori.setGain(gain_set); break; case 255: call_sample(int(samp_choice),hold_val); hold_val = true; g.source.setGain(gain_set); break; } println(); // println("here7"); } } // send a byte to ask for more data: myPort.write("A"); } |