Previous and Next tracks

edited October 8 in Library Questions

I am following a tutorial on how to process audio into visual arts using a library such as a minim. However, I do have a problem. How can I toggle between one track and another using the KeyPressed Function, please?

import hype.*; import hype.extended.behavior.*; import hype.extended.colorist.*; import hype.extended.layout.*; import hype.interfaces.*;

import ddf.minim.;//pull the classes import ddf.minim.analysis.;

Minim minim;//instance AudioPlayer myAudio;//object FFT myAudioFFT;

boolean showVisualizer = true;

int myAudioRange = 11; // spectrum of audio. high and lows. It spans of over 256 bands int myAudioMax = 100;

float myAudioAmp = 20.0; //any nubmer being sampled while music is being played, amplify it by 20 float myAudioIndex = 0.05; float myAudioIndexAmp = myAudioIndex; float myAudioIndexStep = 0.25;

float[] myAudioData = new float[myAudioRange];

////////////////////************************************** HDrawablePool pool; int poolMax = 100;

color[] pallete = {#FF3300, #FF620C, #FF9519, #0095AB, #FFC725, #F8EF33, #FFFF33, #CCEA4A, #9AD561, #64BE7A, #2EAB93}; ////////////////////**************************************

final String [] audioFiles = { "burners.mp3", "back.mp3", "bustamove.mp3", "bylina.mp3", "causation.mp3", "cone.mp3", "crew.mp3", "deathwarrent.mp3", "dontyou.mp3", "down.mp3", "flip.mp3", "halo.mp3", "horsesoul.mp3", "hotplate.mp3", "intro.mp3", "kamikaze.mp3", "lone.mp3", "master.mp3", "rise.mp3", "run.mp3", "safe&sound.mp3", "samurai.mp3", "threealphs.mp3", "together.mp3", "wildthing.mp3", "youngjesus.mp3" };

int currentAudioFile =0;

void setup(){

fullScreen(P2D);
H.init(this).background(#000000).autoClear(true);

 minim = new Minim(this);//name of the library constructor
 myAudio = minim.loadFile(audioFiles[currentAudioFile]);
 myAudio.play(0);

 myAudioFFT = new FFT(myAudio.bufferSize(), myAudio.sampleRate());//bufferSize = 1024 / sampleRate = 44100.0
 myAudioFFT.linAverages(myAudioRange);// calculate the averages by grouping frequency bands lineraly. use 256 averages.
 myAudioFFT.window(FFT.GAUSS);

 pool = new HDrawablePool(poolMax);
 pool.autoAddToStage()
 .add(new HRect(100).rounding(10))

 .onCreate (
 new HCallback() {
    public void run(Object obj) {
      int randIndex = (int)random(myAudioRange);
      HDrawable d = (HDrawable) obj;
      d
        .stroke(0)
        .fill(pallete[randIndex] , 225)
        .anchorAt(H.CENTER)
        .rotation(45)
        .loc((int)random(width), (int) random(height))
        .extras(new HBundle().num("i", randIndex));
    }

  }

) .requestAll();

}

void draw() { //perform a forwardFFT on the samples in the audio's mix buffer //note that if the audio were a MONO file, this would be the same as using audio.left or audio.right myAudioFFT.forward(myAudio.mix); myAudioDataUpade();

H.drawStage();

for(HDrawable d: pool){ HBundle tempExtra = d.extras(); int i = (int)tempExtra.num("i");

int fftSize;
if(i==0 || i==3) fftSize = (int)map(myAudioData[i], 0, myAudioMax, 0, 300);
else fftSize = (int)map(myAudioData[i], 0, myAudioMax, 0, 100);
d.size(fftSize);

} }

void myAudioDataUpade(){

for(int i =0; i < myAudioRange; i++){ float tempIndexAvg = (myAudioFFT.getAvg(i) * myAudioAmp) * myAudioAmp; float tempIndexCon = constrain(tempIndexAvg,0,myAudioMax); myAudioData[i] = tempIndexCon; myAudioIndexAmp += myAudioIndexStep; }

myAudioIndexAmp = myAudioIndex;

println(myAudioData);

}

void stop(){ myAudio.close(); minim.stop(); super.stop();

}

void keyPressed () { if(!myAudio.isPlaying()){ myAudio.play(); } else{ myAudio.pause(); }

if(key == CODED){ if(keyCode == RIGHT){

} else if(keyCode == LEFT){

} } }

Answers

  • edited October 11

    Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code.

    Kf

Sign In or Register to comment.