Fix for [#27142] manipulator in particle mode does not work
[blender-staging.git] / intern / audaspace / fftw / AUD_BandPassReader.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * Copyright 2009-2011 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * Audaspace is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Audaspace; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file audaspace/fftw/AUD_BandPassReader.cpp
28  *  \ingroup audfftw
29  */
30
31
32 #include "AUD_BandPassReader.h"
33 #include "AUD_Buffer.h"
34
35 #include <cstring>
36 #include <stdio.h>
37
38 AUD_BandPassReader::AUD_BandPassReader(AUD_IReader* reader, float low,
39                                                                            float high) :
40                 AUD_EffectReader(reader), m_low(low), m_high(high)
41 {
42         m_buffer = new AUD_Buffer(); AUD_NEW("buffer")
43         m_in = new AUD_Buffer(); AUD_NEW("buffer")
44         m_out = new AUD_Buffer(); AUD_NEW("buffer")
45         m_length = 0;
46 }
47
48 AUD_BandPassReader::~AUD_BandPassReader()
49 {
50         if(m_length != 0)
51         {
52                 fftw_destroy_plan(m_forward);
53                 fftw_destroy_plan(m_backward);
54         }
55
56         delete m_buffer; AUD_DELETE("buffer")
57         delete m_in; AUD_DELETE("buffer")
58         delete m_out; AUD_DELETE("buffer")
59 }
60
61 AUD_ReaderType AUD_BandPassReader::getType()
62 {
63         return m_reader->getType();
64 }
65
66 void AUD_BandPassReader::read(int & length, sample_t* & buffer)
67 {
68         AUD_Specs specs = m_reader->getSpecs();
69
70         m_reader->read(length, buffer);
71
72         if(length > 0)
73         {
74                 if(length * AUD_SAMPLE_SIZE(specs) > m_buffer->getSize())
75                         m_buffer->resize(length * AUD_SAMPLE_SIZE(specs));
76
77                 if(length != m_length)
78                 {
79                         if(m_length != 0)
80                         {
81                                 fftw_destroy_plan(m_forward);
82                                 fftw_destroy_plan(m_backward);
83                         }
84
85                         m_length = length;
86
87                         if(m_length * sizeof(double) > m_in->getSize())
88                         {
89                                 m_in->resize(m_length * sizeof(double));
90                                 m_out->resize((m_length / 2 + 1) * sizeof(fftw_complex));
91                         }
92
93                         m_forward = fftw_plan_dft_r2c_1d(m_length,
94                                                                                          (double*)m_in->getBuffer(),
95                                                                                          (fftw_complex*)m_out->getBuffer(),
96                                                                                          FFTW_ESTIMATE);
97                         m_backward = fftw_plan_dft_c2r_1d(m_length,
98                                                                                           (fftw_complex*)m_out->getBuffer(),
99                                                                                           (double*)m_in->getBuffer(),
100                                                                                           FFTW_ESTIMATE);
101                 }
102
103                 double* target = (double*) m_in->getBuffer();
104                 sample_t* target2 = m_buffer->getBuffer();
105                 fftw_complex* complex = (fftw_complex*) m_out->getBuffer();
106                 float frequency;
107
108                 for(int channel = 0; channel < specs.channels; channel++)
109                 {
110                         for(int i = 0; i < m_length; i++)
111                                 target[i] = buffer[i * specs.channels + channel];
112
113                         fftw_execute(m_forward);
114
115                         for(int i = 0; i < m_length / 2 + 1; i++)
116                         {
117                                 frequency = i * specs.rate / (m_length / 2.0f + 1.0f);
118                                 if((frequency < m_low) || (frequency > m_high))
119                                         complex[i][0] = complex[i][1] = 0.0;
120                         }
121
122                         fftw_execute(m_backward);
123
124                         for(int i = 0; i < m_length; i++)
125                                 target2[i * specs.channels + channel] = target[i] / m_length;
126                 }
127         }
128
129         buffer = m_buffer->getBuffer();
130 }