ian@0: // Copyright 2011, Ian Hobson. ian@0: // ian@0: // This file is part of gpsynth. ian@0: // ian@0: // gpsynth is free software: you can redistribute it and/or modify ian@0: // it under the terms of the GNU General Public License as published by ian@0: // the Free Software Foundation, either version 3 of the License, or ian@0: // (at your option) any later version. ian@0: // ian@0: // gpsynth is distributed in the hope that it will be useful, ian@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of ian@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ian@0: // GNU General Public License for more details. ian@0: // ian@0: // You should have received a copy of the GNU General Public License ian@0: // along with gpsynth in the file COPYING. ian@0: // If not, see http://www.gnu.org/licenses/. ian@0: ian@0: // Wrapper classes for FFTW ian@0: ian@0: #pragma once ian@0: ian@0: #include "fftw3.h" ian@0: ian@0: #include ian@0: ian@0: namespace dsp { ian@0: ian@0: // Float version ian@0: class SpectrumAnalyzerFloat { ian@0: fftwf_plan plan_; ian@0: int frame_size_; ian@0: float* input_; ian@0: std::complex* output_; ian@0: ian@0: public: ian@0: SpectrumAnalyzerFloat(int frame_size = 1024) ian@0: : frame_size_(frame_size) ian@0: { ian@0: input_ = (float*)fftw_malloc(sizeof(float) * frame_size); ian@0: int buffer_size = sizeof(std::complex) * frame_size / 2; ian@0: output_ = (std::complex*)fftw_malloc(buffer_size); ian@0: plan_ = fftwf_plan_dft_r2c_1d(frame_size, ian@0: input_, ian@0: reinterpret_cast(output_), ian@0: FFTW_ESTIMATE); ian@0: } ian@0: ian@0: SpectrumAnalyzerFloat() { ian@0: fftw_free(input_); ian@0: fftw_free(output_); ian@0: fftwf_destroy_plan(plan_); ian@0: } ian@0: ian@0: void Execute() { ian@0: fftwf_execute(plan_); ian@0: } ian@0: ian@0: int Size() { return frame_size_; } ian@0: float* Input() { return input_; } ian@0: std::complex* Output() { return output_; } ian@0: }; ian@0: ian@0: // Double version ian@0: class SpectrumAnalyzer { ian@0: fftw_plan plan_; ian@0: int frame_size_; ian@0: double* input_; ian@0: std::complex* output_; ian@0: ian@0: public: ian@0: SpectrumAnalyzer(int frame_size = 1024) ian@0: : frame_size_(frame_size) ian@0: { ian@0: input_ = (double*)fftw_malloc(sizeof(double) * frame_size); ian@0: int buffer_size = sizeof(std::complex) * frame_size; ian@0: output_ = (std::complex*)fftw_malloc(buffer_size); ian@0: plan_ = fftw_plan_dft_r2c_1d(frame_size, ian@0: input_, ian@0: reinterpret_cast(output_), ian@0: FFTW_ESTIMATE); ian@0: } ian@0: ian@0: SpectrumAnalyzer() { ian@0: fftw_free(input_); ian@0: fftw_free(output_); ian@0: fftw_destroy_plan(plan_); ian@0: } ian@0: ian@0: void Execute() { ian@0: fftw_execute(plan_); ian@0: } ian@0: ian@0: int Size() { return frame_size_; } ian@0: double* Input() { return input_; } ian@0: std::complex* Output() { return output_; } ian@0: }; ian@0: ian@0: } // dsp namespace