1 | /*
|
---|
2 | =============================================================================
|
---|
3 | initi.c -- MIDAS-VII - instrument editor support functions
|
---|
4 | Version 2 -- 1989-12-19 -- D.N. Lynx Crowe
|
---|
5 | =============================================================================
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define DEBUGPS 0 /* define non-zero to debug pntsel() */
|
---|
9 |
|
---|
10 | #include "ram.h"
|
---|
11 |
|
---|
12 | #include "dfltins.h" /* short dfltins[]; default instrument */
|
---|
13 | #include "ptoftab.h" /* int16_t ptoftab[]; pitch to frequency table */
|
---|
14 |
|
---|
15 | int16_t finival[NFINST] = { /* initial function values */
|
---|
16 |
|
---|
17 | FPUVAL(500), /* 0: Freq 1 */
|
---|
18 | FPUVAL(500), /* 1: Freq 2 */
|
---|
19 | FPUVAL(500), /* 2: Freq 3 */
|
---|
20 | FPUVAL(500), /* 3: Freq 4 */
|
---|
21 | FPUVAL(500), /* 4: Filter / Resonance */
|
---|
22 | FPUVAL(500), /* 5: Location */
|
---|
23 |
|
---|
24 | FPUVAL(0), /* 6: Index 1 */
|
---|
25 | FPUVAL(0), /* 7: Index 2 */
|
---|
26 | FPUVAL(0), /* 8: Index 3 */
|
---|
27 | FPUVAL(0), /* 9: Index 4 */
|
---|
28 | FPUVAL(0), /* 10: Index 5 */
|
---|
29 | FPUVAL(0), /* 11: Index 6 */
|
---|
30 |
|
---|
31 | FPUVAL(0) /* 12: Level */
|
---|
32 | };
|
---|
33 |
|
---|
34 | uint16_t expbit[16] = { /* FPU time exponent bit expansion table */
|
---|
35 |
|
---|
36 | 0x0001, /* 0 */
|
---|
37 | 0x0002, /* 1 */
|
---|
38 | 0x0004, /* 2 */
|
---|
39 | 0x0008, /* 3 */
|
---|
40 | 0x0010, /* 4 */
|
---|
41 | 0x0020, /* 5 */
|
---|
42 | 0x0040, /* 6 */
|
---|
43 | 0x0080, /* 7 */
|
---|
44 | 0x0100, /* 8 */
|
---|
45 | 0x0200, /* 9 */
|
---|
46 | 0x0400, /* 10 */
|
---|
47 | 0x0800, /* 11 */
|
---|
48 | 0x1000, /* 12 */
|
---|
49 | 0x2000, /* 13 */
|
---|
50 | 0x4000, /* 14 */
|
---|
51 | 0x8000 /* 15 */
|
---|
52 | };
|
---|
53 |
|
---|
54 | /*
|
---|
55 | =============================================================================
|
---|
56 | segtime() -- determine time to a point using a new segment time
|
---|
57 | =============================================================================
|
---|
58 | */
|
---|
59 |
|
---|
60 | int16_t segtime(int16_t pn, uint16_t ptime)
|
---|
61 | {
|
---|
62 | register struct instpnt *pp;
|
---|
63 | register int16_t i;
|
---|
64 | register uint16_t tacc;
|
---|
65 |
|
---|
66 | pp = &vbufs[curvce].idhpnt[vbufs[curvce].idhfnc[curfunc].idfpt1];
|
---|
67 | tacc = 0;
|
---|
68 |
|
---|
69 | for (i = 0; i < pn; i++)
|
---|
70 | tacc += fromfpu(pp++->iptim);
|
---|
71 |
|
---|
72 | tacc += fromfpu(ptime);
|
---|
73 |
|
---|
74 | return(tacc);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /*
|
---|
78 | =============================================================================
|
---|
79 | setseg() -- set the time to a segment
|
---|
80 | =============================================================================
|
---|
81 | */
|
---|
82 |
|
---|
83 | void setseg(int16_t pn, uint16_t ptime)
|
---|
84 | {
|
---|
85 | register int16_t i;
|
---|
86 | register uint16_t tacc;
|
---|
87 | register struct instpnt *pp;
|
---|
88 |
|
---|
89 | pp = &vbufs[curvce].idhpnt[vbufs[curvce].idhfnc[curfunc].idfpt1];
|
---|
90 | tacc = 0;
|
---|
91 |
|
---|
92 | for (i = 0; i < pn; i++)
|
---|
93 | tacc += fromfpu(pp++->iptim);
|
---|
94 |
|
---|
95 | pp->iptim = tofpu(ptime - tacc);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /*
|
---|
99 | =============================================================================
|
---|
100 | timeto() -- find the time at a point in a function
|
---|
101 | =============================================================================
|
---|
102 | */
|
---|
103 |
|
---|
104 | int16_t timeto(int16_t fn, int16_t pj)
|
---|
105 | {
|
---|
106 | register struct instdef *ip;
|
---|
107 | register struct instpnt *pt;
|
---|
108 | register int16_t tf, pm, pn;
|
---|
109 |
|
---|
110 | ip = &vbufs[curvce];
|
---|
111 | pn = ip->idhfnc[fn].idfpt1;
|
---|
112 | pt = &ip->idhpnt[pn];
|
---|
113 | pm = pn + pj;
|
---|
114 | tf = 0;
|
---|
115 |
|
---|
116 | while (pn LE pm) {
|
---|
117 |
|
---|
118 | tf += fromfpu(pt->iptim);
|
---|
119 | ++pt;
|
---|
120 | ++pn;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return(tf);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /*
|
---|
127 | =============================================================================
|
---|
128 | pntsel() -- setup editing limits for subj in the current function
|
---|
129 | =============================================================================
|
---|
130 | */
|
---|
131 |
|
---|
132 | void pntsel(void)
|
---|
133 | {
|
---|
134 | register struct idfnhdr *fp;
|
---|
135 |
|
---|
136 | fp = &vbufs[curvce].idhfnc[curfunc];
|
---|
137 |
|
---|
138 | curpnt = fp->idfpt1 + subj;
|
---|
139 | pntptr = &vbufs[curvce].idhpnt[curpnt];
|
---|
140 |
|
---|
141 | npts = fp->idfpif;
|
---|
142 |
|
---|
143 | if (npts EQ 1) { /* detemine editing case */
|
---|
144 |
|
---|
145 | pecase = 0; /* single point */
|
---|
146 | temin = 1;
|
---|
147 | temax = 32767;
|
---|
148 |
|
---|
149 | } else if (subj EQ (npts - 1)) {
|
---|
150 |
|
---|
151 | pecase = 1; /* last point */
|
---|
152 | temin = timeto(curfunc, subj - 1);
|
---|
153 | temax = 32767;
|
---|
154 |
|
---|
155 | } else {
|
---|
156 |
|
---|
157 | pecase = 2; /* interior point */
|
---|
158 | temin = timeto(curfunc, subj - 1);
|
---|
159 | temax = timeto(curfunc, subj + 1);
|
---|
160 | }
|
---|
161 |
|
---|
162 | #if DEBUGPS
|
---|
163 | printf("pntsel(): curpnt=%d, subj=%d, fp=0x%08lx, pntptr=0x%08lx\r\n",
|
---|
164 | curpnt, subj, fp, pntptr);
|
---|
165 | printf("pntsel(): npts=%d, pecase=%d, temin=%d, temax=%d\r\n",
|
---|
166 | npts, pecase, temin, temax);
|
---|
167 | #endif
|
---|
168 | }
|
---|
169 |
|
---|
170 | /*
|
---|
171 | =============================================================================
|
---|
172 | vtoy() -- convert a value to a y coordinate for display
|
---|
173 | =============================================================================
|
---|
174 | */
|
---|
175 |
|
---|
176 | int16_t vtoy(int16_t val, int16_t window)
|
---|
177 | {
|
---|
178 | register int16_t yval;
|
---|
179 |
|
---|
180 | if (val GT 1000)
|
---|
181 | val = 1000;
|
---|
182 |
|
---|
183 | yval = 56 + (((1000 - val) * 14) / 100);
|
---|
184 |
|
---|
185 | if (window < 12)
|
---|
186 | return((int16_t)(idbox[window][1] + (((yval - 56) * 100L) / 540L)));
|
---|
187 | else
|
---|
188 | return(yval);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /*
|
---|
192 | =============================================================================
|
---|
193 | ttox() -- convert a time to an x coordinate for display
|
---|
194 | =============================================================================
|
---|
195 | */
|
---|
196 |
|
---|
197 | int16_t ttox(uint16_t time, int16_t window)
|
---|
198 | {
|
---|
199 | register int16_t xval;
|
---|
200 |
|
---|
201 | if (time < 128) /* 0..127 */
|
---|
202 | xval = 8 + (int16_t)(((time * 1000L) / 2309L));
|
---|
203 | else if (time < 256) /* 128..255 */
|
---|
204 | xval = 64 + (int16_t)((((time - 128) * 1000L) / 2309L));
|
---|
205 | else if (time < 512) /* 256..511 */
|
---|
206 | xval = 120 + (int16_t)((((time - 256) * 1000L) / 4636L));
|
---|
207 | else if (time < 1024) /* 512..1023 */
|
---|
208 | xval = 176 + (int16_t)((((time - 512) * 1000L) / 9290L));
|
---|
209 | else if (time < 2048) /* 1024..2047 */
|
---|
210 | xval = 232 + (int16_t)((((time - 1024) * 1000L) / 18600L));
|
---|
211 | else if (time < 4096) /* 2048..4095 */
|
---|
212 | xval = 288 + (int16_t)((((time - 2048) * 1000L) / 37218L));
|
---|
213 | else if (time < 8192) /* 4096..8191 */
|
---|
214 | xval = 344 + (int16_t)((((time - 4096) * 1000L)/ 74454L));
|
---|
215 | else if (time < 16384) /* 8192..16383 */
|
---|
216 | xval = 400 + (int16_t)((((time - 8192) * 1000L) / 148927L));
|
---|
217 | else if (time < (uint16_t)32768) /* 16384..32767 */
|
---|
218 | xval = 456 + (int16_t)((((time - 16384) * 1000L) / 309113L));
|
---|
219 | else
|
---|
220 | xval = 509;
|
---|
221 |
|
---|
222 | if (xval > 509)
|
---|
223 | xval = 509;
|
---|
224 |
|
---|
225 | if (window < 12)
|
---|
226 | return((int16_t)(idbox[window][0] + (((xval - 8) * 100L) / 599L)));
|
---|
227 | else
|
---|
228 | return(xval);
|
---|
229 | }
|
---|
230 |
|
---|
231 | /*
|
---|
232 | =============================================================================
|
---|
233 | selpnt() -- select a point for editing
|
---|
234 | =============================================================================
|
---|
235 | */
|
---|
236 |
|
---|
237 | int16_t selpnt(void)
|
---|
238 | {
|
---|
239 | register struct idfnhdr *fp;
|
---|
240 |
|
---|
241 | register int16_t i, xv, np;
|
---|
242 |
|
---|
243 | int16_t lo_x, lo_x_pt, hi_x, hi_x_pt;
|
---|
244 |
|
---|
245 | fp = &vbufs[curvce].idhfnc[curfunc];
|
---|
246 | np = fp->idfpif;
|
---|
247 | lo_x_pt = 0;
|
---|
248 | lo_x = ttox(timeto(curfunc, lo_x_pt), 12);
|
---|
249 | hi_x_pt = np - 1;
|
---|
250 | hi_x = ttox(timeto(curfunc, hi_x_pt), 12);
|
---|
251 |
|
---|
252 | for (i = 0; i < np; i++) {
|
---|
253 |
|
---|
254 | xv = ttox(timeto(curfunc, i), 12);
|
---|
255 |
|
---|
256 | if (xv LE cxval) /* largest x LE cxval */
|
---|
257 | if (xv GE lo_x) {
|
---|
258 |
|
---|
259 | lo_x = xv;
|
---|
260 | lo_x_pt = i;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (xv GE cxval) /* smallest x GE cxval*/
|
---|
264 | if (xv LT hi_x) {
|
---|
265 |
|
---|
266 | hi_x = xv;
|
---|
267 | hi_x_pt = i;
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | if (lo_x EQ hi_x)
|
---|
272 | return(lo_x_pt);
|
---|
273 |
|
---|
274 | if ((cxval - lo_x) LT (hi_x - cxval))
|
---|
275 | return(lo_x_pt);
|
---|
276 | else
|
---|
277 | return(hi_x_pt);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /*
|
---|
281 | =============================================================================
|
---|
282 | setinst() -- setup for editing a new instrument
|
---|
283 | =============================================================================
|
---|
284 | */
|
---|
285 |
|
---|
286 | void setinst(void)
|
---|
287 | {
|
---|
288 | register struct instdef *ip;
|
---|
289 | register struct idfnhdr *fp;
|
---|
290 |
|
---|
291 | curfunc = 12; /* current function = 12 = level */
|
---|
292 | subj = 0; /* current edited point = 0 (first point) */
|
---|
293 | pntsv = 0; /* point selection state = unselected */
|
---|
294 | pecase = 0; /* point selection case = 0 (first point) */
|
---|
295 |
|
---|
296 | ip = &vbufs[curvce]; /* instrument definition pointer */
|
---|
297 | fp = &ip->idhfnc[curfunc]; /* function pointer */
|
---|
298 |
|
---|
299 | curpnt = subj + fp->idfpt1; /* current point in function */
|
---|
300 | pntptr = &ip->idhpnt[curpnt]; /* current point pointer */
|
---|
301 |
|
---|
302 | newws(); /* setup waveshape variables */
|
---|
303 | }
|
---|
304 |
|
---|
305 | /*
|
---|
306 | =============================================================================
|
---|
307 | newinst() -- select a new instrument
|
---|
308 | =============================================================================
|
---|
309 | */
|
---|
310 |
|
---|
311 | void newinst(int16_t inst)
|
---|
312 | {
|
---|
313 | curinst = inst;
|
---|
314 | s_inst[curvce] = inst;
|
---|
315 | setinst();
|
---|
316 | }
|
---|
317 |
|
---|
318 | /*
|
---|
319 | =============================================================================
|
---|
320 | newvce() -- select a new voice
|
---|
321 | =============================================================================
|
---|
322 | */
|
---|
323 |
|
---|
324 | void newvce(int16_t voice)
|
---|
325 | {
|
---|
326 | curvce = voice;
|
---|
327 | curinst = s_inst[curvce];
|
---|
328 | setinst();
|
---|
329 | }
|
---|
330 |
|
---|
331 | /*
|
---|
332 | =============================================================================
|
---|
333 | setivce() -- set instrument definition for a voice
|
---|
334 | =============================================================================
|
---|
335 | */
|
---|
336 |
|
---|
337 | void setivce(int16_t voice)
|
---|
338 | {
|
---|
339 | register int16_t j, k, oldi;
|
---|
340 | register struct instdef *ip;
|
---|
341 |
|
---|
342 | ip = &vbufs[voice];
|
---|
343 |
|
---|
344 | oldi = setipl(FPU_DI); /* disable FPU interrupts */
|
---|
345 |
|
---|
346 | /* +++++++++++++++++++++++ FPU interrupts disabled ++++++++++++++++++++++++++ */
|
---|
347 |
|
---|
348 | for (j = 0; j < NFINST; j++) { /* set funcndx[] entries */
|
---|
349 |
|
---|
350 | k = (voice << 4) + fnoff[j];
|
---|
351 | funcndx[k][0] = &ip->idhfnc[j];
|
---|
352 | funcndx[k][1] = ip->idhpnt;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /* set waveshapes */
|
---|
356 |
|
---|
357 | memcpyw((io_fpu + FPU_OWST + (voice << 9) + 0x0100 + 1),
|
---|
358 | ip->idhwvaf, NUMWPNT);
|
---|
359 |
|
---|
360 | memcpyw((io_fpu + FPU_OWST + (voice << 9) + 1),
|
---|
361 | ip->idhwvbf, NUMWPNT);
|
---|
362 |
|
---|
363 | setipl(oldi); /* restore interrupts */
|
---|
364 |
|
---|
365 | /* ++++++++++++++++++++++++++ Interrupts restored +++++++++++++++++++++++++++ */
|
---|
366 |
|
---|
367 | instmod[voice] = FALSE; /* instrument matches library */
|
---|
368 | }
|
---|
369 |
|
---|
370 | /*
|
---|
371 | =============================================================================
|
---|
372 | modinst() -- handle instrument modifications
|
---|
373 | =============================================================================
|
---|
374 | */
|
---|
375 |
|
---|
376 | void modinst(void)
|
---|
377 | {
|
---|
378 | int16_t f, i, grp, oldi;
|
---|
379 |
|
---|
380 | if (NOT instmod[curvce]) {
|
---|
381 |
|
---|
382 | instmod[curvce] = TRUE;
|
---|
383 | dswin(19);
|
---|
384 | }
|
---|
385 |
|
---|
386 | if (NOT editsw) {
|
---|
387 |
|
---|
388 | if ((grp = vce2grp[curvce]) > 0) {
|
---|
389 |
|
---|
390 | oldi = setipl(FPU_DI); /* +++++ disable FPU interrupts */
|
---|
391 |
|
---|
392 | for (i = 0; i < 12; i++) {
|
---|
393 |
|
---|
394 | if (vce2grp[i] EQ grp) {
|
---|
395 |
|
---|
396 | memcpyw(&vbufs[i], &vbufs[curvce],
|
---|
397 | sizeof (struct instdef) / 2);
|
---|
398 |
|
---|
399 | for (f = 0; f < 12; f++)
|
---|
400 | vbufs[i].idhfnc[f].idftmd
|
---|
401 | &= ~I_ACTIVE;
|
---|
402 |
|
---|
403 | s_inst[i] = curinst;
|
---|
404 | clrvce(i);
|
---|
405 | setivce(i);
|
---|
406 | dosync(i);
|
---|
407 | execkey(0, 0, i, 1);
|
---|
408 | vce2trg[i] = -1;
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | setipl(oldi); /* +++++ enable FPU interrupts */
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | /*
|
---|
418 | =============================================================================
|
---|
419 | initi() -- initialize an instrument definition
|
---|
420 | =============================================================================
|
---|
421 | */
|
---|
422 |
|
---|
423 | void initi(struct instdef *ip)
|
---|
424 | {
|
---|
425 | register int16_t i, mintfpu, rb;
|
---|
426 |
|
---|
427 | memsetw(ip, 0, sizeof (struct instdef) / 2);
|
---|
428 |
|
---|
429 | memcpy(ip->idhname, " ", MAXIDLN+1);
|
---|
430 | memcpy(ip->idhcom1, " ", MAXIDLN+1);
|
---|
431 | memcpy(ip->idhcom2, " ", MAXIDLN+1);
|
---|
432 | memcpy(ip->idhcom3, " ", MAXIDLN+1);
|
---|
433 |
|
---|
434 | ip->idhplft = NIPNTS - NFINST;
|
---|
435 | ip->idhcfg = (initcfg >> 8) & 0x00FF;
|
---|
436 | mintfpu = tofpu(1);
|
---|
437 |
|
---|
438 | for (i = 0; i < NFINST; i++) { /* for each function ... */
|
---|
439 |
|
---|
440 | /* initialize the function header */
|
---|
441 |
|
---|
442 | rb = ((i < 4) AND (i NE 0)) ? 0 : I_NRATIO;
|
---|
443 | ip->idhfnc[i].idfpif = 1;
|
---|
444 | ip->idhfnc[i].idfpt1 = i;
|
---|
445 | ip->idhfnc[i].idftmd = (I_TM_KEY | 0x0010) | rb;
|
---|
446 |
|
---|
447 | /* initialize the first point in the function */
|
---|
448 |
|
---|
449 | ip->idhpnt[i].iptim = mintfpu;
|
---|
450 | ip->idhpnt[i].ipval = finival[i];
|
---|
451 | }
|
---|
452 |
|
---|
453 | for (i = 0; i < NUMWPNT; i++) {
|
---|
454 |
|
---|
455 | ip->idhwvaf[i] = (ip->idhwvao[i] = 0x8000 ^ ((i + 1) << 8));
|
---|
456 | ip->idhwvbf[i] = (ip->idhwvbo[i] = 0x8000 ^ ((i + 1) << 8));
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | /*
|
---|
461 | =============================================================================
|
---|
462 | initil() -- initialize instrument library
|
---|
463 | =============================================================================
|
---|
464 | */
|
---|
465 |
|
---|
466 | void initil(void)
|
---|
467 | {
|
---|
468 | register int16_t i;
|
---|
469 |
|
---|
470 | setipl(FPU_DI); /* disable FPU interrupts */
|
---|
471 |
|
---|
472 | /* +++++++++++++++++++++++ FPU interrupts disabled ++++++++++++++++++++++++++ */
|
---|
473 |
|
---|
474 | fpuclr(); /* clear the FPU */
|
---|
475 |
|
---|
476 | memcpyw(&idefs[0], dfltins, sizeof (struct instdef) / 2);
|
---|
477 |
|
---|
478 | for (i = 1; i < NINST; i++) /* initialize each instrument */
|
---|
479 | initi(&idefs[i]);
|
---|
480 |
|
---|
481 | memset(funcndx, 0, sizeof funcndx);
|
---|
482 |
|
---|
483 | for (i = 0; i < 12; i++) { /* initialize voices */
|
---|
484 |
|
---|
485 | memcpyw(&vbufs[i], &idefs[0], sizeof (struct instdef) / 2);
|
---|
486 | setivce(i);
|
---|
487 | }
|
---|
488 |
|
---|
489 | setipl(FPU_EI); /* enable FPU interrupts */
|
---|
490 |
|
---|
491 | /* ++++++++++++++++++++++++++ Interrupts enabled ++++++++++++++++++++++++++++ */
|
---|
492 |
|
---|
493 | newvce(0); /* setup editing for instrument 0, voice 0 */
|
---|
494 | newinst(0);
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|