Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 51 additions & 33 deletions src/libprojectM/PCM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ void PCM::_initPCM(int samples) {
start=0;

//Allocate FFT workspace
w= (double *)wipemalloc(maxsamples*sizeof(double));
ip= (int *)wipemalloc(maxsamples*sizeof(int));
// per rdft() documentation
// length of ip >= 2+sqrt(n) and length of w == n/2
#if FFT_LENGTH > 1024
#error update this code
#endif
w = (double *)wipemalloc(FFT_LENGTH/2*sizeof(double));
ip = (int *)wipemalloc(34 * sizeof(int));
ip[0]=0;


/** PCM data */
// this->maxsamples = 2048;
// this->numsamples = 0;
Expand Down Expand Up @@ -134,8 +138,8 @@ void PCM::addPCMfloat(const float *PCMdata, int samples)
if (newsamples>maxsamples) newsamples=maxsamples;
numsamples = getPCMnew(pcmdataR,1,0,waveSmoothing,0,0);
getPCMnew(pcmdataL,0,0,waveSmoothing,0,1);
getPCM(vdataL,512,0,1,0,0);
getPCM(vdataR,512,1,1,0,0);
getPCM(vdataL,FFT_LENGTH,0,1,0,0);
getPCM(vdataR,FFT_LENGTH,1,1,0,0);
}


Expand All @@ -158,8 +162,8 @@ void PCM::addPCMfloat_2ch(const float *PCMdata, int samples)
newsamples=maxsamples;
numsamples = getPCMnew(pcmdataR,1,0,waveSmoothing,0,0);
getPCMnew(pcmdataL,0,0,waveSmoothing,0,1);
getPCM(vdataL,512,0,1,0,0);
getPCM(vdataR,512,1,1,0,0);
getPCM(vdataL,FFT_LENGTH,0,1,0,0);
getPCM(vdataR,FFT_LENGTH,1,1,0,0);
}


Expand All @@ -178,8 +182,8 @@ void PCM::addPCM16Data(const short* pcm_data, short samples) {
if (newsamples>maxsamples) newsamples=maxsamples;
numsamples = getPCMnew(pcmdataR,1,0,waveSmoothing,0,0);
getPCMnew(pcmdataL,0,0,waveSmoothing,0,1);
getPCM(vdataL,512,0,1,0,0);
getPCM(vdataR,512,1,1,0,0);
getPCM(vdataL,FFT_LENGTH,0,1,0,0);
getPCM(vdataR,FFT_LENGTH,1,1,0,0);
}


Expand Down Expand Up @@ -210,8 +214,8 @@ void PCM::addPCM16(short PCMdata[2][512])

numsamples = getPCMnew(pcmdataR,1,0,waveSmoothing,0,0);
getPCMnew(pcmdataL,0,0,waveSmoothing,0,1);
getPCM(vdataL,512,0,1,0,0);
getPCM(vdataR,512,1,1,0,0);
getPCM(vdataL,FFT_LENGTH,0,1,0,0);
getPCM(vdataR,FFT_LENGTH,1,1,0,0);
}


Expand Down Expand Up @@ -243,8 +247,8 @@ void PCM::addPCM8( unsigned char PCMdata[2][1024])
if (newsamples>maxsamples) newsamples=maxsamples;
numsamples = getPCMnew(pcmdataR,1,0,waveSmoothing,0,0);
getPCMnew(pcmdataL,0,0,waveSmoothing,0,1);
getPCM(vdataL,512,0,1,0,0);
getPCM(vdataR,512,1,1,0,0);
getPCM(vdataL,FFT_LENGTH,0,1,0,0);
getPCM(vdataR,FFT_LENGTH,1,1,0,0);
}

void PCM::addPCM8_512( const unsigned char PCMdata[2][512])
Expand Down Expand Up @@ -275,8 +279,8 @@ void PCM::addPCM8_512( const unsigned char PCMdata[2][512])
if (newsamples>maxsamples) newsamples=maxsamples;
numsamples = getPCMnew(pcmdataR,1,0,waveSmoothing,0,0);
getPCMnew(pcmdataL,0,0,waveSmoothing,0,1);
getPCM(vdataL,512,0,1,0,0);
getPCM(vdataR,512,1,1,0,0);
getPCM(vdataL,FFT_LENGTH,0,1,0,0);
getPCM(vdataR,FFT_LENGTH,1,1,0,0);
}


Expand All @@ -291,43 +295,57 @@ void PCM::addPCM8_512( const unsigned char PCMdata[2][512])

void PCM::getPCM(float *PCMdata, int samples, int channel, int freq, float smoothing, int derive)
{
int index;

index=start-1;

if (index<0) index=maxsamples+index;
if (smoothing == 0)
{
for (int i = 0; i < samples; i++)
{
int index = start - 1 - i;
if (index < 0)
index = maxsamples + index;
PCMdata[i] = PCMd[channel][index];
}
}
else
{
int index=start-1;

PCMdata[0]=PCMd[channel][index];
if (index<0)
index=maxsamples+index;

for(int i=1;i<samples;i++)
{
index=start-1-i;
if (index<0) index=maxsamples+index;
PCMdata[0] = PCMd[channel][index];

PCMdata[i]=(1-smoothing)*PCMd[channel][index]+smoothing*PCMdata[i-1];
for (int i = 1; i < samples; i++)
{
index = start - 1 - i;
if (index < 0)
index = maxsamples + index;
PCMdata[i] = (1 - smoothing) * PCMd[channel][index] + smoothing * PCMdata[i - 1];
}
}

//return derivative of PCM data
if(derive)
if (derive)
{
for(int i=0;i<samples-1;i++)
{
PCMdata[i]=PCMdata[i]-PCMdata[i+1];
}
{
PCMdata[i]=PCMdata[i]-PCMdata[i+1];
}
PCMdata[samples-1]=0;
}

//return frequency data instead of PCM (perform FFT)

if (freq)

{
// NOTE some presets set bSpectrum=1 and samples!=2^n, not sure what rdft() does with that
assert(samples <= 1024);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hitting this assert on linux. What to do about it?

samples = std::min(1024,samples);
double temppcm[1024];
for (int i=0;i<samples;i++)
{temppcm[i]=(double)PCMdata[i];}
{temppcm[i]=(double)PCMdata[i];}
rdft(samples, 1, temppcm, ip, w);
for (int j=0;j<samples;j++)
{PCMdata[j]=(float)temppcm[j];}
{PCMdata[j]=(float)temppcm[j];}
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/libprojectM/PCM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@

#include "dlldefs.h"


// 1024 is more computationally intensive, but maybe better at detecting lower bass
#define FFT_LENGTH 1024


class
#ifdef WIN32
DLLEXPORT
Expand All @@ -52,8 +57,8 @@ PCM {
float *pcmdataR; //holder for most recent pcm data

/** PCM data */
float vdataL[512]; //holders for FFT data (spectrum)
float vdataR[512];
float vdataL[FFT_LENGTH]; //holders for FFT data (spectrum)
float vdataR[FFT_LENGTH];

static int maxsamples;
PCM();
Expand Down
Loading