aboutsummaryrefslogtreecommitdiff
path: root/src/fft_riff.cpp
blob: bce1fd180b5f4a02859da9c439a270c5dded6965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
    cw_binaural~: a binaural synthesis external for pure data
    by David Doukhan - david.doukhan@gmail.com - http://www.limsi.fr/Individu/doukhan
    and Anne Sedes - sedes.anne@gmail.com
    Copyright (C) 2009-2011  David Doukhan and Anne Sedes

    For more details, see CW_binaural~, a binaural synthesis external for Pure Data
    David Doukhan and Anne Sedes, PDCON09


    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <math.h>
#include <m_pd.h>

#include "fft_riff.hpp"
#include "logstring.hpp"


FftRiff::FftRiff(int size): GenericRiff(size)
{
	delete [] _coeffs;
	_coeffs = new float[2* size];

	for (int i = 0; i < 2*size; ++i)
		_coeffs[i] = 0;

	_tmp_input = new float[2 * size];
	_tmp_output = new float[2 * size];
	_tmp_rest = new float[2*size];
	// we divide by size for not having to normalize after a fft + ifft
	for (int i = 0; i < 2*size; ++i)
		_tmp_rest[i] = 0;
}

FftRiff::~FftRiff()
{
	delete [] _tmp_input;
	delete [] _tmp_output;
	delete [] _tmp_rest;
}


void FftRiff::process(float* input, float* output, int n)
{
	int		i;

	for (i = 0; i < n; ++i)
		_tmp_input[i] = input[i] / (_size * 2);
	for (i = n; i < _size * 2; ++i)
		_tmp_input[i] = 0;

	mayer_realfft(2* _size, _tmp_input);

	_tmp_output[0] = _tmp_input[0] * _coeffs[0];
	_tmp_output[_size] = _tmp_input[_size] * _coeffs[_size];

	/// multiply the spectrum of the input and the hrtf
	for (int i = 1; i < _size; ++i)
	{
		// real part
		_tmp_output[i] = _tmp_input[i] * _coeffs[i] - _tmp_input[2*_size-i] * _coeffs[2*_size-i];
		// imag part
		_tmp_output[2*_size-i] = _tmp_input[i] * _coeffs[2*_size-i] + _tmp_input[2*_size-i] * _coeffs[i];
	} 


	mayer_realifft(2*_size, _tmp_output);

	// output the current block beginning + the corresponding rests
	for (i = 0; i < n; ++i)
		output[i] = _tmp_output[i] + _tmp_rest[i];
	for (i = n; i < 2*_size-n; ++i)
		_tmp_rest[i-n] = _tmp_rest[i] + _tmp_output[i];
	for (i = 2*_size - n; i < 2 * _size; ++i)
		_tmp_rest[i-n] = _tmp_output[i];
}