Sound and Acceleration

I have been bombarding this post with many questions and I’m sorry that I have to rely so much on the help of you guys, I may assure that everything I’m asking it took me at least 1-2 hours of attempt and failure by myself until the point that I realized i better ask people who know more than me, that being said, my objective is the following

SOUND PLAYED #TIMES DECIDED BY ACCELERATION
ACCELERATION+ AMOUNT OF TIMES +
ACCELERATION- AMOUNT OF TIMES -

my issue is that I’m able to control it through the acceleration, but the amount of times played is too many, would it be possible to limit the number of times played for a given amount of times ( for example every 500 milliseconds or 1000 milliseconds) again thank you for your time, and i hope one day my coding skills are high enough to either give you guys the help back , or to help others who need it, thanks in advance!.

import processing.sound.*;

float x;
float playbackSpeed;
float x_po, pr_x, pa_x, x_d;
SoundFile soundfile;
Delay delay;
float currTime, prevTime, dt, velocity = 1,sound_time;


void setup() {
  size(640, 360);
  background(255);
  soundfile = new SoundFile(this, "sound_0.mp3");
  currTime = prevTime = millis();
  delay = new Delay(this);
  delay.process(soundfile,10);
  delay.time(10);
  //soundfile.loop();
  
}

void draw() {
  //ACCELERATION TEST CODE//
  background(255);
  x_po=mouseX;

  x_d = abs((pa_x-x_po));

  println("x_po = "+x_po);
  println("pa_x = "+ pa_x);
  println("x_d = "+x_d);
  
  pa_x = x_po;


  println("");

  currTime = millis();
  if (x_d != 0) {
    dt = abs(prevTime-currTime);
  } else if (x_d == 0) {
    dt = 0.1;
  }
  println("currTime"+currTime);
  println("prevTime"+prevTime);
  println("dt ="+dt);
  velocity =x_d/(dt/1000);
  println("Velocity= "+velocity);
  prevTime = currTime;
  float velocity_c = map(velocity, 0, 250,0,100);
  velocity_c = constrain(velocity_c, 0,100);
  println("velocity_c"+velocity_c);

  fill(255, 0, 0);
  ellipse(x_po, height/2, 45, 45);
  
  
  println("");
  
   x = width/10;
    if (x_d <= 20) {
      if (velocity_c >= 15) {
        playbackSpeed = 0.9;
        fill(0, 255, 0, 30);
        rect(0, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
        soundfile.play();
      
     } else if (velocity_c >= 15 && velocity_c < 50) {
        playbackSpeed = 1.5;
        fill(255, 0, 0, 30);
        rect(x, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
     }
     /* } else if (velocity_c >= 20 && velocity_c < 30) {
        playbackSpeed = 3;
        fill(0, 0, 255, 30);
        rect(x*2, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
      } else if (velocity_c >= 30 && velocity_c < 40) {
        playbackSpeed = 4;
        fill(255, 0, 0, 30);
        rect(x*3, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
      } else if (velocity_c >= 40 && velocity_c < 50) {
        playbackSpeed = 5;
        fill(0, 255, 0, 30);
        rect(x*4, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
      } else if (velocity_c >= 50 && velocity_c < 60) {
        playbackSpeed = 6;
        fill(0, 0, 255, 30);
        rect(x*5, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
      } else if (velocity_c >= 60 && velocity_c < 70) {
        playbackSpeed = 7;
        fill(255, 0, 0, 30);
        rect(x*6, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
      } else if (velocity_c >= 70 && velocity_c < 80) {
        playbackSpeed = 8;
        fill(0, 255, 0, 30);
        rect(x*7, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
      } else if (velocity_c >= 80 && velocity_c < 90) {
        playbackSpeed = 9;
        fill(0, 0, 255, 30);
        rect(x*8, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
      } else if (velocity_c >= 90 && velocity_c < 100) {
        playbackSpeed = 10;
        fill(255, 0, 0, 30);
        rect(x*9, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
      }
      */
    }
  //FINNISH//
}
1 Like

No worries, this is a place to ask questions (someone replies or not is another problem though). It would be helpful if you can post the sound file or at least give the duration of it to get the sense of what you want to achieve (is it a percussive sound lasts for a fraction of a second or is it an entire song for a few minutes? I’m assuming for former for now).

If you want to make something repeatedly, you can use frameCount or millis() for timing. For example, easy one is

void setup() {
  frameRate(60);
}
void draw() {
  if(frameCount % 60 == 0) {
    println("bang!");
  }
}

but unfortunately this is not super accurate because there is no guarantee that one frame takes 1/60 seconds. A better approach would be a bit longer

void setup() {
  frameRate(60);
}
float lastT = 0;
float interval = 1;
void draw() {
  float t = millis() * 0.001;
  if(t - lastT > interval) {
    println("bang!");
    lastT += interval;
  }
}

A nice thing is that you can change the interval on the fly, for example:

  interval = map(mouseX, 0, width, 1, 2);
2 Likes

Hello man first of all thanks for your and answer and for the time you took to think, elaborate and write it, as I’m reading through your answer and the code you wrote (thanks for leading me through a clear example) i realized that you asked me about the audio file here you may find it: (AUDIO FILE ), as you said it’s only a one second or so sound, I will take a good look to the examples you wrote and will do my best for implementing it with my original code, without concerns about the result, thanks again man!.

MAN THIS LOOKS GOOD!, THANK YOU SO MUCH!, SO FAR SO GOOD , This is exactly what I was looking for I just tested the code, and its working like a marvel!.

CORRECTED CODE

import processing.sound.*;

float x;
float playbackSpeed;
float x_po, pr_x, pa_x, x_d;
float prevTime;
float dt, velocity = 1;
float currTime;
float lastT = 0;
float interval = 0.5;
float vel_1,vel_2;
SoundFile soundfile;


void setup() {
  frameRate(60);
  size(640, 360);
  background(255);
  soundfile = new SoundFile(this, "sound_0.mp3");
  
  
}

void draw() {
  //ACCELERATION TEST CODE//
  float t = millis() * 0.001;
  background(255);
  x_po=mouseX;

  x_d = abs((pa_x-x_po));

  //println("x_po = "+x_po);
  //println("pa_x = "+ pa_x);
  //println("x_d = "+x_d);
  
  pa_x = x_po;


  //println("");

  currTime = millis();
  if (x_d != 0) {
    dt = abs(prevTime-currTime);
  } else if (x_d == 0) {
    dt = 0.1;
  }
  //println("currTime"+currTime);
  //println("prevTime"+prevTime);
  //println("dt ="+dt);
  velocity =x_d/(dt/1000);
  //println("Velocity= "+velocity);
  prevTime = currTime;
  float velocity_c = map(velocity, 0, 3000,0,100);
  velocity_c = constrain(velocity_c, 0,100);
  //println("velocity_c"+velocity_c);

  fill(255, 0, 0);
  ellipse(x_po, height/2, 45, 45);
  
  
  println("");
  println("velocity_c" + velocity_c);
   x = width/10;
   if(t-lastT > interval){
    if (x_d <= 10) {
      if (velocity_c >= 1  ) {
        playbackSpeed = map(x_po, 0, width, 0.50, 0.50);
        fill(0, 255, 0, 30);
        rect(0, 0, x, height, 25);
        soundfile.rate(playbackSpeed);
        soundfile.play();
      }
    }
    lastT+=interval;
   }
  //FINNISH//
}
1 Like