0

I'm trying to create a simple spectrum via QAudioProbe but my spectrum does not "feel the beat". every bin in spectrum just goes high and low. Here is my code processing buffer from QAudioProbe :

void Waveform::bufferReady(QAudioBuffer buffer){  

int n = buffer.frameCount();
cfg = kiss_fft_alloc(n, 0/*is_inverse_fft*/, NULL, NULL);
QAudioBuffer::S16U *frames = buffer.data<QAudioBuffer::S16U>();
qDeleteAll(m_finalData);
m_finalData.clear();

kiss_fft_cpx output[n],input[n];
for (int i=0; i < n; i++)
{
    // frames[i].right contains the i-th sample from the right channel
    // frames[i].left contains the i-th sample from the left channel
    // if the signal is mono and not stereo, then only one of the channels will have data
    qreal hanawindow = 0.5 * (1 - qCos((2 * M_PI * i) / (n - 1)));
    input[i].r = frames[i].right * hanawindow; // WindowFunction
    input[i].i = 0;
}

kiss_fft(cfg, input, output);  // DO FFT
int step = n/(2*60); // distance to take value for bin from list. Here is 60bins

for(int i=0; i< n/2;i+=step){
    qreal magnitude = qSqrt(output[i].i*output[i].i + output[i].r*output[i].r);
    qreal amplitude = 0.15 * log10(magnitude);
    amplitude = qMax(qreal(0.0), amplitude);
    amplitude = qMin(qreal(1.0), amplitude);
    m_finalData.append(new Sample(amplitude));
}

qDebug() << "Number of Bins : " << m_finalData.count();
emit dataReady();
}

I don't know what are problems with the above code. I've been trying a lot of other ways but the spectrum still weird.

Cristik
  • 30,989
  • 25
  • 91
  • 127
SuperStar
  • 1
  • 1
  • 1
    I think that the problem is you're using `QAudioBuffer::S16U (unsigned short)` with range from 0 to 65535, which means that sometimes the sample maybe a number bigger than the range -1 to 1 that floats gives, resulting in an overflow. Try using `QAudioBuffer::S32F(float)` instead! If you want an inspiration please take a look at my [code](https://github.com/antonypro/AudioStreaming/blob/master/Demos/BroadcastClient/spectrumanalyzer.cpp), part of my [AudioStreamingLib](https://github.com/antonypro/AudioStreaming/). ;) – Antonio Dias Oct 10 '18 at 15:46
  • Thank you very much for helping :) – SuperStar Oct 11 '18 at 16:48

0 Answers0