Files
inspectrum/inputsource.cpp
Mike Walters c8940932fb Abstract out data loading/processing
Current super naive, eats all your CPU on repaint
ref #7
2015-06-21 22:42:24 +01:00

79 lines
2.2 KiB
C++

#include "inputsource.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define M_TAU 6.28318530717958647693
InputSource::InputSource(const char *filename, int fft_size) {
m_fft_size = fft_size;
m_file = fopen(filename, "rb");
// TODO: throw exception if failed
struct stat sb;
fstat(fileno(m_file), &sb);
// TODO: throw exception if failed
m_file_size = sb.st_size;
m_data = (fftwf_complex*)mmap(NULL, m_file_size, PROT_READ, MAP_SHARED, fileno(m_file), 0);
// TODO: throw exception if failed
m_fftw_in = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * m_fft_size);
m_fftw_out = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * m_fft_size);
m_fftw_plan = fftwf_plan_dft_1d(m_fft_size, m_fftw_in, m_fftw_out, FFTW_FORWARD, FFTW_MEASURE);
m_window = new float[m_fft_size];
for (int i = 0; i < m_fft_size; i++) {
m_window[i] = 0.5f * (1.0f - cos(M_TAU * i / (m_fft_size - 1)));
}
}
InputSource::~InputSource() {
delete[] m_window;
fftwf_destroy_plan(m_fftw_plan);
fftwf_free(m_fftw_in);
fftwf_free(m_fftw_out);
munmap(m_data, m_file_size);
fclose(m_file);
}
void InputSource::GetViewport(float *dest, int x, int y, int width, int height, int zoom) {
fftwf_complex *sample_ptr = &m_data[y * m_fft_size];
for (int i = 0; i < height; i++) {
memcpy(m_fftw_in, sample_ptr, m_fft_size * sizeof(fftw_complex));
// Apply window
for (int j = 0; j < m_fft_size; j++) {
m_fftw_in[j][0] *= m_window[j];
m_fftw_in[j][1] *= m_window[j];
}
fftwf_execute(m_fftw_plan);
for (int j = x; j < width; j++) {
int k = (j + m_fft_size / 2) % m_fft_size;
float re = m_fftw_out[k][0];
float im = m_fftw_out[k][1];
float mag = sqrt(re * re + im * im) / m_fft_size;
float magdb = 10 * log(mag);
*dest = magdb;
dest++;
}
sample_ptr += m_fft_size;
}
}
int InputSource::GetHeight() {
return m_file_size / sizeof(fftwf_complex) / m_fft_size;
}
int InputSource::GetWidth() {
return m_fft_size;
}