~ubuntu-branches/ubuntu/intrepid/aeolus/intrepid

« back to all changes in this revision

Viewing changes to addsynth.cc

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2007-05-14 22:18:54 UTC
  • Revision ID: james.westby@ubuntu.com-20070514221854-274rj6fqs5tegu7q
Tags: upstream-0.6.6+2
ImportĀ upstreamĀ versionĀ 0.6.6+2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2003-2006 Fons Adriaensen <fons.adriaensen@skynet.be>
 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
*/
 
18
 
 
19
 
 
20
#include <string.h>
 
21
#include <endian.h>
 
22
#include "addsynth.h"
 
23
 
 
24
 
 
25
#define M (N_NOTE - 1)
 
26
 
 
27
 
 
28
#define swap4(a, b) { (a)[0] = (b)[3], (a)[1] = (b)[2], (a)[2] = (b)[1], (a)[3] = (b)[0]; }
 
29
 
 
30
 
 
31
N_func::N_func (void)
 
32
{
 
33
    reset (0.0f);
 
34
}
 
35
 
 
36
 
 
37
void N_func::reset (float v)
 
38
{
 
39
    _b = 16;
 
40
    for (int i = 0; i < N_NOTE; i++) _v [i] = v;
 
41
}
 
42
 
 
43
 
 
44
void N_func::setv (int i, float v)
 
45
{
 
46
    int    j;
 
47
    float  d;
 
48
 
 
49
    if ((i < 0) || (i > M)) return;
 
50
    _v [i] = v;
 
51
    _b |= 1 << i;   
 
52
 
 
53
    for (j = i - 1; (j >= 0) && ! (_b & (1 << j)); j--);
 
54
    if (j < 0) while (++j != i) _v [j] = v;
 
55
    else
 
56
    {
 
57
        d = (_v [j] - v) / (j - i);
 
58
        while (++j != i) _v [j] = v + (j - i) * d;
 
59
    }
 
60
 
 
61
    for (j = i + 1; (j <= M) && ! (_b & (1 << j)); j++);
 
62
    if (j > M) while (--j != i) _v [j] = v;
 
63
    else
 
64
    {
 
65
        d = (_v [j] - v) / (j - i);
 
66
        while (--j != i) _v [j] = v + (j - i) * d;
 
67
    }
 
68
}
 
69
 
 
70
 
 
71
void N_func::clrv (int i)
 
72
{
 
73
    int   j, k, m; 
 
74
    float d;
 
75
 
 
76
    if ((i < 0) || (i > M)) return;
 
77
    m = 1 << i;
 
78
    if (! (_b & m) || (_b == m)) return;
 
79
    _b ^= m;
 
80
 
 
81
    for (j = i - 1; (j >= 0) && ! (_b & (1 << j)); j--);
 
82
    for (k = i + 1; (k <= M) && ! (_b & (1 << k)); k++);
 
83
 
 
84
    if ((j >= 0) && (k <= M))
 
85
    {
 
86
        d = (_v [k] - _v [j]) / (k - j);
 
87
        for (i = j + 1; i < k; i++) _v [i] = _v [i] = _v [j] + (i - j) * d;
 
88
    }
 
89
    else if (j >= 0)
 
90
    {
 
91
        d = _v [j];
 
92
        while (j < M) _v [++j] = d; 
 
93
    }
 
94
    else if (k <= M)
 
95
    {
 
96
        d = _v [k];
 
97
        while (k > 0) _v [--k] = d; 
 
98
    }
 
99
}
 
100
 
 
101
 
 
102
void N_func::write (FILE *F)
 
103
{
 
104
#ifdef __BYTE_ORDER
 
105
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
 
106
 
 
107
    fwrite (&_b, 1, sizeof (S32), F);
 
108
    fwrite (_v, N_NOTE, sizeof (float), F);
 
109
 
 
110
#elif (__BYTE_ORDER == __BIG_ENDIAN)
 
111
 
 
112
    int  i;
 
113
    char d [N_NOTE * sizeof (float)];
 
114
 
 
115
    swap4 (d, (char *)(&_b));    
 
116
    fwrite (d, 1, sizeof (S32), F);
 
117
    for (i = 0; i < N_NOTE; i++) swap4 (d + i * sizeof (float), (char *)(_v + i));
 
118
    fwrite (d, N_NOTE, sizeof (float), F);
 
119
 
 
120
#else
 
121
#error Byte order is undefined !
 
122
#endif
 
123
#else
 
124
#error Byte order is undefined !
 
125
#endif
 
126
}
 
