1 // -----------------------------------------------------------------------
2 //
3 //  Copyright (C) 2009-2011 Fons Adriaensen <fons@linuxaudio.org>
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 2 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //
19 // -----------------------------------------------------------------------
20 
21 #ifndef __RETUNER_H
22 #define __RETUNER_H
23 
24 #include "resampler.h"
25 #include <fftw3.h>
26 
27 namespace LV2AT {
28 
29 class Retuner
30 {
31 public:
32 	Retuner (int fsamp);
33 	~Retuner (void);
34 
35 	int process (int nfram, float const* inp, float* out);
36 
37 	void
set_refpitch(float v)38 	set_refpitch (float v)
39 	{
40 		_refpitch = v;
41 	}
42 
43 	void
set_notebias(float v)44 	set_notebias (float v)
45 	{
46 		_notebias = v / 13.0f;
47 	}
48 
49 	void
set_corrfilt(float v)50 	set_corrfilt (float v)
51 	{
52 		_corrfilt = (4 * _frsize) / (v * _fsamp);
53 	}
54 
55 	void
set_corrgain(float v)56 	set_corrgain (float v)
57 	{
58 		_corrgain = v;
59 	}
60 
61 	void
set_corroffs(float v)62 	set_corroffs (float v)
63 	{
64 		_corroffs = v;
65 	}
66 
67 	void
set_notemask(int k)68 	set_notemask (int k)
69 	{
70 		_notemask = k;
71 	}
72 
73 	void
set_notescale(int i,float v)74 	set_notescale (int i, float v)
75 	{
76 		_notescale[i] = i + v;
77 	}
78 
79 	void
set_fastmode(float v)80 	set_fastmode (float v)
81 	{
82 		_fastmode = (bool)v;
83 	}
84 
85 	int
get_noteset(void)86 	get_noteset (void)
87 	{
88 		int k     = _notebits;
89 		_notebits = 0;
90 		return k;
91 	}
92 
93 	float
get_error(void)94 	get_error (void)
95 	{
96 		return 12.0f * _error;
97 	}
98 
99 private:
100 	void  findcycle (void);
101 	void  finderror (void);
102 
103 	static float cubic (float* v, float a);
104 
105 	int              _fsamp;
106 	int              _ifmin;
107 	int              _ifmax;
108 	bool             _upsamp;
109 	int              _fftlen;
110 	int              _ipsize;
111 	int              _frsize;
112 	int              _ipindex;
113 	int              _frindex;
114 	int              _frcount;
115 	float            _refpitch;
116 	float            _notebias;
117 	float            _corrfilt;
118 	float            _corrgain;
119 	float            _corroffs;
120 	int              _notemask;
121 	int              _notebits;
122 	int              _lastnote;
123 	int              _count;
124 	float            _cycle;
125 	float            _error;
126 	float            _ratio;
127 	float            _phase;
128 	bool             _xfade;
129 	float            _rindex1;
130 	float            _rindex2;
131 	float*           _ipbuff;
132 	float*           _xffunc;
133 	float*           _fftTwind;
134 	float*           _fftWcorr;
135 	float*           _fftTdata;
136 	fftwf_complex*   _fftFdata;
137 	fftwf_plan       _fwdplan;
138 	fftwf_plan       _invplan;
139 	LV2AT::Resampler _resampler;
140 
141 	float _notescale[12];
142 	bool  _fastmode;
143 	bool  _lastfastmode;
144 	int   _readahed;
145 };
146 
147 }; // namespace LV2AT
148 
149 #endif
150