svn merge -r39781:39792 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[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                 m_buffer->assureSize(length * AUD_SAMPLE_SIZE(specs));
75
76                 if(length != m_length)
77                 {
78                         if(m_length != 0)
79                         {
80                                 fftw_destroy_plan(m_forward);
81                                 fftw_destroy_plan(m_backward);
82                         }
83
84                         m_length = length;
85
86                         if(m_length * sizeof(double) > m_in->getSize())
87                         {
88                                 m_in->resize(m_length * sizeof(double));
89                                 m_out->resize((m_length / 2 + 1) * sizeof(fftw_complex));
90                         }
91
92                         m_forward = fftw_plan_dft_r2c_1d(m_length,
93                                                                                          (double*)m_in->getBuffer(),
94                                                                                          (fftw_complex*)m_out->getBuffer(),
95                                                                                          FFTW_ESTIMATE);
96                         m_backward = fftw_plan_dft_c2r_1d(m_length,
97                                                                                           (fftw_complex*)m_out->getBuffer(),
98                                                                                           (double*)m_in->getBuffer(),
99                                                                                           FFTW_ESTIMATE);
100                 }
101
102                 double* target = (double*) m_in->getBuffer();
103                 sample_t* target2 = m_buffer->getBuffer();
104                 fftw_complex* complex = (fftw_complex*) m_out->getBuffer();
105                 float frequency;
106
107                 for(int channel = 0; channel < specs.channels; channel++)
108                 {
109                         for(int i = 0; i < m_length; i++)
110                                 target[i] = buffer[i * specs.channels + channel];
111
112                         fftw_execute(m_forward);
113
114                         for(int i = 0; i < m_length / 2 + 1; i++)
115                         {
116                                 frequency = i * specs.rate / (m_length / 2.0f + 1.0f);
117                                 if((frequency < m_low) || (frequency > m_high))
118                                         complex[i][0] = complex[i][1] = 0.0;
119                         }
120
121                         fftw_execute(m_backward);
122
123                         for(int i = 0; i < m_length; i++)
124                                 target2[i * specs.channels + channel] = target[i] / m_length;
125                 }
126         }
127
128         buffer = m_buffer->getBuffer();
129 }