127
 
 
128
 
 
129
 
 
130
void N_func::read (FILE *F)
 
131
{
 
132
#ifdef __BYTE_ORDER
 
133
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
 
134
 
 
135
    fread (&_b, 1, sizeof (S32), F);
 
136
    fread (&_v, N_NOTE, sizeof (float), F);
 
137
 
 
138
#elif (__BYTE_ORDER == __BIG_ENDIAN)
 
139
 
 
140
    int  i;
 
141
    char d [sizeof (int) + N_NOTE * sizeof (float)];
 
142
 
 
143
    fread (d, 1, sizeof (S32), F);
 
144
    swap4 ((char *)(&_b), d);    
 
145
    fread (d, N_NOTE, sizeof (float), F);
 
146
    for (i = 0; i < N_NOTE; i++) swap4 ((char *)(_v + i), d + i * sizeof (float));
 
147
 
 
148
#else
 
149
#error Byte order is undefined !
 
150
#endif
 
151
#else
 
152
#error Byte order is undefined !
 
153
#endif
 
154
}
 
155
 
 
156
 
 
157
 
 
158
HN_func::HN_func (void)
 
159
{
 
160
}
 
161
 
 
162
 
 
163
void HN_func::reset (float v)
 
164
{
 
165
    for (int j = 0; j < N_HARM; j++) (_h + j)->reset (v);
 
166
}
 
167
 
 
168
 
 
169
void HN_func::setv (int i, float v)
 
170
{
 
171
    for (int j = 0; j < N_HARM; j++) (_h + j)->setv (i, v);
 
172
}
 
173
 
 
174
 
 
175
void HN_func::clrv (int i)
 
176
{
 
177
    for (int j = 0; j < N_HARM; j++) (_h + j)->clrv (i);
 
178
}
 
179
 
 
180
 
 
181
void HN_func::write (FILE *F, int k)
 
182
{
 
183
    for (int j = 0; j < k; j++) (_h + j)->write (F);
 
184
}
 
185
 
 
186
 
 
187
void HN_func::read (FILE *F, int k)
 
188
{
 
189
    for (int j = 0; j < k; j++) (_h + j)->read (F);
 
190
}
 
191
 
 
192
 
 
193
Addsynth::Addsynth (void)
 
194
{
 
195
    reset ();
 
196
}
 
197
 
 
198
 
 
199
void Addsynth::reset (void)
 
200
{
 
201
    *_stopname = 0;
 
202
    *_mnemonic = 0;
 
203
    *_copyrite = 0;
 
204
    *_comments = 0;
 
205
    _fd = 1;
 
206
    _fn = 1;
 
207
    _n0 = NOTE_MIN; 
 
208
    _n1 = NOTE_MAX; 
 
209
    _n_vol.reset (-20.0f);
 
210
    _n_ins.reset (0.0f);
 
211
    _n_off.reset (0.0f);
 
212
    _n_att.reset (0.01f);
 
213
    _n_atd.reset (0.0f);
 
214
    _n_dct.reset (0.01f);
 
215
    _n_dcd.reset (0.0f);
 
216
    _n_ran.reset (0.0f);
 
217
    _h_lev.reset (-100.0f);
 
218
    _h_ran.reset (0.0f);
 
219
    _h_att.reset (0.050f);
 
220
    _h_atp.reset (0.0f);
 
221
}
 
222
 
 
223
 
 
224
int Addsynth::save (const char *sdir)
 
