How to move object according to time of sound


#1

So I am trying to make an audio player and am on the part with the time indicator at the bottom which tells how far you are into the song. The main problem is that the songs all have different lengths but the time indicator has only a fixed distance it can move up to a maximum. I was wondering if there was a way to access the length of the audio and make the time indicator move according to that divided by the maximum limit of movement. (using minim to play my audio)


#2

Please provide an MCVE – a very simple sketch showing what you are trying to do.

Are you drawing your own “time indicator” with e.g. rect(), or are you using a library like G4P or ControlP5? What are you using for the audio – are you using the Processing Sound library, or are you using minim?

For minim, an “AudioPlayer” length is here:

http://code.compartmental.net/minim/audioplayer_method_length.html

For a Processing Sound, a “SoundFile” duration is here:

https://processing.org/reference/libraries/sound/SoundFile_duration_.html

To map that length / duration onto a graphical width in pixels, try using map:

https://processing.org/reference/map_.html

or, if you want to duration normalized to 0-1, lerp:

https://processing.org/reference/lerp_.html


#3

Sorry for the late reply, I made a new program that should be enough to show what I am trying to do. If you attempt to play this in processing, you will need minim and an audio file called “song” in your data folder

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim song;
AudioPlayer player;
float playing;
float xpos;

void setup() {
  size(500,500);
 song  = new Minim(this);
 player = song.loadFile("song.mp3");
 playing = 0;
}

void draw() {
 background(0);
 play();
 indicator();
  if (playing == 1) {
    xpos = xpos + 1;
  }
}


void play() {
 if (keyPressed) {
   player.play();
   playing = 1;
 }
}

void indicator() {
  rect(xpos,250,20,20);
}

The part I was hoping to do is to make that square travel from one side of the window to the other at a speed rate that would make it only reach the other side when the song is over (right now, the square is only travelling at a constant rate of 1 no matter what). I hope that the method is not a constant (for only one song) but could change in accordance with the length of the song


#4

Study this example:

PDE > File > Examples > Contributed Libraries > minim > AudioPlayer > setLoopPoints

It shows you how to use AudioPlayer.length() and AudioPlayer.position().

That is the simple way – as an alternative, check this example:

PDE > File > Examples > Contributed Libraries > minim > Basics > GetMetaData

It shows you how to use AudioPlayer.getMetaData() to return an AudioMetaData, and then use AudioMetaData.length().