1 /*
2     Copyright (C) 2011 Tom Szilagyi
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 #ifndef _IR_H
20 #define _IR_H
21 
22 #ifndef _GNU_SOURCE
23   #define _GNU_SOURCE
24 #endif
25 
26 #include <math.h>
27 #include <gtk/gtk.h>
28 #include <sndfile.h>
29 #include <samplerate.h>
30 #include <zita-convolver.h>
31 
32 #define IR_URI                    "http://tomszilagyi.github.io/plugins/lv2/ir"
33 
34 #define BSIZE       0x4000      /* Blocksize for soundfile data access */
35 #define BSIZE_SR    0x1000      /* Blocksize for SRC */
36 #define MAXSIZE 0x00100000  /* Max. available convolver size of zita-convolver */
37 
38 #define exp10(x) pow(10., x) // TODO not as good as exp10()?
39 #define exp10f(x) powf(10., x) // TODO not as good as exp10f()?
40 
41 #define DB_CO(g) ((g) > -90.0f ? exp10f((g) * 0.05f) : 0.0f)
42 #define CO_DB(g) ((g) > 0.0f ? 20.0f * log10f(g) : -90.0f)
43 
44 #define SMOOTH_CO_0      0.01
45 #define SMOOTH_CO_1      0.99
46 
47 /* Audio I/O ports */
48 #define IR_PORT_INPUT_L       0
49 #define IR_PORT_INPUT_R       1
50 #define IR_PORT_OUTPUT_L      2
51 #define IR_PORT_OUTPUT_R      3
52 
53 /* Control ports */
54 #define IR_PORT_REVERSE       4  /* Reverse impulse response [on/off] */
55 #define IR_PORT_PREDELAY      5  /* Predelay [ms] */
56 #define IR_PORT_ATTACK        6  /* Attack [%] */
57 #define IR_PORT_ATTACKTIME    7  /* Attack time [ms] */
58 #define IR_PORT_ENVELOPE      8  /* Envelope [%] */
59 #define IR_PORT_LENGTH        9  /* Length [%] */
60 #define IR_PORT_STRETCH      10  /* Stretch [%] */
61 #define IR_PORT_STEREO_IN    11  /* Stereo width In [%] */
62 #define IR_PORT_STEREO_IR    12  /* Stereo width IR [%] */
63 #define IR_PORT_AGC_SW       13  /* Autogain switch [on/off] */
64 #define IR_PORT_DRY_SW       14  /* Dry switch [on/off] */
65 #define IR_PORT_DRY_GAIN     15  /* Dry gain [dB] */
66 #define IR_PORT_WET_SW       16  /* Wet switch [on/off] */
67 #define IR_PORT_WET_GAIN     17  /* Wet gain [dB] */
68 
69 /* Save/Restore ports */
70 #define IR_PORT_FHASH_0      18
71 #define IR_PORT_FHASH_1      19
72 #define IR_PORT_FHASH_2      20
73 
74 /* Meter ports (output) */
75 #define IR_PORT_METER_DRY_L  21
76 #define IR_PORT_METER_DRY_R  22
77 #define IR_PORT_METER_WET_L  23
78 #define IR_PORT_METER_WET_R  24
79 
80 /* Latency reporting port */
81 #define IR_PORT_LATENCY      25
82 
83 #define IR_N_PORTS           26
84 
85 #define IR_DEFAULT_JACK_BUFLEN    1024
86 #define IR_MAXIMUM_JACK_BUFLEN   16384
87 
88 typedef struct _ir {
89 	/* Audio I/O ports */
90 	const float * in_L;
91 	const float * in_R;
92 	float * out_L;
93 	float * out_R;
94 
95 	unsigned int bufconv_pos;
96 	float drybuf_L[IR_MAXIMUM_JACK_BUFLEN];
97 	float drybuf_R[IR_MAXIMUM_JACK_BUFLEN];
98 	/* convproc's internal buffer acts as the wetbuf */
99 
100 	/* Control ports */
101 	float * port_reverse;
102 	float * port_predelay;
103 	float * port_attack;
104 	float * port_attacktime;
105 	float * port_envelope;
106 	float * port_length;
107 	float * port_stretch;
108 	float * port_stereo_in;
109 	float * port_stereo_ir;
110 	float * port_agc_sw;
111 	float * port_dry_sw;
112 	float * port_dry_gain;
113 	float * port_wet_sw;
114 	float * port_wet_gain;
115 	float * port_fhash_0;
116 	float * port_fhash_1;
117 	float * port_fhash_2;
118 	float * port_meter_dry_L;
119 	float * port_meter_dry_R;
120 	float * port_meter_wet_L;
121 	float * port_meter_wet_R;
122 	float * port_latency;
123 
124 	/* Thread that loads and computes configurations */
125 	GThread * conf_thread;
126 	int conf_thread_exit;
127 	int first_conf_done;
128 
129 	/* Run notify flag */
130 	int run; /* plugin sets this to 1 in each run(),
131 		    UI or conf_thread may set it to 0 and watch */
132 
133 	/* Configuration state */
134 	char * source_path;  /* path of IR audio file */
135 	SNDFILE * Finp;
136 	SF_INFO Sinp;
137 	uint32_t source_samplerate;
138 	int nchan; /* valid values are 1, 2, 4 */
139 	int source_nfram; /* length of source_samples */
140 	float * source_samples; /* IR audio file loads into this array INTERLEAVED */
141 	int ir_nfram; /* length of resampled & ir_samples */
142 	float * resampled_samples; /* Resampled IR samples INTERLEAVED */
143 	float ** ir_samples;     /* de-interleaved, processed samples loaded into Conv */
144 	float autogain;      /* dB */
145 	float autogain_new;  /* dB */
146 
147 	float src_progress;
148 	SRC_STATE * src_state;
149 	SRC_DATA src_data;
150 	int src_in_frames;
151 	int src_out_frames;
152 
153 	/* Processing state */
154 	float width;     /*  internal  */
155 	float dry_gain;  /* (smoothed) */
156 	float wet_gain;  /* parameters */
157 
158 	double sample_rate;
159 	uint32_t maxsize; /* maximum size of IR supported by Convproc instance */
160 	uint32_t block_length; /* defaults to IR_DEFAULT_JACK_BUFLEN, but may grow till IR_MAXIMUM_JACK_BUFLEN */
161 
162 	Convproc * conv_0; /* zita-convolver engine class instances */
163 	Convproc * conv_1;
164 	int conv_in_use;
165 	int conv_req_to_use;
166 	int resample_pending;
167 	int reinit_pending;
168 	int reinit_running;
169 
170 	/* These reference IR lib globals so GUI has access to them */
171 	GKeyFile * keyfile;
172 	GtkListStore * store_bookmarks;
173 
174 	/* Function pointers for GUI */
175 	int (*load_sndfile)(struct _ir *);
176 	int (*resample_init)(struct _ir *);
177 	int (*resample_do)(struct _ir *);
178 	void (*resample_cleanup)(struct _ir *);
179 	void (*prepare_convdata)(struct _ir *);
180 	void (*init_conv)(struct _ir *);
181 } IR;
182 
183 #endif /* _IR_H */
184