225
{
 
226
    FILE  *F;
 
227
    char   d [32];
 
228
    char   path [1024];
 
229
 
 
230
    strcpy (path, sdir);
 
231
    strcat (path, "/");
 
232
    strcat (path, _filename);
 
233
    
 
234
    if (! (F = fopen (path, "w")))
 
235
    {
 
236
        fprintf (stderr, "Can't open '%s' for writing\n", path);
 
237
        return 1;
 
238
    }
 
239
 
 
240
    memset (d, 0, 32);
 
241
    strcpy (d, "AEOLUS");
 
242
    d [7]  =  2;
 
243
    d [26] = N_HARM;
 
244
    d [28] = _n0;
 
245
    d [29] = _n1;
 
246
    d [30] = _fn;
 
247
    d [31] = _fd;
 
248
   
 
249
    fwrite (d, 1, 32, F);
 
250
    fwrite (_stopname, 1, 32, F);
 
251
    fwrite (_copyrite, 1, 56, F);
 
252
    fwrite (_mnemonic, 1,  8, F);
 
253
    fwrite (_comments, 1, 56, F);
 
254
    fwrite (_reserved, 1,  8, F);
 
255
 
 
256
    _n_vol.write (F);
 
257
    _n_off.write (F);
 
258
    _n_ran.write (F);
 
259
    _n_ins.write (F);
 
260
    _n_att.write (F);
 
261
    _n_atd.write (F);
 
262
    _n_dct.write (F);
 
263
    _n_dcd.write (F);
 
264
    _h_lev.write (F, N_HARM);    
 
265
    _h_ran.write (F, N_HARM);    
 
266
    _h_att.write (F, N_HARM);    
 
267
    _h_atp.write (F, N_HARM);    
 
268
    
 
269
    fclose (F);
 
270
    return 0;
 
271
}
 
272
 
 
273
 
 
274
int Addsynth::load (const char *sdir)
 
275
{
 
276
    FILE  *F;
 
277
    char   d [32];
 
278
    char   path [1024];    
 
279
    int    v, k;
 
280
 
 
281
    strcpy (path, sdir);
 
282
    strcat (path, "/");
 
283
    strcat (path, _filename);
 
284
 
 
285
    reset (); 
 
286
 
 
287
    if (! (F = fopen (path, "r")))
 
288
    {
 
289
        fprintf (stderr, "Can't open '%s' for reading\n", path);
 
290
        return 1;
 
291
    }
 
292
 
 
293
    fread (d, 1, 32, F);
 
294
    if (strcmp (d, "AEOLUS"))
 
295
    {
 
296
        fprintf (stderr, "File '%s' is not an Aeolus file\n", _filename);
 
297
        fclose (F); 
 
298
        return 1;
 
299
    }
 
300
    v = d [7];
 
301
    k = d [26];
 
302
    if (! k) k = 48; 
 
303
    _n0 = d [28];
 
304
    _n1 = d [29];
 
305
    if (_n1 == 0x2E) _n1 = 96;   ////// FIX THIS
 
306
    _fn = d [30];
 
307
    _fd = d [31];
 
308
 
 
309
    fread (_stopname, 1, 32, F);
 
310
    fread (_copyrite, 1, 56, F);
 
311
    fread (_mnemonic, 1,  8, F);
 
312
    fread (_comments, 1, 56, F);
 
313
    fread (_reserved, 1,  8, F);
 
314
 
 
315
    _n_vol.read (F);
 
316
    _n_off.read (F);
 
317
    _n_ran.read (F);
 
318
    if (v >= 2)
 
319
    {
 
320
        _n_ins.read (F);
 
321
        _n_att.read (F);
 
322
        _n_atd.read (F);
 
323
        _n_dct.read (F);
 
324
        _n_dcd.read (F);
 
325
    } 
 
326
    _h_lev.reset (-100.0f);
 
327
    _h_ran.reset (0.0f);
 
328
    _h_att.reset (0.050f);
 
329
    _h_atp.reset (0.0f);
 
330
    _h_lev.read (F, k);    
 
331
    _h_ran.read (F, k);    
 
332
    _h_att.read (F, k);    
 
333
    _h_atp.read (F, k);    
 
334
 
 
335
    fclose (F);
 
336
    return 0;
 
337
}
 
338
 
 
339