Skip to main content
Participant
February 25, 2023
Answered

Help with expression for jumping to new X position above an audio threshold

  • February 25, 2023
  • 3 replies
  • 927 views

Hi all,

 

Firstly, many thanks for helping even if you're just reading this thread. I'm wondering if all you good people could help me with a personal project I'm working on. Basically I'm trying to make an image jump randomly in the X direction whenever the audio amplitude goes above a certain level, otherwise it stays at that new position.

 

i.e. Image starts at 900px and at each beat above 10 amplitude it jumps to a random number between 880 and 920px before staying there until the amplitude jumps again and it moves another amount plus or minus a number of pixels (say 20 as above).

 

My current expression is as below, and it's great for a kind of wiggle effect, but I can't seem to get it to increment the position it just always goes back to the origin.

 

audioLev = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");
for (i = 0; i < thisComp.frameDuration; i++) {
	if (audioLev < 11.0){ 
	value;
	} else if (audioLev >= 10.0){
	[transform.xPosition += random(-10, 10)]  
}	
}

 

Seeing other answers that have been given, I have no doubt there's something simple I'm missing.

This topic has been closed for replies.
Correct answer Dan Ebberts

I apologize, I think I steered you in the wrong direction. I think this is more a variation of the beat counter expression ( http://www.motionscript.com/design-guide/audio-count.html ) and would end up looking something like this:

threshold = 10.0;

audioLev = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");

above = false;
frame = Math.round(time / thisComp.frameDuration);
n = 0;
seedRandom(index,true);
xOffset = 0;
while (frame >= 0){
  t = frame * thisComp.frameDuration;
  if (above){
    if (audioLev.valueAtTime(t) < threshold){
      above = false;
    }
  }else if (audioLev.valueAtTime(t) >= threshold){
    above = true;
    xOffset += random(-20,20);
  }
  frame--
}
value + [xOffset,0]

3 replies

Participant
February 26, 2023

So after a bit more playing around, and trying to use Dan Ebbert's example, I've come up with the following but it's now just giving a divide by zero error and I can't figure out why. I'd appreciate any insights anyone might have.

 

AudioLev = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider")
KeyFNum = AudioLev.numKeys;
Key1Time = AudioLev.key(1).time


if (KeyFNum > 0 && time < thisComp.duration) {
	accumulator = transform.xPosition.valueAtTime(Key1Time)
	for (i = 1; i <= KeyFNum; i++){
		if (AudioLev.key(i).time > time) break;
		KeyTime = AudioLev.key(i).time
		k1 = transform.xPosition.valueAtTime(KeyTime);
		k2 = random(-20,20);
		addit += (k1 + k2);
	}
	accumulator = addit;
	} else {
	value = accumulator;

accumulator*multiplier
	}
Mylenium
Legend
February 26, 2023
(i = 1; i <= KeyFNum; i++)

 

You will eventually overflow your key counter. Fix your iterator to align with the actual way keys are counted.

 

Mylenium

Mylenium
Legend
February 25, 2023

AE does not remember values and states of variables and you have to integrate and evaluate them in a continuous loop over and over again. Dan already provided the link to his own site.

 

Mylenium 

Participant
February 25, 2023

Thanks, I thought as much, that's what I've been trying to get it to do with the above code but no luck.

Dan Ebberts
Community Expert
Community Expert
February 25, 2023
Participant
February 25, 2023

Hi Dan,

 

Many thanks, much appreciated - I'll continue taking a look at this and hopefully get somewhere. I've been using your website heavily so many thanks for that too!