average input channels

This commit is contained in:
Sébastien Bourdeauducq 2024-12-28 19:04:35 +08:00
parent 700df97a69
commit 7b4d0677a1

View File

@ -14,6 +14,7 @@
#define SND_BITS 16 #define SND_BITS 16
#define SND_PCHAN 2 #define SND_PCHAN 2
#define SND_RCHAN 2
#define SND_RATE 44100 #define SND_RATE 44100
#define SND_BUFLEN 4096 #define SND_BUFLEN 4096
@ -38,7 +39,7 @@ static void dsp_thread()
par.sig = 1; par.sig = 1;
par.bits = SND_BITS; par.bits = SND_BITS;
par.pchan = SND_PCHAN; par.pchan = SND_PCHAN;
par.rchan = 1; par.rchan = SND_RCHAN;
par.rate = SND_RATE; par.rate = SND_RATE;
par.le = SIO_LE_NATIVE; par.le = SIO_LE_NATIVE;
par.xrun = SIO_ERROR; par.xrun = SIO_ERROR;
@ -96,13 +97,17 @@ static void dsp_thread()
} }
if(revents & POLLIN) { if(revents & POLLIN) {
int16_t buf_in[SND_BUFLEN]; int16_t buf_in[SND_BUFLEN*SND_RCHAN];
size_t read = sio_read(hdl, buf_in, sizeof(buf_in)); size_t read = sio_read(hdl, buf_in, sizeof(buf_in));
double scale = pow(0.5, SND_BITS-1); // input channels are averaged together to reduce uncorrelated noise
double scale = pow(0.5, SND_BITS-1)/SND_RCHAN;
for(int i=0;i<SND_PCHAN;i++) for(int i=0;i<SND_PCHAN;i++)
for(int j=0;j<read/sizeof(buf_in[0]);j++) { for(int j=0;j<read/(SND_RCHAN*sizeof(buf_in[0]));j++) {
double sample = 0.0;
for(int k=0;k<SND_RCHAN;k++)
sample += (double)buf_in[SND_RCHAN*j+k];
std::complex<double> rotated; std::complex<double> rotated;
rotated = (double)buf_in[j]*std::polar(scale, phase_in[i]*2.0*M_PI/(double)UINT32_MAX); rotated = sample*std::polar(scale, phase_in[i]*2.0*M_PI/(double)UINT32_MAX);
phase_in[i] += ftw[i]; // wraps on underflow phase_in[i] += ftw[i]; // wraps on underflow
lpf_y[i] += (rotated - lpf_y[i])*lpf_k[i]; lpf_y[i] += (rotated - lpf_y[i])*lpf_k[i];