display spectrum

This commit is contained in:
Sebastien Bourdeauducq 2023-08-23 21:29:42 +08:00
parent f28c0965ec
commit 020469d1a2
1 changed files with 22 additions and 4 deletions

View File

@ -5,6 +5,7 @@
#include <atomic>
#include <mutex>
#include <vector>
#include <complex>
#include <GLFW/glfw3.h>
@ -27,18 +28,35 @@ static int waterfall_height = 700;
static unsigned int waterfall_data[1600*700];
static void dsp_thread() {
std::vector<short int> frames(6400);
size_t len = 6400;
std::vector<short int> frames(len);
std::vector<float> frames_f(len);
pocketfft::shape_t shape{len};
pocketfft::stride_t stride(shape.size());
size_t tmp=sizeof(float);
for (int i=shape.size()-1; i>=0; --i) {
stride[i] = tmp;
tmp *= shape[i];
};
pocketfft::shape_t axes;
for(size_t i=0; i<shape.size(); ++i)
axes.push_back(i);
std::vector<std::complex<float>> frames_ft(len);
while(!terminate_dsp) {
int read_count = snd_pcm_readi(pcm, frames.data(), 6400);
int read_count = snd_pcm_readi(pcm, frames.data(), len);
if(read_count < 0) {
std::cerr << "read from audio interface failed: " << snd_strerror(read_count) << std::endl;
break;
}
for(size_t i=0;i<len;i++)
frames_f[i] = frames[i];
pocketfft::r2c(shape, stride, stride, axes, true, frames_f.data(), frames_ft.data(), 1.0f);
{
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], 4*waterfall_width*(waterfall_height - 1));
std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], sizeof(int)*waterfall_width*(waterfall_height - 1));
for(int i=0;i<waterfall_width;i++) {
waterfall_data[i] = 0xff000000 | 0x010101*(frames[i*read_count/waterfall_width]/256 + 128);
waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(abs(frames_ft[i*len/(4*waterfall_width)])/10000.), 255);
}
}
};