| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (C) 2003-2013 Fons Adriaensen <[email protected]> |
| 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 3 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, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// ---------------------------------------------------------------------------- |
| 19 |
|
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <math.h> |
| 25 |
#include "sgenwin.h" |
| 26 |
#include "styles.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
Sgenwin::Sgenwin (X_window *parw, X_resman *resman, X_callback *callb) : |
| 31 |
X_window (parw, XPOS, YPOS, XDEF, YDEF, XftColors.main_bg->pixel), |
| 32 |
_callb (callb), |
| 33 |
_rate (0) |
| 34 |
{ |
| 35 |
X_hints H; |
| 36 |
char s [1024]; |
| 37 |
|
| 38 |
_xatom = XInternAtom (dpy (), "WM_DELETE_WINDOW", True); |
| 39 |
XSetWMProtocols (dpy (), win (), &_xatom, 1); |
| 40 |
_xatom = XInternAtom (dpy (), "WM_PROTOCOLS", True); |
| 41 |
|
| 42 |
H.size (XDEF, YDEF); |
| 43 |
H.minsize (XDEF, YDEF); |
| 44 |
H.maxsize (XDEF, YDEF); |
| 45 |
H.position (XPOS, YPOS); |
| 46 |
H.rname (resman->rname ()); |
| 47 |
H.rclas (resman->rclas ()); |
| 48 |
x_apply (&H); |
| 49 |
|
| 50 |
sprintf (s, "%s-%s [%s] Sweep", PROGNAME, VERSION, resman->rname ()); |
| 51 |
x_set_title (s); |
| 52 |
x_add_events (ExposureMask); |
| 53 |
|
| 54 |
_ready = new X_textip (this, 0, &Tst0, 330, 10, 50, 17, 7); |
| 55 |
_ready->set_text ("Ready"); |
| 56 |
_tname = new X_textip (this, this, &Tst1, 100, 10, 220, 17, 256); |
| 57 |
_tname->x_map (); |
| 58 |
_trate = new X_textip (this, this, &Tst1, 100, 34, 80, 17, 256, _tname); |
| 59 |
_trate->x_map (); |
| 60 |
_ttim1 = new X_textip (this, this, &Tst1, 100, 58, 80, 17, 256, _trate); |
| 61 |
_ttim1->set_text ("0.1"); |
| 62 |
_ttim1->x_map (); |
| 63 |
_tfmin = new X_textip (this, this, &Tst1, 100, 82, 80, 17, 256, _ttim1); |
| 64 |
_tfmin->set_text ("20"); |
| 65 |
_tfmin->x_map (); |
| 66 |
_ttim2 = new X_textip (this, this, &Tst1, 100, 106, 80, 17, 256, _tfmin); |
| 67 |
_ttim2->set_text ("10"); |
| 68 |
_ttim2->x_map (); |
| 69 |
_tfmax = new X_textip (this, this, &Tst1, 100, 130, 80, 17, 256, _ttim2); |
| 70 |
_tfmax->set_text ("20e3"); |
| 71 |
_tfmax->x_map (); |
| 72 |
_ttim3 = new X_textip (this, this, &Tst1, 100, 154, 80, 17, 256, _tfmax); |
| 73 |
_ttim3->set_text ("0.03"); |
| 74 |
_ttim3->x_map (); |
| 75 |
Bst0.size.x = 36; |
| 76 |
Bst0.size.y = 18; |
| 77 |
_blin = new X_tbutton (this, this, &Bst0, 240, 106, "Lin", 0, B_LIN); |
| 78 |
_blin->x_map (); |
| 79 |
_blog = new X_tbutton (this, this, &Bst0, 277, 106, "Log", 0, B_LOG); |
| 80 |
_blog->set_stat (2); |
| 81 |
_blog->x_map (); |
| 82 |
Bst0.size.x = 54; |
| 83 |
Bst0.size.y = 18; |
| 84 |
_bsgen = new X_tbutton (this, this, &Bst0, 355, 130, "Create", 0, B_SGEN); |
| 85 |
_bsgen->set_stat (2); |
| 86 |
_bsgen->x_map (); |
| 87 |
_bcanc = new X_tbutton (this, this, &Bst0, 355, 154, "Close", 0, B_CANC); |
| 88 |
_bcanc->set_stat (1); |
| 89 |
_bcanc->x_map (); |
| 90 |
add_text ( 10, 10, 80, 17, "Name", 1); |
| 91 |
add_text ( 10, 34, 80, 17, "Rate", 1); |
| 92 |
add_text ( 10, 58, 80, 17, "Fade in", 1); |
| 93 |
add_text ( 10, 82, 80, 17, "Start freq", 1); |
| 94 |
add_text ( 10, 106, 80, 17, "Sweep time", 1); |
| 95 |
add_text ( 10, 130, 80, 17, "End freq", 1); |
| 96 |
add_text ( 10, 154, 80, 17, "Fade out", 1); |
| 97 |
add_text (185, 106, 45, 17, "Type", 1); |
| 98 |
|
| 99 |
_sess = "."; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
Sgenwin::~Sgenwin (void) |
| 104 |
{ |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
void Sgenwin::handle_callb (int type, X_window *W, _XEvent *E) |
| 109 |
{ |
| 110 |
switch (type) |
| 111 |
{ |
| 112 |
case TEXTIP | X_textip::BUT: |
| 113 |
XSetInputFocus (dpy (), W->win (), RevertToPointerRoot, CurrentTime); |
| 114 |
_ready->x_unmap (); |
| 115 |
break; |
| 116 |
|
| 117 |
case TEXTIP | X_textip::KEY: |
| 118 |
break; |
| 119 |
|
| 120 |
case BUTTON | X_button::RELSE: |
| 121 |
{ |
| 122 |
X_button *B = (X_button *) W; |
| 123 |
switch (B->cbid ()) |
| 124 |
{ |
| 125 |
case B_LIN: |
| 126 |
_blin->set_stat (2); |
| 127 |
_blog->set_stat (0); |
| 128 |
_ready->x_unmap (); |
| 129 |
break; |
| 130 |
|
| 131 |
case B_LOG: |
| 132 |
_blin->set_stat (0); |
| 133 |
_blog->set_stat (2); |
| 134 |
_ready->x_unmap (); |
| 135 |
break; |
| 136 |
|
| 137 |
case B_CANC: |
| 138 |
hide (); |
| 139 |
break; |
| 140 |
|
| 141 |
case B_SGEN: |
| 142 |
gensweep (); |
| 143 |
break; |
| 144 |
} |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
void Sgenwin::handle_event (XEvent *E) |
| 152 |
{ |
| 153 |
switch (E->type) |
| 154 |
{ |
| 155 |
case ClientMessage: |
| 156 |
xcmesg ((XClientMessageEvent *) E); |
| 157 |
break; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
void Sgenwin::xcmesg (XClientMessageEvent *E) |
| 163 |
{ |
| 164 |
if (E->message_type == _xatom) hide (); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
void Sgenwin::show (void) |
| 169 |
{ |
| 170 |
char s [64]; |
| 171 |
|
| 172 |
sprintf (s, "%d", _rate); |
| 173 |
_trate->set_text (s); |
| 174 |
_tname->set_text (""); |
| 175 |
_ready->x_unmap (); |
| 176 |
x_mapraised (); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
void Sgenwin::hide (void) |
| 181 |
{ |
| 182 |
x_unmap (); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
void Sgenwin::add_text (int xp, int yp, int xs, int ys, const char *text, int align) |
| 187 |
{ |
| 188 |
(new X_textln (this, &Tst0, xp, yp, xs, ys, text, align))->x_map (); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
void Sgenwin::askdata (X_textip *T) |
| 193 |
{ |
| 194 |
T->set_text ("???"); |
| 195 |
XSetInputFocus (dpy (), T->win (), RevertToPointerRoot, CurrentTime); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
void Sgenwin::gensweep (void) |
| 200 |
{ |
| 201 |
int rate, k; |
| 202 |
char name [64]; |
| 203 |
char path [1024]; |
| 204 |
float t0, t1, t2; |
| 205 |
|
| 206 |
if ((sscanf (_tname->text (), "%s", name) != 1) || (*name == '?')) |
| 207 |
{ |
| 208 |
askdata (_tname); |
| 209 |
return; |
| 210 |
} |
| 211 |
if ((sscanf (_trate->text (), "%d", &rate) != 1) || (rate < 8000) || (rate > 96000)) |
| 212 |
{ |
| 213 |
askdata (_trate); |
| 214 |
return; |
| 215 |
} |
| 216 |
_rate = rate; |
| 217 |
if ((sscanf (_ttim1->text (), "%f", &t0) != 1) || (t0 < 0.0) || (t0 > 1.0f)) |
| 218 |
{ |
| 219 |
askdata (_ttim1); |
| 220 |
return; |
| 221 |
} |
| 222 |
if ((sscanf (_tfmin->text (), "%f", &_fmin) != 1) || (_fmin < 1.0f) || (_fmin > 0.25f * rate)) |
| 223 |
{ |
| 224 |
askdata (_tfmin); |
| 225 |
return; |
| 226 |
} |
| 227 |
if ((sscanf (_ttim2->text (), "%f", &t1) != 1) || (t1 < 1.0f) || (t1 > 100.0f)) |
| 228 |
{ |
| 229 |
askdata (_ttim2); |
| 230 |
return; |
| 231 |
} |
| 232 |
if ((sscanf (_tfmax->text (), "%f", &_fmax) != 1) || (_fmax < 2 * _fmin) || (_fmax > 0.50f *rate)) |
| 233 |
{ |
| 234 |
askdata (_tfmax); |
| 235 |
return; |
| 236 |
} |
| 237 |
if ((sscanf (_ttim3->text (), "%f", &t2) != 1) || (t2 < 0.0f) || (t2 > 1.0f)) |
| 238 |
{ |
| 239 |
askdata (_ttim3); |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
_k0 = (int)(t0 * rate); |
| 244 |
_k1 = (int)(t1 * rate); |
| 245 |
_k2 = (int)(t2 * rate); |
| 246 |
k = _k0 + _k1 + _k2; |
| 247 |
|
| 248 |
sprintf (path, "%s/sweep/%s", _sess, name); |
| 249 |
Impdata::checkext (path); |
| 250 |
_impd.set_type (Impdata::TYPE_SWEEP); |
| 251 |
_impd.set_rate (rate, 1); |
| 252 |
_impd.set_n_sect (1); |
| 253 |
_impd.set_n_fram (k); |
| 254 |
_impd.alloc (); |
| 255 |
if (_impd.open_write (path)) |
| 256 |
{ |
| 257 |
fprintf (stderr, "Can't create '%s'\n", path); |
| 258 |
return; |
| 259 |
} |
| 260 |
if (_blog->stat ()) genlogsweep (); |
| 261 |
else genlinsweep (); |
| 262 |
_impd.write_sect (0); |
| 263 |
_impd.close (); |
| 264 |
_impd.deall (); |
| 265 |
_ready->x_map (); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
void Sgenwin::genlogsweep (void) |
| 270 |
{ |
| 271 |
int i; |
| 272 |
double a, b, g, q, q0, p, r, x; |
| 273 |
float *s1, *s2; |
| 274 |
|
| 275 |
b = log (_fmax / _fmin) / _k1; |
| 276 |
a = _fmin / (b * _rate); |
| 277 |
r = 0.5 * a * (_fmax / _fmin) * (_k1 + 0.5 * (_k0 + _k2)) / (b * _k1); |
| 278 |
s1 = _impd.data (0); |
| 279 |
s2 = _impd.data (1) + 2 * (_impd.n_fram () - 1); |
| 280 |
q0 = a * exp (-b * _k0); |
| 281 |
for (i = -_k0; i < _k1 + _k2; i++) |
| 282 |
{ |
| 283 |
if (i < 0) g = cos (0.5 * M_PI * i / _k0); |
| 284 |
else if (i < _k1) g = 1.0; |
| 285 |
else g = sin (0.5 * M_PI * (_k1 + _k2 - i) / _k2); |
| 286 |
q = a * exp (b * i); |
| 287 |
p = q - q0; |
| 288 |
x = g * sin (2 * M_PI * (p - floor (p))); |
| 289 |
*s1 = (float)(x); |
| 290 |
*s2 = (float)(x * q / r); |
| 291 |
s1 += 2; |
| 292 |
s2 -= 2; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
void Sgenwin::genlinsweep (void) |
| 298 |
{ |
| 299 |
int i; |
| 300 |
double a, b, g, p, r, x; |
| 301 |
float *s1, *s2; |
| 302 |
|
| 303 |
a = _fmin / _rate; |
| 304 |
b = (_fmax - _fmin) / (2.0 * _rate * _k1); |
| 305 |
r = 2.0 / (_k1 + 0.5 * (_k0 + _k2)); |
| 306 |
s1 = _impd.data (0); |
| 307 |
s2 = _impd.data (1) + 2 * (_impd.n_fram () - 1); |
| 308 |
|
| 309 |
for (i = 0; i < _k0 + _k1 + _k2; i++) |
| 310 |
{ |
| 311 |
if (i < _k0) g = sin (0.5 * M_PI * i / _k0); |
| 312 |
else if (i < _k0 + _k1) g = 1.0; |
| 313 |
else g = sin (0.5 * M_PI * (_k0 + _k1 + _k2 - i) / _k2); |
| 314 |
p = i * (a + b * i); |
| 315 |
x = g * sin (2 * M_PI * (p - floor (p))); |
| 316 |
*s1 = (float)(x); |
| 317 |
*s2 = (float)(x * r); |
| 318 |
s1 += 2; |
| 319 |
s2 -= 2; |
| 320 |
} |
| 321 |
} |