~ubuntu-branches/ubuntu/wily/aliki/wily

« back to all changes in this revision

Viewing changes to source/sgenwin.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jaromír Mikeš
  • Date: 2011-07-26 21:12:17 UTC
  • Revision ID: james.westby@ubuntu.com-20110726211217-v948cgyyjokiq3sl
Tags: upstream-0.1.0
Import upstream version 0.1.0

Show diffs side-by-side

added added

removed removed

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