# Can I acquire the largest FFT magnitude value without playing through the whole file?

**URL:** <https://discourse.processing.org/t/can-i-acquire-the-largest-fft-magnitude-value-without-playing-through-the-whole-file/9939>\
**Category:** Libraries\
**Created:** [April 3, 2019, 10:08pm UTC](https://discourse.processing.org/t/can-i-acquire-the-largest-fft-magnitude-value-without-playing-through-the-whole-file/9939 "2019-04-03T22:08:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aeggy](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aeggy](https://discourse.processing.org/u/Aeggy)\
**Post date:** [April 3, 2019, 10:08pm UTC](https://discourse.processing.org/t/can-i-acquire-the-largest-fft-magnitude-value-without-playing-through-the-whole-file/9939/1 "2019-04-03T22:08:40Z")

</div>

Trying to figure out how to convert the FFT units to an Amp value in processing proved to be very confusing and complex, but when I play through the whole file, I can find the maximum FFT output and just use that to calculate what I assume is the correct Amp. For example, FFTx/maxFFT gives me an Amp value that sounds right to my ears.

What I’m wondering is if I can just quickly find this max FFT output in the file rather than having to play through all of it.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 5, 2019, 11:43pm UTC](https://discourse.processing.org/t/can-i-acquire-the-largest-fft-magnitude-value-without-playing-through-the-whole-file/9939/2 "2019-04-05T23:43:21Z")

</div>

> [@Aeggy](#):
>
> maximum FFT output

Are you referring to Processing Sound FFT, or minim FFT?

> **[FFT / Libraries](https://processing.org/reference/libraries/sound/FFT.html)**
>
> This is a Fast Fourier Transform (FFT) analyzer. It calculates the normalized power spectrum of an audio stream the moment it is queried with the analyze() method.

[http://code.compartmental.net/minim/javadoc/ddf/minim/analysis/FFT.html](http://code.compartmental.net/minim/javadoc/ddf/minim/analysis/FFT.html)  
[http://code.compartmental.net/minim/fft\_class\_fft.html](http://code.compartmental.net/minim/fft_class_fft.html)

Can you give a short example of what you are trying to do?

---

<div class="post-metadata">

**Author:** ![Aeggy](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aeggy](https://discourse.processing.org/u/Aeggy)\
**Post date:** [April 7, 2019, 5:16am UTC](https://discourse.processing.org/t/can-i-acquire-the-largest-fft-magnitude-value-without-playing-through-the-whole-file/9939/3 "2019-04-07T05:16:16Z")

</div>

I was just testing if I could replicate a sound file using a sine oscillator, which I got fairly close to (but limited by the draw frames and amount of oscillators I can run - about 185 at a time, also bands). I wanted to replicate the volume it plays at as well, so I tried to figure out how to use the value i get in the frequency bins of the FFT spectrum. So I have a value update throughout the clip with the highest FFT value. Then use that as the maximum volume. I was just wondering if I could acquire that maximum without having to play through the whole file.

Edit - example code:

```auto
import processing.sound.*;

SinOsc[] sine = new SinOsc[22000];
SoundFile file;

FFT fft;
int bands = 1024;
float[] spectrum = new float[bands];
float[] ampOl = new float[bands];

float maxAmp = 0;

void setup() {
  String audio ="Example.wav";
  file = new SoundFile(this, audio);
  file.amp(0.01);

  fft = new FFT(this, bands);
  fft.input(file);

  for (int i = 0; i < 22000; i++) {
    sine[i] = new SinOsc(this);
  }

}

void draw() {
if (file.isPlaying()){
  playSines(185);
  }

}

void playSines (int sineNum) {
  fft.analyze(spectrum);
  ampOl = sort(spectrum);
  int[] ampNy = new int[sineNum];
  for (int i = 0; i < sineNum; i++) {
    for (int j = 0; j < bands; j++) {
      if (spectrum[j] == ampOl[bands - 1 - i]){
        sine[i].stop();
        ampNy[i] = j * file.sampleRate()/(bands *2);
        //if (spectrum[j] > maxAmp) { maxAmp = spectrum[j]; println(maxAmp); }
        sine[i].play(ampNy[i], ampOl[bands - 1 - i]/0.0057671145); //0.0057671145 is maxAmp value
        
      }
    }
  }
  
}

void mouseReleased() {
  if (!file.isPlaying()) {
    file.play();
  }
}

```

I know this is a very incorrect way of doing what I want, but I’m still figuring this out… -\_-’
