1 /*
2  * Copyright (C) 2018 Johannes Mueller <github@johannes-mueller.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 
20 /* Shared code for a-comp.c and a-exp.c
21  *
22  * This file contains some code that draws the basic layout of for the inline
23  * rendering of a-comp.c and a-exp.c. It is put into a shared file for better
24  * maintainability. It is not meant to be compiled as a individual compilation
25  * unit but to be included like
26  *
27  * #include "dynamic_screen.c"
28  *
29  */
30 
31 
32 static inline void
draw_grid(cairo_t * cr,const float w,const float h)33 draw_grid (cairo_t* cr, const float w, const float h)
34 {
35 	// clear background
36 	cairo_rectangle (cr, 0, 0, w, h);
37 	cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
38 	cairo_fill (cr);
39 
40 	cairo_set_line_width(cr, 1.0);
41 
42 	// draw grid 10dB steps
43 	const double dash1[] = {1, 2};
44 	const double dash2[] = {1, 3};
45 	cairo_save (cr);
46 	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
47 	cairo_set_dash(cr, dash2, 2, 2);
48 	cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
49 
50 	for (uint32_t d = 1; d < 7; ++d) {
51 		const float x = -.5 + floorf (w * (d * 10.f / 70.f));
52 		const float y = -.5 + floorf (h * (d * 10.f / 70.f));
53 
54 		cairo_move_to (cr, x, 0);
55 		cairo_line_to (cr, x, h);
56 		cairo_stroke (cr);
57 
58 		cairo_move_to (cr, 0, y);
59 		cairo_line_to (cr, w, y);
60 		cairo_stroke (cr);
61 	}
62 	cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
63 	cairo_set_dash(cr, dash1, 2, 2);
64 
65 	// diagonal unity
66 	cairo_move_to (cr, 0, h);
67 	cairo_line_to (cr, w, 0);
68 	cairo_stroke (cr);
69 	cairo_restore (cr);
70 
71 	{ // 0, 0
72 		cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
73 		const float x = -.5 + floorf (w * (60.f / 70.f));
74 		const float y = -.5 + floorf (h * (10.f / 70.f));
75 		cairo_move_to (cr, x, 0);
76 		cairo_line_to (cr, x, h);
77 		cairo_stroke (cr);
78 		cairo_move_to (cr, 0, y);
79 		cairo_line_to (cr, w, y);
80 		cairo_stroke (cr);
81 	}
82 }
83 
84 static inline void
draw_GR_bar(cairo_t * cr,const float w,const float h,const float gainr)85 draw_GR_bar (cairo_t* cr, const float w, const float h, const float gainr)
86 {
87 	const float x = -.5 + floorf (w * (62.5f / 70.f));
88 	const float y = -.5 + floorf (h * (10.0f / 70.f));
89 	const float wd = floorf (w * (5.f / 70.f));
90 	const float ht = floorf (h * (55.f / 70.f));
91 	cairo_rectangle (cr, x, y, wd, ht);
92 	cairo_fill (cr);
93 
94 	const float h_gr = fminf (ht, floorf (h * gainr / 70.f));
95 	cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
96 	cairo_rectangle (cr, x, y, wd, h_gr);
97 	cairo_fill (cr);
98 	cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
99 	cairo_rectangle (cr, x, y, wd, ht);
100 	cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 1.0);
101 	cairo_stroke (cr);
102 }
103 
104 static inline void
draw_inline_bars(cairo_t * cr,const float w,const float h,const float thresdb,const float ratio,const float peakdb,const float gainr,const float level_in,const float level_out)105 draw_inline_bars (cairo_t* cr, const float w, const float h,
106 		  const float thresdb, const float ratio,
107 		  const float peakdb, const float gainr,
108 		  const float level_in, const float level_out)
109 {
110 	cairo_rectangle (cr, 0, 0, w, h);
111 	cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
112 	cairo_fill (cr);
113 
114 
115 	cairo_save (cr);
116 
117 	const float ht = 0.25f * h;
118 
119 	const float x1 = w*0.05;
120 	const float wd = w - 2.0f*x1;
121 
122 	const float y1 = 0.17*h;
123 	const float y2 = h - y1 - ht;
124 
125 	cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
126 
127 	cairo_rectangle (cr, x1, y1, wd, ht);
128 	cairo_fill (cr);
129 
130 	cairo_rectangle (cr, x1, y2, wd, ht);
131 	cairo_fill (cr);
132 
133 	cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
134 	const float w_gr = (gainr > 60.f) ? wd : wd * gainr * (1.f/60.f);
135 	cairo_rectangle (cr, x1+wd-w_gr, y2, w_gr, ht);
136 	cairo_fill (cr);
137 
138 	if (level_in > -60.f) {
139 		if (level_out > 6.f) {
140 			cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
141 		} else if (level_out > 0.f) {
142 			cairo_set_source_rgba (cr, 0.66, 0.66, 0.0, 1.0);
143 		} else {
144 			cairo_set_source_rgba (cr, 0.0, 0.66, 0.0, 1.0);
145 		}
146 		const float w_g = (level_in > 10.f) ? wd : wd * (60.f+level_in) / 70.f;
147 		cairo_rectangle (cr, x1, y1, w_g, ht);
148 		cairo_fill (cr);
149 	}
150 
151 	cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
152 
153 	const float tck = 0.33*ht;
154 
155 	cairo_set_line_width (cr, .5);
156 
157 	for (uint32_t d = 1; d < 7; ++d) {
158 		const float x = x1 + (d * wd * (10.f / 70.f));
159 
160 		cairo_move_to (cr, x, y1);
161 		cairo_line_to (cr, x, y1+tck);
162 
163 		cairo_move_to (cr, x, y1+ht);
164 		cairo_line_to (cr, x, y1+ht-tck);
165 
166 		cairo_move_to (cr, x, y2);
167 		cairo_line_to (cr, x, y2+tck);
168 
169 		cairo_move_to (cr, x, y2+ht);
170 		cairo_line_to (cr, x, y2+ht-tck);
171 	}
172 
173 	cairo_stroke (cr);
174 
175 	const float x_0dB = x1 + wd*(60.f/70.f);
176 
177 	cairo_move_to (cr, x_0dB, y1);
178 	cairo_line_to (cr, x_0dB, y1+ht);
179 
180 	cairo_rectangle (cr, x1, y1, wd, ht);
181 	cairo_rectangle (cr, x1, y2, wd, ht);
182 	cairo_stroke (cr);
183 
184 	cairo_set_line_width (cr, 2.0);
185 
186 	// visualize threshold
187 	const float tr = x1 + wd * (60.f+thresdb) / 70.f;
188 	cairo_set_source_rgba (cr, 0.95, 0.95, 0.0, 1.0);
189 	cairo_move_to (cr, tr, y1);
190 	cairo_line_to (cr, tr, y1+ht);
191 	cairo_stroke (cr);
192 
193 	// visualize ratio
194 	const float reduced_0dB = thresdb * (1.f - 1.f/ratio);
195 	const float rt = x1 + wd * (60.f+reduced_0dB) / 70.f;
196 	cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
197 	cairo_move_to (cr, rt, y1);
198 	cairo_line_to (cr, rt, y1+ht);
199 	cairo_stroke (cr);
200 
201 	// visualize in peak
202 	if (peakdb > -60.f) {
203 		cairo_set_source_rgba (cr, 0.0, 1.0, 0.0, 1.0);
204 		const float pk = (peakdb > 10.f) ? x1+wd : wd * (60.f+peakdb) / 70.f;
205 		cairo_move_to (cr, pk, y1);
206 		cairo_line_to (cr, pk, y1+ht);
207 		cairo_stroke (cr);
208 	}
209 }
210