syedhali / EZAudio

An iOS and macOS audio visualization framework built upon Core Audio useful for anyone doing real-time, low-latency audio processing and visualizations.
Other
4.96k stars 824 forks source link

Trying to understand FFT data #225

Open mikeydelamonde opened 9 years ago

mikeydelamonde commented 9 years ago

I am trying to create an app that outputs a pattern based on the FFT of the audio source. Basically as the audio plays time is along the x axis and the FFT is along the y-axis. As the music plays a pattern is created of squares and the colour of the square will be dictated by the amplitude of the frequency in that FFT sub-region.

The code looks like this and the FFTViewControllerFFTWindowSize = 150;

//------------------------------------------------------------------------------
#pragma mark - EZAudioPlayerDelegate
//------------------------------------------------------------------------------

- (void)  audioPlayer:(EZAudioPlayer *)audioPlayer
          playedAudio:(float **)buffer
       withBufferSize:(UInt32)bufferSize
 withNumberOfChannels:(UInt32)numberOfChannels
          inAudioFile:(EZAudioFile *)audioFile
{
    [self.fft computeFFTWithBuffer:buffer[0] withBufferSize:bufferSize];
}

//------------------------------------------------------------------------------
#pragma mark - EZAudioFFTDelegate
//------------------------------------------------------------------------------

- (void)        fft:(EZAudioFFT *)fft
 updatedWithFFTData:(float *)fftData
         bufferSize:(vDSP_Length)bufferSize
{
    __weak typeof (self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.audioPlot updateBuffer:fftData withBufferSize:(UInt32)bufferSize];
        for (int i = 0; i<bufferSize; i++) {
            NSLog(@"fft val %f", fftData[i]);
        }
    });
}

The NSLog is there to demonstrate my problem. I've picked two random lists from a time point and pasted them below. Basically I thought that the maximum that an FFT value could be was 1 but in one of them there is a value of 6.952471 and also in the other all the values are tiny e.g. 0.001 even though I can hear it's playing something. What am I doing wrong?

I am trying to reproduce something that uses Flash's computeSpectrum function which returns an array of 256 floats between 0 and 1. This seems pretty easy!

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundMixer.html#computeSpectrum()

Point 1 Point 2
0.005816 6.952471
0.010704 1.260185
0.004519 0.266269
0.002781 0.152540
0.038111 0.073087
0.274509 0.125507
0.004770 0.010155
0.001016 0.023328
0.000611 0.032141
0.000115 0.003583
0.001193 0.009749
0.000731 0.011863
0.000630 0.027307
0.000996 0.002040
0.000452 0.005508
0.002430 0.004133
0.001266 0.006598
0.000089 0.001877
0.000483 0.002913
0.000826 0.004395
0.005266 0.003958
0.014211 0.002250
0.008589 0.008281
0.004920 0.002627
0.008350 0.002476
0.002999 0.003924
0.002559 0.007930
0.005831 0.004289
0.011384 0.001885
0.012413 0.005358
0.006980 0.002461
0.013374 0.000612
0.015251 0.005339
0.003759 0.002789
0.006180 0.000378
0.003345 0.001412
0.008930 0.001558
0.003787 0.002587
0.000106 0.001820
0.001123 0.000735
0.000924 0.001579
0.000151 0.001727
0.002845 0.000791
0.008067 0.001092
0.005768 0.002239
0.000094 0.001397
0.001194 0.002666
0.001379 0.001862
0.006607 0.002025
0.000655 0.002483
0.004555 0.001759
0.000482 0.001794
0.002681 0.002802
0.000500 0.000716
0.002257 0.002413
0.000055 0.001505
0.001836 0.002478
0.002179 0.001258
0.006315 0.001065
0.000939 0.001652
0.000145 0.002018
0.004384 0.001556
0.002653 0.002018
0.000954 0.001747
0.000014 0.001198
0.000021 0.001133
0.000004 0.000924
0.000018 0.000901
0.000051 0.000748
0.000023 0.000646
0.000022 0.000739
0.000114 0.000669
0.000567 0.000658
0.001699 0.000710
0.000680 0.000622
aronsuarez commented 9 years ago

Do you have a solution for the problem?