diff --git a/src/audiosource-base/audiosourcewspectrumcapture.cpp b/src/audiosource-base/audiosourcewspectrumcapture.cpp index b689f99..04897aa 100644 --- a/src/audiosource-base/audiosourcewspectrumcapture.cpp +++ b/src/audiosource-base/audiosourcewspectrumcapture.cpp @@ -204,7 +204,7 @@ void AudioSourceWSpectrumCapture::pwLoop() &pwData); struct spa_audio_info_raw audio_info; - audio_info.format = SPA_AUDIO_FORMAT_S16_LE; + audio_info.format = SPA_AUDIO_FORMAT_S16P; audio_info.channels = SPECTRUM_DATA_CHANNELS; audio_info.rate = SPECTRUM_DATA_SAMPLE_RATE; params[0] = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, diff --git a/src/view-player/spectrumwidget.cpp b/src/view-player/spectrumwidget.cpp index 11c90ff..8a5029d 100644 --- a/src/view-player/spectrumwidget.cpp +++ b/src/view-player/spectrumwidget.cpp @@ -19,11 +19,11 @@ float pcmToFloat(qint16 pcm) static void floatPcmToMono(const float *data, float *mono, int channels) { if (channels == 1) { - memcpy(mono, data, sizeof(float) * 512); + memcpy(mono, data, sizeof(float) * N); } else { float *set = mono; - while (set < &mono[512]) { + while (set < &mono[N]) { *set++ = (data[0] + data[1]) / 2; data += channels; } @@ -185,7 +185,7 @@ void SpectrumWidget::paintEvent (QPaintEvent *) if(m_playing) { float mono[N]; float freq[N / 2]; - int channels = 2; // TODO get from format + int channels = m_format.channelCount(); floatPcmToMono(m_data, mono, channels); calc_freq(mono, freq); @@ -225,20 +225,31 @@ void SpectrumWidget::setData(const QByteArray &data, QAudioFormat format) m_format = format; Q_ASSERT(m_format.sampleFormat() == QAudioFormat::Int16); + if(m_format.sampleFormat() != QAudioFormat::Int16) { + // Bad sample format, only Int16 is currently supported + return; + } + const int bytesPerFrame = format.bytesPerFrame(); + Q_ASSERT(bytesPerFrame == 4); // Expecting Stereo Int16 + + if(bytesPerFrame != 4) { + // Bad bytes per frame, expecting 4 (Int16 Stereo) + return; + } - if(data.length() < DFT_SIZE * 4) { + if(data.length() < DFT_SIZE * bytesPerFrame) { // Not enough data for processing, ignore return; } const char *ptr = data.constData(); - for (int i = 0; i < DFT_SIZE * 2; ++i) { + for (int i = 0; i < DFT_SIZE * bytesPerFrame; ++i) { const qint16 pcmSample = *reinterpret_cast(ptr); // Scale down to range [-1.0, 1.0] float floatSample = pcmToFloat(pcmSample); m_data[i] = floatSample; - ptr += bytesPerFrame; + ptr += 2; } } diff --git a/src/view-player/spectrumwidget.h b/src/view-player/spectrumwidget.h index f8d2062..41d234a 100644 --- a/src/view-player/spectrumwidget.h +++ b/src/view-player/spectrumwidget.h @@ -21,7 +21,7 @@ class SpectrumWidget : public QWidget void paintEvent (QPaintEvent *); private: - float m_data[DFT_SIZE * 2]; + float m_data[DFT_SIZE * 4]; float m_xscale[N_BANDS + 1]; int m_bandValues[N_BANDS + 1]; int m_bandDelays[N_BANDS + 1];