2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * Copyright 2009-2011 Jörg Hermann Müller
6 * This file is part of AudaSpace.
8 * Audaspace is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * AudaSpace is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Audaspace; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file audaspace/fftw/AUD_BandPassReader.cpp
30 #include "AUD_BandPassReader.h"
31 #include "AUD_Buffer.h"
36 AUD_BandPassReader::AUD_BandPassReader(AUD_IReader* reader, float low,
38 AUD_EffectReader(reader), m_low(low), m_high(high)
40 m_buffer = new AUD_Buffer(); AUD_NEW("buffer")
41 m_in = new AUD_Buffer(); AUD_NEW("buffer")
42 m_out = new AUD_Buffer(); AUD_NEW("buffer")
46 AUD_BandPassReader::~AUD_BandPassReader()
50 fftw_destroy_plan(m_forward);
51 fftw_destroy_plan(m_backward);
54 delete m_buffer; AUD_DELETE("buffer")
55 delete m_in; AUD_DELETE("buffer")
56 delete m_out; AUD_DELETE("buffer")
59 AUD_ReaderType AUD_BandPassReader::getType()
61 return m_reader->getType();
64 void AUD_BandPassReader::read(int & length, sample_t* & buffer)
66 AUD_Specs specs = m_reader->getSpecs();
68 m_reader->read(length, buffer);
72 m_buffer->assureSize(length * AUD_SAMPLE_SIZE(specs));
74 if(length != m_length)
78 fftw_destroy_plan(m_forward);
79 fftw_destroy_plan(m_backward);
84 if(m_length * sizeof(double) > m_in->getSize())
86 m_in->resize(m_length * sizeof(double));
87 m_out->resize((m_length / 2 + 1) * sizeof(fftw_complex));
90 m_forward = fftw_plan_dft_r2c_1d(m_length,
91 (double*)m_in->getBuffer(),
92 (fftw_complex*)m_out->getBuffer(),
94 m_backward = fftw_plan_dft_c2r_1d(m_length,
95 (fftw_complex*)m_out->getBuffer(),
96 (double*)m_in->getBuffer(),
100 double* target = (double*) m_in->getBuffer();
101 sample_t* target2 = m_buffer->getBuffer();
102 fftw_complex* complex = (fftw_complex*) m_out->getBuffer();
105 for(int channel = 0; channel < specs.channels; channel++)
107 for(int i = 0; i < m_length; i++)
108 target[i] = buffer[i * specs.channels + channel];
110 fftw_execute(m_forward);
112 for(int i = 0; i < m_length / 2 + 1; i++)
114 frequency = i * specs.rate / (m_length / 2.0f + 1.0f);
115 if((frequency < m_low) || (frequency > m_high))
116 complex[i][0] = complex[i][1] = 0.0;
119 fftw_execute(m_backward);
121 for(int i = 0; i < m_length; i++)
122 target2[i * specs.channels + channel] = target[i] / m_length;
126 buffer = m_buffer->getBuffer();