Skip to content

Commit

Permalink
Merge pull request #11 from Rodmg/experimental
Browse files Browse the repository at this point in the history
Fix spectrum analyzer glitches
  • Loading branch information
Rodmg authored Dec 7, 2024
2 parents 86bdd74 + 27bfc42 commit 23163b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/audiosource-base/audiosourcewspectrumcapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 17 additions & 6 deletions src/view-player/spectrumwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<const qint16 *>(ptr);
// Scale down to range [-1.0, 1.0]
float floatSample = pcmToFloat(pcmSample);
m_data[i] = floatSample;
ptr += bytesPerFrame;
ptr += 2;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/view-player/spectrumwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 23163b2

Please sign in to comment.