switch to double precision

This commit is contained in:
Sébastien Bourdeauducq 2024-12-27 19:15:37 +08:00
parent 3c838c533e
commit 4902a05d2b

View File

@ -18,8 +18,8 @@
#define SND_BUFLEN 4096
static std::atomic<bool> shutdown_threads;
static std::atomic<float> frequency[SND_PCHAN];
static std::atomic<float> lpf_bandwidth[SND_PCHAN];
static std::atomic<double> frequency[SND_PCHAN];
static std::atomic<double> lpf_bandwidth[SND_PCHAN];
static std::mutex lpf_hist_mutex;
static float lpf_hist[SND_PCHAN][512];
@ -64,7 +64,7 @@ static void dsp_thread()
size_t buf_out_idx = SND_BUFLEN*SND_PCHAN;
uint32_t phase_in[SND_PCHAN] = { 0 };
std::complex<float> lpf_y[SND_PCHAN];
std::complex<double> lpf_y[SND_PCHAN];
int lpf_count[SND_PCHAN] = { 0 };
nfds_t nfds = sio_nfds(hdl);
@ -75,18 +75,18 @@ static void dsp_thread()
int revents = sio_revents(hdl, pfd);
uint32_t ftw[SND_PCHAN];
float lpf_k[SND_PCHAN];
double lpf_k[SND_PCHAN];
for(int i=0;i<SND_PCHAN;i++) {
ftw[i] = frequency[i]*(float)UINT32_MAX/SND_RATE;
lpf_k[i] = 2.0f*(float)M_PI*lpf_bandwidth[i]/SND_RATE;
ftw[i] = frequency[i]*(double)UINT32_MAX/SND_RATE;
lpf_k[i] = 2.0*M_PI*lpf_bandwidth[i]/SND_RATE;
}
if(revents & POLLOUT) {
float scale = powf(2.0f, SND_BITS-1) - 1.0f;
double scale = pow(2.0, SND_BITS-1) - 1.0;
if(buf_out_idx == SND_BUFLEN*SND_PCHAN) {
for(int i=0;i<SND_PCHAN;i++)
for(int j=0;j<SND_BUFLEN;j++) {
buf_out[2*j+i] = scale*sinf(phase_out[i]*2.0f*(float)M_PI/(float)UINT32_MAX);
buf_out[2*j+i] = scale*sin(phase_out[i]*2.0*M_PI/(double)UINT32_MAX);
phase_out[i] += ftw[i]; // wraps on overflow
}
buf_out_idx = 0;
@ -98,15 +98,15 @@ static void dsp_thread()
if(revents & POLLIN) {
int16_t buf_in[SND_BUFLEN];
size_t read = sio_read(hdl, buf_in, sizeof(buf_in));
float scale = powf(0.5f, SND_BITS-1);
double scale = pow(0.5, SND_BITS-1);
for(int i=0;i<SND_PCHAN;i++)
for(int j=0;j<read/sizeof(buf_in[0]);j++) {
std::complex<float> rotated;
rotated = (float)buf_in[j]*std::polar(scale, phase_in[i]*2.0f*(float)M_PI/(float)UINT32_MAX);
std::complex<double> rotated;
rotated = (double)buf_in[j]*std::polar(scale, phase_in[i]*2.0*M_PI/(double)UINT32_MAX);
phase_in[i] += ftw[i]; // wraps on underflow
lpf_y[i] += (rotated - lpf_y[i])*lpf_k[i];
float mag = std::abs(lpf_y[i]);
double mag = std::abs(lpf_y[i]);
lpf_count[i]++;
if(lpf_count[i] == 200) {
@ -165,8 +165,8 @@ int main(int argc, char* argv[])
std::atexit(ImGui_ImplOpenGL3_Shutdown);
for(int i=0;i<SND_PCHAN;i++) {
frequency[i] = 441.0f + 202.0f*i;
lpf_bandwidth[i] = 10.0f;
frequency[i] = 441.0 + 202.0*i;
lpf_bandwidth[i] = 10.0;
}
static std::thread dsp_thread_h = std::thread(dsp_thread);