svn merge ^/trunk/blender -r41226:41227 .
[blender.git] / intern / audaspace / fftw / AUD_BandPassReader.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * Copyright 2009-2011 Jörg Hermann Müller
5  *
6  * This file is part of AudaSpace.
7  *
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.
12  *
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.
17  *
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.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file audaspace/fftw/AUD_BandPassReader.cpp
26  *  \ingroup audfftw
27  */
28
29
30 #include "AUD_BandPassReader.h"
31 #include "AUD_Buffer.h"
32
33 #include <cstring>
34 #include <stdio.h>
35
36 AUD_BandPassReader::AUD_BandPassReader(AUD_IReader* reader, float low,
37                                                                            float high) :
38                 AUD_EffectReader(reader), m_low(low), m_high(high)
39 {
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")
43         m_length = 0;
44 }
45
46 AUD_BandPassReader::~AUD_BandPassReader()
47 {
48         if(m_length != 0)
49         {
50                 fftw_destroy_plan(m_forward);
51                 fftw_destroy_plan(m_backward);
52         }
53
54         delete m_buffer; AUD_DELETE("buffer")
55         delete m_in; AUD_DELETE("buffer")
56         delete m_out; AUD_DELETE("buffer")
57 }
58
59 AUD_ReaderType AUD_BandPassReader::getType()
60 {
61         return m_reader->getType();
62 }
63
64 void AUD_BandPassReader::read(int & length, sample_t* & buffer)
65 {
66         AUD_Specs specs = m_reader->getSpecs();
67
68         m_reader->read(length, buffer);
69
70         if(length > 0)
71         {
72                 m_buffer->assureSize(length * AUD_SAMPLE_SIZE(specs));
73
74                 if(length != m_length)
75                 {
76                         if(m_length != 0)
77                         {
78                                 fftw_destroy_plan(m_forward);
79                                 fftw_destroy_plan(m_backward);
80                         }
81
82                         m_length = length;
83
84                         if(m_length * sizeof(double) > m_in->getSize())
85                         {
86                                 m_in->resize(m_length * sizeof(double));
87                                 m_out->resize((m_length / 2 + 1) * sizeof(fftw_complex));
88                         }
89
90                         m_forward = fftw_plan_dft_r2c_1d(m_length,
91                                                                                          (double*)m_in->getBuffer(),
92                                                                                          (fftw_complex*)m_out->getBuffer(),
93                                                                                          FFTW_ESTIMATE);
94                         m_backward = fftw_plan_dft_c2r_1d(m_length,
95                                                                                           (fftw_complex*)m_out->getBuffer(),
96                                                                                           (double*)m_in->getBuffer(),
97                                                                                           FFTW_ESTIMATE);
98                 }
99
100                 double* target = (double*) m_in->getBuffer();
101                 sample_t* target2 = m_buffer->getBuffer();
102                 fftw_complex* complex = (fftw_complex*) m_out->getBuffer();
103                 float frequency;
104
105                 for(int channel = 0; channel < specs.channels; channel++)
106                 {
107                         for(int i = 0; i < m_length; i++)
108                                 target[i] = buffer[i * specs.channels + channel];
109
110                         fftw_execute(m_forward);
111
112                         for(int i = 0; i < m_length / 2 + 1; i++)
113                         {
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;
117                         }
118
119                         fftw_execute(m_backward);
120
121                         for(int i = 0; i < m_length; i++)
122                                 target2[i * specs.channels + channel] = target[i] / m_length;
123                 }
124         }
125
126         buffer = m_buffer->getBuffer();
127 }