diff --git a/inputsource.cpp b/inputsource.cpp index a2f15d4..899a474 100644 --- a/inputsource.cpp +++ b/inputsource.cpp @@ -17,6 +17,7 @@ InputSource::InputSource(const char *filename, int fft_size) { if (fstat(fileno(m_file), &sb) != 0) throw std::runtime_error("Error fstating file"); m_file_size = sb.st_size; + sampleCount = m_file_size / sizeof(fftwf_complex); m_data = (fftwf_complex*)mmap(NULL, m_file_size, PROT_READ, MAP_SHARED, fileno(m_file), 0); if (m_data == 0) @@ -37,6 +38,15 @@ void InputSource::cleanupFFTW() { if (m_fftw_out != nullptr) fftwf_free(m_fftw_out); } +bool InputSource::getSamples(fftwf_complex *dest, int start, int length) +{ + if (start + length >= sampleCount) + return false; + + memcpy(dest, &m_data[start], length * sizeof(fftwf_complex)); + return true; +} + void InputSource::getViewport(float *dest, int x, int y, int width, int height, int zoom) { fftwf_complex *sample_ptr = &m_data[y * getFFTStride()]; diff --git a/inputsource.h b/inputsource.h index 4588fd1..45bd189 100644 --- a/inputsource.h +++ b/inputsource.h @@ -7,6 +7,7 @@ class InputSource private: FILE *m_file; off_t m_file_size; + off_t sampleCount; fftwf_complex *m_data; int m_fft_size; @@ -27,6 +28,7 @@ public: InputSource(const char *filename, int fft_size); ~InputSource(); + bool getSamples(fftwf_complex *dest, int start, int length); void getViewport(float *dest, int x, int y, int width, int height, int zoom); int getHeight(); int getWidth();